From 4a316dc93243c34089393f031a7c54053c00b22d Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Mon, 6 May 2024 10:57:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/LoginController.php | 13 + app/api/controller/user/UserController.php | 38 +++ app/api/logic/LoginLogic.php | 14 +- app/api/logic/user/UserLogic.php | 64 +++++ app/api/service/WechatUserService.php | 5 +- app/api/validate/UserValidate.php | 50 ++++ app/api/validate/WechatLoginValidate.php | 4 +- app/common/enum/notice/NoticeEnum.php | 261 +++++++++++++++++++++ app/common/model/user/User.php | 4 +- 9 files changed, 446 insertions(+), 7 deletions(-) create mode 100644 app/api/controller/user/UserController.php create mode 100644 app/api/logic/user/UserLogic.php create mode 100644 app/api/validate/UserValidate.php create mode 100644 app/common/enum/notice/NoticeEnum.php diff --git a/app/api/controller/LoginController.php b/app/api/controller/LoginController.php index e5aea0a..31259c8 100644 --- a/app/api/controller/LoginController.php +++ b/app/api/controller/LoginController.php @@ -65,4 +65,17 @@ class LoginController extends BaseApiController } return $this->success('绑定成功', [], 1, 1); } + + /** + * @notes 更新用户头像昵称 + * @return \think\response\Json + * @author 段誉 + * @date 2023/2/22 11:15 + */ + public function updateUser() + { + $params = (new WechatLoginValidate())->post()->goCheck("updateUser"); + LoginLogic::updateUser($params, $this->userId); + return $this->success('操作成功', [], 1, 1); + } } diff --git a/app/api/controller/user/UserController.php b/app/api/controller/user/UserController.php new file mode 100644 index 0000000..5108d0c --- /dev/null +++ b/app/api/controller/user/UserController.php @@ -0,0 +1,38 @@ +post()->goCheck('getMobileByMnp'); + $params['user_id'] = $this->userId; + $result = UserLogic::getMobileByMnp($params); + if ($result === false) { + return $this->fail(UserLogic::getError()); + } + return $this->success('绑定成功', [], 1, 1); + } + + +} \ No newline at end of file diff --git a/app/api/logic/LoginLogic.php b/app/api/logic/LoginLogic.php index 59e7ae5..e995015 100644 --- a/app/api/logic/LoginLogic.php +++ b/app/api/logic/LoginLogic.php @@ -421,10 +421,16 @@ class LoginLogic extends BaseLogic */ public static function updateUser($params, $userId) { - return User::where(['id' => $userId])->update([ - 'nickname' => $params['nickname'], - 'avatar' => FileService::setFileUrl($params['avatar']), + $data=[ + 'mobile'=>$params['mobile'], 'is_new_user' => YesNoEnum::NO - ]); + ]; + if(!empty($params['nickname'])){ + $data['nickname'] = $params['nickname']; + } + if(!empty($params['avatar'])){ + $data['avatar'] = FileService::setFileUrl($params['avatar']); + } + return User::where(['id' => $userId])->update($data); } } \ No newline at end of file diff --git a/app/api/logic/user/UserLogic.php b/app/api/logic/user/UserLogic.php new file mode 100644 index 0000000..73b2e1e --- /dev/null +++ b/app/api/logic/user/UserLogic.php @@ -0,0 +1,64 @@ +getUserPhoneNumber($params['code']); + $phoneNumber = $response['phone_info']['purePhoneNumber'] ?? ''; + if (empty($phoneNumber)) { + throw new \Exception('获取手机号码失败'); + } + + $user = User::where([ + ['mobile', '=', $phoneNumber], + ['id', '<>', $params['user_id']] + ])->findOrEmpty(); + + if (!$user->isEmpty()) { + throw new \Exception('手机号已被其他账号绑定'); + } + + // 绑定手机号 + User::update([ + 'id' => $params['user_id'], + 'mobile' => $phoneNumber + ]); + + return true; + } catch (\Exception $e) { + self::setError($e->getMessage()); + return false; + } + } + + +} \ No newline at end of file diff --git a/app/api/service/WechatUserService.php b/app/api/service/WechatUserService.php index 6da7c6c..3fe97ae 100644 --- a/app/api/service/WechatUserService.php +++ b/app/api/service/WechatUserService.php @@ -76,8 +76,10 @@ class WechatUserService ->findOrEmpty(); $this->user = $user; + if(!$user->isEmpty()){ + $this->user->supplier=Supplier::where('uid',$user['id'])->field('id,mer_name')->find(); + } // $this->user->merchat=Merchant::where('uid',$user['id'])->find(); - $this->user->supplier=Supplier::where('uid',$user['id'])->field('id,mer_name')->find(); return $this; } @@ -215,6 +217,7 @@ class WechatUserService */ public function authUserLogin(): self { + if ($this->user->isEmpty()) { $this->createUser(); } else { diff --git a/app/api/validate/UserValidate.php b/app/api/validate/UserValidate.php new file mode 100644 index 0000000..0f8bf14 --- /dev/null +++ b/app/api/validate/UserValidate.php @@ -0,0 +1,50 @@ + 'require', + ]; + + protected $message = [ + 'code.require' => '参数缺失', + ]; + + + /** + * @notes 获取小程序手机号场景 + * @return UserValidate + * @author 段誉 + * @date 2022/9/21 16:44 + */ + public function sceneGetMobileByMnp() + { + return $this->only(['code']); + } + + + /** + * @notes 绑定/变更 手机号 + * @return UserValidate + * @author 段誉 + * @date 2022/9/21 17:37 + */ + public function sceneBindMobile() + { + return $this->only(['mobile', 'code']); + } + + +} \ No newline at end of file diff --git a/app/api/validate/WechatLoginValidate.php b/app/api/validate/WechatLoginValidate.php index 323a5ed..b9f435e 100644 --- a/app/api/validate/WechatLoginValidate.php +++ b/app/api/validate/WechatLoginValidate.php @@ -32,6 +32,7 @@ class WechatLoginValidate extends BaseValidate 'access_token' => 'require', 'terminal' => 'require', 'avatar' => 'require', + 'mobile' => 'require', ]; protected $message = [ @@ -42,6 +43,7 @@ class WechatLoginValidate extends BaseValidate 'access_token.require' => 'access_token缺少', 'terminal.require' => '终端参数缺少', 'avatar.require' => '头像缺少', + 'mobile.require' => '手机号', ]; @@ -89,7 +91,7 @@ class WechatLoginValidate extends BaseValidate */ public function sceneUpdateUser() { - return $this->only(['nickname', 'avatar']); + return $this->only(['mobile']); } diff --git a/app/common/enum/notice/NoticeEnum.php b/app/common/enum/notice/NoticeEnum.php new file mode 100644 index 0000000..864cff7 --- /dev/null +++ b/app/common/enum/notice/NoticeEnum.php @@ -0,0 +1,261 @@ + '业务通知', + self::VERIFICATION_CODE => '验证码' + ]; + if ($value === true) { + return $data; + } + return $data[$value]; + } + + + /** + * @notes 获取场景描述 + * @param $sceneId + * @param false $flag + * @return string|string[] + * @author 段誉 + * @date 2022/3/29 11:33 + */ + public static function getSceneDesc($sceneId, $flag = false) + { + $desc = [ + self::LOGIN_CAPTCHA => '登录验证码', + self::BIND_MOBILE_CAPTCHA => '绑定手机验证码', + self::CHANGE_MOBILE_CAPTCHA => '变更手机验证码', + self::FIND_LOGIN_PASSWORD_CAPTCHA => '找回登录密码验证码', + ]; + + if ($flag) { + return $desc; + } + + return $desc[$sceneId] ?? ''; + } + + + /** + * @notes 更具标记获取场景 + * @param $tag + * @return int|string + * @author 段誉 + * @date 2022/9/15 15:08 + */ + public static function getSceneByTag($tag) + { + $scene = [ + // 手机验证码登录 + 'YZMDL' => self::LOGIN_CAPTCHA, + // 绑定手机号验证码 + 'BDSJHM' => self::BIND_MOBILE_CAPTCHA, + // 变更手机号验证码 + 'BGSJHM' => self::CHANGE_MOBILE_CAPTCHA, + // 找回登录密码 + 'ZHDLMM' => self::FIND_LOGIN_PASSWORD_CAPTCHA, + ]; + return $scene[$tag] ?? ''; + } + + + /** + * @notes 获取场景变量 + * @param $sceneId + * @param false $flag + * @return array|string[] + * @author 段誉 + * @date 2022/3/29 11:33 + */ + public static function getVars($sceneId, $flag = false) + { + $desc = [ + self::LOGIN_CAPTCHA => '验证码:code', + self::BIND_MOBILE_CAPTCHA => '验证码:code', + self::CHANGE_MOBILE_CAPTCHA => '验证码:code', + self::FIND_LOGIN_PASSWORD_CAPTCHA => '验证码:code', + ]; + + if ($flag) { + return $desc; + } + + return isset($desc[$sceneId]) ? ['可选变量 ' . $desc[$sceneId]] : []; + } + + + /** + * @notes 获取系统通知示例 + * @param $sceneId + * @param false $flag + * @return array|string[] + * @author 段誉 + * @date 2022/3/29 11:33 + */ + public static function getSystemExample($sceneId, $flag = false) + { + $desc = []; + + if ($flag) { + return $desc; + } + + return isset($desc[$sceneId]) ? [$desc[$sceneId]] : []; + } + + + /** + * @notes 获取短信通知示例 + * @param $sceneId + * @param false $flag + * @return array|string[] + * @author 段誉 + * @date 2022/3/29 11:33 + */ + public static function getSmsExample($sceneId, $flag = false) + { + $desc = [ + self::LOGIN_CAPTCHA => '您正在登录,验证码${code},切勿将验证码泄露于他人,本条验证码有效期5分钟。', + self::BIND_MOBILE_CAPTCHA => '您正在绑定手机号,验证码${code},切勿将验证码泄露于他人,本条验证码有效期5分钟。', + self::CHANGE_MOBILE_CAPTCHA => '您正在变更手机号,验证码${code},切勿将验证码泄露于他人,本条验证码有效期5分钟。', + self::FIND_LOGIN_PASSWORD_CAPTCHA => '您正在找回登录密码,验证码${code},切勿将验证码泄露于他人,本条验证码有效期5分钟。', + ]; + + if ($flag) { + return $desc; + } + + return isset($desc[$sceneId]) ? ['示例:' . $desc[$sceneId]] : []; + } + + + /** + * @notes 获取公众号模板消息示例 + * @param $sceneId + * @param false $flag + * @return array|string[]|\string[][] + * @author 段誉 + * @date 2022/3/29 11:33 + */ + public static function getOaExample($sceneId, $flag = false) + { + $desc = []; + + if ($flag) { + return $desc; + } + + return $desc[$sceneId] ?? []; + } + + + /** + * @notes 获取小程序订阅消息示例 + * @param $sceneId + * @param false $flag + * @return array|mixed + * @author 段誉 + * @date 2022/3/29 11:33 + */ + public static function getMnpExample($sceneId, $flag = false) + { + $desc = []; + + if ($flag) { + return $desc; + } + + return $desc[$sceneId] ?? []; + } + + + /** + * @notes 提示 + * @param $type + * @param $sceneId + * @return array|string|string[]|\string[][] + * @author 段誉 + * @date 2022/3/29 11:33 + */ + public static function getOperationTips($type, $sceneId) + { + // 场景变量 + $vars = self::getVars($sceneId); + // 其他提示 + $other = []; + // 示例 + switch ($type) { + case self::SYSTEM: + $example = self::getSystemExample($sceneId); + break; + case self::SMS: + $other[] = '生效条件:1、管理后台完成短信设置。 2、第三方短信平台申请模板 3、若是腾讯云模板变量名须换成变量名出现顺序对应的数字(例:您好{nickname},您的订单{order_sn}已发货! 须改为 您好{1},您的订单{2}已发货!)'; + $example = self::getSmsExample($sceneId); + break; + case self::OA: + $other[] = '配置路径:公众号后台 > 广告与服务 > 模板消息'; + $other[] = '推荐行业:主营行业:IT科技/互联网|电子商务 副营行业:消费品/消费品'; + $example = self::getOaExample($sceneId); + break; + case self::MNP: + $other[] = '配置路径:小程序后台 > 功能 > 订阅消息'; + $example = self::getMnpExample($sceneId); + break; + } + $tips = array_merge($vars, $example, $other); + + return $tips; + } +} \ No newline at end of file diff --git a/app/common/model/user/User.php b/app/common/model/user/User.php index 6c11f40..c54b059 100644 --- a/app/common/model/user/User.php +++ b/app/common/model/user/User.php @@ -106,7 +106,9 @@ class User extends BaseModel */ public function getAvatarAttr($value) { - return trim($value) ? FileService::getFileUrl($value) : ''; + if($value){ + return trim($value) ? FileService::getFileUrl($value) : ''; + } }