1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: 18: 19:
20: class WxRobot_Table_Records{
21:
22: 23: 24:
25: public $table_prefix = 'midoks_';
26:
27: 28: 29:
30: public static $_instance = null;
31:
32:
33: 34: 35: 36: 37:
38: public static function instance(){
39: if( is_null (self::$_instance)){
40: self::$_instance = new self();
41: }
42: return self::$_instance;
43: }
44:
45: 46: 47: 48: 49:
50: private function get_table_name(){
51: return $this->table_prefix.'weixin_robot';
52: }
53:
54: 55: 56: 57: 58:
59: private function get_table_name_menu(){
60: return $this->table_prefix.'weixin_robot_menu';
61: }
62:
63: 64: 65: 66: 67:
68: public function insert($from, $to, $msgid, $msgtype, $createtime, $content, $picurl, $location_x, $location_y,
69: $scale, $label, $title, $description, $url, $event,$eventkey,$format, $recognition, $mediaid,$thumbmediaid, $response, $response_time){
70:
71: global $wpdb;
72: $table_name = $this->get_table_name();
73:
74: $sql = "INSERT INTO `{$table_name}` (`id`, `from`, `to`, `msgid`, `msgtype`, `createtime`, `content`, `picurl`, `location_x`, `location_y`, `scale`, `label`, `title`, `description`, `url`, `event`, `eventkey`, `format`,`recognition`,`mediaid`, `thumbmediaid`, `response`, `response_time`) VALUES(null,'{$from}','{$to}','{$msgid}', '{$msgtype}','{$createtime}', '{$content}','{$picurl}','{$location_x}', '{$location_y}','{$scale}', '{$label}', '{$title}','{$description}', '{$url}', '{$event}','{$eventkey}','{$format}', '{$recognition}', '{$mediaid}','{$thumbmediaid}', '{$response}', '{$response_time}')";
75: return $wpdb->query($sql);
76: }
77:
78: 79: 80:
81: public function weixin_get_msgtype_count($text = 'text'){
82: global $wpdb;
83: $table_name = $this->get_table_name();
84: $sql = 'select count(`id`) as count from `'.$table_name."` where `msgtype`='{$text}'";
85: $result = $wpdb->get_results($sql);
86: return $result[0]->count;
87: }
88:
89: 90: 91: 92: 93:
94: public function weixin_get_count(){
95: global $wpdb;
96: $table_name = $this->get_table_name();
97: $sql = "select count(id) as count from `{$table_name}`";
98: $data = $wpdb->get_results($sql);
99: return $data[0]->count;
100: }
101:
102: 103: 104: 105: 106: 107: 108:
109: public function weixin_get_data($page_no = 1, $num = 20){
110: global $wpdb;
111: $table_name = $this->get_table_name();
112:
113: if($page_no < 1){
114: $page_no = 1;
115: }
116: $start = ($page_no-1)*$num;
117: $sql = "select `id`,`from`,`to`,`msgtype`,`createtime`,`content`,`picurl`,`location_x`,`location_y`, `scale`, `label`, `title`,"
118: ."`description`,`url`,`event`, `eventkey`,`format`,`recognition`,`mediaid`,`thumbmediaid`,`response`, `response_time`"
119: ." from `{$table_name}` order by `id` desc limit {$start},{$num}";
120: $data = $wpdb->get_results($sql);
121: $newData = array();
122: foreach($data as $k=>$v){
123: $arr = array();
124: $arr['id'] = $v->id;
125: $arr['from'] = $v->from;
126: $arr['to'] = $v->to;
127: $arr['msgtype'] = $v->msgtype;
128:
129:
130: switch($v->msgtype){
131: case 'text':$arr['content'] = $v->content;break;
132: default:$arr['content'] = $v->content;
133: }
134:
135:
136: if('CLICK' == $v->event){
137: $data = $this->select_menu_key($v->eventkey);
138: if($data){
139: $arr['content'] = '菜单:'.$data;
140: }else{
141: $arr['content'] = '菜单:已经不存在';
142: }
143: }else if('subscribe' == $v->event){
144: $arr['content'] = '订阅事件';
145: }else if('unsubscribe' == $v->event){
146: $arr['content'] = '取消订阅事件';
147: }else if('LOCATION' == $v->event){
148: $arr['content'] = '地理位置上报告事件';
149: }else if('location' == $v->msgtype){
150: $arr['content'] = '地理位置上报告事件';
151: }else if('voice' == $v->msgtype){
152: $arr['content'] = '语音事件';
153: }
154:
155: $arr['createtime'] = date('Y-m-d H:i:s', $v->createtime);
156: $arr['response'] = $v->response;
157: $arr['response_time'] = $v->response_time;
158: $newData[] = $arr;
159: }
160: return $newData;
161: }
162:
163: 164: 165:
166: public function select_menu_key($key){
167:
168: global $wpdb;
169: $table_name = $this->get_table_name_menu();
170:
171: $sql = "select `id`,`menu_name`, `menu_type`, `menu_key`, `menu_callback`, `pid`"
172: ." from `{$table_name}` where `menu_key`='{$key}' limit 1";
173: $data = $wpdb->get_results($sql);
174: if(empty($data)){
175: return false;
176: }else{
177: return $data[0]->menu_name;
178: }
179: return false;
180: }
181:
182:
183:
184:
185:
186: }
187: ?>
188: