Initial commit
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
|
||||
namespace app\mzl\modules\wechat\service;
|
||||
|
||||
use EasyWeChat\MiniApp\Application as xcxApplication;
|
||||
use EasyWeChat\OfficialAccount\Application as gzhApplication;
|
||||
use support\exception\BusinessException;
|
||||
use support\Log;
|
||||
|
||||
class WechatService
|
||||
{
|
||||
|
||||
protected $config = [];
|
||||
|
||||
public function __construct(string $type)
|
||||
{
|
||||
$this->config = $this->loadConfig($type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载配置
|
||||
*
|
||||
* @param string $type 类型
|
||||
* @return array
|
||||
* @throws BusinessException
|
||||
*/
|
||||
public function loadConfig(string $type): array
|
||||
{
|
||||
switch ($type) {
|
||||
case 'OfficialAccount':
|
||||
$data = [
|
||||
'app_id' => getenv('OFFICIALACCOUNT_APP_ID'),
|
||||
'secret' => getenv('OFFICIALACCOUNT_SECRET'),
|
||||
'oauth' => [
|
||||
'scopes' => getenv('OFFICIALACCOUNT_OAUTH_SCOPES')
|
||||
]
|
||||
];
|
||||
break;
|
||||
|
||||
case 'MiniApp':
|
||||
$data = [
|
||||
'app_id' => getenv('MINIAPP_APP_ID'),
|
||||
'secret' => getenv('MINIAPP_SECRET'),
|
||||
'response_type' => 'array',
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
'file' => runtime_path().'/mzl/wechat.log',
|
||||
]
|
||||
];
|
||||
break;
|
||||
|
||||
case 'Pay':
|
||||
$data = [
|
||||
'mch_id' => getenv('MCH_ID'),
|
||||
'private_key' => base_path() . getenv('PRIVATE_KEY'),
|
||||
'certificate' => base_path() . getenv('CERTIFICATE'),
|
||||
'secret_key' => getenv('SECRET_KEY'),
|
||||
'v2_secret_key' => getenv('V2_SECRET_KEY'),
|
||||
];
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new BusinessException('不支持的类型');
|
||||
break;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
// === === === === === === 小程序相关方法 === === === === === ===
|
||||
|
||||
/**
|
||||
* 小程序获取openid
|
||||
* @param string $code
|
||||
* @return array
|
||||
* @throws BusinessException
|
||||
*/
|
||||
public function xcxOpenid(string $code): array
|
||||
{
|
||||
// 创建微信小程序对象
|
||||
$wechat = new xcxApplication($this->config);
|
||||
// 获取 session_key 和 openid
|
||||
return $wechat->getUtils()->codeToSession($code);
|
||||
}
|
||||
|
||||
/**
|
||||
* 小程序获取手机号
|
||||
* @param string $phone_code
|
||||
* @return mixed
|
||||
* @throws BusinessException
|
||||
*/
|
||||
public function xcxPhoneNumber(string $phone_code)
|
||||
{
|
||||
// 创建微信小程序对象
|
||||
$wechat = new xcxApplication($this->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']);
|
||||
}
|
||||
}
|
||||
|
||||
// === === === === === === 公众号相关方法 === === === === === ===
|
||||
|
||||
/**
|
||||
* 公众号获取openid
|
||||
* @param string $code
|
||||
* @return mixed
|
||||
* @throws BusinessException
|
||||
*/
|
||||
public function gzhOpenid(string $code)
|
||||
{
|
||||
$app = new gzhApplication($this->config);
|
||||
$app->getClient();
|
||||
$oauth = $app->getOAuth();
|
||||
return $oauth->userFromCode($code);
|
||||
}
|
||||
|
||||
/**
|
||||
* 公众号发送模板消息
|
||||
* @param string $openid
|
||||
* @param string $cagetory
|
||||
* @param string $area
|
||||
* @param string $time
|
||||
* @param int $city_id
|
||||
* @param int $category_id
|
||||
* @return bool
|
||||
* @throws BusinessException
|
||||
*/
|
||||
public function gzhSendNotice(string $openid, string $cagetory, string $area, string $time, $city_id, $category_id)
|
||||
{
|
||||
$wechat = new gzhApplication($this->config);
|
||||
$token = $wechat->getAccessToken()->getToken();
|
||||
$template = [
|
||||
'touser' => $openid,
|
||||
'template_id' => 'NKThzslzC2XabRjfrq5M-TQO1zILJts5gklQlwvG1W8',
|
||||
"miniprogram" => [
|
||||
"appid" => "wxdf4bb512456d3463",
|
||||
"pagepath" => "pages/index/index?category_id=$category_id&city_id=$city_id&city_name=$area"
|
||||
],
|
||||
'data' => [
|
||||
'thing3' => ['value' => $cagetory],
|
||||
'thing23' => ['value' => $area],
|
||||
'time25' => ['value' => $time]
|
||||
]
|
||||
];
|
||||
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;
|
||||
$result = $wechat->getClient()->postJson($url, $template);
|
||||
if ($result['errcode'] == 0) {
|
||||
return true;
|
||||
}else{
|
||||
Log::warning($result->getContent());
|
||||
throw new BusinessException($result->getContent());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 公众号获取用户信息
|
||||
* @param string $openid
|
||||
* @return string
|
||||
* @throws BusinessException
|
||||
*/
|
||||
public function gzhUserinfo(string $openid)
|
||||
{
|
||||
$wechat = new gzhApplication($this->config);
|
||||
$token = $wechat->getAccessToken()->getToken();
|
||||
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$token&openid=$openid&lang=zh_CN";
|
||||
$result = $wechat->getClient()->get($url);
|
||||
return $result->getContent();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user