UserLogic.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\api\logic;
  15. use app\common\{enum\notice\NoticeEnum,
  16. enum\user\UserTerminalEnum,
  17. enum\YesNoEnum,
  18. logic\BaseLogic,
  19. model\user\User,
  20. model\user\UserAuth,
  21. service\FileService,
  22. service\sms\SmsDriver,
  23. service\wechat\WeChatMnpService};
  24. use think\facade\Config;
  25. /**
  26. * 会员逻辑层
  27. * Class UserLogic
  28. * @package app\shopapi\logic
  29. */
  30. class UserLogic extends BaseLogic
  31. {
  32. /**
  33. * @notes 个人中心
  34. * @param array $userInfo
  35. * @return array
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. * @author 段誉
  40. * @date 2022/9/16 18:04
  41. */
  42. public static function center(array $userInfo): array
  43. {
  44. $user = User::where(['id' => $userInfo['user_id']])
  45. ->field('id,sn,sex,account,nickname,real_name,avatar,mobile,create_time,is_new_user,user_money,password')
  46. ->findOrEmpty();
  47. if (in_array($userInfo['terminal'], [UserTerminalEnum::WECHAT_MMP, UserTerminalEnum::WECHAT_OA])) {
  48. $auth = UserAuth::where(['user_id' => $userInfo['user_id'], 'terminal' => $userInfo['terminal']])->find();
  49. $user['is_auth'] = $auth ? YesNoEnum::YES : YesNoEnum::NO;
  50. }
  51. $user['has_password'] = !empty($user['password']);
  52. $user->hidden(['password']);
  53. return $user->toArray();
  54. }
  55. /**
  56. * @notes 个人信息
  57. * @param $userId
  58. * @return array
  59. * @author 段誉
  60. * @date 2022/9/20 19:45
  61. */
  62. public static function info(int $userId)
  63. {
  64. $user = User::where(['id' => $userId])
  65. ->field('id,sn,sex,account,password,nickname,real_name,avatar,mobile,create_time,user_money')
  66. ->findOrEmpty();
  67. $user['has_password'] = !empty($user['password']);
  68. $user['has_auth'] = self::hasWechatAuth($userId);
  69. $user['version'] = config('project.version');
  70. $user->hidden(['password']);
  71. return $user->toArray();
  72. }
  73. /**
  74. * @notes 设置用户信息
  75. * @param int $userId
  76. * @param array $params
  77. * @return User|false
  78. * @author 段誉
  79. * @date 2022/9/21 16:53
  80. */
  81. public static function setInfo(int $userId, array $params)
  82. {
  83. try {
  84. if ($params['field'] == "avatar") {
  85. $params['value'] = FileService::setFileUrl($params['value']);
  86. }
  87. return User::update([
  88. 'id' => $userId,
  89. $params['field'] => $params['value']]
  90. );
  91. } catch (\Exception $e) {
  92. self::$error = $e->getMessage();
  93. return false;
  94. }
  95. }
  96. /**
  97. * @notes 是否有微信授权信息
  98. * @param $userId
  99. * @return bool
  100. * @author 段誉
  101. * @date 2022/9/20 19:36
  102. */
  103. public static function hasWechatAuth(int $userId)
  104. {
  105. //是否有微信授权登录
  106. $terminal = [UserTerminalEnum::WECHAT_MMP, UserTerminalEnum::WECHAT_OA,UserTerminalEnum::PC];
  107. $auth = UserAuth::where(['user_id' => $userId])
  108. ->whereIn('terminal', $terminal)
  109. ->findOrEmpty();
  110. return !$auth->isEmpty();
  111. }
  112. /**
  113. * @notes 重置登录密码
  114. * @param $params
  115. * @return bool
  116. * @author 段誉
  117. * @date 2022/9/16 18:06
  118. */
  119. public static function resetPassword(array $params)
  120. {
  121. try {
  122. // 校验验证码
  123. $smsDriver = new SmsDriver();
  124. if (!$smsDriver->verify($params['mobile'], $params['code'], NoticeEnum::FIND_LOGIN_PASSWORD_CAPTCHA)) {
  125. throw new \Exception('验证码错误');
  126. }
  127. // 重置密码
  128. $passwordSalt = Config::get('project.unique_identification');
  129. $password = create_password($params['password'], $passwordSalt);
  130. // 更新
  131. User::where('mobile', $params['mobile'])->update([
  132. 'password' => $password
  133. ]);
  134. return true;
  135. } catch (\Exception $e) {
  136. self::setError($e->getMessage());
  137. return false;
  138. }
  139. }
  140. /**
  141. * @notes 修稿密码
  142. * @param $params
  143. * @param $userId
  144. * @return bool
  145. * @author 段誉
  146. * @date 2022/9/20 19:13
  147. */
  148. public static function changePassword(array $params, int $userId)
  149. {
  150. try {
  151. $user = User::findOrEmpty($userId);
  152. if ($user->isEmpty()) {
  153. throw new \Exception('用户不存在');
  154. }
  155. // 密码盐
  156. $passwordSalt = Config::get('project.unique_identification');
  157. if (!empty($user['password'])) {
  158. if (empty($params['old_password'])) {
  159. throw new \Exception('请填写旧密码');
  160. }
  161. $oldPassword = create_password($params['old_password'], $passwordSalt);
  162. if ($oldPassword != $user['password']) {
  163. throw new \Exception('原密码不正确');
  164. }
  165. }
  166. // 保存密码
  167. $password = create_password($params['password'], $passwordSalt);
  168. $user->password = $password;
  169. $user->save();
  170. return true;
  171. } catch (\Exception $e) {
  172. self::setError($e->getMessage());
  173. return false;
  174. }
  175. }
  176. /**
  177. * @notes 获取小程序手机号
  178. * @param array $params
  179. * @return bool
  180. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  181. * @author 段誉
  182. * @date 2023/2/27 11:49
  183. */
  184. public static function getMobileByMnp(array $params)
  185. {
  186. try {
  187. $response = (new WeChatMnpService())->getUserPhoneNumber($params['code']);
  188. $phoneNumber = $response['phone_info']['purePhoneNumber'] ?? '';
  189. if (empty($phoneNumber)) {
  190. throw new \Exception('获取手机号码失败');
  191. }
  192. $user = User::where([
  193. ['mobile', '=', $phoneNumber],
  194. ['id', '<>', $params['user_id']]
  195. ])->findOrEmpty();
  196. if (!$user->isEmpty()) {
  197. throw new \Exception('手机号已被其他账号绑定');
  198. }
  199. // 绑定手机号
  200. User::update([
  201. 'id' => $params['user_id'],
  202. 'mobile' => $phoneNumber
  203. ]);
  204. return true;
  205. } catch (\Exception $e) {
  206. self::setError($e->getMessage());
  207. return false;
  208. }
  209. }
  210. /**
  211. * @notes 绑定手机号
  212. * @param $params
  213. * @return bool
  214. * @author 段誉
  215. * @date 2022/9/21 17:28
  216. */
  217. public static function bindMobile(array $params)
  218. {
  219. try {
  220. // 变更手机号场景
  221. $sceneId = NoticeEnum::CHANGE_MOBILE_CAPTCHA;
  222. $where = [
  223. ['id', '=', $params['user_id']],
  224. ['mobile', '=', $params['mobile']]
  225. ];
  226. // 绑定手机号场景
  227. if ($params['type'] == 'bind') {
  228. $sceneId = NoticeEnum::BIND_MOBILE_CAPTCHA;
  229. $where = [
  230. ['mobile', '=', $params['mobile']]
  231. ];
  232. }
  233. // 校验短信
  234. $checkSmsCode = (new SmsDriver())->verify($params['mobile'], $params['code'], $sceneId);
  235. if (!$checkSmsCode) {
  236. throw new \Exception('验证码错误');
  237. }
  238. $user = User::where($where)->findOrEmpty();
  239. if (!$user->isEmpty()) {
  240. throw new \Exception('该手机号已被使用');
  241. }
  242. User::update([
  243. 'id' => $params['user_id'],
  244. 'mobile' => $params['mobile'],
  245. ]);
  246. return true;
  247. } catch (\Exception $e) {
  248. self::setError($e->getMessage());
  249. return false;
  250. }
  251. }
  252. }