点赞,收藏,消息,分类图标
This commit is contained in:
@@ -4,6 +4,7 @@ namespace app\common\service;
|
||||
|
||||
use app\common\model\User;
|
||||
|
||||
use EasyWeChat\MiniApp\Application as xcxApplication;
|
||||
use EasyWeChat\OpenPlatform\Application;
|
||||
use support\Redis;
|
||||
use GuzzleHttp\Client;
|
||||
@@ -178,4 +179,58 @@ class WechatService
|
||||
|
||||
return json_decode(file_get_contents($url, false, stream_context_create($options)), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 小程序获取openid
|
||||
* @param string $code
|
||||
* @return array
|
||||
* @throws BusinessException
|
||||
*/
|
||||
public function xcxOpenid(string $code): array
|
||||
{
|
||||
|
||||
$config = [
|
||||
'app_id' => getenv('WECHAT_MINI_APPID'),
|
||||
'secret' => getenv('WECHAT_MINI_SECRET'),
|
||||
'response_type' => 'array',
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
'file' => runtime_path().'/mzl/wechat.log',
|
||||
]
|
||||
];
|
||||
// 创建微信小程序对象
|
||||
$wechat = new xcxApplication($config);
|
||||
// 获取 session_key 和 openid
|
||||
return $wechat->getUtils()->codeToSession($code);
|
||||
}
|
||||
|
||||
/**
|
||||
* 小程序获取手机号
|
||||
* @param string $phone_code
|
||||
* @return mixed
|
||||
* @throws BusinessException
|
||||
*/
|
||||
public function xcxPhoneNumber(string $phone_code)
|
||||
{
|
||||
$config = [
|
||||
'app_id' => getenv('WECHAT_MINI_APPID'),
|
||||
'secret' => getenv('WECHAT_MINI_SECRET'),
|
||||
'response_type' => 'array',
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
'file' => runtime_path().'/mzl/wechat.log',
|
||||
]
|
||||
];
|
||||
// 创建微信小程序对象
|
||||
$wechat = new xcxApplication($config);
|
||||
// getPhoneNumber
|
||||
$data = ['code' => $phone_code];
|
||||
$result = $wechat->getClient()->postJson('wxa/business/getuserphonenumber', $data);
|
||||
if (isset($result['phone_info'])) {
|
||||
// 业务处理 $result['phone_info']['phoneNumber']
|
||||
return $result;
|
||||
} else {
|
||||
throw new BusinessException($result['errmsg']);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user