Overview

Packages

  • None
  • WxRobot
    • Admin
    • Cmd
    • CoreFunctions
    • Exends
    • Install
    • Table
    • Uninstall
    • WxSDK

Classes

  • ErrorCode
  • PKCS7Encoder
  • Prpcrypt
  • SHA1
  • Weixin_BaseCore
  • WeiXin_SDK
  • Weixin_Template
  • WXBizMsgCrypt
  • WxRobot_Admin
  • WxRobot_Admin_Menu_Extends
  • WxRobot_Admin_Menu_Instro
  • WxRobot_Admin_Menu_Menu
  • WxRobot_Admin_Menu_Records
  • WxRobot_Admin_Menu_Reply
  • WxRobot_Admin_Menu_Setting
  • WxRobot_Admin_Menu_Statistics
  • WxRobot_Cmd
  • WxRobot_Cmd_Event
  • WxRobot_Cmd_Event_User
  • WxRobot_Cmd_Text
  • WxRobot_Extends
  • WxRobot_Install
  • WxRobot_Robot
  • WxRobot_SDK
  • WxRobot_Table_Extends
  • WxRobot_Table_Menu
  • WxRobot_Table_Records
  • WxRobot_Table_Reply
  • WxRobot_Uninstall
  • WxRobot_Wp
  • XMLParse

Functions

  • wx_admin_log
  • wx_is_xml
  • wx_notice_msg
  • wx_parse_xml
  • wx_random_big_pic
  • wx_random_small_pic
  • wx_request_array
  • wx_request_decode
  • wx_request_is_encode
  • wx_request_xml
  • wx_send_encode
  • Overview
  • Package
  • Class
  1: <?php
  2: /**
  3:  *  WxRobot 命令响应类
  4:  *  
  5:  *  @author     midoks
  6:  *  @category   WxRobot
  7:  *  @package    WxRobot/Cmd
  8:  *  @since      5.3.0
  9:  */
 10: 
 11: /**
 12:  *  WxRobot_Cmd_Text 文本命令响应类
 13:  */
 14: class WxRobot_Cmd_Text{
 15: 
 16:     /**
 17:      * WxRobot_Cmd_Text Instance
 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:      * @return void
 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:      * WxRobot 文本消息处理类实例化
 41:      * 
 42:      * @return WxRobot_Cmd_Text instance
 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:      * @param object $obj 模板引用对象
 55:      * @param string $kw 关键字
 56:      * @return xml
 57:      */
 58:     public function setValue($obj, $kw){
 59:         $this->obj = $obj;
 60:         $this->kw = $this->convert($kw);
 61:     }
 62: 
 63:     /**
 64:      * 文本结果返回
 65:      *
 66:      * @return xml
 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:      * 多个关键字提取,(建议关键字不要重叠,自己思量,ID降序)
 92:      *
 93:      * @param string $kw 请求的文本
 94:      * @param string $keyword 自己设定的关键子
 95:      * @return bool
 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:      * @return bool
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:      * @param $kw 关键字
123:      * @return $mixed
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:                             //title
153:                             if(empty($mlist[0])){
154:                                 $title = $kw;
155:                             }else{
156:                                 $title = $mlist[0];
157:                             }
158:                             //desc
159:                             if(empty($mlist[1])){
160:                                 $desc = date('Y-m-d H:i:s');
161:                             }else{
162:                                 $desc = $mlist[1];
163:                             }
164:                             //relpy
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:      * WordPress 相关命令数据
187:      *
188:      * @return array
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:      * @param   string  $str 待转换的字符串
224:      * @param   int     $type TODBC:转换为半角;TOSBC,转换为全角
225:      * @return  string
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: 
API documentation generated by ApiGen