微信登录、绑定手机号

This commit is contained in:
gcd 2025-03-29 09:22:16 +08:00
parent a36157d406
commit aa5f46bdd3
5 changed files with 81 additions and 32 deletions

View File

@ -14,5 +14,4 @@ class Worker extends Model
];
}

View File

@ -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接口地址

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -1,4 +1,5 @@
<?php
namespace app\worker\controller;
use app\common\controller\WorkerApi;
@ -7,7 +8,7 @@ class Worker extends WorkerApi
{
protected $noNeedLogin = ['login', 'bindPhoneNumber'];
function login()
public function login()
{
$params = $this->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);
}
}