| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | namespace app\controller\api; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use app\exception\RepositoryException; | 
					
						
							|  |  |  | use app\model\Account; | 
					
						
							|  |  |  | use app\repository\AccountRepository; | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  | use app\repository\BusinessRepository; | 
					
						
							| 
									
										
										
										
											2021-12-01 14:56:09 +08:00
										 |  |  | use app\repository\CouponRepository; | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | use app\service\File; | 
					
						
							|  |  |  | use app\service\Jwt; | 
					
						
							|  |  |  | use app\service\wx\WechatApplets; | 
					
						
							|  |  |  | use app\validate\User as UserValidate; | 
					
						
							|  |  |  | use EasyWeChat\Kernel\Exceptions\InvalidConfigException; | 
					
						
							|  |  |  | use Exception; | 
					
						
							| 
									
										
										
										
											2021-12-01 14:56:09 +08:00
										 |  |  | use think\exception\ValidateException; | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | use think\response\Json; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 13:59:29 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * 用户相关 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Class User | 
					
						
							|  |  |  |  * @package app\controller\api | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | class User extends Base | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     protected $noNeedLogin = [ | 
					
						
							|  |  |  |         'login', | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 登录  成功返回token及用户信息 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Json | 
					
						
							|  |  |  |      * @throws InvalidConfigException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function login(): Json | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-11-24 16:19:54 +08:00
										 |  |  |         $params = [ | 
					
						
							|  |  |  |             'code'          => $this->request->param('code', ''), | 
					
						
							|  |  |  |             'nick_name'     => $this->request->param('nickName', ''), | 
					
						
							|  |  |  |             'avatar_url'    => $this->request->param('avatarUrl', ''), | 
					
						
							|  |  |  |             'gender'        => $this->request->param('gender', 0), | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $validate = new UserValidate(); | 
					
						
							|  |  |  |         if (!$validate->scene('wx_applets')->check($params)) { | 
					
						
							|  |  |  |             return $this->json(4000, $validate->getError()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $minApp = WechatApplets::getInstance(); | 
					
						
							|  |  |  |         $jsCode = $params['code']; | 
					
						
							|  |  |  |         $wxUser = $minApp->auth->session($jsCode); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (isset($wxUser['errcode']) && $wxUser['errcode'] != 0) { | 
					
						
							|  |  |  |             return $this->json(4001, $wxUser['errcode'].';'.$wxUser['errmsg'] ?? '登录失败'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // $wxUser success has [session_key, openid, unionid]
 | 
					
						
							|  |  |  |         // 有效期2小时
 | 
					
						
							|  |  |  |         $wxUser['expire_time'] = time() + 7200; | 
					
						
							|  |  |  |         $wxUser['session_key'] = $wxUser['session_key'] ?? ''; | 
					
						
							| 
									
										
										
										
											2021-11-24 16:19:54 +08:00
										 |  |  |         $openID                = $wxUser['openid'] ?? ''; | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (empty($openID)) { | 
					
						
							|  |  |  |             return $this->json(4002, '登录失败'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2021-11-24 16:19:54 +08:00
										 |  |  |             $repo       = AccountRepository::getInstance(); | 
					
						
							|  |  |  |             $account    = $repo->findByOpenID($openID); | 
					
						
							|  |  |  |             $nowDate    = date('Y-m-d H:i:s'); | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (!$account) { | 
					
						
							|  |  |  |                 // 自动注册
 | 
					
						
							|  |  |  |                 $account = $repo->create([ | 
					
						
							| 
									
										
										
										
											2021-11-24 16:19:54 +08:00
										 |  |  |                     'user_code'         => createUuid(), // 用户UUID
 | 
					
						
							|  |  |  |                     'open_id'           => $openID, | 
					
						
							|  |  |  |                     'create_time'       => $nowDate, | 
					
						
							|  |  |  |                     'login_time'        => $nowDate, | 
					
						
							|  |  |  |                     'type'              => Account::type_consumer, // 默认为普通消费者
 | 
					
						
							|  |  |  |                     'nick_name'         => $params['nick_name'] ?: generateDefaultNickName(), | 
					
						
							|  |  |  |                     'avatar_url'        => $params['avatar_url'] ?: Account::DEFAULT_AVATAR, | 
					
						
							|  |  |  |                     'gender'            => $params['gender'], | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |                 ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $updateData     = [ | 
					
						
							| 
									
										
										
										
											2021-11-24 16:19:54 +08:00
										 |  |  |                     'login_time'  => $nowDate, | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |                 ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // 更新资料
 | 
					
						
							| 
									
										
										
										
											2021-11-24 16:19:54 +08:00
										 |  |  |                 $modifyStringList = ['nick_name', 'avatar_url']; | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |                 foreach ($modifyStringList as $modifyKey) { | 
					
						
							| 
									
										
										
										
											2021-11-24 16:19:54 +08:00
										 |  |  |                     if (isset($params[$modifyKey]) && !empty($params[$modifyKey])) { | 
					
						
							|  |  |  |                         $updateData[$modifyKey] = $params[$modifyKey]; | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 $repo->update($updateData, ['id' => $account['id']]); | 
					
						
							|  |  |  |                 $account = $repo->findById($account['id']); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } catch (RepositoryException | Exception $e) { | 
					
						
							|  |  |  |             return $this->json(4003, '登录失败!'.$e->getMessage()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $account = $account->toArray(); | 
					
						
							| 
									
										
										
										
											2021-11-24 16:19:54 +08:00
										 |  |  |         $account['avatar_url'] = File::convertCompleteFileUrl($account['avatar_url']); | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |         $jwtData = [ | 
					
						
							| 
									
										
										
										
											2021-11-24 16:19:54 +08:00
										 |  |  |             'user_id'       => $account['id'], | 
					
						
							|  |  |  |             'user_code'     => $account['user_code'], | 
					
						
							|  |  |  |             'open_id'       => $openID, | 
					
						
							|  |  |  |             'session_key'   => $wxUser['session_key'], | 
					
						
							|  |  |  |             'expire_time'   => $wxUser['expire_time'], | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-24 16:19:54 +08:00
										 |  |  |         $data   = [ | 
					
						
							|  |  |  |             'avatar'        => File::convertCompleteFileUrl($account['avatar_url']), | 
					
						
							|  |  |  |             'nickName'      => $account['nick_name'], | 
					
						
							|  |  |  |             'token'         => Jwt::generate($jwtData), | 
					
						
							|  |  |  |             'userType'      => $account['type'], | 
					
						
							|  |  |  |             'userTypeDes'   => Account::accountTypeDescList()[$account['type']] ?? '游客', | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |         ]; | 
					
						
							|  |  |  |         return $this->json(0, 'success', $data); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-01 14:56:09 +08:00
										 |  |  |     /* | 
					
						
							|  |  |  |      * 获取个人中心资料 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getUserCenterInfo() | 
					
						
							| 
									
										
										
										
											2021-11-25 18:54:59 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-01 14:56:09 +08:00
										 |  |  |         $accountId = $this->request->user['user_id'] ?? 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $accountRepo    = AccountRepository::getInstance(); | 
					
						
							|  |  |  |             $couponRepo     = CouponRepository::getInstance(); | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  |             $busRepo        = BusinessRepository::getInstance(); | 
					
						
							| 
									
										
										
										
											2021-12-01 14:56:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $account        = $accountRepo->findById($accountId, [], function ($q) { | 
					
						
							|  |  |  |                 return $q->with(['business', 'parent']); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |             if (empty($account)) { | 
					
						
							|  |  |  |                 throw new ValidateException('用户无效!'); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-11-25 18:54:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-01 14:56:09 +08:00
										 |  |  |             if ($account['type'] == Account::type_business) { | 
					
						
							|  |  |  |                 // 商家用户
 | 
					
						
							|  |  |  |                 if (empty($account['business'])) { | 
					
						
							|  |  |  |                     throw new ValidateException('用户无效!没有相关的商户信息记录'); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 $businessRes    = [ | 
					
						
							|  |  |  |                     'avatar'    => File::convertCompleteFileUrl($account['avatar_url']), | 
					
						
							|  |  |  |                     'nickName'  => $account['nick_name'], | 
					
						
							|  |  |  |                     'userType'  => $account['type'], | 
					
						
							|  |  |  |                     'userTypeDes'   => Account::accountTypeDescList()[$account['type']] ?? '游客', | 
					
						
							|  |  |  |                     'couponCount'   => 0, | 
					
						
							|  |  |  |                     'business'      => [ | 
					
						
							|  |  |  |                         'code'          => $account['business']['code'], | 
					
						
							|  |  |  |                         'businessName'  => $account['business']['business_name'], | 
					
						
							|  |  |  |                         'state'         => $account['business']['state'], | 
					
						
							|  |  |  |                         'reason'        => $account['business']['reason'], | 
					
						
							|  |  |  |                         'balance'       => $account['business']['balance'], | 
					
						
							|  |  |  |                         'enable'        => $account['business']['enable'], | 
					
						
							|  |  |  |                     ], | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  |                     "alertMoney"=>config("wechat.balance") | 
					
						
							| 
									
										
										
										
											2021-12-01 14:56:09 +08:00
										 |  |  |                 ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $this->json(0, 'success', $businessRes); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 // 用户领取的优惠卷总数量
 | 
					
						
							|  |  |  |                 $couponCount    = $couponRepo->getModel()::getCountByWhere(['consumer_code' => $account['user_code']]); | 
					
						
							|  |  |  |                 $personalRes    = [ | 
					
						
							|  |  |  |                     'avatar'    => File::convertCompleteFileUrl($account['avatar_url']), | 
					
						
							|  |  |  |                     'nickName'  => $account['nick_name'], | 
					
						
							|  |  |  |                     'userType'  => $account['type'], | 
					
						
							|  |  |  |                     'userTypeDes'   => Account::accountTypeDescList()[$account['type']] ?? '游客', | 
					
						
							|  |  |  |                     'couponCount'   => $couponCount, | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  |                     "alertMoney"=>config("wechat.balance") | 
					
						
							| 
									
										
										
										
											2021-12-01 14:56:09 +08:00
										 |  |  |                 ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if ($account['type'] == Account::type_staff) { | 
					
						
							|  |  |  |                     // 员工
 | 
					
						
							|  |  |  |                     $businessCode = $account['parent']['business_code'] ?? ''; | 
					
						
							|  |  |  |                     if (!empty($businessCode)) { | 
					
						
							|  |  |  |                         $business = $busRepo->findOneByWhere(['code' => $businessCode]); | 
					
						
							|  |  |  |                         if ($business) { | 
					
						
							|  |  |  |                             $personalRes['business'] = [ | 
					
						
							|  |  |  |                                 'code'          => $business['code'], | 
					
						
							|  |  |  |                                 'businessName'  => $business['business_name'], | 
					
						
							|  |  |  |                                 'state'         => $business['state'], | 
					
						
							|  |  |  |                                 'reason'        => $business['reason'], | 
					
						
							|  |  |  |                                 'balance'       => $business['balance'], | 
					
						
							|  |  |  |                                 'enable'        => $business['enable'], | 
					
						
							|  |  |  |                             ]; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     $personalRes['parent'] = []; | 
					
						
							|  |  |  |                     if (!empty($account['parent'])) { | 
					
						
							|  |  |  |                         $personalRes['parent'] = [ | 
					
						
							|  |  |  |                             'avatar'    => File::convertCompleteFileUrl($account['parent']['avatar_url']), | 
					
						
							|  |  |  |                             'nickName'  => $account['parent']['nick_name'], | 
					
						
							|  |  |  |                             'userType'  => $account['parent']['type'], | 
					
						
							|  |  |  |                             'userTypeDes'   => Account::accountTypeDescList()[$account['parent']['type']] ?? '', | 
					
						
							|  |  |  |                             'couponCount'   => 0, | 
					
						
							|  |  |  |                             'businessCode'  => $account['parent']['business_code'], | 
					
						
							|  |  |  |                         ]; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 } elseif(!empty($account['business'])) { | 
					
						
							|  |  |  |                     // 商户认证记录
 | 
					
						
							| 
									
										
										
										
											2021-12-01 14:56:09 +08:00
										 |  |  |                     $personalRes['business'] = [ | 
					
						
							|  |  |  |                         'state'     => $account['business']['state'], | 
					
						
							|  |  |  |                         'reason'    => $account['business']['reason'], | 
					
						
							|  |  |  |                     ]; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $this->json(0, 'success', $personalRes); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } catch (ValidateException $e) { | 
					
						
							|  |  |  |             return $this->json(4001, $e->getError()); | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             return $this->json(5001, '服务器繁忙!获取用户个人信息失败'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-25 18:54:59 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | } |