<?php
namespace appstoreapicontroller;
use appstoreapimodelWx as WxModel;
use thinkDb;
class Wx extends BaseController
{
public $model = '';
/**
* 初始化数据
* Wx constructor.
*/
public function __construct()
{
parent::__construct();
$this->model = new WxModel();
}
public function index()
{
if(!isset($_GET['echostr'])){
$this->responseMsg();
}else{
$this->valid();
}
/*$appid = config('wx.wx_app_id');
$secret=config('wx.wx_app_secret');
if(!$this->request->get('code','')){
$redirect_uri = urlEncode('https://test.yishaxiyi.com/storeapi/wxUserInfo');
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
header("Location:$url");
}else{
$code = $this->request->get('code','');
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$return = json_decode(curl_get($url),true);
$access_token = $return['access_token'];
$openid = $return['openid'];
$user_info = json_decode(curl_get("https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN"),true);
$user = Db::name('wx_user')->where('wx_openid','=',$openid)->find();
if($user){
}else{
$post['wx_openid'] = $openid;
$post['code'] = $code;
$post['unionid'] = isset($user_info['unionid'])?$user_info['unionid']:'';
$post['nickname'] = isset($user_info['nickname'])?$user_info['nickname']:'';
$post['sex'] = isset($user_info['sex'])?$user_info['sex']:'';
$post['avatar'] = isset($user_info['headimgurl'])?$user_info['headimgurl']:'';
$post['create_time'] = time();
return $this->model->addData($post);
}
}*/
}
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = config('wx.token');
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if($tmpStr == $signature){
return true;
}else{
return false;
}
}
public function responseMsg()
{
$postStr = file_get_contents("php://input");
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
switch ($RX_TYPE)
{
case "event":
$result = $this->receiveEvent($postObj);
break;
}
echo $result;
}else {
echo "";
exit;
}
}
private function receiveEvent($object)
{
$content = "";
switch ($object->Event)
{
case "subscribe": //关注事件
$content = "欢迎关注马上洗公众号,更多功能持续开发中,敬请期待...";
break;
case "unsubscribe": //取消关注事件
$content = "";
break;
}
$result = $this->transmitText($object, $content);
return $result;
}
private function transmitText($object, $content)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
$res = $this->model->where('wx_openid',$object->FromUserName)->find();
if($res){
return $result;
}
$appid = config('wx.wx_app_id');
$secret=config('wx.wx_app_secret');
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
$return = json_decode(curl_get($url),true);
$access_token = $return['access_token'];
$getUserInfoUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$object->FromUserName&lang=zh_CN";
$return = json_decode(curl_get($getUserInfoUrl),true);
$this->model->insert([
'subscribe' => $return['subscribe'],
'unionid' => $return['unionid'],
'wx_openid' => $return['openid'],
'nickname' =>$return['nickname'],
'sex' => $return['sex'],
'avatar' => $return['headimgurl'],
'create_time' => time(),
]);
return $result;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。