UserLogic.php 9.5 KB

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