作者 lihan
1 个管道 的构建 通过 耗费 0 秒

测试

... ... @@ -18,13 +18,11 @@ class IndexController extends HomeBaseController
$wx->code();
} else {
$code = request()->param('code');
$openid = $wx->getOpenid($code);
print_r($openid);exit();
if(Db::name('user')->where(['openid'=>$openid])->count() == 0) {
$info = $wx->getOpenid($code);
if(Db::name('user')->where(['openid'=>$info['openid']])->count() == 0) {
//注册新用户
//拉去用户信息
$return = $wx->getUserInfo($openid);
print_r($return);exit();
$return = $wx->getUserInfo($info);
$data = [
'user_type' => 2,
'create_time' => time(),
... ... @@ -35,12 +33,12 @@ class IndexController extends HomeBaseController
if(Db::name('user')->insert($data)) {
$userId = Db::name('user')->getLastInsID();
session('user.id', $userId);
session('user.openid', $openid);
session('user.openid', $info['openid']);
}
}else {
$userId = Db::name('user')->where(['openid'=>$openid])->value('id');
$userId = Db::name('user')->where(['openid'=>$info['openid']])->value('id');
session('user.id', $userId);
session('user.openid', $openid);
session('user.openid', $info['openid']);
}
}
}
... ...
<?php
class WeChatCommon {
class WeChatCommon
{
/**
* 判断是否已关注公众号
*/
public function isAuth() {
public function isAuth()
{
$access_token = $this->getAccessToken();
$subscribe_msg = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".session('openid');
$subscribe_msg = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $access_token . "&openid=" . session('openid');
$subscribe = json_decode(file_get_contents($subscribe_msg));
$gzxx = $subscribe->subscribe;
if($gzxx === 1){
if ($gzxx === 1) {
return true;
}else {
} else {
return false;
}
}
... ... @@ -20,9 +22,10 @@ class WeChatCommon {
/**
* 获取code
*/
public function code(){
$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".config('AppID')."&redirect_uri=http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
header('location:'.$url);
public function code()
{
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . config('AppID') . "&redirect_uri=http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . "&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
header('location:' . $url);
}
/**
... ... @@ -30,8 +33,9 @@ class WeChatCommon {
* @param $code
* @return mixed
*/
public function getOpenid($code){
$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.config('AppID').'&secret='.config('AppSecret').'&code='.$code .'&grant_type=authorization_code';
public function getOpenid($code)
{
$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . config('AppID') . '&secret=' . config('AppSecret') . '&code=' . $code . '&grant_type=authorization_code';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $get_token_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
... ... @@ -47,13 +51,13 @@ class WeChatCommon {
* 获取access_token,全局缓存7200s
* @return mixed
*/
public function getAccessToken(){
$data=cache('Vendor/access_token');
if(!empty($data) && ((time()-$data['time']) < 7000)) {
public function getAccessToken()
{
$data = cache('Vendor/access_token');
if (!empty($data) && ((time() - $data['time']) < 7000)) {
return $data['access_token'];
}
else {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".config('AppID')."&secret=".config('AppSecret')."";
} else {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . config('AppID') . "&secret=" . config('AppSecret') . "";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
... ... @@ -63,10 +67,10 @@ class WeChatCommon {
curl_close($ch);
$jsoninfo = json_decode($output, true);
$access_token = $jsoninfo["access_token"];
$time=time();
$data=array(
'access_token' => $access_token,
'time' => $time
$time = time();
$data = array(
'access_token' => $access_token,
'time' => $time
);
cache('Vendor/access_token', $data);
return $access_token;
... ... @@ -77,8 +81,9 @@ class WeChatCommon {
* 获取用户信息(头像、昵称等)
* @return array
*/
public function getUserInfo($openid){
$url='https://api.weixin.qq.com/sns/userinfo?access_token='.$this->getAccessToken().'&openid='.$openid.'&lang=zh_CN';
public function getUserInfo($info)
{
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $info['access_token'] . '&openid=' . $info['openid'] . '&lang=zh_CN';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
... ... @@ -96,16 +101,17 @@ class WeChatCommon {
* @param $filename
* @return mixed
*/
public function curl_file_get_contents($url,$filename){
public function curl_file_get_contents($url, $filename)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
curl_setopt($ch, CURLOPT_REFERER,_REFERER_);
curl_setopt($ch, CURLOPT_REFERER, _REFERER_);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$r = curl_exec($ch);
curl_close($ch);
file_put_contents('data/upload/headimg/'.$filename, $r);
file_put_contents('data/upload/headimg/' . $filename, $r);
return $filename;
}
... ... @@ -113,12 +119,13 @@ class WeChatCommon {
* 获取js-sdkp票据,全局缓存7200s
* @return mixed
*/
public function get_jsapi_ticket(){
$ticket=cache('Vendor/ticket');
if(!empty($ticket) && ((time()-$ticket['time']) < 7000)) {
public function get_jsapi_ticket()
{
$ticket = cache('Vendor/ticket');
if (!empty($ticket) && ((time() - $ticket['time']) < 7000)) {
return $ticket['ticket'];
}else {
$url='https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$this->getAccessToken().'&type=jsapi';
} else {
$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $this->getAccessToken() . '&type=jsapi';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
... ... @@ -128,10 +135,10 @@ class WeChatCommon {
curl_close($ch);
$json_obj = json_decode($res, true);
$jsapi_ticket = $json_obj['ticket'];
$time=time();
$data=array(
'ticket' => $jsapi_ticket,
'time' => $time
$time = time();
$data = array(
'ticket' => $jsapi_ticket,
'time' => $time
);
cache('Vendor/ticket', $data);
return $jsapi_ticket;
... ... @@ -142,14 +149,15 @@ class WeChatCommon {
* JS_SDK
* @return array
*/
public function js_sdk(){
$timestamp=time();
$string='jsapi_ticket='.$this->get_jsapi_ticket().'&noncestr=je9omv03bc5ryqz1&timestamp='.$timestamp.'&url='.'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$signature=sha1($string);
public function js_sdk()
{
$timestamp = time();
$string = 'jsapi_ticket=' . $this->get_jsapi_ticket() . '&noncestr=je9omv03bc5ryqz1&timestamp=' . $timestamp . '&url=' . 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$signature = sha1($string);
return array(
'appId' => config('AppId'),
'appId' => config('AppId'),
'timestamp' => $timestamp,
'nonceStr' => 'je9omv03bc5ryqz1',
'nonceStr' => 'je9omv03bc5ryqz1',
'signature' => $signature,
);
}
... ... @@ -158,9 +166,10 @@ class WeChatCommon {
* 创建菜单
* @return mixed|string
*/
public function creatMenu(){
public function creatMenu()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->getAccessToken());
curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $this->getAccessToken());
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
... ... @@ -181,7 +190,8 @@ class WeChatCommon {
* 菜单内容JSON
* @return string
*/
public function menuItem(){
public function menuItem()
{
$data = '{
"button":[{
"type":"view",
... ... @@ -205,12 +215,13 @@ class WeChatCommon {
/**
* 上传永久素材
*/
public function eternalMaterial(){
public function eternalMaterial()
{
$file_info = array('filename' => '/public/images/soul_of_cinder.png', //国片相对于网站根目录的路径
'content-type' => 'image/jpg/png', //文件类型
'filelength' => '71' //图文大小
);
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$this->getAccessToken()."&type=png";
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=" . $this->getAccessToken() . "&type=png";
$ch1 = curl_init();
$timeout = 5;
$real_path = "{$_SERVER['DOCUMENT_ROOT']}{$file_info['filename']}";
... ...