1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13:
14: class WxRobot_Cmd{
15:
16: 17: 18:
19: public static $_instance = null;
20:
21: 22: 23: 24: 25:
26: public function __construct(){
27:
28: include_once('weixin-sdk-api/weixin_crypt/wxBizMsgCrypt.php');
29:
30: $this->options = get_option(WEIXIN_ROBOT_OPTIONS);
31: $this->extends = WxRobot_Extends::instance();
32: $this->info = wx_request_array();
33: }
34:
35: 36: 37: 38: 39:
40: public static function instance(){
41: if( is_null (self::$_instance)){
42: self::$_instance = new self();
43: }
44: return self::$_instance;
45: }
46:
47: 48: 49: 50: 51:
52: public function cmd(){
53:
54:
55: if($wp_plugins = $this->extends->dealwith('all', $this->info)){
56: $this->result = $wp_plugins;
57: }else{
58: $this->result = $this->cmd_choose();
59: }
60:
61:
62: if($this->options['weixin_robot_record']){
63: $this->weixin_robot_insert();
64: }
65:
66:
67: if(!empty($this->options['EncodingAESKey']) && !empty($_GET['encrypt_type'])){
68: $this->result = wx_send_encode($this->result);
69: }
70:
71: return $this->result;
72: }
73:
74: 75: 76: 77: 78:
79: public function weixin_robot_insert(){
80: $info = $this->info;
81:
82: $from = $info['FromUserName'];
83: $to = $info['ToUserName'];
84: $msgid = isset($info['MsgId']) ? $info['MsgId']: '';
85: $msgtype = $info['MsgType'];
86: $createtime = $info['CreateTime'];
87:
88:
89: $content = isset($info['Content']) ? $info['Content']: '';
90:
91:
92: $picurl = isset($info['PicUrl']) ? $info['PicUrl']: '';
93:
94:
95: $location_x = isset($info['Location_X']) ? $info['Location_X']: '0.00';
96: $location_y = isset($info['Location_Y']) ? $info['Location_Y']: '0.00';
97: $scale = isset($info['Scale']) ? $info['Scale']: '0.00';
98: $label = isset($info['Label']) ? $info['Label']: '';
99:
100:
101: $title= isset($info['Title']) ? $info['Title']: '';
102: $description = isset($info['Description']) ? $info['Description']: '';
103: $url = isset($info['Url']) ? $info['Url']: '';
104:
105:
106: $event = isset($info['Event']) ? $info['Event']: '';
107:
108: if(!empty($event)){
109:
110: if('TEMPLATESENDJOBFINISH'==$info['Event']){
111: $content = '模版发送返回消息'.$info['Status'];
112: }else if('MASSSENDJOBFINISH' == $info['Event']){
113: $content = '事件推送群发结果:'.$info['Status'].
114: '成功发送粉丝:'.$info['SentCount'].',失败发送粉丝:'.$info['ErrorCount'];
115: }
116: }
117:
118: $eventkey = isset($info['EventKey']) ? $info['EventKey']: '';
119:
120:
121: $format = isset($info['Format']) ? $info['Format']: '';
122: $recognition = isset($info['Recognition']) ? $info['Recognition']: '';
123:
124:
125: $mediaid = isset($info['MediaId']) ? $info['MediaId']: '';
126: $thumbmediaid = isset($info['ThumbMediaId']) ? $info['ThumbMediaId']: '';
127:
128:
129: $response = $this->ret_reply_type();
130:
131:
132: $response_time = timer_stop(0);
133: $result = WxRobot_Table_Records::instance()->insert($from, $to, $msgid, $msgtype, $createtime, $content,
134: $picurl, $location_x, $location_y,$scale, $label, $title, $description,
135: $url, $event,$eventkey,$format, $recognition, $mediaid, $thumbmediaid, $response, $response_time);
136:
137: return $result;
138: }
139:
140: 141: 142: 143: 144:
145: public function ret_reply_type(){
146: $result = wx_parse_xml($this->result);
147: if(isset($result['MsgType']) && !empty($result['MsgType']) ){
148: switch($result['MsgType']){
149: case 'text' : return '文本回复';
150: case 'voice' : return '声音回复';
151: case 'music' : return '音乐回复';
152: case 'video' : return '视频回复';
153: case 'link' : return '声音回复';
154: case 'event' : return '事件回复';
155: case 'news' : return '图文回复';
156: case 'image' : return '图片回复';
157: case 'location' : return '地址回复';
158: }
159: }
160: return '无回复';
161: }
162:
163:
164: 165: 166:
167: public function cmd_choose(){
168: switch($this->info['MsgType']){
169:
170: case 'text':return $this->textReply();break;
171:
172: case 'image':return $this->imageReply();break;
173:
174: case 'voice':return $this->voiceReply();break;
175:
176: case 'video':return $this->videoReply();break;
177:
178: case 'event':return $this->eventReply();break;
179:
180: case 'location':return $this->locationReply();break;
181:
182: case 'link':return $this->linkReply;break;
183:
184: default:return $this->textReply();break;
185: }
186: }
187:
188:
189: public function textReply(){
190: $kw = $this->info['Content'];
191: include_once('weixin-robot/class-wx-cmd-text.php');
192:
193: WxRobot_Cmd_Text::instance()->setValue($this, $kw);
194: return WxRobot_Cmd_Text::instance()->replay();
195: }
196:
197:
198: public function imageReply(){
199:
200: $info['PicUrl'] = $this->info['PicUrl'];
201: $info['MediaId'] = $this->info['MediaId'];
202:
203:
204: if($wp_plugins = $this->extends->dealwith('image', $info)){
205: return $wp_plugins;
206: }
207: }
208:
209:
210: public function voiceReply(){
211:
212: $info['MediaId'] = $this->info['MediaId'];
213: $info['Format'] = $this->info['Format'];
214: $info['Recognition'] = $this->info['Recognition'];
215:
216:
217: if($wp_plugins = $this->extends->dealwith('voice', $info)){
218: return $wp_plugins;
219: }
220: }
221:
222:
223: public function videoReply(){
224: $info['MediaId'] = $this->info['MediaId'];
225: $info['ThumbMediaId'] = $this->info['ThumbMediaId'];
226:
227:
228: if($wp_plugins = $this->extends->dealwith('video', $info)){
229: return $wp_plugins;
230: }
231: }
232:
233:
234:
235: public function eventReply(){
236: $type = $this->info['Event'];
237: if($type == 'CLICK'){
238: include_once('weixin-robot/class-wx-event-user.php');
239: $key = $this->info['EventKey'];
240: if(!empty($key)){
241: $weixin_robot_event_user = WxRobot_Cmd_Event_User::instance();
242: $weixin_robot_event_user->setValue($this);
243: return $weixin_robot_event_user->go($key);
244: }
245: }else{
246: include_once('weixin-robot/class-wx-event.php');
247: $weixin_robot_event = WxRobot_Cmd_Event::instance();
248: $type = $type.'Event';
249: return $weixin_robot_event->$type();
250: }
251: }
252:
253:
254: public function locationReply(){
255:
256: $info['Location_X'] = $this->info['Location_X'];
257: $info['Location_Y'] = $this->info['Location_Y'];
258: $info['Scale'] = $this->info['Scale'];
259: $info['Label'] = $this->info['Label'];
260:
261:
262: if($wp_plugins = $this->extends->dealwith('location', $info)){
263: return $wp_plugins;
264: }
265: }
266:
267:
268: public function linkReply(){
269:
270: $info['Title'] = $this->info['Title'];
271: $info['Description'] = $this->info['Description'];
272: $info['Url'] = $this->info['Url'];
273:
274:
275: if($wp_plugins = $this->extends->dealwith('link', $info)){
276: return $wp_plugins;
277: }
278: }
279:
280: 281: 282: 283: 284: 285:
286: public function help_info($info = ''){
287: if($this->options['weixin_robot_helper_is'] != 'true'){
288: $text = $this->options['weixin_robot_helper'];
289: if(!empty($info)){
290: return $this->toMsgText($info."\n".$text);
291: }else{
292: return $this->toMsgText($text);
293: }
294: }
295: }
296:
297: }
298: ?>
299: