UserLogic.php 12 KB

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