Yuanxiao.php
11.5 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
use Action\HuodongAction;
use Cuxiao\YuanxiaoModel;
use Plugin\Helpers;
/**
* 元宵抽签活动
*/
class YuanxiaoController extends HuodongAction
{
// const STATIC_FILE = 'http://localhost:2222/assets/1.1.3';
const STATIC_FILE = 'http://cdn.yoho.cn/huodong/2016yuanxiao/1.1.5';
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()
{
$playUrl = Helpers::url('/cuxiao/yuanxiao/info');
$playUrlEncode = strtr($playUrl, array('/' => '\\/'));
$this->_view->display('index', array(
'staticTitle' => '2016潮流运势测试',
'staticFile' => self::STATIC_FILE,
'staticJS' => array(
'home.js'
),
'shareDesc' => '元宵节快乐!一起来YOHO!BUY有货玩2016年潮流开运测试!',
'shareImg' => 'http://img02.yohoboys.com/staticimg/2016/02/19/16/02dbd38c84eccf1097b7e240452f0de856.png',
'shareUrl' => Helpers::url('/cuxiao/yuanxiao/index'),
'playUrl' => $this->isApp() ? $playUrl . '?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"' . $playUrlEncode . '","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"N"}}': $playUrl,
));
}
/**
* 元宵抽签 信息
*/
public function infoAction()
{
$uid = $this->getLoggedUid();
if (!$uid) {
if (!$this->_isApp) {
$this->go(Helpers::url('/signin.html', array('refer' => Helpers::url('/cuxiao/yuanxiao/info')), 'default'));
} else {
exit();
}
}
// POST提交请求
$posts = $this->post();
if (!empty($posts)) {
$nickName = $this->post('nick', '');
$birthday = $this->post('birthday', '');
$gender = $this->post('gender', 1);
// 格式化日期以便接口接收
$FormatBirthday = date_format(date_create_from_format('Y年m月d日', $birthday), 'Y-m-d');
// 调用接口
$result = YuanxiaoModel::getChouqianInfo($uid, $nickName, $FormatBirthday, $gender);
if ($result['code'] === 200) { // 处理成功就跳转到等待页
// 将返回的结果存入cookeie中15分钟
$cookieData = array(
'name' => $nickName,
'type' => $result['data']['randomCode']
);
$this->setCookie('yuanxiaochouqian', json_encode($cookieData), time() + 15 * 60);
$waitUrl = Helpers::url('/cuxiao/yuanxiao/wait');
$this->go($this->_isApp ? $waitUrl . '?openby:yohobuy={"action":"go.h5","params":{"islogin":"N","param":{"from":"app"}}}' : $waitUrl);
} else { // 不成功就显示返回的错误信息
$this->_view->display('info', array(
'staticTitle' => '2016潮流运势测试',
'staticFile' => self::STATIC_FILE,
'staticJS' => array(
'info.js'
),
'shareDesc' => '元宵节快乐!一起来YOHO!BUY有货玩2016年潮流开运测试!',
'shareImg' => 'http://img02.yohoboys.com/staticimg/2016/02/19/16/02dbd38c84eccf1097b7e240452f0de856.png',
'shareUrl' => Helpers::url('/cuxiao/yuanxiao/index'),
'birthday' => $birthday,
'birthdayNumber' => $FormatBirthday,
'gender' => $gender,
'isBoy' => $gender == 1, // 性别是否是女标志位
'message' => $result['message']
));
}
} else {
$userData = YuanxiaoModel::getUserProfileData($uid);
$this->_view->display('info', array(
'staticTitle' => '2016潮流运势测试',
'staticFile' => self::STATIC_FILE,
'staticJS' => array(
'info.js'
),
'shareDesc' => '元宵节快乐!一起来YOHO!BUY有货玩2016年潮流开运测试!',
'shareImg' => 'http://img02.yohoboys.com/staticimg/2016/02/19/16/02dbd38c84eccf1097b7e240452f0de856.png',
'shareUrl' => Helpers::url('/cuxiao/yuanxiao/index'),
'birthday' => date_format(date_create($userData['birthday']), 'Y年m月d日'),
'birthdayNumber' => $userData['birthday'],
'gender' => $userData['gender'],
'isBoy' => $userData['gender'] == 1 // 性别是否是女标志位
));
}
}
/**
* 元宵抽签 等待
*/
public function waitAction()
{
// 审判跳转登录页
// $this->auditJumpLogin();
$activityUrl = Helpers::url('/cuxiao/yuanxiao/result');
$this->_view->display('wait', array(
'staticTitle' => '2016潮流运势测试',
'staticFile' => self::STATIC_FILE,
'activityUrl' => $this->_isApp ? $activityUrl . '?openby:yohobuy={"action":"go.h5","params":{"islogin":"N","param":{"from":"app"},"share":"'.$activityUrl.'", shareparam:":{"share_id":"1"}}}' : $activityUrl,
'staticJS' => array(
'wait.js'
),
'shareDesc' => '元宵节快乐!一起来YOHO!BUY有货玩2016年潮流开运测试!',
'shareImg' => 'http://img02.yohoboys.com/staticimg/2016/02/19/16/02dbd38c84eccf1097b7e240452f0de856.png',
'shareUrl' => Helpers::url('/cuxiao/yuanxiao/index'),
));
}
/**
* 元宵抽签 结果
*/
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(
'hash' => $this->encrypt($this->getUid()),
'type' => $this->encrypt($result['type']),
'name' => $this->encrypt($result['name'])
));
$this->_view->display('result', array(
'staticTitle' => '2016潮流运势测试',
'needShare' => $this->checkNeedShare(), // 是否需要微信分享
'staticFile' => self::STATIC_FILE,
'result' => $result,
'shareTitle' => $result['name'] . '的2016年潮流运势是' . $this->getChouqianTitle($result['type']),
'shareDesc' => '元宵节快乐!一起来YOHO!BUY有货玩2016年潮流开运测试!',
'shareImg' => 'http://img02.yohoboys.com/staticimg/2016/02/19/16/02dbd38c84eccf1097b7e240452f0de856.png',
'shareUrl' => $shareUrl,
'staticJS' => array(
'result.js'
)
));
}
/**
* 元宵抽签 分享
*/
public function shareAction()
{
// 用户UID
$uid = $this->decrypt($this->get('hash', ''));
if (empty($uid)) {
$this->go(Helpers::url('/cuxiao/yuanxiao/index'));
}
// 抽签结果
$type = $this->decrypt($this->get('type', ''));
$name = $this->decrypt($this->get('name', ''));
// 调用接口获取type和count
$result = YuanxiaoModel::getChoujiangShareInfo($uid);
$this->_view->display('share', array(
'staticTitle' => '2016潮流运势测试',
'staticFile' => self::STATIC_FILE,
'result' => array(
'name' => $name ?: $result['name'],
'type' => $type ?: $result['type']
),
'count' => $result['total'],
'staticJS' => array(
'share.js'
),
'shareDesc' => '元宵节快乐!一起来YOHO!BUY有货玩2016年潮流开运测试!',
'shareImg' => 'http://img02.yohoboys.com/staticimg/2016/02/19/16/02dbd38c84eccf1097b7e240452f0de856.png'
));
}
/**
* 根据结果类型获取抽签结果标题
* @param $type
*/
private function getChouqianTitle($type)
{
$data = array(
'P1' => '成为一个大写加粗的潮人!',
'P2' => '逼格满载盐值爆表!',
'P3' => '运气爆棚,拥有Yeezy不是梦!',
'P4' => '街拍不断,成为网红不是梦!',
'M1' => '穿着MA-1飞行夹克一飞冲天!',
'M2' => '把妹不花钱!买鞋不排队!',
'M3' => '穿着跑鞋到达人生巅峰!',
'M4' => '帅气多金颜值高!',
'F1' => '拥有拆不完的快递和礼物!',
'F2' => '被男神强势壁咚!',
'F3' => '腿细胸大屁股翘!',
'F4' => '任何style都轻松驾驭!'
);
return $data[$type];
}
/**
* 检测登录状态
*
* @return int
*/
private function getLoggedUid()
{
if ($this->_isApp) {
$uid = $this->get('uid');
} else {
$uid = $this->getUid();
}
return $uid;
}
/**
* 判断是否是APP访问
*
* @return bool
*/
private function isApp()
{
return (null !== $this->get('app_version'));
}
/**
* 判断是否需要微信分享
* @return bool
*/
private function checkNeedShare()
{
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false || $this->_isApp) {
return true;
}
return false;
}
/**
* 加密字符串
*
* @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;
}
}