public function generate($code, $isShow)
{
// 构建二维码参数
$scene = 'C=' . $code.'&path=green';
$params = [
"scene" => $scene,
'page' => 'pages/login/registerSales', // 小程序页面路径
'width' => 200, // 二维码宽度
];
// 调用微信接口获取 access_token
$wechatService = new WechatApiService();
$access_token = $wechatService->getAccessToken(true);
if (empty($access_token)) {
$access_token = $wechatService->getAccessToken(true);
}
$accessToken = $access_token;
// 调用微信接口生成小程序二维码
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$imageData = curl_exec($ch);
curl_close($ch);
if($isShow){
header("Content-Type: image/jpeg;text/html; charset=utf-8");
echo $imageData;
exit;
}
$path = 'uploadfile/admin/green_invite_qrcode/';
if(!file_exists($path)){
mkdir($path, 0777, true);
}
// 保存二维码图片
$filename = 'invite_qrcode_' . $code . '.png';
$file_path = public_path($path) . $filename;
file_put_contents($file_path, $imageData);
return $path . $filename;
}
<?php
/**
* 获取微信相关接口
*/
namespace App\Services\Wechat;
use App\Tools\Common;
use App\Tools\HttpCurl;
use App\Tools\RedisCommon;
class WechatApiService extends \App\Services\BaseService
{
public function getOpenidByCode($code)
{
$url = "https://api.weixin.qq.com/sns/jscode2session";
$data = [
'appid' => Common::getConfigValByEnv('wechat.APP_ID'),
'secret' => Common::getConfigValByEnv('wechat.APP_SECRET'),
'js_code' => $code,
'grant_type' => 'authorization_code',
];
/**
* 正确时返回
*{"session_key": "6I7echmzado/KAk0tw9t1g==",
*"openid": "oy3tU41PkUKSzXgMQB_K515qMS_A"}
* 失败时返回:{"errcode": 40163,"errmsg": "code been used, rid: 6448dd1c-6b6cb729-1abe846a"}
*/
$result = HttpCurl::sendGet($url, $data);
unset($data['secret']);
Common::logInfo('','查询微信openid返回', ['params'=>$data,'result'=>$result]);
$result = json_decode($result,true);
if(Common::getConfigValByEnv('wechat.wx_api_debug') === true && empty($result['openid'])){
$result['openid'] = 'abcfdfsd5454';
}
return $result;
}
/**
* 通过小程序code获取用户手机号
* {
"errcode":0,
"errmsg":"ok",
"phone_info": {
"phoneNumber":"xxxxxx",
"purePhoneNumber": "xxxxxx",
"countryCode": 86,
"watermark": {
"timestamp": 1637744274,
"appid": "xxxx"
}
}
}
* @param $code
* @return void
*/
public function getPhoneNumber($code)
{
$result = $this->getWxPhoneNumber($code);
if($result['errcode'] != '0' && $result['errcode'] == '40001'){
$result = $this->getWxPhoneNumber($code, true);
}
if($result['errcode'] != '0'){
return self::showMsg(self::CODE_FAIL, self::MSG_FAIL, $result);
}
return self::showMsg(self::CODE_SUCCESS, self::MSG_SUCCESS, $result['phone_info']);
}
private function getWxPhoneNumber($code, $refreshToken=false)
{
if(env('APP_ENV') == 'local'){
$url = 'https://consultant.noakcent.com/wp/wechat/get_access_token';
$result = HttpCurl::sendPost($url, ['tk'=>'!@#$%^789_'.date('md')]);
$acObj = json_decode($result, true);
$token = $acObj['data'];
}else{
$token = $this->getAccessToken($refreshToken);
}
$url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=".$token;
$data = ['code'=>$code];
$result = HttpCurl::sendJsonPost($url, $data);
Common::logInfo('','查询微信getPhoneNumber返回', ['url'=>$url,'params'=>$data,'result'=>$result]);
$result = json_decode($result,true);
return $result;
}
/**
* 获取微信access_token值
* @return void
*/
public function getAccessToken($refresh=false)
{
$key = 'nobel.wechat.access_token';
$accessToken = RedisCommon::get($key);
if(!$accessToken || $refresh === true){
$url = "https://api.weixin.qq.com/cgi-bin/token";
$data = [
'appid' => Common::getConfigValByEnv('wechat.APP_ID'),
'secret' => Common::getConfigValByEnv('wechat.APP_SECRET'),
'grant_type' => 'client_credential',
];
$result = HttpCurl::sendGet($url, $data);
unset($data['secret']);
Common::logInfo('','查询微信access_token返回', ['params'=>$data,'result'=>$result]);
$result = json_decode($result,true);
if(!empty($result['access_token'])){
$accessToken = $result['access_token'];
RedisCommon::set($key, $accessToken, 7190);
}
}
// if(Common::getConfigValByEnv('wechat.wx_api_debug') === true && empty($result['openid'])){
// $result['openid'] = 'test';
// }
Common::logInfo('','微信access_token:', ['wx-accesstoken'=>$accessToken]);
return $accessToken;
}
}