# 1.模塊目錄

# 2.消息處理流程
Api.php $engine = new WeEngine();$engine->start();
$message = $this->account->parse($postStr);
WeUtility::logging('trace', $message);
輸出結果:
2016-06-07 14:27:25 trace :
------------
Array:
from : fromUser ;
to : toUser ;
time : 1465280845 ;
type : event ;
event : subscribe ;
tousername : toUser ;
fromusername : fromUser ;
createtime : 1465280845 ;
msgtype : event ;
eventkey : ;
msgid : 1234567890123456 ;
$pars = $this->analyze($message); private function analyze(&$message)
1)in_array($message['type'], array('event', 'qr')(事件或者掃碼消息)call_user_func_array(array($this, 'analyze' . $message['type']), array(&$message));
2)method_exists($this, 'analyze' . $message['type']) (默認調取analyze+消息類型的方法)
$response = $this->process($par); private function process($param)
$processor = WeUtility::createModuleProcessor($param['module']);
(account.class.php里的createModuleProcessor()根據module名稱查詢)
public static function createModuleProcessor($name)
//拼裝模塊處理類名
$classname = "{$name}ModuleProcessor";
1)if(!class_exists($classname)) { 不存在模塊的類,直接加載插件里面的processor.php中的類聲明
$file = IA_ROOT . "/addons/{$name}/processor.php"
if(!is_file($file)) {
$file = IA_ROOT . "/framework/builtin/{$name}/processor.php";
}
2)$o = new $classname();
load()->model('module');
$o->module = module_fetch($name);//module.mod.php module_fetch() 獲取當前公號下安裝好的指定模塊及模塊信息
$o->__define = $file;
self::defineConst($o);
if($o instanceof WeModule) {
return $o;
}
$response = $processor->respond(); //執行具體的模塊的processor.處理邏輯
例如CoverModuleProcessor類的處理邏輯
class CoverModuleProcessor extends WeModuleProcessor {
public function respond() {
global $_W;
$content = $this->message['content'];
//查詢回復表中的id對應的回復內容
$reply = pdo_fetch('SELECT * FROM ' . tablename('cover_reply') . ' WHERE `rid`=:rid', array(':rid' => $this->rule));
if(!empty($reply)) {
load()->model('module');
$module = module_fetch($reply['module']);
if (empty($module) && !in_array($reply['module'], array('site', 'mc', 'card'))) {
return '';
}
$url = $reply['url'];
if(empty($reply['url'])) {
$entry = pdo_fetch("SELECT eid FROM ".tablename('modules_bindings')." WHERE module = :module AND do = :do", array(':module' => $reply['module'], ':do' => $reply['do']));
$url = url('entry', array('eid' => $entry['eid']));
}
if (!strexists($url, '&j=') && !empty($_W['acid'])) {
$url = str_replace("?i={$_W['uniacid']}&", "?i={$_W['uniacid']}&j={$_W['acid']}&", $url);
}
$news = array();
$news[] = array(
'title' => $reply['title'],
'description' => $reply['description'],
'picurl' => $reply['thumb'],
'url' => $url
);
return $this->respNews($news);
}
return '';
}
if($this->isValidResponse($response)) {
$hitParam = $par;
if(!empty($par['keyword'])) {
$hitKeyword = $par['keyword'];
}
break;
}
WeUtility::logging('params', $hitParam);
2016-06-07 14:27:25 params :
------------
Array:
message : Array ;
module : cover ;
rule : 32 ;
priority : 0 ;
keyword : Array ;
WeUtility::logging('response', $response);
2016-06-07 14:27:25 response :
------------
Array:
FromUserName : toUser ;
ToUserName : fromUser ;
MsgType : news ;
ArticleCount : 1 ;
Articles : Array ;
$resp = $this->account->response($response);
$resp = $this->clip($resp, $hitParam);
if(!empty($_GET['encrypt_type']) && $_GET['encrypt_type'] == 'aes') {
$resp = $this->account->encryptMsg($resp);
$resp = $this->account->xmlDetract($resp);
}
echo $resp;
$this->receive($hitParam, $hitKeyword, $response);
private function receive($par, $keyword, $response) {
if (in_array($this->message['event'], array('subscribe', 'unsubscribe')) || in_array($this->message['type'], array('subscribe', 'unsubscribe'))) {
if (in_array($this->message['event'], array('subscribe', 'unsubscribe')) || in_array($this->message['type'], array('subscribe', 'unsubscribe'))) {
$modules = uni_modules();
$core = array();
$core['name'] = 'core';
$core['subscribes'] = array('core');
array_unshift($modules, $core);
foreach($modules as $m) {
if(!empty($m['subscribes'])) {
if ($m['name'] == 'core' || in_array($this->message['type'], $m['subscribes'])
|| in_array($this->message['event'], $m['subscribes'])) {
$obj = WeUtility::createModuleReceiver($m['name']);
$obj->message = $this->message;
$obj->params = $par;
$obj->response = $response;
$obj->keyword = $keyword;
$obj->module = $m;
$obj->uniacid = $_W['uniacid'];
$obj->acid = $_W['acid'];
if(method_exists($obj, 'receive')) {
@$obj->receive();
}
}
}
}
} else {
$row = array();
$row['uniacid'] = $_W['uniacid'];
$row['acid'] = $_W['acid'];
$row['dateline'] = $par['message']['time'];
$row['message'] = iserializer($par['message']);
$row['keyword'] = iserializer($keyword);
unset($par['message']);
unset($par['keyword']);
$row['params'] = iserializer($par);
$row['response'] = iserializer($response);
$row['module'] = $par['module'];
$row['type'] = 1;
pdo_insert('core_queue', $row);
}
}
數據表的查詢:
Ims_Rule(關鍵詞回復規則表),加載module+processor.php文件時,查詢的就是IMS_Rule這個表
# 3.視圖頁
display.html
# 4.文本
消息類型同公眾平臺官方不同之處在于將 event 類型拆分開為獨立的消息類型, 避免了重復判斷.
根據消息類型不同, 消息對象結構還存在不同的附加數據,按照類型定義如下:
文本消息
粉絲用戶向公眾號發送了一條普通文本消息(包括包含表情的消息, 或者純表情消息)
處理文本消息可以實現簡單的文本對話, 結合使用文本上下文(請參閱上下文處理)可以實現調查, 測試等復雜的交互.
$text_message = array(
// 全局數據
'tousername' => 'toUser'
'fromusername' => 'fromUser'
'createtime' => '123456789'
'msgtype' => 'text' // string: 消息類型
'content' => // string: 文本消息內容
'redirection' => false, // bool: 是否是重定向
'source' => null // string: 消息來源, 消息二次分析(目前來源:qr,click, 將掃碼等事件轉換為 text 事件.)
)