|
|
@@ -2,12 +2,15 @@
|
|
|
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 think\facade\Config;
|
|
|
+use app\common\service\wechat\WeChatMnpService;
|
|
|
+use think\facade\Config;
|
|
|
use app\workerapi\service\MasterWokerTokenService;
|
|
|
|
|
|
/**
|
|
|
@@ -110,4 +113,66 @@ class LoginLogic extends BaseLogic
|
|
|
return MasterWokerTokenService::expireToken($userInfo['token']);
|
|
|
}
|
|
|
|
|
|
+ public static function mnpAuthLogin($params)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ //通过code获取微信openid
|
|
|
+ $response = (new WeChatMnpService())->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;
|
|
|
+ }
|
|
|
}
|