Wechat.php
5.9 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
<?php
namespace addons\wechat\library;
use app\common\model\Page;
use app\common\model\WechatConfig;
use EasyWeChat\Message\News;
use EasyWeChat\Message\Transfer;
/**
* Wechat服务类
*/
class Wechat
{
public function __construct()
{
}
public static function appConfig()
{
return array(
'signin' => array(
'name' => '签到送积分',
'config' => array()
),
'blog' => array(
'name' => '关联博客',
'config' => array(
array(
'type' => 'text',
'caption' => '日志ID',
'field' => 'id',
'options' => ''
)
)
),
'article' => array(
'name' => '关联文章',
'config' => array(
array(
'type' => 'text',
'caption' => '文章ID',
'field' => 'id',
'options' => ''
)
)
),
'page' => array(
'name' => '关联单页',
'config' => array(
array(
'type' => 'text',
'caption' => '单页ID',
'field' => 'id',
'options' => ''
)
)
),
'service' => array(
'name' => '在线客服',
'config' => array()
),
);
}
// 微信输入交互内容指令
public function command($obj, $openid, $content, $context)
{
$response = FALSE;
if (isset($content['app'])) {
switch ($content['app']) {
case 'signin':
case 'blog':
case 'article':
case 'page':
break;
case 'service':
$service = (array)json_decode(WechatConfig::value('service'), true);
list($begintime, $endtime) = explode('-', $service['onlinetime']);
$session = $obj->app->staff_session;
$staff = $obj->app->staff;
$kf_account = $session->get($openid)->kf_account;
$time = time();
if (!$kf_account && ($time < strtotime(date("Y-m-d {$begintime}")) || $time > strtotime(date("Y-m-d {$endtime}")))) {
return $service['offlinemsg'];
}
if (!$kf_account) {
$kf_list = $staff->onlines()->kf_online_list;
if ($kf_list) {
$kfarr = [];
foreach ($kf_list as $k => $v) {
$kfarr[$v['kf_account']] = $v['accepted_case'];
}
$kfkeys = array_keys($kfarr, min($kfarr));
$kf_account = reset($kfkeys);
$session->create($kf_account, $openid);
$response = $service['waitformsg'];
} else {
$response = $service['nosessionmsg'];
}
} else {
$server = $obj->app->server;
$server->setMessageHandler(function ($message) {
return new Transfer();
});
$response = $server->serve();
$response->send();
exit;
}
break;
default:
break;
}
} else {
$response = isset($content['content']) ? $content['content'] : $response;
}
return $response;
}
// 微信点击菜单event指令
public function response($obj, $openid, $content, $context)
{
$response = FALSE;
if (isset($content['app'])) {
switch ($content['app']) {
case 'signin':
break;
case 'blog':
$id = explode(',', $content['id']);
$bloglist = addons\blog\model\Post::all($id);
$response = [];
foreach ($bloglist as $k => $blog) {
if ($blog) {
$news = new News();
$news->title = $blog['title'];
$news->url = addon_url('blog/index/post', ['id' => $blog['id']], true, true);
$news->image = cdnurl($blog['thumb']);
$news->description = $blog['description'];
$response[] = $news;
}
}
case 'page':
$id = isset($content['id']) ? $content['id'] : 0;
$pageinfo = Page::get($id);
if ($pageinfo) {
$news = new News();
$news->title = $pageinfo['title'];
$news->url = $pageinfo['url'] ? $pageinfo['url'] : url('index/page/show', ['id' => $pageinfo['id']], true, true);
$news->image = cdnurl($pageinfo['image']);
$news->description = $pageinfo['description'];
return $news;
}
break;
case 'service':
$response = $this->command($obj, $openid, $content, $context);
break;
default:
break;
}
} else {
$response = isset($content['content']) ? $content['content'] : $response;
}
return $response;
}
}