1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13:
14: class WxRobot_Cmd_Text{
15:
16: 17: 18:
19: public static $_instance = null;
20:
21: 22: 23:
24: public $list_cmd = array('today','n', 'h', 'r', '?');
25:
26:
27: 28: 29: 30: 31:
32: public function __construct(){
33: $this->wxSdk = WxRobot_SDK::instance();
34:
35: include_once(WEIXIN_ROBOT.'includes/class-wx-wp.php');
36: $this->wp = WxRobot_Wp::instance();
37: }
38:
39: 40: 41: 42: 43:
44: public static function instance(){
45: if( is_null (self::$_instance)){
46: self::$_instance = new self();
47: }
48: return self::$_instance;
49: }
50:
51: 52: 53: 54: 55: 56: 57:
58: public function setValue($obj, $kw){
59: $this->obj = $obj;
60: $this->kw = $this->convert($kw);
61: }
62:
63: 64: 65: 66: 67:
68: public function replay(){
69: $kw = $this->kw;
70:
71: if($kw == '?'){
72: return $this->wxSdk->toMsgText($this->obj->options['weixin_robot_helper']);
73: }
74:
75: if($wp_cmd = $this->wordpress_cmd($kw)){
76: return $wp_cmd;
77: }
78:
79: if($user_cmd = $this->user_keyword_cmd($kw)){
80: return $user_cmd;
81: }
82:
83: if($wx_extends = $this->obj->extends->dealwith('text', $kw)){
84: return $wx_extends;
85: }
86:
87: return $this->wxSdk->toMsgText($this->obj->options['weixin_robot_helper']);
88: }
89:
90: 91: 92: 93: 94: 95: 96:
97: private function user_keyword_cmd_multi($kw, $keyword){
98: $list1 = explode(',', $keyword);
99: foreach($list1 as $v){
100: if($kw==$v) return true;
101: }
102: if($kw==$keyword) return true;
103: return false;
104: }
105:
106: 107: 108: 109: 110:
111: private function internal_cmd($reply){
112: if(in_array($reply, $this->list_cmd) || in_array(strtolower(substr($reply, 0, 1)), $this->list_cmd)){
113: return true;
114: }else{
115: return false;
116: }
117: }
118:
119: 120: 121: 122: 123: 124:
125: public function user_keyword_cmd($kw){
126: $data = WxRobot_Table_Reply::instance()->weixin_get_relpy_data($kw);
127:
128: if(!empty($data)){
129: $arr = array();
130: foreach($data as $k=>$v){
131: if($this->user_keyword_cmd_multi($kw, $v['keyword']) && 'text' == $v['type']){
132: if($this->internal_cmd($v['relpy'])){
133: return $this->wordpress_cmd($v['relpy']);
134: }else{
135: return $this->wxSdk->toMsgText($v['relpy']);
136: }
137: }else if($this->user_keyword_cmd_multi($kw, $v['keyword']) && 'id' == $v['type']){
138: if($this->internal_cmd($v['relpy'])){
139: return $this->wordpress_cmd($v['relpy']);
140: }else{
141: if(count($idsc = explode(',', $v['relpy']))>1){
142: $data = $this->wp->Qids($idsc);
143: }else{
144: $data = $this->wp->Qid(trim($v['relpy'],','));
145: }
146: return $data;
147: }
148: }else if( $this->user_keyword_cmd_multi($kw, $v['keyword']) && 'music' == $v['type']){
149: if(!empty($v['relpy'])){
150: $mlist = explode('|', $v['relpy']);
151: if('3'==count($mlist)){
152:
153: if(empty($mlist[0])){
154: $title = $kw;
155: }else{
156: $title = $mlist[0];
157: }
158:
159: if(empty($mlist[1])){
160: $desc = date('Y-m-d H:i:s');
161: }else{
162: $desc = $mlist[1];
163: }
164:
165: if(empty($mlist[2])){
166: return false;
167: }else{
168: $relpy = $mlist[2];
169: }
170:
171: return $this->wxSdk->toMsgMusic($title, $desc, $relpy, $relpy);
172: }
173: return false;
174:
175: }else{
176: return false;
177: }
178: }
179: }
180: return $arr;
181: }
182: return false;
183: }
184:
185: 186: 187: 188: 189:
190: public function wordpress_cmd($kw){
191: $prefix = substr($kw, 0, 1);
192: $suffix = substr($kw, 1);
193: $result = '';
194:
195: $int = intval($suffix);
196: if($int<1){
197: $int = 5;
198: }else if($int >10){
199: $int = 10;
200: }
201:
202:
203: $prefix = strtolower($prefix);
204: switch($prefix){
205: case 'n': $result = $this->wp->news($suffix);break;
206: case 'h': $result = $this->wp->hot($suffix);break;
207: case 'r': $result = $this->wp->rand($suffix);break;
208: default: break;
209: }
210:
211: if(!empty($result)){
212: return $result;
213: }
214: switch($kw){
215: case 'today': $result = $this->wp->today();break;
216: }
217: return $result;
218: }
219:
220: 221: 222: 223: 224: 225: 226:
227: public function convert($str, $type = 'TOSBC'){
228: $dbc = array('!','?','。','@','#','$');
229: $sbc = array('!', '?', '.', '@', '#', '$');
230: if($type == 'TODBC'){
231: return str_replace($sbc, $dbc, $str);
232: }elseif($type == 'TOSBC'){
233: return str_replace($dbc, $sbc, $str);
234: }else{
235: return false;
236: }
237: }
238: }
239: ?>
240: