HaniBlindBox/server/php/app/common/server/WechatOfficialAccount.php
2026-01-01 20:46:07 +08:00

316 lines
11 KiB
PHP

<?php
namespace app\common\server;
use app\MyController;
class WechatOfficialAccount extends MyController
{
private static $wx_appid = null;
private static $wx_secret = null;
private static $mch = null;
private static $key = null;
public function initialize()
{
$weixinpay = getConfig("wechatofficialaccount");
$wechatofficialaccount_setting = getConfig("wechatofficialaccount_setting");
// 如果系统设置中存在微信公众号配置,则优先使用
if (!empty($wechatofficialaccount_setting) && !empty($wechatofficialaccount_setting['appid']) && !empty($wechatofficialaccount_setting['appSecret'])) {
self::$wx_appid = $wechatofficialaccount_setting['appid'];
self::$wx_secret = $wechatofficialaccount_setting['appSecret'];
} else {
self::$wx_appid = $weixinpay['appid'];
self::$wx_secret = $weixinpay['appSecret'];
}
self::$mch = $weixinpay['mch_id'];
self::$key = $weixinpay['keys'];
}
/**
* 获取用户基础信息
* @param mixed $code
* @return array|\think\response\Json
*/
public function getAccessToken($code)
{
//connect/oauth2/authorize
//https://open.weixin.qq.com/connect/oauth2/authorize
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . self::$wx_appid . "&secret=" . self::$wx_secret . "&code=" . $code . "&grant_type=authorization_code";
$resUserInfo = $this->get_curl_data($url);
if (isset($resUserInfo['errcode'])) {
return $this->renderError('获取用户信息失败');
}
$openid = $resUserInfo['openid'];#openid
$access_token = $resUserInfo['access_token']; # 7200
$unionid = ''; # 7200
if (isset($resUserInfo['unionid'])) {
$unionid = $resUserInfo['unionid'];
}
$data = [
'openid' => $openid,
'unionid' => $unionid,
'access_token_time' => $access_token,
];
return $data;
}
/**
* 拉取用户信息(需scope为 snsapi_userinfo)
* @param mixed $data
* @return mixed
*/
public function getUserInfo($data)
{
$openid = $data['openid'];
$access_token = $data['access_token_time'];
//connect/oauth2/authorize
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN";
$resUserInfo = $this->get_curl_data($url);
if (isset($resUserInfo['errcode'])) {
return $this->renderError('获取用户信息失败');
}
$user = $resUserInfo;#openid
return $user;
}
/**
* 小程序获取手机号
* @param string $code
* @return int|mixed|\think\response\Json
*/
public function getMobile($code = '')
{
$access_token = $this->getAccessTokenOffiaccount();
$mobile_url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" . $access_token;
$mobile_info = $this->post_curl_data($mobile_url, ['code' => $code]);
if ($mobile_info['errcode'] == 0) {
return $mobile_info['phone_info']['phoneNumber'];
} else {
return $this->renderError('获取手机号失败,请刷新重试1');
}
}
/**
* 获取公众号token
* @return mixed
*/
public function getAccessTokenOffiaccount()
{
$offiaccount_access_token_info = getConfig('offiaccount_access_token');
if ($offiaccount_access_token_info && $offiaccount_access_token_info['access_token_time'] > time()) {
$access_token = $offiaccount_access_token_info['access_token'];
return $access_token;
} else {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . self::$wx_appid . "&secret=" . self::$wx_secret . "";
$res_access_token = $this->get_curl_data($url);
if (isset($res_access_token['errcode'])) {
return $this->renderError('获取用户信息失败');
}
$access_token = $res_access_token['access_token'];
$expires_in = $res_access_token['expires_in'];
$access_token_time = time() + 3600;
$data = [
'access_token' => $access_token,
'access_token_time' => $access_token_time,
];
setConfig('offiaccount_access_token', $data);
}
return $access_token;
}
/**
* 获取jsapi
*/
public function get_wx_jsapi()
{
$offiaccount_access_token_info = getConfig('offiaccount_access_jsapi');
if ($offiaccount_access_token_info && $offiaccount_access_token_info['access_token_time'] > time()) {
$access_token = $offiaccount_access_token_info['access_token'];
return $access_token;
} else {
$token = $this->getAccessTokenOffiaccount();
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" . $token . "&type=jsapi";
$res_access_token = $this->get_curl_data($url);
if (isset($res_access_token['errcode']) && $res_access_token['errcode'] != 0) {
return $this->renderError('获取用户信息失败');
}
$access_token = $res_access_token['ticket'];
$expires_in = $res_access_token['expires_in'];
$access_token_time = time() + 3600;
$data = [
'access_token' => $access_token,
'access_token_time' => $access_token_time,
];
setConfig('offiaccount_access_jsapi', $data);
}
return $access_token;
}
public function getAccessTokenOffiaccountSign($url)
{
$noncestr = "Wm3WZYTPz0wzccnW";
$jsapi_ticket = $this->get_wx_jsapi();
$timestamp = time();
$string1 = "jsapi_ticket=" . $jsapi_ticket . "&noncestr=" . $noncestr . "&timestamp=" . $timestamp . "&url=" . $url . "";
//jsapi_ticket=O3SMpm8bG7kJnF36aXbe8-L58ZIF-XLkZpoZHk0az3lwDnVE_M4v2aSPqrR1c8JZ_Nsi_blGIaPmLq8ZLm09uQ&noncestr=Wm3WZYTPz0wzccnW&timestamp=1721798091&url=http://192.168.1.10:8080/h5/
$key = sha1($string1);
$data = [
'signature' => $key,
// 'jsapi_ticket' => $jsapi_ticket,
'timestamp' => $timestamp,
'noncestr' => $noncestr,
'appId' => self::$wx_appid
];
return $data;
}
/**
* @param $url 请求链接
*/
public function get_curl_data($url)
{
$headerArray = array("Content-type:application/json;", "Accept:application/json");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
$response = curl_exec($ch);
if ($response) {
curl_close($ch);
return json_decode($response, true);
} else {
$error = curl_errno($ch);
curl_close($ch);
return ['errcode' => 1];
}
}
/**
* @param $url 链接
* @param $data 参数
* @return mixed
*/
public function post_curl_data($url, $data)
{
$data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data)
)
);
$return_content = curl_exec($ch);
if ($return_content) {
curl_close($ch);
$return_content = json_decode($return_content, true);
return $return_content;
} else {
$error = curl_errno($ch);
curl_close($ch);
return ['errcode' => 1];
}
}
/**
* http - post请求
* @param $url
* @param $data
* @return bool|string
*/
public function http_post_data($url, $data)
{
$data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data)
)
);
$return_content = curl_exec($ch);
return $return_content;
}
public function post_order($openid, $access_token, $order_num, $title = '订单发货')
{
$request_url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=" . $access_token;
$param = '{
"order_key": {
"order_number_type": 1,
"mchid":"' . self::$mch . '",
"out_trade_no":"' . $order_num . '"
},
"logistics_type": 4,
"delivery_mode": 1,
"shipping_list": [
{
"item_desc": "本单购买商品已发放至[小程序盒柜]"
}
],
"upload_time": "2023-08-07T17:16:31+08:00",
"payer": {
"openid":"' . $openid . '"
}
}';
$res = curlPost($request_url, $param);
$res = json_decode($res, true);
if ($res['errcode'] == 0 && $res['errmsg'] == 'ok') {
return 1;
} else {
return 2;
}
}
public function post_order_one($openid, $access_token, $order_num, $title = '订单发货')
{
$request_url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=" . $access_token;
$param = '{
"order_key": {
"order_number_type": 1,
"mchid":"' . self::$mch . '",
"out_trade_no":"' . $order_num . '"
},
"logistics_type": 4,
"delivery_mode": 1,
"shipping_list": [
{
"item_desc": "本单购买商品已发放至[小程序盒柜]"
}
],
"upload_time": "2023-08-07T17:16:31+08:00",
"payer": {
"openid":"' . $openid . '"
}
}';
$res = curlPost($request_url, $param);
$res = json_decode($res, true);
if ($res['errcode'] == 0 && $res['errmsg'] == 'ok') {
return 1;
} else {
return 2;
}
}
}