*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }
:)
ThinkPHP V5
这是8116labourunion
[ V5.0 版本由 七牛云 独家赞助发布 ] ';
}
public function getOpenid()
{
$userinfo = request()->param();
$code = $userinfo['code'];
$appid = 'wxcacf6eb6e7478e29';
$appSecret = '3722c3f95df0569498a0e32ae6e85153';
$wxUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';
$getUrl = sprintf($wxUrl, $appid, $appSecret, $code);
// echo $getUrl;
$result = $this->curl_get($getUrl);
// echo $result;
$wxResult = json_decode($result, true);
if (empty($wxResult)) {
$res_r['code'] = 200;
$res_r['errmsg'] = '错误';
return json($res_r);
} else {
$logfail = array_key_exists('errcode', $wxResult);
if ($logfail) {
var_dump($wxResult);
Log::write(json_encode($wxResult));
} else {
$openid = $wxResult['openid'];
// $unionid=null;
$unionid = '';
if (array_key_exists('unionid', $wxResult)) {
$unionid = $wxResult['unionid'];
} else {
Log::write(json_encode($wxResult));
}
$res_r['code'] = 200;
$res_r['resultData']['openId'] = $openid;
$res_r['resultData']['openid'] = $openid;
if (array_key_exists('unionid', $wxResult)) {
$res_r['resultData']['unionid'] = $unionid;
$t_m = new memodel();
$urec = $t_m->getinfobyopenid($openid);
if ($urec) {
if (!$urec['union_id']) {
$t_m->updunionidbyopenid($openid, $unionid);
}
}
}
return json($res_r);
// return json($wxResult);
}
}
}
public function curl_get($url, &$httpCode = 0)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//不做证书校验,部署在linux环境下请改为true
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$file_contents = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $file_contents;
}
public function getAccessToken()
{
$appid = 'wxcacf6eb6e7478e29';
$appSecret = '3722c3f95df0569498a0e32ae6e85153';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appSecret;
//echo $url;
$result = $this->curl_get($url);
//echo $result;
$wxResult = json_decode($result, true);
//echo $wxResult['access_token'];
return $wxResult;
}
public function getAccessTokenWCGZH()
{
$appid = 'wxc77d58456db8082b'; //服务号
$appSecret = '456c9c2a328b3bee5001f2326d862916';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appSecret;
//echo $url;
$result = $this->curl_get($url);
//echo $result;
$wxResult = json_decode($result, true);
//echo $wxResult['access_token'];
return $wxResult;
}
//获得二维码
public function create_qrcode()
{
$sharepage = request()->param();
$qr_path = "./Uploads/";
if (!file_exists($qr_path . 'user/')) {
mkdir($qr_path . 'user/', 0700, true); //判断保存目录是否存在,不存在自动生成文件目录
}
$filename = 'user/' . time() . '.png';
$file = $qr_path . $filename;
$access = $this->getAccessToken(); //json_decode($this->getAccessToken(),true);
$access_token = $access['access_token'];
$url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $access_token;
$qrcode = array(
'scene' => $sharepage['scene'], //二维码所带参数
'width' => 200,
'page' => $sharepage['page'], //二维码跳转路径(要已发布小程序)
'auto_color' => true,
);
$result = $this->sendCmd($url, json_encode($qrcode)); //请求微信接口
$errcode = json_decode($result, true)['errcode'];
$errmsg = json_decode($result, true)['errmsg'];
if ($errcode) {
echo $errcode;
echo $errmsg;
//$this->render(0,$errmsg);
}
$res = file_put_contents($file, $result); //将微信返回的图片数据流写入文件
if ($res === false) {
echo '生成二维码失败';
// $this->render(0,'生成二维码失败');
} else {
$result_qrcode['resultData'] = '127.0.0.1:8094/Uploads/' . $filename;
$result_qrcode['code'] = '200';
return json_encode($result_qrcode);
//$this->return['data'] = $this->config->item('base_url').'/Uploads/'.$filename;//返回图片地址链接给前端
}
}
//开启curl post请求
public function sendCmd($url, $data)
{
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); //解决数据包大不能提交
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
echo 'Errno' . curl_error($curl);
}
curl_close($curl); // 关键CURL会话
return $tmpInfo; // 返回数据
}
public function getticket()
{
$access = $this->getAccessTokenWCGZH();
$access_token = $access['access_token'];
$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=' . $access_token;
$result = $this->sendCmd($url, []);
$result_arr = json_decode($result, true);
if ('ok' != $result_arr['errmsg']) {
echo 'error:' . $result_arr['errmsg'];
exit;
}
$ticket = $result_arr['ticket'];
return $ticket;
}
//网上代骊,待修改
public function WxDecode()
{
// 接收参数
$data = request()->param();
// 引入解密文件 在微信小程序开发文档下载
// vendor('wx.WXBizDataCrypt');
//vendor('wx.ErrorCode');
//
// $appid = config('APPID');
// $appsecret = config('APPSECREET');
$appid = 'wxcacf6eb6e7478e29';
$appsecret = '3722c3f95df0569498a0e32ae6e85153';
$grant_type = "authorization_code"; //授权(必填)
$code = $data['code']; //有效期5分钟 登录会话
$encryptedData = $data['encryptedData'];
$iv = $data['iv'];
// $signature = $data['signature'];
// $rawData = $data['rawData'];
// 拼接url
$url = "https://api.weixin.qq.com/sns/jscode2session?" . "appid=" . $appid . "&secret=" . $appsecret . "&js_code=" . $code . "&grant_type=" . $grant_type;
$res = json_decode($this->curl_get($url), true);
// var_dump($res);
$sessionKey = $res['session_key']; //取出json里对应的值
// $signature2 = sha1(htmlspecialchars_decode($rawData).$sessionKey);
// // 验证签名
// if ($signature2 !== $signature){
// return json("验签失败");
// }
$pc = new wxBizDataCrypt($appid, $sessionKey);
$errCode = $pc->decryptData($encryptedData, $iv, $data);
// var_dump($data);
//这里是按二手设备做的,以后应该单独变成逻辑,由逻辑层完成,以应对不同的需求
if ($errCode == 0) {
$result['code'] = 200;
$resultData['msg'] = '解密成功';
$resultData['userInfo'] = json_decode($data);
$result['resultData'] = $resultData;
echo json_encode($result);
// exit(json_encode($data,JSON_UNESCAPED_UNICODE));
} else {
return json($errCode);
}
}
//获得二维码
public function download_qrcode()
{
$sharepage = request()->param();
$qr_path = "./Uploads/";
if (!file_exists($qr_path . 'user/')) {
mkdir($qr_path . 'user/', 0700, true); //判断保存目录是否存在,不存在自动生成文件目录
}
$filename = 'user/' . time() . '.png';
$file = $qr_path . $filename;
$access = $this->getAccessToken(); //json_decode($this->getAccessToken(),true);
$access_token = $access['access_token'];
$url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $access_token;
$qrcode = array(
'scene' => $sharepage['scene'], //二维码所带参数
'width' => 200,
'page' => $sharepage['page'], //二维码跳转路径(要已发布小程序)
'auto_color' => true,
);
$result = $this->sendCmd($url, json_encode($qrcode)); //请求微信接口
$errcode = json_decode($result, true)['errcode'];
$errmsg = json_decode($result, true)['errmsg'];
if ($errcode) {
echo $errcode;
echo $errmsg;
//$this->render(0,$errmsg);
}
$res = file_put_contents($file, $result); //将微信返回的图片数据流写入文件
$strfile = 'Uploads/' . $filename;
//echo $strfile;
$file1 = fopen($strfile, 'r');
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="' . $strfile . '"');
ob_clean();
flush();
echo fread($file1, filesize($file));
fclose($file1);
// $result->save('php://output');
if ($res === false) {
echo '生成二维码失败';
// $this->render(0,'生成二维码失败');
} else {
$result_qrcode['resultData'] = 'https://app.gasgrid.cn::21443/Uploads/' . $filename;
$result_qrcode['code'] = '200';
return json_encode($result_qrcode);
//$this->return['data'] = $this->config->item('base_url').'/Uploads/'.$filename;//返回图片地址链接给前端
}
}
public function getsharedata()
{
$appid = 'wxc77d58456db8082b'; //服务号
$ticket = $this->getticket();
$timestamp = time();
$nonceStr = getRandomStrings();
$data = ['ticket' => $ticket, 'timestamp' => $timestamp, 'nonceStr' => $nonceStr, 'appid' => $appid];
return $data;
}
}