From 7068fa69c2fcfac2a6db27f42b47ec65950914ec Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Mon, 5 Jan 2026 15:49:25 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/controller/UserController.php | 119 ++--------- app/common/logic/AuthLogic.php | 10 +- app/common/logic/UserLogic.php | 95 +-------- app/common/model/User.php | 78 +------- app/common/service/UserService.php | 242 +---------------------- app/common/validate/UserValidate.php | 145 ++------------ app/controller/IndexController.php | 12 +- app/middleware/AuthCheck.php | 7 +- config/route.php | 7 - 9 files changed, 63 insertions(+), 652 deletions(-) diff --git a/app/common/controller/UserController.php b/app/common/controller/UserController.php index f49fba0..38c768b 100644 --- a/app/common/controller/UserController.php +++ b/app/common/controller/UserController.php @@ -1,14 +1,12 @@ service->serviceTeam($request); - $data = $this->transformData($raw, method_exists($this, 'indexFormat') - ? [$this, 'indexFormat'] - : null); - return $this->success($data); - } catch (\Throwable $e) { - return $this->error($e->getMessage()); - } - } - - /** - * 标记组员离职 - * - * @param Request $request 请求对象 - * @return mixed 返回分页数据及总数 - */ - public function resignMember(Request $request) - { - try { - $userId = $request->get('user_id'); - if(empty($userId)){ - return $this->error('组员id不能为空'); - } - $user = $this->service->resignMember($request); - return $this->success($user, '获取用户信息'); - } catch (\Throwable $e) { - return $this->error($e->getMessage()); - } - } - - /** - * 用户信息(登录后调用) - */ - public function info(Request $request) - { - try { - $uid = $request->user->uid; - $systemCode = $request->system_code; - $result = (new UserService())->getUserInfo($uid, $systemCode); - return $this->success($result, '获取用户信息'); - } catch (\Throwable $e) { - return $this->error($e->getMessage()); - } - } - - /** - * 用户信息 - */ - public function getUser(Request $request) - { - try { - $uid = $request->user->uid; - $corp_abbr = $request->basic_data['corp_abbr']; - $result = (new UserService())->getUser($uid,$corp_abbr); - return $this->success($result, '获取用户信息'); - } catch (\Throwable $e) { - return $this->error($e->getMessage()); - } - } - - /** - * 编辑用户 - */ - public function edit(Request $request) - { - try { - $uid = $request->user->uid; - $data = $request->all(); - $result = (new UserService())->edit($uid,$data); - unset($result['password']); - unset($result['union_id']); - unset($result['open_id']); - unset($result['tenant_key']); - return $this->success($result, '编辑成功'); - } catch (\Throwable $e) { - return $this->error($e->getMessage()); - } - } - - /** - * 修改密码 - * @param Request $request - * @return \support\Response - * @throws \Random\RandomException - */ - public function savePassword(Request $request) - { - try { - $uid = $request->user->uid; - $data = $request->all(); - $result = (new UserService())->savePassword($uid,$data);; - return $this->success($result, '编辑成功'); - } catch (\Throwable $e) { - return $this->error($e->getMessage()); - } - } - - + // protected function indexFormat($data) + // { + // if (isset($data['created_at'])) + // $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']); + // if (isset($data['updated_at'])) + // $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']); + // return $data; + // } } \ No newline at end of file diff --git a/app/common/logic/AuthLogic.php b/app/common/logic/AuthLogic.php index 03b264c..ec8da32 100644 --- a/app/common/logic/AuthLogic.php +++ b/app/common/logic/AuthLogic.php @@ -213,8 +213,14 @@ class AuthLogic if ($userInfo['code'] != 0) { throw new BusinessException('授权失败,请重试'); } - - return $userInfo['data'] + $data ; + + + User::firstOrCreate(['union_id'=>$userInfo['data']['union_id']],[ + 'union_id'=>$userInfo['data']['union_id'], + 'name'=>$userInfo['data']['name'], + 'app_id'=>$data['app_id'] + ]); + return $userInfo['data'] + $data; } /** diff --git a/app/common/logic/UserLogic.php b/app/common/logic/UserLogic.php index 55e9608..4d83053 100644 --- a/app/common/logic/UserLogic.php +++ b/app/common/logic/UserLogic.php @@ -2,12 +2,8 @@ namespace app\common\logic; -use app\common\model\User; -use app\common\model\UserGroupMembers; use app\common\traits\crud\CrudLogicTrait; -use support\exception\BusinessException; -use support\Request; -use app\common\model\UserRoles; +use app\common\model\User; class UserLogic { @@ -18,93 +14,4 @@ class UserLogic */ protected string $modelClass = User::class; - /** - * 用户信息 - */ - public function userInfo(int $uid) - { - $user = User::with('reportToInfo')->find($uid); - - if (!$user) { - throw new BusinessException('账户不存在', 404); - } - - if ($user->status != 1) { - throw new BusinessException('账号已禁用', 403); - } - - return $user; - } - - /** - * 用户信息 - */ - public function edit(int $uid, array $data) - { - // - $user = User::find($uid); - - if (!$user) { - throw new BusinessException('账户不存在', 404); - } - - $user->fill($data); - $user->save(); - - // 绑定内勤 10.13 add - if (isset($data['office_clerk_id']) && $data['office_clerk_id'] > 0) { - $ug = UserGroupMembers::where('user_id', $uid)->where('status', 1)->first(); - if ($ug) { - $ug->delete(); - } - - $group = UserGroupMembers::where('user_id', $data['office_clerk_id'])->where('is_leader', 1)->where('status', 1)->first(); - if (!$group) { - throw new BusinessException('该内勤还未创建分组', 404); - } - - UserGroupMembers::create([ - 'user_id' => $uid, - 'group_id' => $group->group_id, - 'is_leader' => 0, - 'status' => 1 - ]); - } - - return $user; - } - - /** - * 修改密码 - */ - public function savePassword(int $uid, array $data) - { - $password = password_hash($data['password_new'], PASSWORD_DEFAULT); - $res = User::where('id',$uid)->update(['password' => $password]); - - return $res; - } - - /** - * 获取用户列表 - */ - public function getList(Request $request) - { - $p = $request->all(); - - $user = User::where('status', 1); - if (isset($p['role_id']) && $p['role_id'] > 0) { - $userIds = UserRoles::where('role_id', $p['role_id'])->pluck('user_id'); - $user->whereIn('id', $userIds); - } - $list = $user->orderBy('id', 'desc')->get(); - $arr = []; - foreach ($list->toArray() as $item) { - $arr[] = [ - 'name' => $item['name'], - 'id' => $item['id'], - ]; - } - return $arr; - } } \ No newline at end of file diff --git a/app/common/model/User.php b/app/common/model/User.php index 9a9a4fa..58e9baa 100644 --- a/app/common/model/User.php +++ b/app/common/model/User.php @@ -4,21 +4,6 @@ namespace app\common\model; use support\Model; -/** - * users 用户表 - * @property integer $id 用户ID(主键) - * @property string $sn 编号 - * @property string $name 姓名 - * @property string $avatar 头像 - * @property string $password 密码 - * @property string $phone 手机 - * @property string $email 邮箱 - * @property integer $status 状态 - * @property string $system_code 系统码 - * @property string $report_to 上级用户ID - * @property string $created_at - * @property string $updated_at - */ class User extends Model { /** @@ -48,60 +33,17 @@ class User extends Model * @var bool */ public $timestamps = true; - + + protected $dateFormat = 'U'; - - protected $fillable = [ - 'sn', - 'name', - 'avatar', - 'sex', - 'password', - 'phone', - 'email', - 'status', - 'corp_id', - 'user_type', - 'union_id', - 'user_id', - 'open_id', - 'tenant_key', - 'country_id', - 'position', - 'socialize', - 'socialize_pic', - 'report_to', - ]; - - // 用户所有角色 - public function roles() - { - return $this->belongsToMany(Role::class, 'user_roles', 'user_id', 'role_id'); - } - // 用户直接拥有的资源(用于特殊补权) - public function resources() - { - return $this->belongsToMany(Resource::class, 'user_resources', 'user_id', 'resource_id')->orderBy('sort', 'asc'); - } + /** + * 允许自动填充模型字段 + */ + protected $fillable = ['id', 'union_id', 'app_id', 'name']; - // 用户可访问的系统 - public function systems() - { - return $this->belongsToMany(Systems::class, 'user_systems', 'user_id', 'system_id'); - } - - // 账号查询的作用域 - public function scopeByAccount($query, $account) - { - return $query->where('sn', $account) - ->orWhere('phone', $account) - ->orWhere('email', $account); - } - - - public function reportToInfo() - { - return $this->hasOne(User::class, 'id', 'report_to'); - } + /** + * 不允许自动填充模型字段 + */ + protected $guarded = []; } \ No newline at end of file diff --git a/app/common/service/UserService.php b/app/common/service/UserService.php index bb8ae75..89c5725 100644 --- a/app/common/service/UserService.php +++ b/app/common/service/UserService.php @@ -2,21 +2,13 @@ namespace app\common\service; -use app\common\logic\UserLogic; -use app\common\model\Corp; -use app\common\model\User; -use app\common\model\UserGroup; -use app\common\model\UserGroupMembers; -use app\common\traits\crud\CrudLogicTrait; +use app\common\validate\UserValidate; use app\common\traits\crud\CrudServiceTrait; -use app\common\utils\CrudUtil; -use app\crm\model\Country; -use app\crm\model\Customer; -use support\exception\BusinessException; -use support\Request; +use app\common\model\User; class UserService { + use CrudServiceTrait; /** @@ -24,230 +16,16 @@ class UserService */ protected string $modelClass = User::class; - protected $logic; - - public function __construct() - { - $this->logic = (new UserLogic()); - } + /** + * 验证器 + */ + protected $validator = UserValidate::class; /** - * 获取用户信息(包括菜单和权限) + * 获取验证器 */ - public function getUserInfo(int $uid, string $systemCode) + public function getValidator() { - $user = $this->logic->userInfo($uid); - // $permService = new PermService(); - // $menuTree = $permService->getMenuTree($uid, $systemCode); - // $permissionCodes = $permService->getPermissionCodes($uid, $systemCode); - $corp_name = Corp::where('abbr', $user->corp_id)->value('name'); - return [ - 'user' => [ - 'id' => $user->id, - 'name' => $user->name, - 'avatar' => $user->avatar, - 'phone' => $user->phone, - 'email' => $user->email, - 'status' => $user->status, - 'roles' => $user->roles->pluck('name'), - 'sex' => $user->sex, - 'sn' => $user->sn, - 'corp_id' => $user->corp_id, - 'corp_name' => $corp_name, - 'report_to' => $user->report_to, - 'sign_img' => $user->sign_img, - 'sign_img_url' => (!empty($user->sign_img))?(new \app\common\service\OssService)->generatePrivateUrl($user->sign_img):'', - ], - // 'menu' => $menuTree, - // 'permissions' => $permissionCodes - ]; - } - - - /** - * 获取用户信息(包括菜单和权限) - */ - public function getUser(int $uid, $corp_abbr) - { - $user = $this->logic->userInfo($uid); - - // 查找直属上级 10.13 add - $reportToInfo = []; - if ($user->reportToInfo) { - $reportToInfo = [ - 'id' => $user->reportToInfo->id, - 'name' => $user->reportToInfo->name - ]; - } - - // 查找绑定内勤 10.13 add - $officeClerkInfo = []; - $ugm = UserGroupMembers::where('user_id', $uid)->where('status', 1)->first(); - if ($ugm) { - $lugm = UserGroupMembers::where('group_id', $ugm->group_id)->where('is_leader', 1)->first(); - if ($lugm) { - $officeClerkInfo['id'] = $lugm->user_id; - $officeClerkInfo['name'] = User::where('id', $lugm->user_id)->value('name'); - } - } - - $corp_name = Corp::where('abbr', $corp_abbr)->value('name'); - $ossservice = new OssService(); - return [ - 'id' => $user->id, - 'sn' => $user->sn, - 'name' => $user->name, - 'position' => $user->position, - 'socialize' => $user->socialize, - 'socialize_pic' => $user->socialize_pic, - 'socialize_pic_full' => (!empty($user->socialize_pic))?$ossservice->generatePrivateUrl($user->socialize_pic):'', - 'country_id' => $user->country_id, - 'country_txt' => Country::where('id',$user->country_id)->value('cn_addr'), - 'avatar' => $user->avatar, - 'avatar_full' => $user->avatar, - 'phone' => $user->phone, - 'corp_name' => $corp_name, - 'email' => $user->email, - 'sex' => $user->sex, - 'status' => $user->status, - 'roles' => $user->roles->pluck('name'), - 'sign_img' => $user->sign_img, - 'sign_img_url' => (!empty($user->sign_img))?$ossservice->generatePrivateUrl($user->sign_img):'', - 'report_to_info' => $reportToInfo, - 'office_clerk_info' => $officeClerkInfo - ]; - } - - /** - * 编辑用户信息 - */ - public function edit(int $uid, array $data) - { - unset($data['password']); - unset($data['phone']); - unset($data['email']); - unset($data['sn']); - - $user = $this->logic->edit($uid,$data); - return $user; - } - - /** - * 修改用户密码 - */ - public function savePassword(int $uid, array $data) - { - // 对密码进行哈希处理 - $password = $data['password']; - $user = User::find($uid); - if (!password_verify($password, $user->password)) { - throw new BusinessException('密码错误'); - } - - $user = $this->logic->savePassword($uid,$data); - return $user; - } - - /** - * 获取用户列表 - */ - public function getList(Request $request) - { - return $this->logic->getList($request); - } - - /** 分页列表 */ - public function serviceTeam(Request $request) - { - $config = $this->buildQueryConfig($request, 'team'); - $limit = (int)$request->get('size', 15); - $page = (int)$request->get('current', 1); - $result = $this->logicList($config, $limit, $page); - - foreach ($result as &$data){ - $data['customer_count'] = Customer::where('user_id', $data['id'])->count(); - unset($data['password']); - unset($data['open_id']); - unset($data['tenant_key']); - unset($data['union_id']); - } - - return $this->postProcess('List', $result); - } - - /** 分页列表 */ - public function resignMember(Request $request) - { - $uid = $request->user->uid; - $userId = $request->get('user_id'); - $user = User::find($userId); - if ($user->report_to != $uid) { - throw new BusinessException('禁止操作非组员'); - } - - $data = [ - 'status' => 2, - ]; - $user = $this->logic->edit($userId,$data); - $user = $user->toArray(); - unset($user['password']); - unset($user['open_id']); - unset($user['tenant_key']); - unset($user['union_id']); - return $user; - } - - /** - * 构造 CrudUtil 查询配置 - */ - protected function buildQueryConfig(Request $request, string $scene = 'default'): array - { - $p = $request->all(); - - $config = []; - $config['model'] = $this->modelClass; - switch ($scene) { - case 'team': -// $config['where']['status'] = 1; // 关键字 - $config['order']['id'] = 'desc'; - - if (isset($p['status']) && $p['status']) { - $config['where']['status'] = $p['status']; - } - - $config['with']= ['reportToInfo']; - if (!in_array(1, $request->user->role_ids)) { - $config['where']['reportToInfo.id'] = $request->user->uid; - } - - // $config['dataLimit']['type'] = 'relation'; - // $config['dataLimit']['userId'] = $request->user->uid; - // $config['dataLimit']['userField'] = 'report_to'; - break; - } - - return $config; - } - - - public function checkBind($userId) - { - $user = \app\common\model\User::find($userId); - if (!$user->report_to) { - throw new BusinessException('请先前往个人中心-绑定直属上级'); - } - - if ($user->sign_img === null || $user->sign_img === '') { - throw new BusinessException('请先前往个人中心-操作签名'); - } - - $ug = \app\common\model\UserGroupMembers::with(['group' => function($query) { - $query->where('scenario_code', 'stock_orders'); - }])->where('user_id', $userId)->first(); - if (!$ug) { - throw new BusinessException('请先前往个人中心-绑定内勤'); - } - - return true; + return new $this->validator; } } \ No newline at end of file diff --git a/app/common/validate/UserValidate.php b/app/common/validate/UserValidate.php index dcdaa9b..d792cb3 100644 --- a/app/common/validate/UserValidate.php +++ b/app/common/validate/UserValidate.php @@ -2,149 +2,28 @@ namespace app\common\validate; -use app\common\model\Corp; -use app\common\model\Systems; -use app\common\model\User; use think\Validate; class UserValidate extends Validate { + // 规则 protected $rule = [ - 'name' => 'chsDash|max:60', - 'avatar' => 'regex:/^(https?:\/\/\S+|\\/[\S\/]+)$/', - 'password' => 'require|min:8|max:20|alphaNum', - 'confirm_password' => 'require|confirm:password', - 'account' => 'require|checkAccount|checkAccountUnique', - 'phone' => 'checkMobile|checkPhoneUnique', - 'email' => 'email|checkEmailUnique', - 'login_type' => 'require|in:upwd,feishu,qrcode', - 'system_code' => 'require|checkSystemCode', - 'corp_abbr' => 'require|checkCorpAbbr', - 'feishu_code' => 'require|string' + ]; + // 提示 protected $message = [ - 'name.chsDash' => '姓名只能是汉字、字母、数字和下划线_及破折号-', - 'name.max' => '姓名最多不能超过60个字符', - 'avatar.regex' => '头像必须是合法的URL或有效的文件路径', - 'password.require' => '密码不能为空', - 'password.min' => '密码长度不能少于8位', - 'password.max' => '密码长度不能超过20位', - 'password.alphaNum' => '密码只能包含字母和数字', - 'confirm_password.require' => '请再次输入密码', - 'confirm_password.confirm' => '两次输入的密码不一致', - 'account.require' => '账号不能为空', - 'account.checkAccount' => '账号必须是有效的手机号或邮箱', - 'account.checkAccountUnique' => '账号已被注册', - 'phone.checkMobile' => '手机号格式不正确', - 'phone.checkPhoneUnique' => '手机号已被使用', - 'email.email' => '邮箱格式不正确', - 'email.checkEmailUnique' => '邮箱已被使用', - 'login_type.require' => '请选择登录方式', - 'login_type.in' => '登录方式不支持', - 'system_code.require' => '系统码不能为空', - 'system_code.checkSystemCode' => '系统码不存在或不合法', - 'corp_abbr.require' => '公司标识不能为空', - 'corp_abbr.checkCorpAbbr' => '公司标识不存在或不合法', - 'feishu_code.require' => '飞书code不能为空', - 'feishu_code.string' => '飞书code必须是字符串' + ]; + // 场景 protected $scene = [ - 'upwd_register' => ['name', 'account', 'password', 'confirm_password'], - 'feishu_register' => ['feishu_code'], - 'upwd_login' => ['account' => 'require|checkAccount', 'password'], - 'feishu_auth' => ['corp_abbr', 'system_code', 'login_type'], - 'feishu_login' => ['feishu_code'], + 'store' => [ + + ], + + 'update' => [ + + ] ]; - - /** - * 验证账号格式(手机号或邮箱) - */ - protected function checkAccount($value) - { - return $this->isMobile($value) || $this->isEmail($value); - } - - /** - * 验证账号唯一性(注册场景用) - */ - protected function checkAccountUnique($value, $rule, $data, $field, $title) - { - // 登录场景不检查唯一性 - if (isset($this->currentScene) && $this->currentScene === 'login') { - return true; - } - - $type = $this->getAccountType($value); - $field = ($type === 'phone') ? 'phone' : 'email'; - $value = ($type === 'phone') ? $this->formatPhone($value) : $value; - - return !User::where($field, $value)->exists(); - } - - /** - * 判断是否为手机号 - */ - protected function isMobile($value) - { - $mobile = preg_replace('/[\s+\-]/', '', $value); - return preg_match('/^(\+86|0086)?1[3-9]\d{9}$/', $mobile); - } - - /** - * 判断是否为邮箱 - */ - protected function isEmail($value) - { - return filter_var($value, FILTER_VALIDATE_EMAIL); - } - - /** - * 获取账号类型(phone/email) - */ - public function getAccountType($account) - { - return $this->isMobile($account) ? 'phone' : 'email'; - } - - /** - * 格式化手机号(移除+86和空格) - */ - public function formatPhone($phone) - { - return preg_replace('/^(\+86|0086)|[\s\-]/', '', $phone); - } - - /** - * 手机号唯一性验证 - */ - protected function checkPhoneUnique($value) - { - return !User::where('phone', $this->formatPhone($value))->exists(); - } - - /** - * 邮箱唯一性验证 - */ - protected function checkEmailUnique($value) - { - return !User::where('email', $value)->exists(); - } - - /** - * 验证 system_code 是否在系统表中存在 - */ - protected function checkSystemCode($value, $rule, $data, $field) - { - return Systems::where('code', $value)->exists(); - } - - /** - * 验证 corp_abbr 企业标识是否存在 - */ - protected function checkCorpAbbr($value, $rule, $data, $field) - { - return Corp::where('abbr', $value)->exists(); - } } \ No newline at end of file diff --git a/app/controller/IndexController.php b/app/controller/IndexController.php index d13af2f..3f279cd 100644 --- a/app/controller/IndexController.php +++ b/app/controller/IndexController.php @@ -74,25 +74,25 @@ class IndexController $options = [ 'controller' => [ 'module' => 'common', - 'name' => 'used_car' + 'name' => 'user' ], 'model' => [ 'module' => 'common', - 'name' => 'used_car' + 'name' => 'user' ], 'service' => [ 'module' => 'common', - 'name' => 'used_car' + 'name' => 'user' ], 'logic' => [ 'module' => 'common', - 'name' => 'used_car' + 'name' => 'user' ], 'validate' => [ 'module' => 'common', - 'name' => 'used_car' + 'name' => 'user' ], - 'table' => 'used_car' + 'table' => 'user' ]; $result = (new Generate($options))->doGenerate(); diff --git a/app/middleware/AuthCheck.php b/app/middleware/AuthCheck.php index a03e632..0c44789 100644 --- a/app/middleware/AuthCheck.php +++ b/app/middleware/AuthCheck.php @@ -1,6 +1,7 @@ header('Fdesk-Auth-Token'); - if (!$authHeader || !preg_match('/Bearer\s(\S+)/', $authHeader, $matches)) { + $user = User::where('union_id',$authHeader)->first(); + if (!$user) { $body = [ 'status' => 'fail', 'code' => 401, @@ -31,8 +33,7 @@ class AuthCheck implements MiddlewareInterface ]; return response(json_encode($body, JSON_UNESCAPED_UNICODE), 401, ['Content-Type' => 'application/json']); } - $token = $matches[1]; - $request->user = (new Jwt())->verifyToken($token); + $request->user = $user; return $handler($request); } catch (\Throwable $e) { $body = [ diff --git a/config/route.php b/config/route.php index 2597f6b..450c176 100644 --- a/config/route.php +++ b/config/route.php @@ -51,13 +51,6 @@ Route::group('/common', function () { // // 调试 Route::any('/feishu/auth', [app\common\controller\AuthController::class, 'feishuAuth']); }); - - Route::group('/user', function () { - Route::any('/info', [app\common\controller\UserController::class, 'info']); - Route::any('/get', [app\common\controller\UserController::class, 'getUser']); - Route::any('/team', [app\common\controller\UserController::class, 'team']); - Route::any('/resign', [app\common\controller\UserController::class, 'resignMember']); - }); Route::group('/usedcar', function () { Route::any('/list', [app\common\controller\UsedCarController::class, 'index']);