| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- namespace app\workerapi\logic;
- use app\common\enum\LoginEnum;
- use app\common\enum\notice\NoticeEnum;
- use app\common\enum\user\UserTerminalEnum;
- use app\common\logic\BaseLogic;
- use app\common\model\master_worker\MasterWorker;
- use app\common\model\master_worker\MasterWorkerAuth;
- use app\common\model\master_worker_register\MasterWorkerRegister;
- use app\common\service\FileService;
- use app\common\service\sms\SmsDriver;
- use app\common\service\wechat\WeChatMnpService;
- use app\common\service\wechat\WorkerWeChatMnpService;
- use think\facade\Config;
- use app\workerapi\service\MasterWokerTokenService;
- /**
- * @author 林海涛
- * @date ${DATA}
- */
- class LoginLogic extends BaseLogic
- {
- /**
- * @notes 确认手机号
- * @param $params
- * @return bool
- * @author 段誉
- * @date 2022/9/21 17:28
- */
- public static function confirmMobile(array $params)
- {
- try {
- // 验证码请求
- $sceneId = NoticeEnum::GCSSJHM_CAPTCHA;
- // 校验短信
- $checkSmsCode = (new SmsDriver())->verify($params['mobile'], $params['code'], $sceneId);
- if (!$checkSmsCode) {
- throw new \Exception('验证码错误');
- }
- return true;
- } catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function register(array $params)
- {
- try {
- MasterWorkerRegister::create([
- 'maintain_exp_type' => $params['maintain_exp_type'],
- 'other_exp_type' => $params['other_exp_type'],
- 'city' => $params['city'],
- 'vehicle_type' => $params['vehicle_type'],
- 'name' => $params['name'],
- 'age' => $params['age'],
- 'mobile' => $params['mobile'],
- ]);
- return true;
- } catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function login($params)
- {
- try {
- // 账号/手机号 密码登录
- $where = ['account' => $params['account']];
- if ($params['scene'] == LoginEnum::MOBILE_CAPTCHA) {
- //手机验证码登录
- $where = ['mobile' => $params['account']];
- }
- $user = MasterWorker::where($where)->findOrEmpty();
- if ($user->isEmpty()) {
- throw new \Exception('用户不存在');
- }
- //更新登录信息
- $user->login_time = time();
- $user->login_ip = request()->ip();
- $user->save();
- //设置token
- $userInfo = MasterWokerTokenService::setToken($user->id, 1);
- //返回登录信息
- $avatar = $user->avatar ?: Config::get('project.default_image.user_avatar');
- $avatar = FileService::getFileUrl($avatar);
- return [
- 'nickname' => $userInfo['nickname'],
- 'sn' => $userInfo['sn'],
- 'mobile' => $userInfo['mobile'],
- 'avatar' => $avatar,
- 'token' => $userInfo['token'],
- ];
- } catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function logout($userInfo)
- {
- //token不存在,不注销
- if (!isset($userInfo['token'])) {
- return false;
- }
- //设置token过期
- return MasterWokerTokenService::expireToken($userInfo['token']);
- }
- public static function mnpAuthLogin($params)
- {
- try {
- //通过code获取微信openid
- $response = (new WorkerWeChatMnpService())->getMnpResByCode($params['code']);
- $response['user_id'] = $params['user_id'];
- $response['terminal'] = UserTerminalEnum::WECHAT_MMP;
- return self::createAuth($response);
- } catch (\Exception $e) {
- self::$error = $e->getMessage();
- return false;
- }
- }
- /**
- * @notes 生成授权记录
- * @param $response
- * @return bool
- * @throws \Exception
- * @author 段誉
- * @date 2022/9/16 10:43
- */
- public static function createAuth($response)
- {
- //先检查openid是否有记录
- $isAuth = MasterWorkerAuth::where('openid', '=', $response['openid'])->findOrEmpty();
- if (!$isAuth->isEmpty()) {
- if($isAuth->user_id != $response['user_id']) {
- throw new \Exception('该微信已被绑定');
- }
- if($isAuth->user_id == 0) {
- //更新操作
- $isAuth->user_id = $response['user_id'];
- $isAuth->save();
- return true;
- }
- if($isAuth->user_id == $response['user_id']) {
- return true;
- }
- }
- if (isset($response['unionid']) && !empty($response['unionid'])) {
- //在用unionid找记录,防止生成两个账号,同个unionid的问题
- $userAuth = MasterWorkerAuth::where(['unionid' => $response['unionid']])
- ->findOrEmpty();
- if (!$userAuth->isEmpty() && $userAuth->user_id != $response['user_id']) {
- throw new \Exception('该微信已被绑定');
- }
- }
- //如果没有授权,直接生成一条微信授权记录
- MasterWorkerAuth::create([
- 'user_id' => $response['user_id'],
- 'openid' => $response['openid'],
- 'unionid' => $response['unionid'] ?? '',
- 'terminal' => $response['terminal'],
- ]);
- return true;
- }
- }
|