diff --git a/application/common/model/Worker.php b/application/common/model/Worker.php index d6ebe9b..fad9738 100644 --- a/application/common/model/Worker.php +++ b/application/common/model/Worker.php @@ -14,5 +14,4 @@ class Worker extends Model ]; - } diff --git a/application/config.php b/application/config.php index 49a2749..44e6b12 100755 --- a/application/config.php +++ b/application/config.php @@ -312,7 +312,7 @@ return [ //插件纯净模式,插件启用后是否删除插件目录的application、public和assets文件夹 'addon_pure_mode' => true, //允许跨域的域名,多个以,分隔 - 'cors_request_domain' => 'localhost,127.0.0.1,chrome-extension://gahjcfmabepcnfmfanjhcobdaalgpjoh', + 'cors_request_domain' => 'localhost,127.0.0.1,*', //版本号 'version' => '1.5.3.20250217', //API接口地址 diff --git a/application/services/WorkerService.php b/application/services/WorkerService.php index ee25a91..6d0fb72 100644 --- a/application/services/WorkerService.php +++ b/application/services/WorkerService.php @@ -2,7 +2,6 @@ namespace app\services; -use app\api\library\ApiException; use app\common\library\Token; use EasyWeChat\Factory; use EasyWeChat\Kernel\Exceptions\InvalidConfigException; @@ -12,43 +11,41 @@ class WorkerService extends BaseService { public function bindPhoneNumber(string $code, string $vendorToken) { - //getPhoneNumber 方法通过魔术方法 __call 获取 -// $phoneInfo = $this->getMiniProgramApp()->getPhoneNumber($code); -// -// if (empty($phoneInfo)) { -// $this->apiError('获取手机号失败', 0, $phoneInfo); -// } -// -// if ($phoneInfo['errcode'] !== 0) { -// $this->apiError('获取手机号失败', 0, $phoneInfo); -// } -// -// $phone = $phoneInfo['phone_info']['phoneNumber']; + $phone = $this->getPhoneNumber($code); + $worker = $this->getWorkerService()->getByTel($phone); + $this->tryBindPhoneNumber($worker); - $phone = '18628195903'; $tokenData = Token::get($vendorToken); if (!$tokenData) { $this->apiError('vendor_token 无效'); } - $workerVendorId = $tokenData['user_id']; - dump($phone); + $workerVendor = $this->getWorkerVendorModel()->find($tokenData['user_id']); + if ($workerVendor->worker_id) { + $this->apiError('绑定失败,该微信已绑定手机号'); + } + + //绑定手机号 + $workerVendor->worker_id = $worker->id; + $workerVendor->save(); + + Token::delete($vendorToken); + return $worker->id; } public function login(string $code) { -// $app = $this->getMiniProgramApp(); -// try { -// $info = $app->auth->session($code); -// } catch (InvalidConfigException $e) { -// $this->apiError('登录失败', $e); -// } -// -// if (isset($info['errcode']) && $info['errcode'] !== 0) { -// $this->apiError('登录失败', 0, $info); -// } + $app = $this->getMiniProgramApp(); + try { + $info = $app->auth->session($code); + } catch (InvalidConfigException $e) { + $this->apiError('登录失败', $e); + } + + if (isset($info['errcode']) && $info['errcode'] !== 0) { + $this->apiError('登录失败', 0, $info); + } - $info['openid'] = 'oNgcn42DRwoG_qE6jjvsCSbM4cX8'; $workerVendor = $this->getWorkerVendorService()->getVendorByOpenid($info['openid']); //创建 @@ -77,4 +74,48 @@ class WorkerService extends BaseService ]; return Factory::miniProgram($config); } + + public function getPhoneNumber(string $code) + { + //getPhoneNumber 方法通过魔术方法 __call 获取 + $phoneInfo = $this->getMiniProgramApp()->getPhoneNumber($code); + + if (empty($phoneInfo)) { + $this->apiError('获取手机号失败', 0, $phoneInfo); + } + + if ($phoneInfo['errcode'] !== 0) { + $this->apiError('获取手机号失败', 0, $phoneInfo); + } + + return $phoneInfo['phone_info']['phoneNumber']; + } + + private function getByTel(string $phone) + { + return $this->getWorkerModel()->where('tel', $phone)->find(); + } + + /** + * 尝试绑定手机号 + * @param $worker + * @return true + */ + private function tryBindPhoneNumber($worker) + { + if (empty($worker)) { + $this->apiError('绑定失败,该手机号未注册'); + } + + if ($worker->status === 0) { + $this->apiError('绑定失败,您的账号不可用'); + } + + $workerVendor = $this->getWorkerVendorService()->getByWorkerIdAndPlatform($worker->id, 'WechatMp'); + if ($workerVendor) { + $this->apiError('绑定失败,当前手机号已被其他微信绑定'); + } + + return true; + } } diff --git a/application/services/WorkerVendorService.php b/application/services/WorkerVendorService.php index 4a7f1ad..bfaf513 100644 --- a/application/services/WorkerVendorService.php +++ b/application/services/WorkerVendorService.php @@ -30,4 +30,9 @@ class WorkerVendorService extends BaseService return $vendor->id; } + + public function getByWorkerIdAndPlatform(int $workerId, string $platform) + { + return $this->getWorkerVendorModel()->where(['worker_id' => $workerId, 'platform' => $platform])->find(); + } } diff --git a/application/worker/controller/Worker.php b/application/worker/controller/Worker.php index 9a9dd13..737545c 100644 --- a/application/worker/controller/Worker.php +++ b/application/worker/controller/Worker.php @@ -1,4 +1,5 @@ request->request(); $validate = $this->validate($params, \app\worker\validate\Worker::class . '.login'); @@ -26,7 +27,7 @@ class Worker extends WorkerApi $this->error('请绑定手机号', ['vendor_token' => $this->getTokenByUserId($workerVendor['id'])]); } - function bindPhoneNumber() + public function bindPhoneNumber() { $params = $this->request->request(); $validate = $this->validate($params, \app\worker\validate\Worker::class . '.bindPhoneNumber'); @@ -34,6 +35,9 @@ class Worker extends WorkerApi $this->error($validate); } - $this->success('操作成功', $this->getWorkerService()->bindPhoneNumber($params['code'], $params['vendor_token'])); + $workerId = $this->getWorkerService()->bindPhoneNumber($params['code'], $params['vendor_token']); + $this->workerLogin($workerId); + + $this->success('绑定成功', $this->user); } }