diff --git a/application/services/WorkerService.php b/application/services/WorkerService.php index a98feb5..80587f6 100644 --- a/application/services/WorkerService.php +++ b/application/services/WorkerService.php @@ -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);