Helpers.php
37.3 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
<?php
namespace WebPlugin;
/**
* 辅助类
*/
class Helpers
{
/**
* 构建网站的URL
*
* 备注:所有的URL构建都尽量使用该方法,便于以后维护.
*
* @param string $uri 如 "/passport/reg/index"
* @param array $param 参数项 array(key1 => value1, key2 => value2,),默认为array()
* @param string $module 模块名 如"index"表示默认, "guang"表示逛,"list"表示商品列表,"search"表示搜索
* @return string
*/
public static function url($uri, $param = array(), $module = 'index')
{
$url = '';
switch ($module) {
case 'default':
$url = 'http://www.yohobuy.com';
break;
case 'guang': // 逛
$url = 'http://guang' . SUB_DOMAIN;
break;
case 'list': // 商品列表
$url = 'http://list' . SUB_DOMAIN;
break;
case 'search': // 搜索
$url = 'http://search' . SUB_DOMAIN;
break;
case 'index': // 默认
$url = SITE_MAIN;
break;
case '': // 相对地址
break;
default: // 其它子域名
$url = 'http://' . $module . SUB_DOMAIN;
}
$url .= $uri;
if (!empty($param)) {
$url .= '?' . http_build_query($param, null, '&');
}
return $url;
}
/**
* 根据尺寸获得图片url
*
* @param string $url 路径
* @param integer $width 图片宽度
* @param integer $height 图片高度
* @param integer $mode 模式
* @return string 图片地址
*/
public static function getImageUrl($url, $width, $height, $mode = 2)
{
return strtr($url, array('{width}' => $width, '{height}' => $height, '{mode}' => $mode));
}
/**
* 获取过滤APP里附加参数后的URL链接
*
* @param string $url 路径
* @return string 去除掉如&openby:yohobuy={"action":"go.brand"}这样的APP附加参数
*/
public static function getFilterUrl($url)
{
$url = strtr($url, array('.m.yohobuy.com' => SUB_DOMAIN, OLD_MAIN => SITE_MAIN, 'www.yohobuy.com' => SITE_MAIN));
$filter = strstr($url, 'openby:yohobuy=', true);
if ($filter) {
return rtrim(rtrim($filter, '?'), '&');
} else {
return $url;
}
}
/**
* 根据用户访问的COOKIE判断出性别
*
* @return string
*/
public static function getGenderByCookie()
{
$cookie = isset($_COOKIE['_Channel']) ? $_COOKIE['_Channel'] : 'boys';
switch (strval($cookie)) {
case 'boys': // 男
return '1,3';
case 'girls': // 女
return '2,3';
default: // 其它
return '1,2,3';
}
}
/**
* 根据用户访问的COOKIE判断出频道
*
* @return int
*/
public static function getChannelByCookie()
{
$cookie = isset($_COOKIE['_Channel']) ? $_COOKIE['_Channel'] : 'boys';
switch (strval($cookie)) {
case 'boys': // 男
return 1;
case 'girls': // 女
return 2;
case 'kids': // 潮童
return 3;
case 'lifestyle': // 创意生活
return 4;
default: // 其它
return 1;
}
}
/**
* 根据用户访问的COOKIE判断出频道名称
*
* @return string
*/
public static function getChannelNameByCookie()
{
$cookie = isset($_COOKIE['_Channel']) ? strval($_COOKIE['_Channel']) : 'boys';
switch ($cookie) {
case 'boys': // 男
return 'boys';
case 'girls': // 女
return 'girls';
case 'kids': // 潮童
return 'kids';
case 'lifestyle': // 创意生活
return 'lifestyle';
default: // 其它
return 'boys';
}
}
/**
* 从用户加入购物车的COOKIE取出购物车凭证
*
* @return string
*/
public static function getShoppingKeyByCookie()
{
$cookie = isset($_COOKIE['_SPK']) ? $_COOKIE['_SPK'] : '';
return $cookie;
}
/**
* 获取商品的ICON
*
* @param int $type
* @return array
*/
public static function getProductIcon($type)
{
static $icons = array(
1 => 'cloth',
3 => 'pants',
4 => 'dress',
6 => 'shoe',
7 => 'bag',
10 => 'lamp',
241 => 'headset',
8 => 'watch',
360 => 'swim-suit',
308 => 'under'
);
$type = intval($type);
return isset($icons[$type]) ? $icons[$type] : '';
}
/**
* 根据排序类型和类型值获得正确的排序参数
* @param integer $order 类型值
* @param string $type 排序类型
* @return string 转换之后的排序参数
*/
public static function transOrder($order, $type)
{
switch ($type) {
case 'price':
$result = ($order == 0) ? 's_p_desc' : 's_p_asc';
break;
case 'discount':
$result = ($order == 0) ? 'p_d_desc' : 'p_d_asc';
break;
case 'newest':
default:
$result = ($order == 1) ? 's_t_desc' : 's_t_asc';
break;
}
return $result;
}
/**
* 转换价格
*
* @param float|string $price 价格
* @return float|string 转换之后的价格
*/
public static function transPrice($price)
{
return !empty($price) ? number_format($price, 2, '.', '') : '0.00';
}
/**
* 格式化商品信息
*
* @param array $productData 需要格式化的商品数据
* @param bool $showTags 控制是否显示标签
* @param bool $showNew 控制是否显示NEW图标
* @param bool $showSale 控制是否显示SALE图标
* @param int $width 图片的宽度
* @param int $height 图片的高度
* @param bool $isApp 判断是不是APP访问
* @param bool $showPoint 商品价格是否显示小数位,默认显示
* @return array | false
*/
public static function formatProduct($productData, $showTags = true, $showNew = true, $showSale = true, $width = 290, $height = 388, $isApp = false, $showPoint = true)
{
// 商品信息有问题,则不显示
if (!isset($productData['product_skn']) || !isset($productData['goods_list'][0])) {
return false;
}
// 市场价和售价一样,则不显示市场价
if (intval($productData['market_price']) === intval($productData['sales_price'])) {
$productData['market_price'] = false;
}
// 判别默认的商品是否将默认的图片URL赋值到skn
$flag = false;
// 如果设置了默认图片,就取默认的图片
foreach ($productData['goods_list'] as $oneGoods) {
// 此skc是默认的,则将图片赋值给skn
if ($oneGoods['is_default'] === 'Y') {
$productData['default_images'] = self::procProductImg($oneGoods);
$flag = true;
break;
}
}
// 如果还未赋值,则取第一个skc产品的默认图片
if (!$flag) {
$productData['default_images'] = self::procProductImg($productData['goods_list'][0]);
}
$result = array();
$result['id'] = $productData['product_skn'];
$result['product_id'] = $productData['product_id'];
$result['thumb'] = Images::getImageUrl($productData['default_images'], $width, $height);
$result['name'] = $productData['product_name'];
$result['price'] = empty($productData['market_price']) ? false : $productData['market_price'];
$result['salePrice'] = $productData['sales_price'];
if ($showPoint) {
$result['price'] && $result['price'] .= '.00';
$result['salePrice'] && $result['salePrice'] .= '.00';
}
$result['is_soon_sold_out'] = ($productData['is_soon_sold_out'] === 'Y');
$result['url'] = self::url('/product/pro_' . $productData['product_id'] . '_'
. $productData['goods_list'][0]['goods_id']
. '/' . $productData['cn_alphabet'] . '.html', array(), 'item');
// APP访问需要加附加的参数
// 备注:如果以后APP的接口太多,可以把这边参数提取出来,变成一个公共的方法来生成,便于以后管理维护
if ($isApp) {
$result['url'] .= '?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":' . $productData['product_skn'] . '}}';
}
if ($showTags) {
$result['tags'] = array();
$result['tags']['is_new'] = $showNew && isset($productData['is_new']) && $productData['is_new'] === 'Y'; // 新品
$result['tags']['is_discount'] = $showSale && isset($productData['is_discount']) && $productData['is_discount'] === 'Y'; // 在售
$result['tags']['is_limited'] = isset($productData['is_limited']) && $productData['is_limited'] === 'Y'; // 限量
$result['tags']['is_yohood'] = isset($productData['is_yohood']) && $productData['is_yohood'] === 'Y'; // YOHOOD
$result['tags']['midYear'] = isset($productData['mid-year']) && $productData['mid-year'] === 'Y'; // 年中
$result['tags']['yearEnd'] = isset($productData['year-end']) && $productData['year-end'] === 'Y'; // 年末
$result['tags']['is_advance'] = isset($productData['is_advance']) && $productData['is_advance'] === 'Y'; // 再到着
// 打折与即将售完组合显示打折
if ($result['is_soon_sold_out'] && $result['tags']['is_discount']) {
$result['tags']['is_new'] = false;
}
// 打折与其它组合则隐藏打折
elseif ($result['tags']['is_discount'] &&
($result['tags']['is_new'] || $result['tags']['is_limited'] || $result['tags']['is_yohood'] || $result['tags']['is_advance'])) {
$result['tags']['is_discount'] = false;
}
// YOHOOD和新品组合显示YOHOOD
elseif ($result['tags']['is_yohood'] && $result['tags']['is_new']) {
$result['tags']['is_new'] = false;
}
}
return $result;
}
/**
* 根据性别来决定 默认图片获取字段 如果是 2、3
*
* 2、3: cover2 --> images_url -> cover1
* 1,3 :cover1 --> images_url -> cover2
* 其他: cover1 --> cover2 --> images_url
*
* @param array $images
* @return string 商品图片
*/
public static function procProductImg($images)
{
$imgUrl = isset($images['images_url']) ? $images['images_url'] : '';
$cover1 = isset($images['cover_1']) ? $images['cover_1'] : '';
$cover2 = isset($images['cover_2']) ? $images['cover_2'] : '';
$gender = self::getGenderByCookie();
if ($gender === '2,3') {
return !empty($cover2) ? $cover2 : (!empty($imgUrl) ? $imgUrl :$cover1);
}
else if ($gender == '1,3') {
return !empty($cover1) ? $cover1 : (!empty($imgUrl) ? $imgUrl : $cover2);
}
else {
return !empty($cover1) ? $cover1 : (!empty($cover2) ? $cover2 : $imgUrl);
}
}
/**
* 格式化资讯文章
*
* @param array $articleData 需要格式化的资讯数据
* @param bool $showTag 是否显示左上角标签
* @param mixed $isApp 是否显示分享,在APP客户端里嵌入需要传url链接
* @param bool $showAuthor 控制是否显示作者信息
* @param int $uid 当前登录的用户ID
* @return array | false
*/
public static function formatArticle($articleData, $showTag = true, $isApp = false, $showAuthor = true, $uid = null)
{
// 资讯ID不存在,则不显示
if (!isset($articleData['id'])) {
return false;
}
$width = $height = 360;
$isSquareImage = true;
if($articleData['conver_image_type'] == 2) {
$width = 360;
$height = 240;
$isSquareImage = false;
}
$result = array();
$result['id'] = $articleData['id'];
$result['classification'] = $articleData['category_name'];
$result['isReco'] = $articleData['is_recommended'] ? true : false;
$result['url'] = $isApp ? $articleData['url'] : self::url('/'.$articleData['id'].'.html', array(), 'guang');
$result['img'] = self::getImageUrl($articleData['src'], $width, $height);
$result['isSquareImg'] = $isSquareImage;
$result['title'] = $articleData['title'];
if(empty($articleData['author'])){
$articleData['author']['name'] = '';
$articleData['author']['avatar'] = '';
}
$result['author'] = $articleData['author']['name'];
if (isset($articleData['author']['author_id']) && !empty($articleData['author']['author_id'])) {
$author_id = $articleData['author']['author_id'];
$result['editorUrl'] = self::url('/Index/editor', array('author_id' => $author_id), 'guang');
}
$result['pTime'] = $articleData['publish_time'];
$result['pView'] = $articleData['views_num'];
$result['content'] = $articleData['intro'];
$result['showTags'] = $showTag;
$tags = array();
if (!empty($articleData['tags'])) {
foreach ($articleData['tags'] as $tag) {
$tags[] = self::formatTag($tag, $isApp);
}
}
$result['tags'] = $tags;
$result['comment'] = $articleData['comment_num'];
// 收藏
if ($isApp) {
$result['collect'] = array();
$result['collect']['isCollected'] = isset($articleData['isFavor']) && $articleData['isFavor'] === 'Y';
$result['collect']['url'] = !empty($uid) ? 'javascript:;' : strtr($articleData['url'], array('"islogin":"N"' => '"islogin":"Y"'));
}
// 点赞
else {
$result['like'] = $articleData['praise_num'];
$result['liked'] = isset($articleData['isPraise']) && $articleData['isPraise'] === 'Y';
}
// 分享链接
$result['share'] = $isApp && isset($articleData['share']['url']) ? $articleData['share']['url'] : false;
// 判断是否显示作者信息
if ($showAuthor && !empty($articleData['author'])) {
if (!$isApp) {
$articleData['author']['url'] = Helpers::getFilterUrl($articleData['author']['url']);
}
$result['author'] = $articleData['author'];
}
// 模板中需要的标签标识
if ($showTag && isset($articleData['category_id'])) {
switch (strval($articleData['category_id'])) {
case '1': // 话题
$result['isTopic'] = true;
break;
case '2': // 搭配
$result['isCollocation'] = true;
break;
case '3': // 潮人
$result['isFashionMan'] = true;
break;
case '4': // 潮品
$result['isFashionGood'] = true;
break;
case '5': // 小贴士
$result['isTip'] = true;
break;
}
}
return $result;
}
public static function formatTag($tagData, $isApp = false, $uid = null) {
$result = array();
$result['tag'] = $tagData['name'];
$result['url'] = Helpers::url('/tags/index', array('query'=>urlencode($tagData['name'])), 'guang');
return $result;
}
public static function formatAd($adData, $isApp = false, $uid = null) {
//此处不需要ad的id
//if (!isset($adData['id'])) {
// return false;
//}
$result = array();
// $result['id'] = $adData['id'];
$result['img'] = self::getImageUrl($adData['src'], 640, 640);
$result['url'] = $isApp ? $adData['url'] : self::url('/info/index', array('id' => $adData['id']), 'guang');
$result['title'] = $adData['title'];
$result['bgColor'] = $adData['bgColor'];
return $result;
}
/**
* 格式化广告焦点图数据
*
* @param array $bannerData 需要格式化的广告图数据
* @param int $width 图片的宽度
* @param int $height 图片的高度
* @param int $mode 使用的七牛模式
* @return array
*/
public static function formatBanner($bannerData, $width, $height, $mode = 2)
{
$result = array();
$result['img'] = self::getImageUrl($bannerData['src'], $width, $height, $mode);
if (isset($bannerData['url'])) {
$result['url'] = self::getFilterUrl($bannerData['url']);
}
$result['title'] = $bannerData['title'];
return $result;
}
/**
* 生成公开的TOKEN凭证
*
* @param string $string 字符串
* @return string
*/
public static function makeToken($string)
{
return md5(md5($string . '#@!@#'));
}
/**
* 验证TOKEN凭证
*
* @param string $string 字符串
* @param string $token 公开访问TOKEN
* @return bool
*/
public static function verifyToken($string, $token)
{
if ($token === self::makeToken($string)) {
return true;
} else {
return false;
}
}
/**
* 验证手机是否合法
*
* @param int $mobile
* @return boolean
*/
public static function verifyMobile($mobile)
{
if (empty($mobile)) {
return false;
}
return (bool) preg_match('/^1[3|4|5|8|7][0-9]{9}$/', trim($mobile));
}
/**
* 验证密码是否合法
*
* @param int $password
* @return boolean
*/
public static function verifyPassword($password)
{
if (empty($password)) {
return false;
}
return (bool) preg_match('/^([a-zA-Z0-9\-\+_!@\#$%\^&\*\(\)\:\;\.=\[\]\\\',\?]){6,20}$/', trim($password));
}
/**
* 验证邮箱是否合法
*
* @param string $email
* @return boolean
*/
public static function verifyEmail($email)
{
if (empty($email)) {
return false;
}
return !!filter_var($email, FILTER_VALIDATE_EMAIL);
}
/**
* 验证国际手机号是否合法
*
* @param string $areaMobile
* @return boolean
*/
public static function verifyAreaMobile($areaMobile)
{
if (empty($areaMobile)) {
return false;
}
if (!strpos($areaMobile, '-')) {
return self::areaMobielVerify($areaMobile);
} else {
$mobileData = explode('-', $areaMobile);
if (count($mobileData) != 2) {
return false;
}
}
return self::areaMobielVerify($mobileData[1], $mobileData[0]);
}
/**
* 根据url获取拼接之后的地址,用于用户清理缓存
* @param string $url url地址
* @param string $channel 频道,默认为woman
*
* @return string 处理之后的地址
*/
public static function transUrl($url, $channel='woman')
{
$extra = '';
if(!empty($url) && stripos($url, '?') === false) {
$extra = '?channel='.$channel;
}
if(!empty($url) && stripos($url, '?') !== false){
$extra = '&channel='.$channel;
}
return $url . $extra;
}
/**
* 各国手机号规则
*/
private static function areaMobielVerify($mobile, $area = 86)
{
$verify = array(
86 => array(
'name' => '中国',
'match' => (bool) preg_match('/^1[3|4|5|8|7][0-9]{9}$/', trim($mobile)),
),
852 => array(
'name' => '中国香港',
'match' => (bool) preg_match('/^[9|6|5][0-9]{7}$/', trim($mobile)),
),
853 => array(
'name' => '中国澳门',
'match' => (bool) preg_match('/^[0-9]{8}$/', trim($mobile)),
),
886 => array(
'name' => '中国台湾',
'match' => (bool) preg_match('/^[0-9]{10}$/', trim($mobile)),
),
65 => array(
'name' => '新加坡',
'match' => (bool) preg_match('/^[9|8][0-9]{7}$/', trim($mobile)),
),
60 => array(
'name' => '马来西亚',
'match' => (bool) preg_match('/^1[1|2|3|4|6|7|9][0-9]{8}$/', trim($mobile)),
),
1 => array(
'name' => '加拿大&美国',
'match' => (bool) preg_match('/^[0-9]{10}$/', trim($mobile)),
),
82 => array(
'name' => '韩国',
'match' => (bool) preg_match('/^01[0-9]{9}$/', trim($mobile)),
),
44 => array(
'name' => '英国',
'match' => (bool) preg_match('/^7[7|8|9][0-9]{8}$/', trim($mobile)),
),
81 => array(
'name' => '日本',
'match' => (bool) preg_match('/^0[9|8|7][0-9]{9}$/', trim($mobile)),
),
61 => array(
'name' => '澳大利亚',
'match' => (bool) preg_match('/^[0-9]{11}$/', trim($mobile)),
),
);
if (isset($verify[$area])) {
return $verify[$area]['match'];
}
return false;
}
/**
* 格式化订单商品
*
* @param array $orderGoods 订单
* @param int $count 计订单件数
* @param bool $haveLink 控制是否需要商品链接
* @return array $arr 处理之后的订单商品数据
*/
public static function formatOrderGoods($orderGoods, &$count = 0, $haveLink = false)
{
$arr = array();
foreach ($orderGoods as $key => $vo) {
$arr[$key]['thumb'] = Helpers::getImageUrl($vo['goods_image'], 90, 120);
$arr[$key]['name'] = $vo['product_name'];
$arr[$key]['color'] = $vo['color_name'];
$arr[$key]['size'] = $vo['size_name'];
$arr[$key]['price'] = $vo['goods_price'];
$arr[$key]['count'] = $vo['buy_number'];
//gift=>是否赠品,advanceBuy=>是否加价购;
if ($vo['goods_type'] == 'gift') {
$arr[$key]['gift'] = true;
} elseif ($vo['goods_type'] == 'price_gift') {
$arr[$key]['advanceBuy'] = true;
}
// 上市期
if (!empty($vo['expect_arrival_time'])) {
$arr[$key]['appearDate'] = $vo['expect_arrival_time'];
}
// 商品链接
if ($haveLink && isset($vo['product_skn'])) {
$arr[$key]['link'] = self::url('/product/show_' . $vo['product_skn'] . '.html');
}
// 累计购买数
$count += intval($vo['buy_number']);
}
return $arr;
}
/**
* 格式化购物车商品
*
* @param array $cartGoods 购物车商品列表
* @param bool $isAdvanceCart 是否是预售购物车(和上市期有关)
* @param bool $isValid 是否是可用商品(非失效商品),默认是
* @param bool $isOffShelves 是否卖光了, 默认否
* @param array $analysis 第三方数据分析用的
* @return array 处理之后的购物车商品数据
*/
public static function formatCartGoods($cartGoods, $isAdvanceCart, $isValid = true, $isOffShelves = false, &$analysis = array())
{
$result = array();
$oneGoods = array();
$cnAlphaBet = '';
foreach ($cartGoods as $value) {
$oneGoods = array();
$oneGoods['id'] = $value['product_sku'];
$oneGoods['skn'] = $value['product_skn'];
$oneGoods['pid'] = $value['product_id'];
$oneGoods['isChecked'] = $value['selected'] === 'Y';
$oneGoods['productTitle'] = $value['product_name'];
$oneGoods['imgCover'] = !empty($value['goods_images']) ? Images::getImageUrl($value['goods_images'], 60, 60) : '';
$oneGoods['productColor'] = $value['color_name'];
$oneGoods['productSize'] = $value['size_name'];
$oneGoods['productPrice'] = self::transPrice($value['last_vip_price']); //self::transPrice($value['real_price']);
$oneGoods['productNum'] = $value['buy_number'];
$oneGoods['isVipPrice'] = $value['sales_price'] !== $value['last_vip_price'];
$oneGoods['yohoIcon'] = isset($value['get_yoho_coin']) ? $value['get_yoho_coin'] : 0;
$oneGoods['productSubtotal'] = self::transPrice( floatval($value['last_vip_price']) * intval($value['buy_number']) );
$oneGoods['promotionId'] = empty($value['promotion_id']) ? '0' : $value['promotion_id'];
// 已下架
if ($isOffShelves) {
$oneGoods['isTipNoStore'] = true;
$oneGoods['tipMessage'] = '库存不足';
}
else if ($isValid) {
if ($value['buy_number'] > $value['storage_number']) {
$oneGoods['isTipNoStore'] = true; // 是否在结算时候显示库存不足
$oneGoods['tipMessage'] = '库存不足';
} elseif ($value['storage_number'] <= 2) {
$oneGoods['tipMessage'] = '余量有限';
}
} else { // 失效商品
$oneGoods['inValid'] = true;
}
// advanceBuy=>是否加价购,soldOut=>失效商品;
if (!isset($value['goods_type'])) {
$oneGoods['inValid'] = true;
}
// gift=>是否赠品
elseif ($value['goods_type'] == 'gift' && !isset($value['isAdvanceBuy'])) {
$oneGoods['isGift'] = true;
$oneGoods['productPrice'] = self::transPrice($value['last_price']);
$oneGoods['productSubtotal'] = isset($value['subtotal']) ? self::transPrice($value['subtotal']) : $oneGoods['productPrice'];
}
// price_gift=>是否加价购
elseif ($value['goods_type'] == 'price_gift') {
$oneGoods['isPriceGift'] = true;
$oneGoods['productPrice'] = self::transPrice($value['last_price']);
$oneGoods['productSubtotal'] = isset($value['subtotal']) ? self::transPrice($value['subtotal']) : $oneGoods['productPrice'];
}
// 免单
elseif ($value['real_price'] == 0) {
$oneGoods['productPrice'] = self::transPrice($value['sales_price']);
$oneGoods['xForOne'] = true;
// 分析用: 商品ID列表
$analysis['ids'][] = $value['product_id'];
// 分析用: 品友
$analysis['ipinyou'] .= $value['product_id'] . ',' . $value['buy_number'] . ';';
// 分析用: CRITEO
$analysis['criteo'][] = array(
'id' => $value['product_skn'],
'quantity' => intval($value['buy_number']),
'price' => $value['last_vip_price'],
);
}
else {
// 分析用: 商品ID列表
$analysis['ids'][] = $value['product_id'];
// 分析用: 品友
$analysis['ipinyou'] .= $value['product_id'] . ',' . $value['buy_number'] . ';';
// 分析用: CRITEO
$analysis['criteo'][] = array(
'id' => $value['product_skn'],
'quantity' => intval($value['buy_number']),
'price' => $value['last_vip_price'],
);
}
// 商品类型:预售或普通
$oneGoods['goodsType'] = $isAdvanceCart ? 'advance' : 'ordinary';
// 上市期
if ($isAdvanceCart && !empty($value['expect_arrival_time'])) {
$oneGoods['preSellDate'] = $value['expect_arrival_time'];
}
// 商品链接
$cnAlphaBet = isset($value['cn_alphabet']) ? $value['cn_alphabet'] : md5($value['product_name']);
$oneGoods['link'] = self::url('/product/pro_' . $value['product_id'] . '_' . $value['goods_id'] . '/' . $cnAlphaBet . '.html', null, 'item');
$result[] = $oneGoods;
}
return $result;
}
/**
* 格式化加价购和赠品商品
*
* @param array $goods 加价购商品列表
* @param bool $isGift 是否是赠品 (true:赠品,false:加价购)
* @return array $arr 处理之后的加价购商品数据
*/
public static function formatGiftPriceGoods($goods, $isGift = false)
{
$result = array();
$gift = array();
$oneGoods = array();
$subjoinInfo = array();
$number = 0;
foreach ($goods as $value) {
$gift = array();
$gift['isShowGift'] = $isGift; // 控制是否显示赠品
$gift['isFold'] = true; // 控制是否[展开]
$gift['promotionId'] = $value['promotion_id'];
$gift['subjoinType'] = $value['promotion_title'] . '(注:您看到的以下商品可能因为下单时间差已售完)';
$gift['subjoinPage'] = array();
$number = 0;
$subjoinInfo = array();
$cnAlphaBet = '';
foreach ($value['goods_list'] as $single) {
++ $number;
$oneGoods = array();
$oneGoods['id'] = $single['product_id'];
$oneGoods['skn'] = $single['product_skn'];
$oneGoods['subjoinTitle'] = $single['product_name'];
$oneGoods['imgCover'] = !empty($single['goods_images']) ? Images::getImageUrl($single['goods_images'], 60, 60) : '';
$oneGoods['subjoinPrice'] = self::transPrice($single['last_price']); // 当前价
$oneGoods['marketPrice'] = $isGift ? '' : self::transPrice($single['market_price']); // 市场价
$oneGoods['yohoIcon'] = 0; // 返YOHO币
// $oneGoods['storageNum'] = $single['storage_number']; // 库存量
// 商品链接
if (isset($single['goods_id'])) {
$cnAlphaBet = isset($single['cn_alphabet']) ? $single['cn_alphabet'] : md5($single['product_name']);
$oneGoods['subjoinLink'] = self::url('/product/pro_' . $single['product_id'] . '_' . $single['goods_id'] . '/' . $cnAlphaBet . '.html', null, 'item');
} else {
$oneGoods['subjoinLink'] = self::url('/product/show_' . $single['product_id'] . '_' . $single['product_skn'] . '/' . Helpers::makeToken($single['product_skn']) . '.html', null, 'item');
}
// 赠品
if ($isGift) {
$oneGoods['isGift'] = true;
}
// 加价购
else {
$oneGoods['isPriceGift'] = true;
}
$subjoinInfo[] = $oneGoods;
// 按一页5条进行分组
if ($number % 5 === 0) {
$gift['subjoinPage'][] = array(
'isHide' => ($number <= 5) ? false : true, // 第一页默认显示, 其余隐藏
'subjoinInfo' => $subjoinInfo,
);
$subjoinInfo = array();
}
}
// 如果数据有剩余,再添加进数组一次
if (array() !== $subjoinInfo) {
$gift['subjoinPage'][] = array(
'isHide' => ($number <= 5) ? false : true, // 第一页默认显示, 其余隐藏
'subjoinInfo' => $subjoinInfo,
);
}
// 页码总数
$gift['subjoinPageNum'] = ($number > 5) ? ceil($number / 5) : 1;
$result[] = $gift;
}
return $result;
}
/**
* 订单状态,按订单支付类型和订单状态
* @var array
*/
public static function getOrderStatus()
{
return array(
'1' => array(//在线支付
0 => '待付款',
1 => '已付款',
2 => '已付款',
3 => '已付款',
4 => '已发货',
5 => '已发货',
6 => '交易成功'
),
'2' => array(//货到付款
0 => '备货中',
1 => '已付款',
2 => '已付款',
3 => '已付款',
4 => '已发货',
5 => '已发货',
6 => '交易成功'
),
'3' => array(//现金支付
0 => '待付款',
1 => '已付款',
2 => '已付款',
3 => '已付款',
4 => '已发货',
5 => '已发货',
6 => '交易成功'
),
'4' => array(//抵消支付
0 => '待付款',
1 => '已付款',
2 => '已付款',
3 => '已付款',
4 => '已发货',
5 => '已发货',
6 => '交易成功'
)
);
}
/**
* 获取会员的级别
*
* @param string $vipInfo
* @return int
*/
public static function getVipLevel($vipInfo)
{
$vipLevel = 0;
switch ($vipInfo) {
case '普通会员':
$vipLevel = 0;
break;
case '银卡会员':
$vipLevel = 1;
break;
case '金卡会员':
$vipLevel = 2;
break;
case '白金会员':
$vipLevel = 3;
break;
}
return $vipLevel;
}
/**
* 同步用户的会话
*
* 转向老的PHP服务器上处理, 因购物车相关的操作会依赖SESSION
*
* @param int $uid 用户ID
* @param string $refer 访问来源
* @param string $callback 回调方法名
* @return string
*/
public static function syncUserSession($uid, $refer = '', $callback = 'call')
{
if(APPLICATION_ENV == 'production' || APPLICATION_ENV == 'preview') {
$domain = 'http://mapi.yohobuy.com';
} else {
$domain = 'http://m1.yohobuy.com';
}
return $domain.'/Passport/session/index?callback=' . $callback
. '&sign=' . md5(md5($uid . 'Js8Yn0!EwPM45-ws')) . '&uid=' . $uid . '&go=' . $refer;
}
/**
* 退出清除用户的会话
*
* 转向老的PHP服务器上处理, 因购物车相关的操作会依赖SESSION
*
* @param int $uid 用户ID
* @param string $refer 访问来源
* @param string $callback 回调方法名
* @return string
*/
public static function logoutSession($token, $refer = '', $callback = 'call')
{
if(APPLICATION_ENV == 'production' || APPLICATION_ENV == 'preview' ) {
$domain = 'http://mapi.yohobuy.com';
} else {
$domain = 'http://m1.yohobuy.com';
}
return $domain.'/Passport/session/logout?callback=' . $callback
. '&sign=' . md5(md5('Js8Yn0!EwPM45-ws')) . '&token=' . $token . '&go=' . $refer;
}
/**
* 根据skc获取商品链接
* @param unknown $productSkc
* @return string
*/
public static function getUrlBySkc($product_id, $goods_id, $cn_alphabet='')
{
if (empty($cn_alphabet)) {
$cn_alphabet = 'goods.html';
}
return 'http://item.yohobuy.com/product/pro_' . $product_id . '_' . $goods_id . '/' . $cn_alphabet. '.html';
}
/**
* 获取真实IP
*
* @return string
*/
public static function getClientIp()
{
$ip = '0.0.0.0';
if (isset($_SERVER['HTTP_CLIENT_IP']) && $_SERVER['HTTP_CLIENT_IP'] != '')
$ip = $_SERVER['HTTP_CLIENT_IP'];
elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] != '')
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] != '')
$ip = $_SERVER['REMOTE_ADDR'];
return $ip;
}
/**
* 组合国际手机号
* @param $area
* @param $mobile
* @return string
*/
public static function makeMobile($area, $mobile)
{
if(empty($area) || $area == 86){
return $mobile;
}
return $area.'-'.$mobile;
}
/**
* 判断是否是新用户 (未下单)
*
* @return bool (true:是, false:否)
*/
public static function isNewUser()
{
if (isset($_COOKIE['_isOldUser']) && $_COOKIE['_isOldUser'] == '4') {
return false;
}
return true;
}
}