Yuanxiao.php
7.36 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
use Action\HuodongAction;
use Cuxiao\YuanxiaoModel;
use Plugin\Helpers;
/**
* 元宵抽签活动
*/
class YuanxiaoController extends HuodongAction
{
// const STATIC_FILE = 'http://localhost:2222/assets/1.0.1';
const STATIC_FILE = 'http://cdn.yoho.cn/huodong/2016yuanxiao/1.0.1';
const ENCRYPT_KEY = 'iamgtskkwhoareyou';
/**
* 通过当前用户审判是否跳到登录页
*
*/
private function auditJumpLogin()
{
$uid = $this->getLoggedUid();
if (!$uid || !is_numeric($uid)) {
$this->go(Helpers::url('/signin.html', array('refer' => Helpers::url('/cuxiao/yuanxiao/info')), 'default'));
}
}
/**
* 元宵抽签
*/
public function indexAction()
{
// 该变量用来控制和显示页面的弹出提示
$showPopupFlag = '0';
$this->_view->display('index', array(
'staticTitle' => '元宵抽签',
'staticFile' => self::STATIC_FILE,
'staticJS' => array(
'home.js'
),
'weixinShare' => $this->_isApp ? false : true, // 是否需要微信分享
'shareLink' => 'http://feature.yoho.cn/2016lishifeng/index.html',
'shareTitle' => 'YOHO!BUY有货新年寻宝大作战,千元利是等你赢!',
'shareDesc' => 'YOHO!BUY有货,全球800+潮流品牌每日上新!',
'shareImg' => 'http://img12.static.yhbimg.com/couponImg/2015/12/30/12/02008a9724b898dee56852de9f1a3978bf.jpg',
'showPopupFlag' => $showPopupFlag,
));
}
/**
* 元宵抽签 信息
*/
public function infoAction()
{
// 审判跳转登录页
$this->auditJumpLogin();
// POST提交请求
if (!empty($this->post())) {
$birthday = $this->post('birthday', '');
$gender = $this->post('gender', 0);
// 调用接口
$result = array(
'code' => 200,
'data' => array(
'name' => '李四',
'type' => 'P1',
'count' => 8888
)
);
if ($result['code'] === 200) { // 处理成功就跳转到等待页
// 将返回的结果存入cookeie中15分钟
$this->setCookie('yuanxiaochouqian', json_encode($result['data']), time() + 15*60);
$this->go(Helpers::url('/cuxiao/yuanxiao/wait'));
} else { // 不成功就显示返回的错误信息
$this->_view->display('info', array(
'staticTitle' => '元宵抽签',
'staticFile' => self::STATIC_FILE,
'staticJS' => array(
'info.js'
),
'birthday' => $birthday,
'gender' => $gender,
'message' => $result['message']
));
}
} else {
$uid = $this->getUid();
$userData = YuanxiaoModel::getUserProfileData($uid);
$this->_view->display('info', array(
'staticTitle' => '元宵抽签',
'staticFile' => self::STATIC_FILE,
'staticJS' => array(
'info.js'
),
'birthday' => $userData['birthday'],
'gender' => $userData['gender'],
));
}
}
/**
* 元宵抽签 等待
*/
public function waitAction()
{
// 审判跳转登录页
$this->auditJumpLogin();
$this->_view->display('wait', array(
'staticTitle' => '元宵抽签',
'staticFile' => self::STATIC_FILE,
'staticJS' => array(
'wait.js'
),
));
}
/**
* 元宵抽签 结果
*/
public function resultAction()
{
// 审判跳转登录页
$this->auditJumpLogin();
$result = $this->getCookie('yuanxiaochouqian', null);
if (empty($result)) { // 未取到信息就重新测试
$this->go(Helpers::url('/cuxiao/yuanxiao/info'));
}
// cookie中存的结果
$result = json_decode($result, true);
$shareUrl = Helpers::url('/cuxiao/yuanxiao/share', array(
'name' => $this->encrypt($result['name']),
'hash' => $this->encrypt($this->getUid())
));
$this->_view->display('result', array(
'staticTitle' => '元宵抽签',
'staticFile' => self::STATIC_FILE,
'result' => array(
'name' => '张大龙',
'type' => 'M1'
),
'result' => $result,
'shareUrl' => $shareUrl,
'staticJS' => array(
'result.js'
)
));
}
/**
* 元宵抽签 分享
*/
public function shareAction()
{
// 昵称从url中解析
$name = $this->decrypt($this->get('name', ''));
// 用户UID
$uid = $this->decrypt($this->get('hash', ''));
// 调用接口获取type和count
$result = array('type' => 'M1', 'count' => 9887);
$this->_view->display('share', array(
'staticTitle' => '元宵抽签',
'staticFile' => self::STATIC_FILE,
'result' => array(
'name' => $name,
'type' => $result['type']
),
'count' => $result['count'],
'staticJS' => array(
'share.js'
)
));
}
/**
* 检测登录状态
*
* @return int
*/
private function getLoggedUid()
{
if ($this->_isApp) {
$uid = $this->get('uid');
} else {
$uid = $this->getUid();
}
return $uid;
}
/**
* 加密字符串
*
* @param string $data 需要加密的字符串
* @return string 加密之后的字符串
*/
private function encrypt($data)
{
$data = strval($data);
$key = md5(self::ENCRYPT_KEY);
$x = 0;
$len = strlen($data);
$l = strlen($key);
$char = '';
$str = '';
for ($i = 0; $i < $len; $i++) {
if ($x == $l)
{
$x = 0;
}
$char .= $key{$x};
$x++;
}
for ($i = 0; $i < $len; $i++) {
$str .= chr(ord($data{$i}) + (ord($char{$i})) % 256);
}
return base64_encode($str);
}
/**
* 解密字符串
*
* @param string $data 需要解密的字符串
* @return string 解密之后的字符串
*/
private function decrypt($data)
{
$key = md5(self::ENCRYPT_KEY);
$x = 0;
$data = base64_decode($data);
$len = strlen($data);
$l = strlen($key);
$char = '';
$str = '';
for ($i = 0; $i < $len; $i++) {
if ($x == $l)
{
$x = 0;
}
$char .= substr($key, $x, 1);
$x++;
}
for ($i = 0; $i < $len; $i++) {
if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) {
$str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
} else {
$str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
}
}
return $str;
}
}