oftVf0iXOUBEuBMj2LOhODTE8SoY [nickname] => 朱发刚 [sex] => 1
[language] => zh_CN [city] => 元朗区 [province] => 香港
[country] => 中国
[headimgurl] =>
http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLX6BOQKz25FVs3W1jvqlUkmnibgXne80H4kticKWhiaf0h9lHSG18tjyh7j9ef36lWHno3YyN3hBnjA/132
[privilege] => Array ( ) )
朱发刚oooooooooooooooo您已经授权成功!关闭本页面即可开始咨询.改名成功!
*/
$toUsername=$_GET["toUsername"];;
$json = file_get_contents('json/'.$toUsername.'.json');
//echo $json;
$arr = json_decode($json,true);
$appid = $arr['Gappid'];
if($appid==''){echo '缺少授权服务号的appid';
exit;};
$secret = $arr['GAPPSECRET'];
if($secret==''){echo '缺少授权服务号的GAPPSECRET';
exit;};
// print_r($toUsername);
$code = $_GET["code"];
$zfopenid = $_GET["zfopenid"];
$kfopenid = $_GET["kfopenid"];
$kfname = $_GET["kfname"];
$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = getJson($oauth2Url);
// print_r($oauth2);
//第二步:根据全局access_token和openid查询用户信息
if ($oauth2['errcode']<>0){
echo "参数错误,一般是密钥错误引起,请检查.
";
print_r($oauth2['errmsg']);
exit;
}
$access_token = $oauth2["access_token"];
$openid = $oauth2['openid'];
$get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
//处理授权识别问题???
$userinfo = getJson($get_user_info_url);
//print_r ($userinfo);
if ($userinfo['errcode']==0){
//打印用户信息
$ucid=$arr['uniacid'];
$website=$arr['website'];
if($kfopenid<>''){
$json = file_get_contents("json/".$ucid."_kefu.json");
$kefuarr = json_decode($json,true); //客服列表
if($kefuarr[$kfopenid]=='空闲'){
$kefuarr[$kfopenid]=$zfopenid;
$res=file_put_contents("json/".$ucid."_kefu.json", json_encode($kefuarr));
$okstr='接入客服'.$kfname;
}
}
// print_r("");
print_r('你好!'.$userinfo['nickname'].'授权成功!'.$okstr.' 关闭本页面,回复任意字符即可开始咨询');
//插入数据库 //
edituser($ucid,$zfopenid,$userinfo['nickname'],$website,$userinfo['headimgurl'],$userinfo['city']);
define("APPID", $arr['APPID']);
define("APPSECRET", $arr['APPSECRET']);
$myopenid=trim($arr['openids']);//管理员id
$Atoken=getToken(APPID,APPSECRET,$toUsername);//修正token时间问题
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$Atoken;
// echo $url ;
$zt=$kfname.":";
//给用户说话 //
$data='{ "touser":"'.$zfopenid.'", "msgtype":"text",
"text": { "content":"'.$zt.$userinfo['nickname'].',你好!有什么可以帮你的吗. ' . $zt . ' " } }';
// echo $data;
//生成用户缓存
alluser($ucid,$website);
//删除缓存数据
//exec("rm -f ".'json/'.$ucid.'_token.json');
$aaa = http_post_data($url, $data);
//通知管理员//
if ($userinfo['sex']==1){ $sex=',性别:男';}
if($userinfo['sex']==0){ $sex='性别:女';}
$baogao=' {
"title": "['.$kfname.'用户授权成功]'.$userinfo['nickname'].'",
"description": "来自: '.$userinfo['province'].$userinfo['city'].$sex.' \n本链接仅供通知.",
"url": "'.$website.'addons/qidong_app/user.php?openid='.$zfopenid.'",
"thumb_url": "'.$userinfo['headimgurl'].'"
}';
//echo $baogao;
// logger($baogao);
$caidan=trim($arr['Gjsonid']);//进入通知:
if ($caidan<>''){
$Btoken=getToken($arr['Gappid'],$arr['GAPPSECRET'],$caidan);//修正token时间问题
// logger("通知下发:".$caidan_arr['APPID'].'-状态:'.$caidan_arr['openids']);
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$Btoken;
$myopenid=trim($arr['Gopenids']);
}
$data1='{ "touser":"'.$myopenid.'", "msgtype":"link", "link": '.$baogao.' }';
$aaa = http_post_data($url, $data1); //
if($kfopenid<>''){
$baogao=' {
"title": "[新用户授权进入]'.$zt.'-'.$userinfo['city'].'",
"description": "来自: '.$userinfo['province'].$userinfo['city'].$sex.' \n本链接仅供通知.",
"url": "'.$website.'addons/qidong_app/user.php?openid='.$zfopenid.'",
"thumb_url": "'.$userinfo['headimgurl'].'"
}';
$data1='{ "touser":"'.$kfopenid.'", "msgtype":"link", "link": '.$baogao.' }';
}
echo("");
}else
{
print_r('授权失败,请检查信息是否正确.');
}
function getJson($url){
$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);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}
function edituser($ucid,$openid,$nickname,$website,$avartar,$zhuangtai) //改名取号
{
$token_access_url = "{$website}/app/index.php?i=".$ucid."&c=entry&a=wxapp&do=edituser&m=qidong_app&openid=".$openid."&nickname=".$nickname."&avartar=".$avartar."&zhuangtai=".$zhuangtai; //zhuangtai
// echo $token_access_url;
// exit;
$res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
$result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP
$data=$result['data'];
if ($data==1){$data='改名成功!';}
else{$data='改名失败!';}
return $data ;
}
function getToken($appid,$appsecret,$jsonid){
$file = file_get_contents("json/{$jsonid}_token.json",true);
$result = json_decode($file,true);
if (time() > $result['expires']){
$data = array();
$data['access_token'] = getNewToken($appid,$appsecret);
$data['expires']=time()+7200;
$jsonStr = json_encode($data);
$fp = fopen("json/{$jsonid}_token.json", "w");
fwrite($fp, $jsonStr);
fclose($fp);
return $data['access_token'];
}else{
return $result['access_token'];
}
}
function getNewToken($appid,$appsecret){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
$access_token_Arr = https_request($url);
return $access_token_Arr['access_token'];
}
function https_request ($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$out = curl_exec($ch);
curl_close($ch);
return json_decode($out,true);
}
function http_post_data($url, $data_string) {
set_time_limit ( 0 );
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
function alluser($ucid,$website){
$token_access_url = "{$website}/app/index.php?i=".$ucid."&c=entry&a=wxapp&do=Getallopenid&m=qidong_app";
$res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
$result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP
$data=$result['data'] ;
return json_encode($data) ;
}
?>