195 lines
4.8 KiB
PHP
195 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace app\services;
|
|
|
|
use app\common\library\Token;
|
|
use EasyWeChat\MiniApp\Application;
|
|
|
|
class WorkerService extends BaseService
|
|
{
|
|
/**
|
|
* 绑定手机号
|
|
* @param string $code
|
|
* @param string $vendorToken
|
|
*/
|
|
public function bindPhoneNumber(string $code, string $vendorToken)
|
|
{
|
|
$phone = $this->getPhoneNumber($code);
|
|
$worker = $this->getWorkerService()->getByTel($phone);
|
|
|
|
// debug
|
|
if (empty($worker)) {
|
|
$this->getDebugLogService()->create('小程序登录没通过手机号找到师傅', "手机号:$phone");
|
|
}
|
|
|
|
$this->tryBindPhoneNumber($worker);
|
|
|
|
$tokenData = Token::get($vendorToken);
|
|
if (!$tokenData) {
|
|
$this->apiError('vendor_token 无效');
|
|
}
|
|
|
|
$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;
|
|
}
|
|
|
|
/**
|
|
* 微信登录
|
|
* @param string $code
|
|
* @return array
|
|
*/
|
|
public function login(string $code)
|
|
{
|
|
$app = $this->getMiniProgramApp();
|
|
try {
|
|
$utils = $app->getUtils();
|
|
$info = $utils->codeToSession($code);
|
|
} catch (\Exception $e) {
|
|
$this->apiError('登录失败', $e);
|
|
}
|
|
|
|
if (isset($info['errcode']) && $info['errcode'] !== 0) {
|
|
$this->apiError('登录失败', 0, $info);
|
|
}
|
|
|
|
$workerVendor = $this->getWorkerVendorService()->getVendorByOpenid($info['openid']);
|
|
|
|
//创建
|
|
if (empty($workerVendor)) {
|
|
try {
|
|
$this->getWorkerVendorService()->createWechatMpVendor($info['openid']);
|
|
} catch (\Exception $e) {
|
|
$this->apiError('登录失败', $e, $info);
|
|
}
|
|
|
|
$workerVendor = $this->getWorkerVendorService()->getVendorByOpenid($info['openid']);
|
|
}
|
|
|
|
return $workerVendor->toArray();
|
|
}
|
|
|
|
/**
|
|
* 获取小程序 App
|
|
* @return Application
|
|
*/
|
|
private function getMiniProgramApp(): Application
|
|
{
|
|
$config = [
|
|
'app_id' => config('mini_program.app_id'),
|
|
'secret' => config('mini_program.secret'),
|
|
];
|
|
return new Application($config);
|
|
}
|
|
|
|
/**
|
|
* 通过 code 获取用户手机号
|
|
* @param string $code
|
|
* @return array
|
|
*/
|
|
private function code2PhoneNumberInfo(string $code): array
|
|
{
|
|
$app = $this->getMiniProgramApp();
|
|
$phoneInfo = $app->getClient()->postJson('wxa/business/getuserphonenumber', ['code' => $code]);
|
|
|
|
return $phoneInfo->toArray();
|
|
}
|
|
|
|
/**
|
|
* 获取用户手机号
|
|
* @param string $code
|
|
* @return mixed
|
|
*/
|
|
public function getPhoneNumber(string $code)
|
|
{
|
|
//通过 code 获取手机号信息
|
|
$phoneInfo = $this->code2PhoneNumberInfo($code);
|
|
|
|
if (empty($phoneInfo)) {
|
|
$this->apiError('获取手机号失败', 0, $phoneInfo);
|
|
}
|
|
|
|
if ($phoneInfo['errcode'] !== 0) {
|
|
$this->apiError('获取手机号失败', 0, $phoneInfo);
|
|
}
|
|
|
|
return $phoneInfo['phone_info']['phoneNumber'];
|
|
}
|
|
|
|
/**
|
|
* 通过手机号查询师傅信息
|
|
* @param string $phone
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* 更新师傅位置
|
|
* @param int $workerId
|
|
* @param float $latitude
|
|
* @param float $longitude
|
|
* @return true
|
|
*/
|
|
public function updateWorkerLocation(int $workerId, float $latitude, float $longitude)
|
|
{
|
|
$worker = $this->getWorkerModel()->find($workerId);
|
|
if (empty($worker)) {
|
|
$this->apiError('更新位置失败,用户不存在');
|
|
}
|
|
|
|
$worker->lat = $latitude;
|
|
$worker->lng = $longitude;
|
|
$worker->location_update_time = datetime(time());
|
|
$worker->update_time = datetime(time());
|
|
$worker->save();
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|