Subscribe.class.php
3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* 应用订阅的数据存取类
*
* @name Service_Subscribe
* @package service
* @copyright yoho.inc
* @version 5.0 (2014-02-08 11:02:56)
* @author fei.hong <fei.hong@yoho.cn>
*/
class Service_Subscribe extends Lib_Service
{
const ROUTER = 'subscribe.yh_uuc.subscription';
const EXPIRE = 10800; // 缓存三小时
private static $_key = 'key_yh_uuc_tbl_app_info_';
/**
* 添加订阅
*
* @param string $appId 应用的ID
* @param string $appSecret 应用的密钥
* @param string $name 应用名称
* @param string $scope 订阅信息
* @param integer $state 应用状态(0:禁用,1:有效)
* @return boolean (false:失败, true:成功)
*/
public function add($appId, $appSecret, $name, $scope, $state)
{
$result = false;
if ($appId && $appSecret && is_string($name) && is_string($scope) && is_numeric($state))
{
$key = sprintf(self::$_key . '%s', $appId);
$param = array(
'key' => $appId, 'secret' => $appSecret, 'name' => $name,
'scope' => $scope, 'state' => $state, 'create_time' => time(),
);
$result = self::service(self::ROUTER)->key($key)->insert('add', $param)->status();
}
return $result;
}
/**
* 获取订阅
*
* @param string $appId 应用的ID
* @return array
*/
public function get($appId)
{
$result = array();
if ($appId)
{
$key = sprintf(self::$_key . '%s', $appId);
$param = array('key' => $appId);
$result = self::service(self::ROUTER)->key($key)->expire(self::EXPIRE)->fetchRow('get', $param);
}
return $result;
}
/**
* 获取所有应用
*
* @return array
*/
public function getAllApps()
{
$result = array();
$key = self::$_key . '_allApps';
$result = self::service(self::ROUTER)->key($key)->expire(self::EXPIRE)->fetchAll('getAllApps');
return $result;
}
/**
* 编辑订阅
*
* @param integer $appId 应用的ID
* @param string $newSecret 新的密钥<optional>
* @param string $name 应用名称<optional>
* @param string $scope 订阅信息<optional>
* @param integer $state 应用状态<optional> (0:表示禁用,1:表示有效)
* @return boolean (false:失败, true:成功)
*/
public function upd($appId, $newSecret, $name, $scope, $state)
{
$result = false;
if ($appId)
{
$key = sprintf(self::$_key . '%s', $appId);
$param = array('key' => $appId);
$appInfo = self::service(self::ROUTER)->key($key)->expire(self::EXPIRE)->fetchRow('get', $param);
if ($appInfo && isset($appInfo['id']))
{
$param = array();
empty($newSecret) ? true : $param['secret'] = $newSecret;
empty($name) ? true : $param['name'] = $name;
empty($scope) ? true : $param['scope'] = $scope;
is_numeric($state) ? $param['state'] = $state : true;
if (array() !== $param)
{
$replace = array('STRINGS' => Util_Utils_SqlString::mergeSqlUpstring($param));
$param['id'] = $appInfo['id'];
$result = self::service(self::ROUTER)->key($key)->update('upd', $param, $replace)->status();
}
}
}
return $result;
}
}