weixin.php
5.54 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
<?php
/**
* description...
*
* @author Tinyhu
* @package WeixinController
*/
class WeixinController extends Controller{
private $weixin;
//当前Key
private $currentKey;
//当前公众号ID
private $publicId;
//当前token
private $token;
public function init(){
}
function __call($func, $args=null)
{
$token = Filter::sql($func);
$wx_model = new Model('wx_public');
$wx_obj = $wx_model->where("token='$token'")->find();
if($wx_obj){
$this->publicId = $wx_obj['id'];
$this->token = $token;
$this->weixin = new WeChat($wx_obj['app_id'],$wx_obj['app_secret'],$wx_obj['token']);
$echostr = Req::args('echostr');
if($echostr){
$this->weixin->checkSign();
}else{
//$this->weixin->response($this->event($msg));
$msg = $this->weixin->getMessage();
if(isset($msg->fromUserName)){
$response = $this->event();
if($response!=null) $this->weixin->response($response);
exit;
}else{
Tiny::Msg($this,404);
}
}
}
}
private function event()
{
$object = $this->weixin->currentMessage();
$response = new stdclass();
$open_id = $object->fromUserName;
if($object->msgType == 'event'){
switch ($object->event)
{
case "scancode_waitmsg":
case "scancode_push":
case "pic_sysphoto":
case "pic_photo_or_album":
case "pic_weixin":
case "location_select":
case "click":
$key = $object->eventKey;
break;
default:
$key = $object->event;
break;
}
$model = new Model('wx_response');
if($key=='kf_create_session'){
$KfAccount = $object->kfAccount;
$text = new stdclass();
$text->content = "欢迎您使用人工客服系统!";
$customservice = new stdclass();
$customservice->kf_account = $KfAccount;
$response->customservice = $customservice;
$response->msgtype = 'text';
$response->touser = $open_id;
$response->text = $text;
$this->weixin->sendMessage($response);
exit;
}else if($key=='kf_close_session'){
$KfAccount = $object->kfAccount;
$text = new stdclass();
$text->content = "感谢您使用人工客服系统,再见。";
$customservice = new stdclass();
$customservice->kf_account = $KfAccount;
$response->customservice = $customservice;
$response->msgtype = 'text';
$response->touser = $open_id;
$response->text = $text;
$this->weixin->sendMessage($response);
exit;
}else if($key=='subscribe' || $key == 'unsubscribe'){
$obj = $model->where("event_key='".$this->token.'-'.$key."' or event_key='$key'")->find();
}else{
$obj = $model->where("event_key='$key'")->find();
}
if($obj){
$content = unserialize($obj['content']);
if($obj['type']=='app'){
$weixinService = new WeixinService($this->weixin);
$response = $weixinService->response($content);
$context_model = new Model('wx_context');
$context = $context_model->where('public_id ='.$this->publicId." and open_id='$open_id'")->find();
if($context){
$context_model->data(array('current_key'=>$key,'command'=>''))->where('id='.$context['id'])->update();
}else{
$context_model->data(array('current_key'=>$key,'command'=>'','public_id' => $this->publicId,'open_id'=>$open_id))->insert();
}
}else{
$response->msgType = $obj['type'];
foreach ($content as $key => $value) {
$response->$key = $value;
}
}
}else{
return null;
}
}else{
$context_model = new Model('wx_context');
$context = $context_model->where("public_id = {$this->publicId} and open_id='$open_id'")->find();
if($context){
$this->currentKey = $context['current_key'];
}else{
$this->currentKey = null;
}
if($this->currentKey !=null){
$model = new Model('wx_response');
$obj = $model->where("event_key='$this->currentKey'")->find();
if($obj){
$content = unserialize($obj['content']);
$weixinService = new WeixinService($this->weixin);
$response = $weixinService->command($content,$context);
}
}else{
$weixinService = new WeixinService($this->weixin);
$this->weixin->response($weixinService->searchGoods());
}
}
return $response;
}
}