Accept Merge Request #29: (feature/dgg -> develop)
Merge Request: feat: 微信登录兼容 "w7corp/easywechat": "^6.17" Created By: @大狗哥 Accepted By: @大狗哥 URL: https://g-bcrc3009.coding.net/p/allocatr/d/allocatr/git/merge/29
This commit is contained in:
commit
d02d5b2063
|
|
@ -3,9 +3,7 @@
|
|||
namespace app\services;
|
||||
|
||||
use app\common\library\Token;
|
||||
use EasyWeChat\Factory;
|
||||
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
|
||||
use EasyWeChat\MiniProgram\Application;
|
||||
use EasyWeChat\MiniApp\Application;
|
||||
|
||||
class WorkerService extends BaseService
|
||||
{
|
||||
|
|
@ -47,8 +45,9 @@ class WorkerService extends BaseService
|
|||
{
|
||||
$app = $this->getMiniProgramApp();
|
||||
try {
|
||||
$info = $app->auth->session($code);
|
||||
} catch (InvalidConfigException $e) {
|
||||
$utils = $app->getUtils();
|
||||
$info = $utils->codeToSession($code);
|
||||
} catch (\Exception $e) {
|
||||
$this->apiError('登录失败', $e);
|
||||
}
|
||||
|
||||
|
|
@ -82,18 +81,31 @@ class WorkerService extends BaseService
|
|||
'app_id' => config('mini_program.app_id'),
|
||||
'secret' => config('mini_program.secret'),
|
||||
];
|
||||
return Factory::miniProgram($config);
|
||||
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)
|
||||
{
|
||||
//getPhoneNumber 方法通过魔术方法 __call 获取
|
||||
$phoneInfo = $this->getMiniProgramApp()->getPhoneNumber($code);
|
||||
//通过 code 获取手机号信息
|
||||
$phoneInfo = $this->code2PhoneNumberInfo($code);
|
||||
|
||||
if (empty($phoneInfo)) {
|
||||
$this->apiError('获取手机号失败', 0, $phoneInfo);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user