getUserDetail($id); } public static function levelset($post) { $level = $post['level']; if ($level > 4) { return '最高等级4级'; } $user = User::getUserOrEmptyById($post['id']); if (empty($user)) return '用户不存在'; $level_type = 1; if ($level > $user['level']) { $level_type = 2; } Db::startTrans(); try { $userData = ['level' => $level, 'level_type' => $level_type]; User::update($post['id'], $userData); Db::commit(); } catch (\Exception $e) { Db::rollback(); return '失败' . $e->getMessage(); } return true; } public static function lookpidlevel($id) { $path = Db::name('user')->where('id', $id)->value('path'); $arr = explode(',', $path); $ids = $arr; $ids = implode(',', $ids); $order = 'field(id,' . $ids . ')'; $where = array(); $where[] = ['uid', 'in', $arr]; sr_log($where); $user = new UserModel(); $count = $user ->withJoin('userData', 'INNER') ->where($where) ->count(); $list = $user ->withJoin('userData', 'INNER') ->where($where) ->order(Db::raw($order)) ->select(); return [$count, $list]; } public static function getExportList($where, $page, $limit) { return (new UserModel()) ->where($where) ->withJoin('userData', 'INNER') ->where($where) ->page($page, $limit) ->order('id', 'desc') ->select() ->toArray(); } public static function recycleCountSet($post) { $user = User::getUserById($post['id']); if (empty($user)) return '用户不存在'; if (empty($post['recycle_count']) || $post['recycle_count'] < 0 || $post['recycle_count'] > 500) { return '回收卡数量格式有误'; } try { $updateData = ['recycle_count' => $post['recycle_count']]; $result = User::update($user['id'], $updateData); if (!$result) return "更新临时卡数量失败"; } catch (\Exception $e) { return '失败' . $e->getMessage(); } return true; } public function getList($page, $limit, $where, $sort, $userMap) { $where[] = ['status', '<>', 3]; $userDao = new User(); $count = $userDao->getCount($where, $userMap); $list = $userDao->getPageList($page, $limit, $where, $sort, $userMap); foreach ($list as &$item) { //团队会员(包括1、2级和自身)的积分账户合计、余额账户合计 // 计算团队成员(包括1、2级和自身)ID $teamIds = $this->getTeamMoneyAndScore($item['id']); // 积分账户合计 $item['score_total'] = $teamIds['score']; // 余额账户合计 $item['money_total'] = $teamIds['money']; } $data = [ 'code' => 0, 'msg' => Db::getLastSql(), 'count' => $count, 'data' => $list, ]; return json($data); } /** * 获取团队自身,一级、二级自己的余额/积分 * @param $uid */ private function getTeamMoneyAndScore($uid) { $cacheKey = 'userLogicTeamIds_' . $uid; if (!Cache::has($cacheKey)) { // 自身 $user = Db::table(User::$table) ->where('id', $uid) ->field(['id', 'score', 'money']) ->find(); $totalScore = $user['score']; $totalMoney = $user['money']; // 一级子用户 Db::table(User::$table) ->where('pid', $uid) ->field(['id', 'score', 'money']) ->chunk(100, function ($users) use (&$totalScore, &$totalMoney) { foreach ($users as $user) { $uids[] = $user['id']; $totalScore += $user['score']; $totalMoney += $user['money']; } // 二级子用户 Db::table(User::$table) ->whereIn('pid', $uids) ->field(['id', 'score', 'money']) ->chunk(100, function ($users) use (&$totalScore, &$totalMoney) { foreach ($users as $user) { $totalScore += $user['score']; $totalMoney += $user['money']; } }); }, ['score', 'money']); Cache::set($cacheKey, ['score' => $totalScore, 'money' => sprintf("%.2f", $totalMoney)], 5 * 60); } return Cache::get($cacheKey); } /** * 修改用户手机号码 * @param mixed $uid * @param mixed $phone */ public function modifyPhone($uid, $phone) { // 判断手机号码是否使用过 $user = User::getUserById($uid); if (empty($user)) return '用户不存在'; if ($user['mobile'] == $phone) return "请输入新的手机号码"; if (User::getUserByMobile($phone)) return "该手机号码已注册"; // 修改用户信息 try { $updateData = [ 'mobile' => $phone, ]; // 按照账号原规则,手机号码作为用户名,同时修改新的用户名 if ($user['mobile'] == $user['user_name']) { $updateData['user_name'] = $phone; } $result = User::update($uid, $updateData); if (!$result) return "更新手机号码失败,请稍后重试"; } catch (\Exception $e) { return '失败' . $e->getMessage(); } return true; } /** * 修改用户所属上级 * @param mixed $uid * @param mixed $pid */ public function modifypid($uid, $pid) { $user = User::getUserById($uid); if (empty($user)) return '用户不存在'; // 查询pid是否存在用户path中 if ($uid == $pid) { return "不能修改自己为上级"; } if ($user['pid'] == $pid) { return "该用户已所属待变更的上级,不能修改"; } // 当前用户关系层级链路 $newUserPath = ""; if ($pid == 0) { $newPathPrefix = $user['id']; } else { $pUser = User::getUserById($pid); if (empty($pUser)) { return "待变更的上级用户不存在"; } $userPathArr = explode(',', $user['path']); if (in_array($pid, $userPathArr)) { $pidIndex = array_search($pid, $userPathArr); $userPathArrPrefix = array_slice($userPathArr, 0, $pidIndex + 1); } else { $pidIndex = array_search($user['pid'], $userPathArr); $userPathArrPrefix = array_slice($userPathArr, 0, $pidIndex); $userPathArrPrefix[] = $pid; } $newUserPath = implode(',', $userPathArrPrefix); // 子级用户Path待替换关系层级前部分链表 $userPathArrPrefix[] = $user['id']; $newPathPrefix = implode(',', $userPathArrPrefix); } // 子级用户Path被替换关系层级前部分链表 $oldPathPrefix = $user['path'] == 0 ? $uid : $user['path'] . ',' . $uid; Db::startTrans(); try { // 子级path变更 $result = Db::table(User::$table) ->where([ ['path', 'like', $oldPathPrefix . '%'], ['id', '<>', $uid] ]) ->field(['id', 'path']) ->chunk(100, function ($users) use ($pid, $oldPathPrefix, $newPathPrefix, $uid) { foreach ($users as $user) { $newPath = str_replace($oldPathPrefix, $newPathPrefix, $user['path']); User::modifyUserPath($user['id'], $newPath); } }); if (!$result) { Db::rollback(); return "修改用户所属上级失败,请确认用户的层级关系"; } // 更新用户Pid,Path User::modifyUserPidAndPath($uid, $pid, $newUserPath); Db::commit(); } catch (\Exception $exception) { Db::rollback(); return "失败:" . $exception->getMessage(); } return true; } /** * 修改余额 * @param mixed $post */ public function ModifyMoney($post) { $user = User::getUserById($post['uid']); if (empty($user)) { return "用户不存在"; } Db::startTrans(); try { if ($post['state'] == 1) { $afterMoney = $user['money'] + $post['money']; } else { $afterMoney = $user['money'] - $post['money']; if ($afterMoney < 0) { return "账号余额不足"; } } $afterMoney = round($afterMoney, 2); $moneyLog = [ 'uid' => $post['uid'], 'type' => $post['type'] ?? 0, 'money' => $post['money'], 'create_at' => date('Y-m-d H:i:s'), 'state' => $post['state'] ?? '0', 'from_id' => $post['from_id'] ?? '0', 'before_money' => $user['money'], 'after_money' => $afterMoney, 'uid2' => $post['uid2'] ?? 0, 'free_type' => $post['free_type'] ?? '', 'remark' => $post['remark'], ]; // 子级path变更 MoneyLog::AddMoneyLog($moneyLog); User::UpdateUserMoney($user['id'], $afterMoney); Db::commit(); } catch (\Exception $exception) { Db::rollback(); return "失败:" . $exception->getMessage(); } return true; } /** * 修改积分 * @param mixed $post */ public function ModifyScore($post) { $user = User::getUserById($post['uid']); if (empty($user)) { return "用户不存在"; } Db::startTrans(); try { if ($post['state'] == 1) { $afterScore = $user['score'] + $post['score']; } else { $afterScore = $user['score'] - $post['score']; if ($afterScore < 0) { return "账号积分不足"; } } $moneyLog = [ 'uid' => $post['uid'], 'type' => $post['type'] ?? 0, 'score' => $post['score'], 'create_at' => date('Y-m-d H:i:s'), 'scene' => $post['scene'] ?? 0, 'from_id' => $post['from_id'] ?? '0', 'state' => $post['state'] ?? '0', 'before_score' => $user['score'], 'after_score' => $afterScore, 'uid2' => $post['uid2'] ?? 0, ]; // 子级path变更 ScoreLog::AddScoreLog($moneyLog); User::UpdateUserScore($user['id'], $afterScore); Db::commit(); } catch (\Exception $exception) { Db::rollback(); return "失败:" . $exception->getMessage(); } return true; } /** * 删除用户 * @param $id */ public function delUser($id) { $user = User::getUserById($id); if (empty($user)) { return "用户不存在"; } if (User::updateStatus($id, 3) !== 1) { return false; } return true; } /* 注册用户数量统计 * @param string $time 时间节点如:2023-03-01 * @return mixed * @throws \think\db\exception\DbException */ public static function getCountByTime($time = '0') { $cacheKey = "caches:user:counts_{$time}"; if (!Cache::has($cacheKey)) { $data = UserModel::where(['status' => 1]) ->where(function ($query) use ($time) { if ($time) { $query->where('reg_time', '>=', $time); } })->count('id'); if ($data) { Cache::set($cacheKey, $data, rand(3, 5)); } return $data; } return Cache::get($cacheKey); } }