初始化代码
This commit is contained in:
56
application/api/wxapi/wxmsg/Base.php
Normal file
56
application/api/wxapi/wxmsg/Base.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
namespace app\api\wxapi\wxmsg;
|
||||
|
||||
class Base
|
||||
{
|
||||
protected $OtherData=[];
|
||||
|
||||
public static function className()
|
||||
{
|
||||
return get_called_class();
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->init();
|
||||
}
|
||||
|
||||
public function init()
|
||||
{
|
||||
}
|
||||
|
||||
public function __get($name){
|
||||
$getter='get'.$name;
|
||||
if(method_exists($this, $getter)){
|
||||
return $this->$getter();
|
||||
}else if(isset($this->OtherData[$name])){
|
||||
return $this->OtherData[$name];
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
public function __set($name, $value){
|
||||
$setter='set'.$name;
|
||||
if(method_exists($this, $setter)){
|
||||
$this->$setter($value);
|
||||
}else{
|
||||
$this->OtherData[$name]=$value;
|
||||
}
|
||||
}
|
||||
|
||||
protected function nonceStr($len)
|
||||
{
|
||||
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
|
||||
$arr=[];
|
||||
for($i=0;$i<strlen($chars);$i++){
|
||||
$arr[]=$chars{$i};
|
||||
}
|
||||
shuffle($arr);
|
||||
$arr=array_chunk($arr,$len);
|
||||
$arr=array_shift($arr);
|
||||
$arr=strtoupper(implode("",$arr));
|
||||
return $arr;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
81
application/api/wxapi/wxmsg/BaseObject.php
Normal file
81
application/api/wxapi/wxmsg/BaseObject.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
namespace app\api\wxapi\wxmsg;
|
||||
|
||||
|
||||
use app\api\wxapi\Base;
|
||||
use think\Db;
|
||||
|
||||
class BaseObject extends Base{
|
||||
|
||||
public $token; //响应接收消息URL Token
|
||||
|
||||
public function run(){
|
||||
$resq=$this->xmlToArray(file_get_contents("php://input"));
|
||||
/*$xml='<xml><ToUserName><![CDATA[gh_9f61b83af9e8]]></ToUserName>
|
||||
<FromUserName><![CDATA[oDXoW66sZbQwyYA2Rzpqd_ZA7cyI]]></FromUserName>
|
||||
<CreateTime>1605773759</CreateTime>
|
||||
<MsgType><![CDATA[event]]></MsgType>
|
||||
<Event><![CDATA[subscribe]]></Event>
|
||||
<EventKey><![CDATA[]]></EventKey>
|
||||
</xml>';
|
||||
$resq=json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);*/
|
||||
foreach($resq as $key=>$val){
|
||||
$this->$key=$val;
|
||||
}
|
||||
|
||||
/*$TempDataLog=[];
|
||||
$TempDataLog['event'] = $resq['Event'];
|
||||
$TempDataLog['contents'] = json_encode($resq);
|
||||
$TempDataLog['addtime'] = time();
|
||||
Db::name('wx_temp_log')->insertGetId($TempDataLog);*/
|
||||
|
||||
//error_log(json_encode($resq), 3, 'sub.log');
|
||||
return true;
|
||||
}
|
||||
|
||||
public function CheckSignature(){
|
||||
$sign=$_GET;
|
||||
$arr['token']=$this->token;
|
||||
$arr['timestamp']=$sign['timestamp'];
|
||||
$arr['nonce']=$sign['nonce'];
|
||||
asort($arr);
|
||||
$sha1=sha1(implode('',$arr));
|
||||
if($sha1==$sign['signature']){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function xmlToArray($xml)
|
||||
{
|
||||
//error_log($xml, 3, 'xml.log');
|
||||
//将XML转为array
|
||||
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
|
||||
return $array_data;
|
||||
}
|
||||
|
||||
public function ArrayToXml($arr){
|
||||
$xml='<?xml version="1.0" encoding="UTF-8"?>';
|
||||
$xml='<xml>';
|
||||
$this->ToXml($arr,$xml);
|
||||
$xml.='</xml>';
|
||||
return $xml;
|
||||
}
|
||||
|
||||
public function ToXml($data,&$xml){
|
||||
foreach($data as $k=>$v){
|
||||
if(is_array($v)){
|
||||
$xml.=' <'.$k.'>';
|
||||
$this->ToXml($v, $xml);
|
||||
$xml.=' </'.$k.'>';
|
||||
}else{
|
||||
if(is_int($v)){
|
||||
$xml.=' <'.$k.'>'.$v.'</'.$k.'>';
|
||||
}else{
|
||||
$xml.=' <'.$k.'><![CDATA['.$v.']]></'.$k.'>';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
22
application/api/wxapi/wxmsg/Client.php
Normal file
22
application/api/wxapi/wxmsg/Client.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace app\api\wxapi\wxmsg;
|
||||
|
||||
use app\api\wxapi\wxmsg\BaseObject;
|
||||
use app\api\wxapi\wxmsg\Event;
|
||||
use app\api\wxapi\wxmsg\ReceiveText;
|
||||
|
||||
class Client{
|
||||
public $BaseObject;
|
||||
|
||||
public function run(){
|
||||
switch($this->BaseObject->MsgType){
|
||||
case 'event':
|
||||
$Event=new Event();
|
||||
$Event->BaseObject=$this->BaseObject;
|
||||
$Event->run();
|
||||
break;
|
||||
default:
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
38
application/api/wxapi/wxmsg/Event.php
Normal file
38
application/api/wxapi/wxmsg/Event.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
namespace app\api\wxapi\wxmsg;
|
||||
|
||||
use app\api\wxapi\wxmsg\EventClick;
|
||||
use think\Db;
|
||||
|
||||
/* 扫描带参数二维码事件
|
||||
* */
|
||||
class Event{
|
||||
public $BaseObject;
|
||||
|
||||
public function run(){
|
||||
switch($this->BaseObject->Event){
|
||||
case 'CLICK': //点击菜单拉取消息时的事件推送
|
||||
$EventClick=new EventClick();
|
||||
$EventClick->BaseObject=$this->BaseObject;
|
||||
$EventClick->run();
|
||||
break;
|
||||
case 'subscribe':
|
||||
$Subscribe=new Subscribe();
|
||||
$Subscribe->BaseObject=$this->BaseObject;
|
||||
$Subscribe->run();
|
||||
case 'SCAN':
|
||||
$Subscribe=new Subscribe();
|
||||
$Subscribe->BaseObject=$this->BaseObject;
|
||||
$Subscribe->run();
|
||||
default :
|
||||
$TempDataLog=[];
|
||||
$TempDataLog['event'] = $this->BaseObject->Event;
|
||||
$TempDataLog['from_user_name'] = $this->BaseObject->FromUserName;
|
||||
|
||||
$content=json_decode(json_encode(simplexml_load_string(file_get_contents("php://input"), 'SimpleXMLElement', LIBXML_NOCDATA)), true);
|
||||
$TempDataLog['contents'] = json_encode($content);
|
||||
$TempDataLog['addtime'] = time();
|
||||
Db::name('wx_temp_log')->insertGetId($TempDataLog);
|
||||
}
|
||||
}
|
||||
}
|
||||
23
application/api/wxapi/wxmsg/EventClick.php
Normal file
23
application/api/wxapi/wxmsg/EventClick.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace app\api\wxapi\wxmsg;
|
||||
|
||||
use app\api\wxapi\Base;
|
||||
use app\api\wxapi\wxmsg\Reply;
|
||||
|
||||
/* 点击菜单拉取消息时的事件推送
|
||||
* */
|
||||
class EventClick extends Base{
|
||||
public $BaseObject;
|
||||
|
||||
public function run(){
|
||||
$this->send();
|
||||
}
|
||||
|
||||
//回复消息
|
||||
public function send(){
|
||||
$Reply=new Reply();
|
||||
$Reply->BaseObject=$this->BaseObject;
|
||||
$Reply->run();
|
||||
}
|
||||
|
||||
}
|
||||
25
application/api/wxapi/wxmsg/ReceiveText.php
Normal file
25
application/api/wxapi/wxmsg/ReceiveText.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace app\api\wxapi\wxmsg;
|
||||
|
||||
|
||||
use app\api\wxapi\Base;
|
||||
use app\api\wxapi\wxmsg\Reply;
|
||||
|
||||
/* 接收text消息
|
||||
* */
|
||||
class ReceiveText extends Base{
|
||||
public $BaseObject;
|
||||
|
||||
public function run(){
|
||||
|
||||
$this->send();
|
||||
}
|
||||
|
||||
//回复消息
|
||||
public function send(){
|
||||
$Reply=new Reply();
|
||||
$Reply->BaseObject=$this->BaseObject;
|
||||
$Reply->run();
|
||||
}
|
||||
|
||||
}
|
||||
62
application/api/wxapi/wxmsg/Reply.php
Normal file
62
application/api/wxapi/wxmsg/Reply.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
namespace app\api\wxapi\wxmsg;
|
||||
|
||||
use app\api\wxapi\Base;
|
||||
use app\api\wxapi\wxmsg\ReplyText;
|
||||
|
||||
/* 回复消息
|
||||
* */
|
||||
class Reply extends Base{
|
||||
public $BaseObject;
|
||||
|
||||
public function run(){
|
||||
$EventKey=$this->BaseObject->EventKey;
|
||||
|
||||
$data['ToUserName']=$this->BaseObject->FromUserName;
|
||||
$data['FromUserName']=$this->BaseObject->ToUserName;
|
||||
$data['CreateTime']=time();
|
||||
|
||||
switch($EventKey){
|
||||
case 'click_tel': //回复文本消息
|
||||
$data['MsgType']='text';
|
||||
$data['Content']='4000500510';
|
||||
break;
|
||||
case 'click_down':
|
||||
$data['MsgType']='text';
|
||||
$data['Content']='即将上线';
|
||||
break;
|
||||
case 'click_pic':
|
||||
$data['MsgType']='image';
|
||||
$data['Image']=[
|
||||
'MediaId'=>'',
|
||||
];
|
||||
break;
|
||||
default :
|
||||
exit('');
|
||||
}
|
||||
echo $this->BaseObject->ArrayToXml($data);
|
||||
exit;
|
||||
/*查SQL*/
|
||||
/* $MsgType='';
|
||||
|
||||
if($ReplyTextCout=M("wx_reply_text")->where(['EventKey'=>$this->BaseObject->EventKey,])->count()){
|
||||
$MsgType='text';
|
||||
} */
|
||||
|
||||
/*查SQL*/
|
||||
|
||||
/* switch($MsgType){
|
||||
case 'text': //回复文本消息
|
||||
$ReplyText=new ReplyText();
|
||||
$ReplyText->BaseObject=$this->BaseObject;
|
||||
$ReplyText->send();
|
||||
break;
|
||||
default :
|
||||
exit('');
|
||||
} */
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
35
application/api/wxapi/wxmsg/ReplyText.php
Normal file
35
application/api/wxapi/wxmsg/ReplyText.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace app\api\wxapi\wxmsg;
|
||||
|
||||
use app\api\wxapi\Base;
|
||||
|
||||
/* 回复文本消息
|
||||
* */
|
||||
class ReplyText extends Base{
|
||||
public $BaseObject;
|
||||
|
||||
public function send(){
|
||||
$EventKey=$this->BaseObject->EventKey; //事件KEY值,与自定义菜单接口中KEY值对应
|
||||
$ReplyTextModel=M("wx_reply_text")->where(['EventKey'=>$EventKey,])->order("id asc")->find();
|
||||
|
||||
$data['ToUserName']=$this->BaseObject->FromUserName;
|
||||
$data['FromUserName']=$this->BaseObject->ToUserName;
|
||||
$data['CreateTime']=time();
|
||||
$data['MsgType']='text';
|
||||
|
||||
$str=$ReplyTextModel['Content'];
|
||||
//$str="<a href='http://www.baidu.com/Wap/Shop/index.html?t={time()+60}'>登录入口,点击后失效</a><br>有效期至{date('Y-m-d H:i:s',time()+60)}";
|
||||
preg_match_all("/{(.*)}/Ums",$str,$rel);
|
||||
$rs=[];
|
||||
foreach($rel[1] as $k=>$v){
|
||||
$strs = '<?php return '.$v.'; ?>';
|
||||
$rs[$k]=eval('?>'.$strs.'<?php;');
|
||||
}
|
||||
$Content=str_replace($rel[0],$rs,$str);
|
||||
$Content=htmlspecialchars_decode($Content);
|
||||
$Content=str_replace("<br>","\n",$Content);
|
||||
$data['Content']=$Content;
|
||||
echo $this->BaseObject->ArrayToXml($data);
|
||||
}
|
||||
|
||||
}
|
||||
154
application/api/wxapi/wxmsg/Subscribe.php
Normal file
154
application/api/wxapi/wxmsg/Subscribe.php
Normal file
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
namespace app\api\wxapi\wxmsg;
|
||||
|
||||
use app\api\wxapi\Base;
|
||||
use app\api\wxapi\request\UnionIDRequest;
|
||||
use app\api\wxapi\WxClient;
|
||||
use app\api\wxapi\pay\WxPayConfig;
|
||||
use app\api\wxapi\request\AccessTokenRequest;
|
||||
use think\Db;
|
||||
use app\common\library\Auth;
|
||||
|
||||
/* 扫二维码
|
||||
* */
|
||||
class Subscribe extends Base{
|
||||
|
||||
public $BaseObject;
|
||||
|
||||
protected $auth = null;
|
||||
|
||||
public function run(){
|
||||
/*查SQL*/
|
||||
$FromUserNameOpenID=$this->BaseObject->FromUserName;
|
||||
|
||||
if($this->BaseObject->Event=='subscribe'){
|
||||
$access_token_config=DB::name('config')->where(['name'=>'access_token',])->find();
|
||||
$access_token=$access_token_config['value'];
|
||||
|
||||
$WxPayConfig = new WxPayConfig();
|
||||
$WxClient =new WxClient();
|
||||
$WxClient->appID=$WxPayConfig->GetAppId();
|
||||
$WxClient->appsecret=$WxPayConfig->GetAppSecret();
|
||||
|
||||
if(time()>$access_token_config['ctime']){
|
||||
$AccessTokenRequest=new AccessTokenRequest();
|
||||
$AccessTokenRequest->setAppId($WxClient->appID);
|
||||
$AccessTokenRequest->setSecret($WxClient->appsecret);
|
||||
|
||||
$WxClient->setTypeCurl(WxClient::TYPE_CURL_GET);
|
||||
$AccessTokenRequest=$WxClient->execute($AccessTokenRequest);
|
||||
$access_token=$AccessTokenRequest->access_token;
|
||||
|
||||
DB::name('config')->where(['name'=>'access_token',])->update(['value'=>$access_token,'ctime'=>time()+7200,]);
|
||||
}
|
||||
|
||||
$UnionIDRequest=new UnionIDRequest();
|
||||
$UnionIDRequest->setAccessToken($access_token);
|
||||
$UnionIDRequest->setOpenid($FromUserNameOpenID);
|
||||
$UnionIDRequestResult=$WxClient->execute($UnionIDRequest);
|
||||
|
||||
//echo $FromUserNameOpenID;
|
||||
//print_r($UnionIDRequestResult);
|
||||
|
||||
$openid = $UnionIDRequestResult->openid;
|
||||
|
||||
$pass = '123456abc';
|
||||
$nickname = $UnionIDRequestResult->nickname;
|
||||
$sex = $UnionIDRequestResult->sex;
|
||||
$headimgurl = $UnionIDRequestResult->headimgurl;
|
||||
$wx_unionId=$UnionIDRequestResult->unionid;
|
||||
$phone = time();
|
||||
|
||||
$birthday = date('Y-m-d',time());
|
||||
|
||||
$system = 'wx'; //系统,android或ios
|
||||
$channel = 'wx'; //渠道
|
||||
|
||||
$this->auth = Auth::instance();
|
||||
|
||||
$user = DB::name('users')->field('id,wx_openid,wx_unionId,temp_wx_openid')->where('wx_unionId',$wx_unionId)->find();
|
||||
if(isset($user['id'])){
|
||||
if(!strlen($user['temp_wx_openid'])){
|
||||
$up=[];
|
||||
$up['temp_wx_openid']=$openid;
|
||||
DB::name('users')->where('id',$user['id'])->update($up);
|
||||
}
|
||||
}else{
|
||||
$ret = $this->auth->register($pass,$phone,$nickname,$sex,$headimgurl,$birthday,$system,$channel);
|
||||
if ($ret){
|
||||
$getUserinfo = $this->auth->getUserinfo();
|
||||
|
||||
$up=[];
|
||||
$tag_id=0;
|
||||
$users_tags=[];
|
||||
if($sex==1){
|
||||
$tag_id=1;
|
||||
}
|
||||
if($sex==2){
|
||||
$tag_id=3;
|
||||
}
|
||||
|
||||
if($tag_id){
|
||||
$tagsInfo=DB::name('users_tags')->field('id,tag_name,tag_color,bg_color')->where(['id'=>$tag_id,])->find();
|
||||
if(isset($tagsInfo['id'])){
|
||||
$users_tags[]=$tagsInfo;
|
||||
$users_tags=array_values($users_tags);
|
||||
}
|
||||
}
|
||||
|
||||
$up['users_tags']=json_encode($users_tags);
|
||||
|
||||
$up['wx_unionId']=$wx_unionId;
|
||||
$up['phone']=$getUserinfo['id'];
|
||||
DB::name('users')->where('id',$getUserinfo['id'])->update($up);
|
||||
|
||||
$img = $this->auth->setFilePath($headimgurl);
|
||||
$res = $this->ryHand(1,$getUserinfo['id'],$getUserinfo['id'],$img);
|
||||
|
||||
if($res['code'] == 200){
|
||||
DB::name('users')->where('id',$getUserinfo['id'])->update(['ry_uid'=>$res['userId'],'ry_token'=>$res['token'], ]);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected function getConfig($name = null)
|
||||
{
|
||||
if (!$name) {
|
||||
return '';
|
||||
}
|
||||
$val = DB::name('config')->where('name', $name)->where('status', 1)->value('value');
|
||||
return $val;
|
||||
}
|
||||
|
||||
//融云
|
||||
protected function ryHand($type,$ry_uid,$nickname='',$headimg=''){
|
||||
import('RongCloud/RongCloud', VENDOR_PATH);
|
||||
$AppKey = $this->getConfig('ry_app_key');
|
||||
$AppSecret = $this->getConfig('ry_app_secret');
|
||||
$RongSDK = new \RongCloud\RongCloud($AppKey,$AppSecret);
|
||||
$user = [
|
||||
'id'=> $ry_uid,
|
||||
'name'=> $nickname,//用户名称
|
||||
'portrait'=> $headimg //用户头像
|
||||
];
|
||||
if($type == 1){
|
||||
$res = $RongSDK->getUser()->register($user);
|
||||
|
||||
}elseif($type == 2){
|
||||
$res = $RongSDK->getUser()->register($user);
|
||||
}else{
|
||||
return ['code'=>0,'info'=>'not found type'];
|
||||
}
|
||||
|
||||
// $update = $RongSDK->getUser()->update($user);
|
||||
// $res = $RongSDK->getUser()->MuteGroups()->getList(['test1']);
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user