UCenter 1.6 源码分析记录之同步发送通知 load('note')
UCenter 1.6 服务端操作后,会通过以下语句记录日志并发送同步通知,以修改密码为例:
$this->load('note');//实例化日志模型'note',主要是记录日志跟发送通知,使用 /model/base.php 中类 base 的方法 load(),并返回 return $_ENV[$model]; $_ENV['note']->add('updatepw', 'username='.urlencode($username).'&password='); $_ENV['note']->send();//send()方法
/model/base.php 中类 base 的方法 load() 源码如下:
function load($model, $base = NULL, $release = '') {
$base = $base ? $base : $this;
if(empty($_ENV[$model])) {
$release = !$release ? RELEASE_ROOT : $release;
if(file_exists(UC_ROOT.$release."model/$model.php")) {
require_once UC_ROOT.$release."model/$model.php";
} else {
require_once UC_ROOT."model/$model.php";
}
eval('$_ENV[$model] = new '.$model.'model($base);');
}
return $_ENV[$model];
}
接着看 /model/base.php 中类 base 的方法 send() 源码
function send() {
register_shutdown_function(array($this, '_send'));//register_shutdown_function 在窗口结束后,又异步调用_send()
}
function _send() {
$note = $this->_get_note();//获取要发送的消息
if(empty($note)) {
$this->db->query("REPLACE INTO ".UC_DBTABLEPRE."vars SET name='noteexists', value='0'");
return NULL;
}
$closenote = TRUE;
//循环通知各应用
foreach((array)$this->apps as $appid => $app) {
$appnotes = $note['app'.$appid];
//是否接受通知 && 通知未成功过 && 失败的次数
if($app['recvnote'] && $appnotes != 1 && $appnotes > -UC_NOTE_REPEAT) {
$this->sendone($appid, 0, $note);//转到下面的 sendone() 方法,发送一条通知
$closenote = FALSE;
break;
}
}
//通知结束
if($closenote) {
$this->db->query("UPDATE ".UC_DBTABLEPRE."notelist SET closed='1' WHERE noteid='$note[noteid]'");
}
$this->_gc();//MS是清理未发送的数据
}
接着看 /model/base.php 中类 base 的方法 sendone() 源码
function sendone($appid, $noteid = 0, $note = '') {
require_once UC_ROOT.'./lib/xml.class.php';
$return = FALSE;
$app = $this->apps[$appid];
if($noteid) {
$note = $this->_get_note_by_id($noteid);
}
。。。。。。此处省去N多代码。。。。。。
unset($uc_note);
} else {
//生成接口,把 'getdata' 加到 $_GET 里,也即是接口设置的参数
$url = $this->get_url_code($note['operation'], $note['getdata'], $appid);
$note['postdata'] = str_replace(array("\n", "\r"), '', $note['postdata']);//postdata字段是空的,不用理
$response = trim($_ENV['misc']->dfopen2($url, 0, $note['postdata'], '', 1, $app['ip'], UC_NOTE_TIMEOUT, TRUE));//开始调用接口,传递积分兑换设置
}
$returnsucceed = $response != '' && ($response == 1 || is_array(xml_unserialize($response)));
。。。。。。此处省去N多代码。。。。。。
return $return;
}