Wchat.php
7.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
* Wchat.php
* Niushop商城系统
*/
namespace app\wchat\controller;
use think\Controller;
use clt\WchatOauth;
class Wchat extends Controller
{
public $wchat;
public $weixin_service;
public $author_appid;
public $instance_id;
public $style;
public $token;
public function initialize()
{
parent::initialize();
$this->wchat = new WchatOauth(); // 微信公众号相关类
$this->instance_id = 0;
$value = db('wx_config')->where([ 'key' => 'SHOPWCHAT'])->value('value');
$value = json_decode($value,true);
$this->token = $value['token'];
define("TOKEN", $this->token);
$this->getMessage();
}
/**
* ************************************************************************微信公众号消息相关方法 开始******************************************************
*/
/**
* 关联公众号微信
*/
public function relateWeixin()
{
$sign = input('signature', '');
if (defined("TOKEN") && isset($sign)) {
$signature = $sign;
$timestamp = input('timestamp');
$nonce = input('nonce');
$token = TOKEN;
$tmpArr = array(
$token,
$timestamp,
$nonce
);
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if ($tmpStr == $signature) {
$echostr = input('echostr', '');
if (!empty($echostr)) {
echo $echostr;
}
return 1;
} else {
return 0;
}
}
}
public function templatemessage()
{
$media_id = input('media_id',0);
$info = $this->getWeixinMediaDetailByMediaId($media_id);
if (! empty($info["media_parent"])) {
$this->assign("info", $info);
return view();
} else {
echo "图文消息没有查询到";
}
}
public function getWeixinMediaDetailByMediaId($media_id){
$weixin_media_item =db('wx_media_item');
$item_list = $weixin_media_item->where(['id' => $media_id])->find();
if (!empty($item_list)) {
// 主表
$weixin_media = db('wx_media');
$weixin_media_info["media_parent"] = $weixin_media->where(["media_id" => $item_list["media_id"] ])->find();
// 微信配置
$weixin_auth = db('wx_auth');
$weixin_media_info["weixin_auth"] = $weixin_auth->where(["instance_id" => $weixin_media_info["media_parent"]["instance_id"]])->find();
$weixin_media_info["media_item"] = $item_list;
// 更新阅读次数
$res = $weixin_media_item->where(["id" => $media_id])->update(["hits" => ($item_list["hits"] + 1)]);
return $weixin_media_info;
}
return null;
}
/**
* 微信开放平台模式(需要对消息进行加密和解密)
* 微信获取消息以及返回接口
*/
public function getMessage()
{
$from_xml = file_get_contents('php://input');
if (empty($from_xml)) {
return;
}
$signature = input('msg_signature', '');
$signature = input('timestamp', '');
$nonce = input('nonce', '');
$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];
$ticket_xml = $from_xml;
$postObj = simplexml_load_string($ticket_xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->instance_id = 0;
if (!empty($postObj->MsgType)) {
switch ($postObj->MsgType) {
case "text":
//用户发的消息 存入表中
//$this->addUserMessage((string)$postObj->FromUserName, (string) $postObj->Content, (string) $postObj->MsgType);
$resultStr = $this->MsgTypeText($postObj);
break;
case "event":
$resultStr = $this->MsgTypeEvent($postObj);
break;
default:
$resultStr = "";
break;
}
}
if (!empty($resultStr)) {
echo $resultStr;
} else {
echo '';
}
}
/**
* 文本消息回复格式
*
* @param unknown $postObj
* @return Ambigous <void, string>
*/
private function MsgTypeText($postObj)
{
$funcFlag = 0; // 星标
$wchat_replay = $this->wchat->getWhatReplay($this->instance_id, (string)$postObj->Content);
// 判断用户输入text
if (!empty($wchat_replay)) { // 关键词匹配回复
$contentStr = $wchat_replay; // 构造media数据并返回
} elseif ($postObj->Content == "uu") {
$contentStr = "shopId:" . $this->instance_id;
} elseif ($postObj->Content == "TESTCOMPONENT_MSG_TYPE_TEXT") {
$contentStr = "TESTCOMPONENT_MSG_TYPE_TEXT_callback"; // 微店插件功能 关键词,预留口
} elseif (strpos($postObj->Content, "QUERY_AUTH_CODE") !== false) {
$get_str = str_replace("QUERY_AUTH_CODE:", "", $postObj->Content);
$contentStr = $get_str . "_from_api"; // 微店插件功能 关键词,预留口
} else {
$content = $this->wchat->getDefaultReplay($this->instance_id);
if (!empty($content)) {
$contentStr = $content;
} else {
$contentStr = '欢迎!';
}
}
if (is_array($contentStr)) {
$resultStr = $this->wchat->event_key_news($postObj, $contentStr);
} elseif (!empty($contentStr)) {
$resultStr = $this->wchat->event_key_text($postObj, $contentStr);
} else {
$resultStr = '';
}
return $resultStr;
}
/**
* 事件消息回复机制
*/
// 事件自动回复 MsgType = Event
private function MsgTypeEvent($postObj)
{
$contentStr = "";
switch ($postObj->Event) {
case "subscribe": // 关注公众号 添加关注回复
$content = $this->wchat->getSubscribeReplay($this->instance_id);
if (!empty($content)) {
$contentStr = $content;
}
// 构造media数据并返回
break;
case "unsubscribe": // 取消关注公众号
break;
case "VIEW": // VIEW事件 - 点击菜单跳转链接时的事件推送
// $this->wchat->weichat_menu_hits_view($postObj->EventKey); //菜单计数
$contentStr = "";
break;
case "SCAN": // SCAN事件 - 用户已关注时的事件推送
$contentStr = "";
break;
case "CLICK": // CLICK事件 - 自定义菜单事件
$menu_detail = $this->wchat->getWeixinMenuDetail($postObj->EventKey);
$media_info = $this->wchat->getWeixinMediaDetail($menu_detail['media_id']);
$contentStr = $this->wchat->getMediaWchatStruct($media_info); // 构造media数据并返回
break;
default:
break;
}
// $contentStr = $postObj->Event."from_callback";//测试接口正式部署之后注释不要删除
if (is_array($contentStr)) {
$resultStr = $this->wchat->event_key_news($postObj, $contentStr);
} else {
$resultStr = $this->wchat->event_key_text($postObj, $contentStr);
}
return $resultStr;
}
}