model = new User(); $this->userBillModel = new Bill(); $this->walletModel = new Wallet(); } function findUser($user_id) { $user = $this->model->findById($user_id); if (!$user) return $this->fail("用户不存在!"); return $this->ok(User::fmtUser($user)); } function findWallet($user_id) { $w = $this->walletModel->getUserWallet($user_id); return [ "user_id" => $w->user_id, "score" => $w->score, "money" => (float)$w->money, "give_money" => (float)$w->give_money, "total_money" => fixed2Float($w->money + $w->give_money), ]; } function summary($user_id) { $wallet = $this->findWallet($user_id); return [ "msg_count" => (new Message())->where(["to_user_id" => $user_id, "is_read" => 0])->count(), "collect_count" => (new Collect())->where(["user_id" => $user_id])->count(), "score" => $wallet["score"], "total_money" => $wallet["total_money"], "voucher_count" => (new Voucher())->fetchVoucherCount($user_id), "order_count" => (new Order())->where("user_id", $user_id)->count() ]; } public function fetchCollectMassager($user_id, $page, $size) { $paginate = (new Collect())->fetchCollectMassager($user_id, $page, $size); $items = array_map(function ($data) { return $data["massager"]; }, $paginate->items()); foreach ($items as &$item) { $item['recently_work_date'] = null; $item['is_can_collet'] = false; unset($item['nearly_two_days_orders']); } return [ array_map(function ($data) { return Massager::fmtMassager($data); }, $items), $paginate->total() ]; } public function fetchMassagerComment($user_id, $page, $size) { $paginate = (new Comment())->fetchByUserId($user_id, $page, $size); return [ $paginate->items(), $paginate->total() ]; } public function fetchSystemMessage($user_id, $page, $size) { $messageModel = new Message(); $paginate = $messageModel->fetchUserSystemMessage($user_id, $page, $size); $messageModel->update(["is_read" => 1], [ "to_user_id" => $user_id, "is_read" => 0, "identity_type" => \E_IDENTITY_TYPE::User ]); return [ $paginate->items(), $paginate->total() ]; } public function findByUnionId($union_id) { return $this->model->findByUnionId($union_id); } public function bindAppWx($u_id, $params) { $user = $this->findByUnionId($params["unionId"]); if ($user) return $this->fail("该微信已经绑定用户,请解绑后再进行绑定!"); $this->model->update([ "app_openid" => $params["openId"], "union_id" => $params["unionId"], "nickname" => $params["nickName"], "avatar" => $params["avatarUrl"], ], ["id" => $u_id]); return $this->ok(true); } public function unbindWx($u_id) { $this->model->update([ "app_openid" => null, "web_openid" => null, "union_id" => null, ], ["id" => $u_id]); return $this->ok(true); } public function bindMobile($u_id, $mobile, $sms_code) { $check = \app\common\library\Sms::check($mobile, $sms_code, "bind_mobile"); if (!$check) return $this->fail("短信验证码不正确!"); $user = $this->model->where("mobile", $mobile)->find(); if ($user) return $this->fail("该手机号码已经绑定用户,无法继续绑定!"); $this->model->update([ "mobile" => $mobile ], ["id" => $u_id]); return $this->ok(true); } public function resetPwd($u_id, $sms_code, $new_password) { $user = $this->model->get($u_id); if (!$user || mb_strlen($user["mobile"]) != 11) return $this->fail("请先绑定手机再修改登录密码!"); $check = \app\common\library\Sms::check($user["mobile"], $sms_code, "reset_pwd"); if (!$check) return $this->fail("短信验证码不正确!"); $this->model->update([ "password" => $new_password ], ["id" => $u_id]); return $this->ok(true); } // 更新用户分组 public static function updateGrouping($user_id) { $grouping_config = [ [ "grouping" => "D", "rule" => ["已消费总金额" => 0, "累计下单次数" => 0] ], [ "grouping" => "C", "rule" => config("site.user_level_C") ], [ "grouping" => "B", "rule" => config("site.user_level_B") ], [ "grouping" => "A", "rule" => config("site.user_level_A") ], ]; $total_consume_amount = Order::sumByUser($user_id); $now_place_order_count = Order::countByUser($user_id); $grouping = "D"; foreach ($grouping_config as $item) { if ($item["rule"]["已消费总金额"] >= $total_consume_amount || $item["rule"]["累计下单次数"] >= $now_place_order_count) break; $grouping = $item["grouping"]; } User::update([ "grouping" => $grouping ], ["id" => $user_id]); return $grouping; } public function fetchInviteCount($user_id) { $change_type = \E_USER_BILL_CHANGE_TYPE::ProfitIncrease[0]; $total_profit = \db()->query("select sum(`change`) as total_profit_amount from ma_user_bill where user_id = {$user_id} AND change_type = {$change_type}"); $users = $this->model->where([ "parent_id" => $user_id, "is_use_award" => 0, ])->field("id")->select(); $user_ids = array_map(function ($data) { return $data['id']; }, $users); $two_users = $this->model->where("parent_id", "in", $user_ids)->where("is_use_award", 0)->field("id")->select(); $two_user_ids = array_map(function ($data) { return $data['id']; }, $two_users); $user_ids_str = join(",", array_merge($user_ids, $two_user_ids)); $order_status_str = join(",", ["'" . \E_ORDER_STATUS::Purchase . "'", "'" . \E_ORDER_STATUS::Proceed . "'", "'" . \E_ORDER_STATUS::WaitFeedback . "'"]); $total_wait_profit = 0; if (mb_strlen($user_ids_str) > 0) { $orders = \db()->query(" SELECT o.user_id, o.total_real_amount, o.trip_amount FROM ma_user u JOIN ma_order o ON u.id = o.user_id WHERE u.id IN ({$user_ids_str}) AND o.`status` IN ({$order_status_str}) ORDER BY o.pay_time DESC "); foreach ($orders as $order) { $total_real_amount = ($order["total_real_amount"] - $order["trip_amount"]); if (in_array($order["user_id"], $user_ids)) { $item_wait_profit = fixed2Float($total_real_amount * (config("site.user_direct_invite_rate") / 100)); } else { $item_wait_profit = fixed2Float($total_real_amount * (config("site.user_two_invite_rate") / 100)); } $total_wait_profit += $item_wait_profit; } } return [ "total_number" => $this->model->where("parent_id", $user_id)->count(), "total_profit_amount" => $total_profit[0]["total_profit_amount"] ?? 0, "total_wait_profit" => $total_wait_profit, "users" => $this->model->where("parent_id", $user_id)->field("id,nickname,avatar")->limit(4)->select() ]; } public function fetchInviteDetails($user_id, $page = 1, $size = 10) { $paginate = $this->model->where("parent_id", $user_id)->field("id,nickname,avatar,is_use_award")->page($page)->paginate($size); return [ $paginate->items(), $paginate->total() ]; } public function fetchRanking() { $change_type = \E_USER_BILL_CHANGE_TYPE::ProfitIncrease[0]; $rows = \db()->query("select user_id,sum(`change`) as total from ma_user_bill where change_type = {$change_type} group by user_id order by total DESC limit 20"); $ids = array_map(function ($data) { return $data["user_id"]; }, $rows); $users = $this->model ->where("id", "in", $ids) ->field("id,nickname,avatar") ->select(); $fmt_users = []; foreach ($users as $user) { $fmt_users[$user["id"]] = $user; } return array_map(function ($data) use ($fmt_users) { $now = isset($fmt_users[$data["user_id"]]) ? $fmt_users[$data["user_id"]] : null; return [ "user_id" => $data["user_id"], "nickname" => $now ? $now["nickname"] : null, "avatar" => $now ? $now["avatar"] : null, "total_profit_amount" => $data["total"] ]; }, $rows); } }