Wechat.php
7.3 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
<?php
namespace addons\shopro\controller;
use addons\shopro\library\Wechat as WechatLibrary;
use addons\shopro\model\Wechat as WechatModel;
use addons\shopro\model\Config;
/**
* 微信接口
*/
class Wechat extends Base
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
protected $app = null;
protected $userOpenId = '';
/**
* 微信公众号服务端API对接、处理消息回复
*/
public function index()
{
$wechat = new WechatLibrary('wxOfficialAccount');
$this->app = $wechat->getApp();
$this->app->server->push(function ($message) {
//初始化信息
$this->userOpenId = $message['FromUserName'];
// return json_encode($message, JSON_UNESCAPED_UNICODE); //调试使用
switch ($message['MsgType']) {
case 'event': //收到事件消息
switch ($message['Event']) {
case 'subscribe': //订阅(关注)事件
//获取粉丝信息并保存
$subscribe = WechatModel::get(['type' => 'subscribe']);
if ($subscribe) {
return $this->response($subscribe);
}
break;
case 'unsubscribe': //取消订阅(关注)事件
//获取粉丝信息并保存
break;
case 'CLICK': //自定义菜单事件
return $this->response($message, 'CLICK');
break;
case 'SCAN': //扫码事件
return '';
break;
}
break;
case 'text': //收到文本消息
//检测关键字回复
$content = $message['Content'];
$auto_reply = WechatModel::where('type', 'auto_reply')->where('find_in_set(:keywords,rules)', ['keywords' => $content])->find();
if ($auto_reply) {
return $this->response($auto_reply);
}
case 'image': //收到图片消息
case 'voice': //收到语音消息
case 'video': //收到视频消息
case 'location': //收到坐标消息
case 'link': //收到链接消息
case 'file': //收到文件消息
default: // ... 默认回复消息
$default_reply = WechatModel::where('type', 'default_reply')->find();
if ($default_reply) {
return $this->response($default_reply);
}
}
});
$response = $this->app->server->serve();
// 将响应输出
$response->send();
}
public function jssdk()
{
$params = $this->request->post();
$apis = [
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'getLocation', //获取位置
'openLocation', //打开位置
'scanQRCode', //扫一扫接口
'chooseWXPay', //微信支付
'chooseImage', //拍照或从手机相册中选图接口
'previewImage', //预览图片接口
'uploadImage', //上传图片
'openAddress', // 获取微信地址
];
$uri = urldecode($params['uri']);
$wechat = new WechatLibrary('wxOfficialAccount');
$res = $wechat->getApp()->jssdk->setUrl($uri)->buildConfig($apis, $debug = false, $beta = false, $json = false);
$this->success('sdk', $res);
}
/**
* 微信公众号服务端API对接
*/
public function wxacode()
{
$scene = $this->request->get('scene', '');
$path = $this->request->get('path', '');
if (empty($path)) {
$path = 'pages/index/index';
}
$wechat = new WechatLibrary('wxMiniProgram');
$content = $wechat->getApp()->app_code->getUnlimit($scene, [
'page' => $path,
'is_hyaline' => true,
]);
if ($content instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
return response($content->getBody(), 200, ['Content-Length' => strlen($content)])->contentType('image/png');
} else {
// 小程序码获取失败
$msg = isset($content['errcode']) ? $content['errcode'] : '-';
$msg .= isset($content['errmsg']) ? $content['errmsg'] : '';
\think\Log::write('wxacode-error' . $msg);
$this->error('获取失败', $msg);
}
}
/**
* 回复消息
*/
private function response($replyInfo, $event = 'text')
{
switch ($event) {
case 'SCAN': //解析扫码事件EventKey
break;
case 'CLICK': //解析菜单点击事件EventKey
$key = explode('|', $replyInfo['EventKey']);
if ($key) {
$message['type'] = $key[0];
if ($key[0] === 'text') {
$message['content'] = json_decode(WechatModel::get($key[1])->content, true);
} elseif($key[0] === 'link') {
$link = WechatModel::get($key[1]);
$message = array_merge($message, json_decode($link->content, true));
$message['title'] = $link->name;
// return json_encode($message);
}else {
$message['media_id'] = $key[1];
}
}
break;
default:
$message = json_decode($replyInfo['content'], true);
break;
}
switch ($message['type']) {
case 'text': //回复文本
$content = new \EasyWeChat\Kernel\Messages\Text($message['content']);
break;
case 'image': //回复图片
$content = new \EasyWeChat\Kernel\Messages\Image($message['media_id']);
break;
case 'news': //回复图文
$message = new \EasyWeChat\Kernel\Messages\Media($message['media_id'], 'mpnews');
$this->app->customer_service->message($message)->to($this->userOpenId)->send(); //素材消息使用客服接口回复
break;
case 'voice': //回复语音
$content = new \EasyWeChat\Kernel\Messages\Voice($message['media_id']);
break;
case 'video': //回复视频
$content = new \EasyWeChat\Kernel\Messages\Video($message['media_id']);
break;
case 'link': //回复链接
$items = new \EasyWeChat\Kernel\Messages\NewsItem([
'title' => $message['title'],
'description' => $message['description'],
'url' => $message['url'],
'image' => cdnurl($message['image'], true),
// ...
]);
$content = new \EasyWeChat\Kernel\Messages\News([$items]);
break;
}
return $content;
}
}