UserLogic.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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\coupon\UserCoupon,
  20. model\equity\UserEquity,
  21. model\orders\OrderEffectiveLog,
  22. model\user\User,
  23. model\user\UserAuth,
  24. model\works\ReturnWork,
  25. service\FileService,
  26. service\sms\SmsDriver,
  27. service\wechat\WeChatMnpService};
  28. use think\facade\Config;
  29. /**
  30. * 会员逻辑层
  31. * Class UserLogic
  32. * @package app\shopapi\logic
  33. */
  34. class UserLogic extends BaseLogic
  35. {
  36. /**
  37. * @notes 个人中心
  38. * @param array $userInfo
  39. * @return array
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. * @author 段誉
  44. * @date 2022/9/16 18:04
  45. */
  46. public static function center(array $userInfo): array
  47. {
  48. $user = User::where(['id' => $userInfo['user_id']])
  49. ->field('id,sn,sex,account,nickname,real_name,avatar,mobile,create_time,is_new_user,user_money')
  50. ->findOrEmpty();
  51. if (in_array($userInfo['terminal'], [UserTerminalEnum::WECHAT_MMP, UserTerminalEnum::WECHAT_OA])) {
  52. $auth = UserAuth::where(['user_id' => $userInfo['user_id'], 'terminal' => $userInfo['terminal']])->find();
  53. $user['is_auth'] = $auth ? YesNoEnum::YES : YesNoEnum::NO;
  54. }
  55. //查询用户优惠券、权益卡、保修卡、返修工单数量
  56. $user['coupon_count'] = UserCoupon::where(['user_id' => $userInfo['user_id'] , 'voucher_status'=>0])->where('expire_time','>',time())->where('voucher_count','>',0)->count();
  57. $user['equity_count'] = UserEquity::where(['user_id' => $userInfo['user_id']])->where('number','>',0)->count();
  58. $user['effective_count'] = OrderEffectiveLog::where(['user_id' => $userInfo['user_id']])->where('end_effective_time','>',time())->where('effective_num','>',0)->count();
  59. $user['return_work_count'] = ReturnWork::where(['user_id' => $userInfo['user_id']])->where('return_work_status','<>',2)->count();
  60. return $user->toArray();
  61. }
  62. /**
  63. * @notes 个人信息
  64. * @param $userId
  65. * @return array
  66. * @author 段誉
  67. * @date 2022/9/20 19:45
  68. */
  69. public static function info(int $userId)
  70. {
  71. $user = User::where(['id' => $userId])
  72. ->field('id,sn,sex,account,password,nickname,real_name,avatar,mobile,create_time,user_money')
  73. ->findOrEmpty();
  74. $user['has_password'] = !empty($user['password']);
  75. $user['has_auth'] = self::hasWechatAuth($userId);
  76. $user['version'] = config('project.version');
  77. $user->hidden(['password']);
  78. return $user->toArray();
  79. }
  80. /**
  81. * @notes 设置用户信息
  82. * @param int $userId
  83. * @param array $params
  84. * @return User|false
  85. * @author 段誉
  86. * @date 2022/9/21 16:53
  87. */
  88. public static function setInfo(int $userId, array $params)
  89. {
  90. try {
  91. if ($params['field'] == "avatar") {
  92. $params['value'] = FileService::setFileUrl($params['value']);
  93. }
  94. return User::update([
  95. 'id' => $userId,
  96. $params['field'] => $params['value']]
  97. );
  98. } catch (\Exception $e) {
  99. self::$error = $e->getMessage();
  100. return false;
  101. }
  102. }
  103. /**
  104. * @notes 是否有微信授权信息
  105. * @param $userId
  106. * @return bool
  107. * @author 段誉
  108. * @date 2022/9/20 19:36
  109. */
  110. public static function hasWechatAuth(int $userId)
  111. {
  112. //是否有微信授权登录
  113. $terminal = [UserTerminalEnum::WECHAT_MMP, UserTerminalEnum::WECHAT_OA,UserTerminalEnum::PC];
  114. $auth = UserAuth::where(['user_id' => $userId])
  115. ->whereIn('terminal', $terminal)
  116. ->findOrEmpty();
  117. return !$auth->isEmpty();
  118. }
  119. /**
  120. * @notes 重置登录密码
  121. * @param $params
  122. * @return bool
  123. * @author 段誉
  124. * @date 2022/9/16 18:06
  125. */
  126. public static function resetPassword(array $params)
  127. {
  128. try {
  129. // 校验验证码
  130. $smsDriver = new SmsDriver();
  131. if (!$smsDriver->verify($params['mobile'], $params['code'], NoticeEnum::FIND_LOGIN_PASSWORD_CAPTCHA)) {
  132. throw new \Exception('验证码错误');
  133. }
  134. // 重置密码
  135. $passwordSalt = Config::get('project.unique_identification');
  136. $password = create_password($params['password'], $passwordSalt);
  137. // 更新
  138. User::where('mobile', $params['mobile'])->update([
  139. 'password' => $password
  140. ]);
  141. return true;
  142. } catch (\Exception $e) {
  143. self::setError($e->getMessage());
  144. return false;
  145. }
  146. }
  147. /**
  148. * @notes 修稿密码
  149. * @param $params
  150. * @param $userId
  151. * @return bool
  152. * @author 段誉
  153. * @date 2022/9/20 19:13
  154. */
  155. public static function changePassword(array $params, int $userId)
  156. {
  157. try {
  158. $user = User::findOrEmpty($userId);
  159. if ($user->isEmpty()) {
  160. throw new \Exception('用户不存在');
  161. }
  162. // 密码盐
  163. $passwordSalt = Config::get('project.unique_identification');
  164. if (!empty($user['password'])) {
  165. if (empty($params['old_password'])) {
  166. throw new \Exception('请填写旧密码');
  167. }
  168. $oldPassword = create_password($params['old_password'], $passwordSalt);
  169. if ($oldPassword != $user['password']) {
  170. throw new \Exception('原密码不正确');
  171. }
  172. }
  173. // 保存密码
  174. $password = create_password($params['password'], $passwordSalt);
  175. $user->password = $password;
  176. $user->save();
  177. return true;
  178. } catch (\Exception $e) {
  179. self::setError($e->getMessage());
  180. return false;
  181. }
  182. }
  183. /**
  184. * @notes 获取小程序手机号
  185. * @param array $params
  186. * @return bool
  187. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  188. * @author 段誉
  189. * @date 2023/2/27 11:49
  190. */
  191. public static function getMobileByMnp(array $params)
  192. {
  193. try {
  194. $response = (new WeChatMnpService())->getUserPhoneNumber($params['code']);
  195. $phoneNumber = $response['phone_info']['purePhoneNumber'] ?? '';
  196. if (empty($phoneNumber)) {
  197. throw new \Exception('获取手机号码失败');
  198. }
  199. $user = User::where([
  200. ['mobile', '=', $phoneNumber],
  201. ['id', '<>', $params['user_id']]
  202. ])->findOrEmpty();
  203. if (!$user->isEmpty()) {
  204. throw new \Exception('手机号已被其他账号绑定');
  205. }
  206. // 绑定手机号
  207. User::update([
  208. 'id' => $params['user_id'],
  209. 'mobile' => $phoneNumber
  210. ]);
  211. return true;
  212. } catch (\Exception $e) {
  213. self::setError($e->getMessage());
  214. return false;
  215. }
  216. }
  217. /**
  218. * @notes 绑定手机号
  219. * @param $params
  220. * @return bool
  221. * @author 段誉
  222. * @date 2022/9/21 17:28
  223. */
  224. public static function bindMobile(array $params)
  225. {
  226. try {
  227. // 变更手机号场景
  228. $sceneId = NoticeEnum::CHANGE_MOBILE_CAPTCHA;
  229. $where = [
  230. ['id', '=', $params['user_id']],
  231. ['mobile', '=', $params['mobile']]
  232. ];
  233. // 绑定手机号场景
  234. if ($params['type'] == 'bind') {
  235. $sceneId = NoticeEnum::BIND_MOBILE_CAPTCHA;
  236. $where = [
  237. ['mobile', '=', $params['mobile']]
  238. ];
  239. }
  240. // 校验短信
  241. $checkSmsCode = (new SmsDriver())->verify($params['mobile'], $params['code'], $sceneId);
  242. if (!$checkSmsCode) {
  243. throw new \Exception('验证码错误');
  244. }
  245. $user = User::where($where)->findOrEmpty();
  246. if (!$user->isEmpty()) {
  247. throw new \Exception('该手机号已被使用');
  248. }
  249. User::update([
  250. 'id' => $params['user_id'],
  251. 'mobile' => $params['mobile'],
  252. ]);
  253. return true;
  254. } catch (\Exception $e) {
  255. self::setError($e->getMessage());
  256. return false;
  257. }
  258. }
  259. }