UserLogic.php 9.1 KB

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