userModel = new UserModel; $this->tokenModel = new Token; $this->orgRoleModel = new OrgRole; $this->orgModel = new Org; $this->userRoleModel = new UserRole; $this->maxtrynum = sysconfig('account.PwdMaxTry'); } /** * @title: 组装查询和导出的条件 * @desc: * @param {*} * @return {*} * @Author: Rock * @Date: 2021-11-18 10:55:35 * @LastEditTime: Do not edit */ private function createWhere() { $data = $this->request->param(); $keyword = $data['keyword'] ?? ''; $status = $data['status'] ?? 0; $roleCode = $data['roleCode'] ?? ''; $org_id = $data['org_id'] ?? 0; $ids = $data['ids'] ?? []; $where = []; $whereOr = []; // 有传IDS时 if (!empty($ids)) { $where[] = ['user_id', 'IN', $ids]; } else { //有传组织ID时 if (!empty($org_id) && $org_id > 0) { $org_ids = $this->orgModel->getChildrenIds($org_id, true); $ids = $this->userRoleModel->where('org_id', 'IN', $org_ids)->column('user_id'); $where[] = ['user_id', 'IN', $ids]; } // else { // //组织ID限制 //// $org_ids = $this->orgModel->getChildrenIds($this->userinfo['org_id'], true); //// $ids = $this->userRoleModel->where('org_id', 'IN', $org_ids)->column('user_id'); //// $where[] = ['user_id', 'IN', array_unique($ids)]; //// $whereOr[] = ['create_uid', '=', $this->userinfo['user_id']]; // } //如果有传角色编码 if (!empty($roleCode)) { $ids = $this->userRoleModel->where('role_code', 'LIKE', "%$roleCode%")->column('user_id'); $where[] = ['user_id', 'IN', $ids]; } // 如果有传状态 if (!empty($status)) { $where[] = ['status', '=', $status]; } // 如果是搜索 if (!empty($keyword)) { $where[] = ["username|nickname|phone|idcard|name", "LIKE", "%$keyword%"]; } } return ['where' => $where, 'whereOr' => $whereOr]; } /** * @title: 获取用户列表 * @param {int} {pageNo} {非必填,默认值为1} {页码} * @param {int} {pageSize} {非必填,默认值为10} {每页数量} * @param {int} {org_id} {非必填,自动获取} {组织id} * @param {array} {ids} {非必填} {id区间} * @param {string} {keyword} {非必填} {搜索关键词,帐号,姓名,电话,身份证号码} * @param {int} {status} {非必填} {状态筛选,1=正常,2=禁用} * @param {string} {roleCode} {非必填} {角色code搜索} * @return array * @Author: Rock */ public function getList($pageNo = 1, $pageSize = 10) { $data = $this->request->param(); $whereAry = $this->createWhere(); $where = $whereAry['where']; $whereOr = $whereAry['whereOr']; $list = $this->userModel->where($where)->whereOr($whereOr)->with(['roles'])->append(['sex_txt', 'status_txt', 'is_ensure_txt'])->paginate(['page' => $pageNo, 'list_rows' => $pageSize]); $total = $list->total(); $list = FieldConverList($list->items()); return pageRes(1, "获取成功", $total, $list, $where); } /** * @title 用户登录 * @desc 说明 * @method GET/POST * @param {string} {username} {必填} {登录账户:账号或手机号或身份证号码} * @param {string} {password} {必填} {登录密码} * @param {string} {code} {必填} {图片验证码} * @param {string} {login_role} {必填,默认SUPERADMIN} {登录角色码,总后台只能SUPERADMIN登录} * @param {int} {login_org} {必填,默认1} {登录单位ID,总后台只能集团下的超级管理员登录} * @return array * @author Rock */ public function login($username = '', $password = '', $code = '', $login_role = 'SUPERADMIN', $login_org = 1) { try { // 验证码 if (!IsMobileAccess() && !parent::captcha_check($code)) { return Result(2, "验证码错误", $code); } $res = (new platformAuth())->interfaceRequest('accountLogin', ['username' => $username, 'password' => $password]); if (!$res['code']) return Result(2, $res['msg']); $fetchUser = $res['data']; $user = UserModel::where('uuid', $fetchUser['uuid'])->find(); if (!$user->is_developer) { UserRoleHandle::handleRole($user->user_id); $userRole = UserRole::where('user_id', $user->user_id)->find(); if ($userRole) { $login_org = $userRole['org_id'] ?? $login_org; $login_role = $this->orgRoleModel->where('role_id', $userRole['role_id'])->value('code'); } } $token = $this->tokenModel->updatetoken($user->user_id, $login_org, $login_role); $log = slog(1, $user->username . "使用帐号密码登录成功"); return Result(1, '登录成功', ['token' => $token, 'user_id' => $user->user_id, 'user' => $this->tokenModel->tokenUser($token), 'log' => $log]); // $res = $this->accountLogin($username, $password, $login_org, $login_role); // if ($res['code'] == 1) { // $token = $res['data']['token']; // $user_id = $res['data']['user_id']; // $data = [ // 'token' => $token, // 'role' => $res['data']['user']['role'], // ]; // } // return json($res); } catch (\Exception $e) { return res(2, '系统繁忙', $e->getFile() . '第' . $e->getLine() . '行:' . $e->getMessage(), $e->getTrace()); } } /** * @title: 账号/手机号/身份证号密码登录 * @param {string} {username} {必填} {登录账户:帐号/手机号} * @param {string} {password} {必填} {登录密码} * @param {int} {login_org} {必填} {登录单位} * @param {string} {login_role} {必填} {登录角色编码} * @return array * @Author: Rock */ private function accountLogin($username, $password, $login_org, $login_role) { $where = []; try { // 查找用户信息 $where[] = ['username|phone|idcard', '=', $username]; $user = $this->userModel->where($where)->find(); if (empty($user)) { return Result(0, '帐号或密码错误'); } // 密码比对 if (!CheckEncrypt($user->salt, $password, $user->password)) { // 密码不对,尝试次数+1 $user->inc('trynum', 1)->update(); // 剩余尝试次数 $Surplus = $this->maxtrynum - $user->trynum; if ($user->trynum > $this->maxtrynum) { $user->save(['status' => 2]); slog(1, $user->username . "登录失败次数超过限制,账户已被禁用"); return Result(0, '帐号或密码错误'); } return Result(0, '帐号或密码错误,您还有' . $Surplus . '次机会。'); } // 判断账号状态 if ($user->status == 2) { return Result(0, '此帐号已被禁用'); } // 判断是否是简单密码 $PwdComplexity = UserModel::checkPwdComplexity($password); $complexity = $PwdComplexity['code'] == 1 ? 0 : 1; // 判断是否为默认密码 if ($complexity == 0) { if (trim($password) == trim(sysconfig('account.default_pwd'))) { $complexity = 1; } } if (!$user->is_developer) { $userRole = UserRole::where('user_id', $user->user_id)->find(); if ($userRole) { $login_org = $userRole['org_id'] ?? $login_org; $login_role = $this->orgRoleModel->where('role_id', $userRole['role_id'])->value('code'); } } // 判断用户是否已绑定该角色 $userRole = $this->userRoleModel->where('user_id', $user->user_id)->where('org_id', $login_org)->where('role_code', $login_role)->find(); if (empty($userRole)) { return Result(0, "此账号未绑定对应角色"); } $user->trynum = 0; $user->complexity = $complexity; $user->save(); $token = $this->tokenModel->updatetoken($user->user_id, $userRole['org_id'], $login_role); $log = slog(1, $user->username . "使用帐号密码登录成功"); return Result(1, '登录成功', ['token' => $token, 'user_id' => $user->user_id, 'user' => $this->tokenModel->tokenUser($token), 'log' => $log]); } catch (\Exception $e) { return Result(0, "登录失败", $e->getFile() . "第" . $e->getLine() . "行:" . $e->getMessage(), $e->getTrace()); } } /** * Desc :第三方登录 * User : zwq * Date : 2025-01-18 14:49 */ public function socialLogin() { $data = $this->request->param(); if (empty($data['uuid'])) return res(0, '缺少参数'); $uuid = $data['uuid']; $user = UserModel::where('uuid', $uuid)->find(); if (empty($user)) {//没有账号 //创建账号以及分配角色 $user = $this->userModel->create_user($data); UserRole::create(['user_id' => $user['user_id'], 'role_id' => $data['role_id'], 'org_id' => $data['org_id'], 'role_code' => $data['role_code']]); $token = $this->tokenModel->updatetoken($user->user_id, $data['org_id'], $data['role_code']); } else { //更新账号信息 $user->replace()->save($data); //更新用户绑定角色信息 $token = $this->tokenModel->updatetoken($user->user_id, $data['org_id'], $data['role_code']); } return Result(1, '登录成功', ['token' => $token, 'user_id' => $user->user_id, 'user' => $this->tokenModel->tokenUser($token)]); } /** * @title: 获取指定用户信息 * @param {int} {user_id} {必填} {用户id} * @return array * @Author: wangkewei * @Date: 2021/5/18 10:17 */ public function getUserInfo($user_id = 0) { if ($user_id) { $res = $this->userModel->where('user_id', $user_id)->hidden(['password', 'salt'])->append(['roles'])->find(); $res->avatar_base64 = file2base64(public_path() . $res->avatar); } else { $tokenUser = $this->userinfo; $user_id = $this->userinfo['user_id']; if (empty($tokenUser)) { return res(2, "TOKEN验证失败"); } else { $res = $tokenUser; $res['avatar'] = !empty($res['avatar']) ? $res['avatar'] : sysconfig('account.default_avatar'); $res['avatar_base64'] = file2base64(public_path() . $res['avatar']); } } //todo 获取所有权限路径 $res['permissions'] = \app\common\model\base\user\User::getUserRole($user_id, 'auth', $this->token) ?? []; // $res['permissions'] = UserModel::getReqAuth($user_id); return res(1, "获取成功", $res ?? []); } /** * @title: 新增/修改用户信息 * @param array * @return array * @Author: wangkewei * @Date: 2021/5/18 10:18 */ public function doEdit() { $data = $this->request->param(); $user_id = !empty($data['user_id']) ? $data['user_id'] : 0; $scene = !empty($user_id) ? 'edit' : 'add';//验证场景 $data['avatar'] = !empty($data['avatar']) ? $data['avatar'] : sysconfig('account.default_avatar'); //验证字段 $check = validate(UserValidate::class)->scene($scene)->check($data); if (true !== $check) { return res(2, $check); } // 编辑 if (!empty($user_id)) { // 判断是否有同一个手机号的帐号 $phoneUsed = $this->userModel->where('phone', $data['phone'])->where('user_id', '<>', $user_id)->value('user_id'); if (!empty($phoneUsed) && $phoneUsed != $user_id) { return res(2, "手机号已注册"); } // 判断账号是否已被使用 $accountUsed = $this->userModel->where('username', $data['username'])->where('user_id', '<>', $user_id)->value('user_id'); if (!empty($accountUsed) && $accountUsed != $user_id) { return res(2, "账号已被使用"); } if (isset($data['password'])) { unset($data['password']); } if (isset($data['salt'])) { unset($data['salt']); } $data['name'] = !empty($data['name']) ? $data['name'] : $data['nickname']; $info = $this->userModel->where('user_id', $user_id)->find(); $params = [ 'user' => [ 'uuid' => $info['uuid'], 'name' => $data['name'], 'phone' => $data['phone'], 'sex' => $data['sex'], ], ]; $res = (new platformAuth())->interfaceRequest('createAccount', $params); if (!$res['code']) return res(2, $res['msg']); $info->data($data, true); $info->save(); // 如果是当前用户在修改自己的信息,则更新缓存中的用户信息 $token = $this->token; if ($this->userinfo['user_id'] == $user_id) { Token::updateTokenUser($this->token); } } // 新增 else { // 记录创建人 $data['create_uid'] = $this->userinfo['user_id']; //生成uuid $data['uuid'] = Uuid::uuid4()->toString(); // 判断是否有同一个手机号的帐号 $phoneUsed = $this->userModel->where('phone', $data['phone'])->value('user_id'); if (!empty($phoneUsed)) { return res(2, "手机号已注册"); } $res = $this->userModel->create_user($data); if (is_string($res)) { return res(2, $res); } } // 同步到企业微信 if (1 == sysconfig('account.sync_corp')) { //开发团队和外部用户不同步 $developIds = $this->orgModel->where('org_type_code', 'in', ['SYSTEM', 'EXTERNAL'])->column('org_id'); if (!in_array($data['org_id'], $developIds)) { $this->userModel->createCorpUser($user_id); } } if (empty($user_id)) { slog(1, "创建了用户" . $data['name']); } else { slog(1, '修改了用户' . $data['name']); } cache('USERLIST', null); return res(1, "保存成功", $data); } /** * @title: 退出登录 * @return array * @Author: wangkewei * @Date: 2021/5/18 10:18 */ public function logout() { $token = $this->token; slog(1, "退出了系统");//记录日志需要获取当前用户,所以日志记录完后再销毁token $this->tokenModel->losetoken($token); return Res(1, "成功退出"); } /** * @title: 重置密码 * @param {int} {user_id} {必填} {用户id} * @return array * @Author: wangkewei * @Date: 2021/5/18 10:19 */ public function resetPwd($user_id = '') { if (empty($user_id)) { return res(2, "参数错误"); } $info = $this->userModel->where('user_id', $user_id)->find(); $res = $this->userModel->resetPwd($user_id); if ($res) { slog(1, "重置了" . $info->username . "的密码"); return res(1, "操作成功,密码已被重置为默认密码"); } else { slog(2, "重置" . $info->username . "的密码失败"); return res(2, "操作失败"); } } /** * @title: 修改自己的密码 * @desc: 修改自己的密码 * @param {string} {oldpwd} {必填} {原密码} * @param {string} {newpwd} {必填} {新密码} * @param {string} {renewPwd} {必填} {确认新密码} * @return {boolean} {} {} {修改结果} * @Author: Rock * @Date: 2021-06-05 09:43:13 * @LastEditTime: Do not edit */ public function changePwd($oldPwd = '', $newPwd = '', $renewPwd = '') { //检查密码复杂度 $PwdComplexity = UserModel::checkPwdComplexity($newPwd); if ($PwdComplexity['code'] == 2) { return json($PwdComplexity); } $userInfo = UserModel::find($this->userinfo['user_id']); if (empty($oldPwd) || empty($newPwd)) { return res(2, '原密码和新密码都不能为空'); } elseif (empty($renewPwd)) { return res(2, '确认密码不能为空'); } elseif ($newPwd != $renewPwd) { return res(2, "确认密码与新密码不一致"); } elseif (strlen($newPwd) < 6) { return res(2, '密码不能少于6个字符'); } // $res = $this->userModel->resetPwd($userInfo->user_id, $newPwd); $params = [ 'user' => [ 'uuid' => $userInfo->uuid, 'password' => $oldPwd, 'newPwd' => $renewPwd, ], ]; $res = (new platformAuth())->interfaceRequest('createAccount', $params); if (!$res['code']) return res(2, $res['msg']); return res(1, "密码修改成功"); } /** * @title: 通过手机短信验证码重设密码 * @desc: * @param {string} {mobile} {} {手机号} * @param {string} {newPassword} {} {新密码} * @param {string} {rePassword} {} {确认新密码} * @param {string} {code} {} {短信验证码} * @return {*} * @Author: Rock * @Date: 2021-12-03 15:07:31 * @LastEditTime: Do not edit */ public function resetPwdByCode(string $mobile, string $newPassword, string $rePassword, string $code) { $mobile = trim($mobile); $newPassword = trim($newPassword); $rePassword = trim($rePassword); $code = trim($code); $PwdComplexity = UserModel::checkPwdComplexity($newPassword); if ($PwdComplexity['code'] == 2) { return json($PwdComplexity); } if (empty($mobile) || empty($newPassword) || empty($rePassword) || empty($code)) { return res(2, "重设密码失败"); } if (trim($newPassword) != trim($rePassword)) { return res(2, "两次密码输入不一致"); } if (!smsModel::check($mobile, $code)) { return res(2, "验证码错误或失效"); } $userList = $this->userModel->where('phone', $mobile)->select(); foreach ($userList as $userInfo) { $res = $this->userModel->resetPwd($userInfo->user_id, $newPassword); slog(1, "修改了" . $userInfo->username . "的密码"); if ($res) { wssend($userInfo->user_id, 'changepwd', "您的帐号密码已修改,请重新登录!"); } } return res(1, "操作成功,手机号为$mobile 的所有帐号密码都已修改"); } /** * @title: 修改自己绑定的电话号码 * @param {string} {phone} {必填} {电话号码} * @return array * @Author: wangkewei * @Date: 2021/8/30 10:17 */ public function changeMobile() { $data = $this->request->param(); $in = ['phone' => $data['phone']]; if ($this->userinfo['username'] == $this->userinfo['mobile']) { $in['username'] = $data['phone']; } $this->userModel->where('user_id', $this->userinfo['user_id'])->save($in); return res(1, "操作成功"); } /** * @title: 删除用户 * @desc: * @param {int} {ids} {必填} {id} * @return array * @Author: Rock * @Date: 2021-05-07 15:48:37 * @LastEditTime: Do not edit */ public function doDelete($ids) { $where = []; if (!is_array($ids)) { $ids = explode(',', $ids); } $where[] = ['user_id', 'IN', $ids]; if (in_array($this->userinfo['user_id'], $ids)) { return res(2, '无法删除自己'); } $list = $this->userModel->where($where)->select(); try { UserModel::startTrans(); foreach ($list as $item) { if ($item->role_code == $this->userinfo['role_code']) { UserModel::rollback(); return res(2, "无法删除同级账号"); } $result = $this->userModel->deleteUser($item->user_id); $item->delete(); slog(1, "删除了用户" . $item->username); } cache('USERLIST', null); UserModel::commit(); return res(1, "删除成功"); } catch (\Exception $e) { UserModel::rollback(); return res(2, "删除失败", $e->getMessage()); } } /** * @title: 封禁/启用帐号 * @desc: * @param {mixed} {ids} {} {用户ID} * @return {*} * @Author: Rock * @Date: 2021-06-04 15:05:02 * @LastEditTime: Do not edit * @throws \Exception */ public function changeStatus($ids = [], $status = 0) { $where = []; if (empty($ids)) { return res(2, "参数错误"); } if (is_string($ids)) { $ids = explode(',', $ids); } elseif (is_int($ids)) { $ids = [$ids]; } $where[] = ['user_id', 'IN', $ids]; if (empty($status)) { UserModel::where($where)->update(['status' => Db::raw('ABS(3 * `status` - 5)')]); } else { UserModel::where($where)->update(['status' => $status]); } // 获取被封禁的帐号发送禁用通知 $userList = UserModel::where($where)->where('status', 2)->select(); foreach ($userList as $item) { slog(1, "封禁了用户" . $item->username); } // 获取被激活的帐号并记录日志 $userList = UserModel::where($where)->where('status', 1)->select(); foreach ($userList as $info) { slog(1, "激活了用户" . $info->username); $info->trynum = 0; $info->save(); } return res(1, "操作成功"); } /** * @title: 检查登录状态 * @desc: * @param {string} {token} {} {} * @return {*} * @Author: Rock * @Date: 2021-06-28 10:25:52 * @LastEditTime: Do not edit */ public function checkLogin($token = "") { $res = $this->checkToken($token); return res($res['code'], $res['msg']); } /** * @title: 清除缓存 * @desc: * @param {*} * @return {*} * @Author: Rock * @Date: 2021-11-27 20:39:31 * @LastEditTime: Do not edit */ public function clearCache() { if (!$this->userinfo['is_developer']) { return res(2, "没有权限"); } Cache::clear(); return res(1, "清除成功"); } /** * @title: 获取用户二维码 * @param {int} {user_id} {必填} {用户id} * @return array * @Author: wangkewei * @Date: 2021/5/17 16:03 */ public function getQrcode($user_id = 0) { $info = $this->userModel->where('user_id', $user_id)->find(); if (empty($info)) { return res(2, "用户不存在"); } $basepath = "qrcode" . DS . "user" . DS; $savepath = public_path() . $basepath; $filename = "user_" . $user_id . '.png'; $fullpath = $savepath . $filename; if (!is_file($fullpath)) { $data = $this->createQrcode($user_id, $info->name); } else { $data = [ 'filename' => $filename, 'savepath' => DS . $basepath . $filename, 'url' => WEBURL . DS . $basepath . $filename, ]; } return res(1, "获取成功", $data); } /** * @title: 生成用户二维码 * @desc: * @param {int} {user_id} {} {用户ID} * @param {string} {name} {} {二维码下的文本} * @return {*} * @Author: Rock * @Date: 2021-12-03 11:12:56 * @LastEditTime: Do not edit */ public function createQrcode($user_id, $name = '') { $basepath = "qrcode" . DS . "user" . DS; $savepath = public_path() . $basepath; $filename = "user_" . $user_id . '.png'; $fullpath = $savepath . $filename; if (!is_dir($savepath)) { mkdir($savepath, 0777, true); } //创建二维码 include_once(root_path() . '/extend/phpqrcode/qrcode.php'); $qrcode = CreateQRCode(WEBURL . '/index.php/index/pilot.user/info?user_id=' . $user_id, '400', $name, '', true, $fullpath); //在二维码上加入名字 $data = [ 'filename' => $filename, 'savepath' => DS . $basepath . $filename, 'url' => WEBURL . DS . $basepath . $filename, ]; return $data; } /** * @title: 获取图形验证码(PHP直接输出) * @desc: 描述 * @param {float} {timestamp} {} {随机小数} * @return {*} * @author: Rock * @method: POST * @Date: 2022-05-30 14:44:10 */ public function captcha() { $content = parent::captcha(); return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png'); } /** * @title: 导出数据 * @desc: * @param {*} * @return {*} * @Author: Rock * @Date: 2021-11-19 09:18:20 * @LastEditTime: Do not edit */ public function doExport() { $where = $this->createWhere(); $list = $this->userModel->where($where)->with(['org'])->select(); $rows = []; $header = array_values($this->importField); $rows[0] = $header; foreach ($list as $key => $item) { $index = $key + 1; foreach ($this->importField as $k => $v) { if ($k == 'role_id') { $rows[$index][] = $item['role']['name']; } elseif ($k == 'org_id') { $rows[$index][] = $item['org']['pathName']; } elseif ($k == 'sex') { $rows[$index][] = $item['sex_txt']; } else { $rows[$index][] = isset($item[$k . '_txt']) ? $item[$k . '_txt'] : $item[$k]; } } } //创建文件夹 $basepath = "uploads" . DS . "download" . DS . date('Ymd'); $savepath = public_path() . $basepath; if (!is_dir($savepath)) { mkdir($savepath, 0777, true); } //保存文件 require_once root_path() . "extend/excel/Excel.php"; $filename = time() . GetRandStr() . ".xls"; $fullpath = $savepath . DS . $filename; ArrayToXls($rows, $fullpath); //返回路径 $returnpath = WEBURL . DS . $basepath . DS . $filename; slog(1, "导出了用户列表"); return res(1, "获取成功", ['url' => $returnpath, 'name' => $filename]); } // 导出字段 protected $importField = [ 'org_id' => '所属组织', 'name' => '姓名', 'sex' => '性别', 'phone' => '手机号', 'role_id' => '角色', 'fax' => '传真', 'username' => '帐号', 'remark' => '备注', ]; /** * @title: 保存用户签名图片 * @desc: * @param {string} {sign_img} {} {签名图片} * @return {*} * @Author: Rock * @Date: 2022-01-25 09:33:58 * @LastEditTime: Do not edit */ public function saveSignImg($sign_img = '') { if (empty($sign_img)) { return res(2, "签名图片未上传"); } if (strpos($sign_img, WEBURL)) { $sign_img = $sign_img; $old = public_path() . $this->userinfo['sign_img']; $new = public_path() . $sign_img; } else { $sign_img = str_replace(WEBURL, '', $sign_img); $old = public_path() . $this->userinfo['sign_img']; $new = public_path() . $sign_img; } if (is_file($new)) { $this->userinfo['sign_img'] = $sign_img; $this->userModel->replace()->save($this->userinfo); } if (is_file($old)) { unlink($old); } return res(1, "保存成功"); } /** * @title: 检查密码复杂度 * @desc: 描述 * @param {string} {pwd} {} {密码} * @return {*} * @author: Rock * @method: POST * @Date: 2022-10-13 19:40:29 */ public function checkPwdComplexity(string $pwd = '') { $res = UserModel::checkPwdComplexity($pwd); return json($res); } /** * @title: 获取当前登录用户的权限 * @desc: 描述 * @return {*} * @author: Rock * @method: POST * @Date: 2023-02-02 14:39:05 */ public function getRouterList() { $fields = [ 'menu_id', 'pid', 'name', 'title', 'path', 'component', 'redirect', 'icon', 'is_root', 'is_parent', 'affix', 'status', 'parent_path', 'sort', 'hidden', 'noKeepAlive', 'tabHidden', ]; $where = []; $where[] = ['status', '=', 1]; $whereOr = []; $whereOr[] = ['name', 'IN', ['Root', 'index', 'UserCenter', 'Homepage', 'Workbench', 'System']]; if (!$this->userinfo['is_developer']) { $userInfo = $this->userinfo; $role_id = $userInfo['role_id']; $menu_ids = UserModel::getMenuIds($role_id); $where[] = ['menu_id', 'IN', $menu_ids]; } $list = Menu::where($where)->whereOr($whereOr)->field($fields)->order('sort')->select()->toArray(); foreach ($list as &$row) { $row['meta'] = []; $row['meta']['title'] = $row['title']; unset($row['title']); $row['meta']['icon'] = $row['icon']; unset($row['icon']); if (isset($row['hidden'])) { $row['meta']['hidden'] = $row['hidden'] == 1; unset($row['hidden']); } if (!empty($row['affix'])) { $row['meta']['affix'] = $row['affix'] == 1; unset($row['affix']); } if (!empty($row['dot'])) { $row['meta']['dot'] = $row['dot'] == 1; unset($row['dot']); } if (!empty($row['tabHidden'])) { $row['meta']['tabHidden'] = $row['tabHidden'] == 1; unset($row['tabHidden']); } if (!empty($row['noKeepAlive'])) { $row['meta']['noKeepAlive'] = $row['noKeepAlive'] == 1; unset($row['noKeepAlive']); } if (false !== strpos($row['path'], 'http')) { $row['meta']['target'] = '_blank'; } } $list = array2tree($list, 'pid', 'menu_id'); return res(1, '获取成功', $list); } /** * Notes:获取用户角色 * User: zhang * Date: 2025/2/19 * Time: 11:00 */ public function getUserRole($user_id = 0) { if (empty($user_id)) { $user_id = $this->userinfo['user_id']; } UserRoleHandle::handleRole($user_id); $list = $this->userRoleModel->where('user_id', $user_id)->select(); return res(1, '获取成功', $list); } /** * Notes:变更组织重新颁发token * User: zhang * Date: 2025/2/20 * Time: 14:40 */ public function changeOrg() { $org_id = input('org_id/d', ""); $role_code = input('role_code/s', ""); $userinfo = $this->userinfo; $token = $this->tokenModel->updatetoken($userinfo['user_id'], $org_id, $role_code); return res(1, '变更成功', $token); } }