Helpers.php
37.4 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
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
<?php
namespace Plugin;
use Configs\TicketsConfig;
use Plugin\DataProcess\CouponFloorProcess;
/**
* 辅助类
*/
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 = '//m.yohobuy.com';
break;
case 'guang': // 逛
$url = '//guang' . SUB_DOMAIN;
break;
case 'list': // 商品列表
$url = '//list' . SUB_DOMAIN;
break;
case 'search': // 搜索
$url = '//search' . SUB_DOMAIN;
break;
case 'index': // 默认
$url = SITE_MAIN;
break;
case '': // 相对地址
break;
default: // 其它子域名
$url = '//' . $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, 'http://' => '//')) . '/q/70';
return strtr($url, array('{width}' => $width, '{height}' => $height, '{mode}' => $mode, 'http://' => '//'));
}
/**
* 获得图片原图(去除宽高参数)
* @param $url
*/
public static function getOriginalImg($url)
{
if (!isset($url) || empty($url) || !is_string($url)) {
return;
}
$arr = explode('?', $url);
return $arr[0];
}
/**
* 获取过滤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));
if (strrpos($url, 'm.yohobuy.com') && !strrpos($url, 'sale.m.yohobuy.com') && !strrpos($url, 'cuxiao.m.yohobuy.com')
&& !strrpos($url, 'activity.m.yohobuy.com') && !strrpos($url, 'huodong.m.yohobuy.com') && strrpos($url, 'cdn.yoho.cn/myohobuy') && !strrpos($url, '/home/orders/pay')
) {
$url = strtr($url, array('http://' => '//'));
}
if (strrpos($url, 'feature.yoho.cn')) {
$url = self::transHttpsUrl($url);
}
$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';
}
}
/**
* 根据yh_channel判断频道
*
* @return string
*/
public static function getYhHhannel($yhChannel)
{
if (empty($yhChannel)) {
return '';
}
switch (intval($yhChannel)) {
case 1: // 男
return '1,3';
case 2: // 女
return '2,3';
case 3://潮童
return '3,3';
case 4://创意生活
return '4,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 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 'hot':
$result = ($order == 0) ? 's_n_desc' : 's_n_asc';
break;
case 'newest':
default:
$result = ($order == 1) ? 's_t_desc' : 's_t_asc';
break;
}
return $result;
}
/**
* 转换价格
*
* @param float|string $price 价格
* @param boolean $isSepcialZero 是否需要特殊的0,默认否
* @return float|string 转换之后的价格
*/
public static function transPrice($price, $isSepcialZero = false)
{
return (!empty($price) || $isSepcialZero) ? number_format($price, 2, '.', '') : 0;
}
/**
* 格式化商品信息
*
* @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 商品价格是否显示小数位,默认显示
* @param bool $coverChannel 频道 | 1,3男,2,3女
* @return array | false
*/
public static function formatProduct($productData, $showTags = true, $showNew = true, $showSale = true, $width = 290, $height = 388, $isApp = false, $showPoint = true, $coverChannel = '')
{
// 商品信息有问题,则不显示
if (!isset($productData['product_skn']) || !isset($productData['goods_list'][0])) {
return false;
}
$productData['sales_price'] = empty($productData['sales_price']) ? '' : $productData['sales_price'];
$productData['market_price'] = empty($productData['market_price']) ? '' : $productData['market_price'];
// 市场价和售价一样,则不显示市场价
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' && isset($productData['default_images'])) {
$productData['default_images'] = self::procProductImg($oneGoods, $productData['default_images'], $coverChannel);
$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';
}
// TODO student price
$result['studentPrice'] = sprintf("%.2f",$productData['sales_price']*0.9);
$result['is_soon_sold_out'] = ($productData['is_soon_sold_out'] === 'Y');
$url = self::url('/product/pro_' . $productData['product_id'] . '_'
. $productData['goods_list'][0]['goods_id']
. '/' . $productData['cn_alphabet'] . '.html');
$result['url'] = strtr($url, array('http://' => '//'));
// APP访问需要加附加的参数
// 备注:如果以后APP的接口太多,可以把这边参数提取出来,变成一个公共的方法来生成,便于以后管理维护
if ($isApp) {
$result['url'] .= '?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":' . $productData['product_skn'] . '}}';
}
/* tar add 160909 sale 根据价格处理 sale 标签*/
$isShowSaleTagDis = $productData['sales_price'] * 2 < $productData['market_price'];
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' && $isShowSaleTagDis; // 在售
$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
*
* 则优先从cover2 --》 cover1 -- 》 images_url
* 否则优先从cover1 --》 cover2 -- 》 images_url
*
* @param array $images
* @return string 商品图片
*/
public static function procProductImg($images, $defaultImages = '', $coverChannel = '')
{
$img = '';
$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 = empty($coverChannel) || $coverChannel === '1,2,3' ? self::getGenderByCookie() : $coverChannel;
switch ($gender) {
case '1,3':
$img = empty($cover1) ? $imgUrl : $cover1;
break;
case '2,3':
$img = empty($cover2) ? $imgUrl : $cover2;
break;
default:
$img = $imgUrl;
break;
}
if (empty($img)) {
$img = $defaultImages;
}
return $img;
}
/**
* 格式化资讯文章
*
* @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;
}
$result = array();
$result['id'] = $articleData['id'];
$result['showTags'] = $showTag;
$result['img'] = isset($articleData['src']) ? self::getImageUrl($articleData['src'], 640, 640) : '';
//逛详情页app跳转url处理 20160601
$result['url'] = $isApp ? self::getUrlSafe($articleData['url']) . '&openby:yohobuy={"action":"go.h5","params":{"param":{"id":"' . $articleData['id'] . '"},"shareparam":{"id":"' . $articleData['id'] . '"},"share":"/guang/api/v1/share/guang","id":' . $articleData['id'] . ',"type":1,"url":"' . 'http:' . self::url('/info/index', array(), 'guang') . '","islogin":"N"}}' : $articleData['url'];
//$result['url'] = $articleData['url']; // ? $articleData['url'] : self::url('/info/index', array('id' => $articleData['id']), 'guang');
if (strrpos($result['url'], 'feature.yoho.cn') || strrpos($result['url'], 'cdn.yoho.cn')) {
$result['url'] = self::transHttpsUrl($result['url']);
}
$result['title'] = $articleData['title'];
$result['text'] = $articleData['intro'];
$result['publishTime'] = $articleData['publish_time'];
$result['pageView'] = $articleData['views_num'];
// 收藏
if ($isApp) {
$result['collect'] = array();
$result['collect']['isCollected'] = isset($articleData['isFavor']) && $articleData['isFavor'] === 'Y';
// $originUrl = 'http:' . Helpers::url('/author/index',null,'guang') . $_SERVER["QUERY_STRING"]; // 跳转回的链接 https
$originUrl = Helpers::url('/author/index',null,'guang') . $_SERVER["QUERY_STRING"]; // 跳转回的链接
$collectUrl = 'javascript:;'; // 根据用户是否登录做处理的链接
if (empty($uid)) {
$playUrlEncode = strtr($originUrl, array('/' => '\\/'));
$collectUrl = $originUrl . '?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"' . $playUrlEncode . '","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"N"}}';
}
$result['collect']['url'] = $collectUrl;
} // 点赞
else {
$result['like'] = array();
$result['like']['count'] = $articleData['praise_num'];
$result['like']['isLiked'] = isset($articleData['isPraise']) && $articleData['isPraise'] === 'Y';
}
if ($isApp && isset($articleData['share']['url'])) {
// 分享链接
$result['share'] = $articleData['share']['url'] . '?openby:yohobuy={"action":"go.share","params":{"title":"' . $articleData['title'] . '","content":"' . $articleData['intro'] . '","url":"' . $articleData['share']['url'] . '","pic":"https:' . $result['img'] . '"}}';
}
// 判断是否显示作者信息
if ($showAuthor && !empty($articleData['author'])) {
if (!$isApp) {
$articleData['author']['url'] = Helpers::getFilterUrl($articleData['author']['url']);
}
//编辑人员 app跳转url处理 20160601
$isLogin = is_null($uid) ? 'N' : 'Y';
$articleData['author']['url'] = self::getUrlSafe($articleData['author']['url']) . '&openby:yohobuy={"action":"go.h5","params":{"param":{"id":"' . $articleData['author']['author_id'] . '"},"share":"","id":' . $articleData['author']['author_id'] . ',"type":0,"islogin":"' . $isLogin . '","url":"' . 'https:' . self::url('/author/index', array('uid' => $uid), 'guang') . '"}}&uid=' . $uid;
$result['author'] = $articleData['author'];
if (isset($result['author']['avatar'])) {
$result['author']['avatar'] = strtr($result['author']['avatar'], array('http://' => '//'));
}
}
// 模板中需要的标签标识
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;
case '19': // 专题
$result['isSpecialTopic'] = true;
break;
}
}
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 控制是否需要商品链接
* @param bool $tickets 门票
* @return array $arr 处理之后的订单商品数据
*/
public static function formatOrderGoods($orderGoods, &$count = 0, $haveLink = false, $tickets = 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']);
//门票
if ($tickets) {
//展览票不显示区域
if ($vo['product_skn'] == TicketsConfig::SINGLE_TICKETS_SKN) {
unset($arr[$key]['size']);
}
$arr[$key]['tickets'] = true;
}
}
return $arr;
}
/**
* 格式化购物车商品
*
* @param array $cartGoods 购物车商品列表
* @param boolean $isValid 是否是可用商品(非失效商品),默认是
* @param bool $isAdvanceCart 是否是预售购物车(和上市期有关)
* @return array 处理之后的购物车商品数据
*/
public static function formatCartGoods($cartGoods, $isAdvanceCart, $isValid = true)
{
$arr = array();
$oneGoods = array();
foreach ($cartGoods as $key => $value) {
$oneGoods = array();
$oneGoods['id'] = $value['product_sku'];
$oneGoods['skn'] = $value['product_skn'];
$oneGoods['name'] = $value['product_name'];
$oneGoods['thumb'] = !empty($value['goods_images']) ? Images::getImageUrl($value['goods_images'], 120, 160) : '';
$oneGoods['color'] = $value['color_name'];
$oneGoods['size'] = $value['size_name'];
$oneGoods['checked'] = $value['selected'] === 'Y';
$oneGoods['price'] = self::transPrice($value['last_vip_price']);
$oneGoods['isVipPrice'] = $value['sales_price'] !== $value['last_vip_price'] && $value['discount_tag'] === 'V';
$oneGoods['isStudents'] = $value['sales_price'] !== $value['last_vip_price'] && $value['discount_tag'] === 'S';
$oneGoods['count'] = $value['buy_number'];
$oneGoods['promotion_id'] = $value['promotion_id'];
$oneGoods['factoryColor'] = $value['factory_goods_name'] ? $value['factory_goods_name'] : $value['color_name'];
if ($isValid) {
// 库存不足
$oneGoods['lowStocks'] = ($value['buy_number'] > $value['storage_number']);
} else { // 失效商品
$oneGoods['inValid'] = true;
}
//gift=>是否赠品,advanceBuy=>是否加价购,soldOut=>失效商品;
if (!isset($value['goods_type'])) {
$oneGoods['inValid'] = true;
} elseif ($value['goods_type'] == 'gift' && !isset($value['isAdvanceBuy'])) {
$oneGoods['isGift'] = true;
$oneGoods['salesPrice'] = self::transPrice($value['sales_price']);
$oneGoods['price'] = self::transPrice($value['last_price']);
} elseif ($value['goods_type'] == 'price_gift') {
$oneGoods['showCheckbox'] = true;
$oneGoods['isAdvanceBuy'] = true;
$oneGoods['salesPrice'] = self::transPrice($value['sales_price']);
$oneGoods['price'] = self::transPrice($value['last_price']);
} else {
$oneGoods['showCheckbox'] = true;
}
// 上市期
if ($isAdvanceCart && !empty($value['expect_arrival_time'])) {
$oneGoods['appearDate'] = $value['expect_arrival_time'];
}
// 商品链接
$oneGoods['link'] = self::url('/product/show_' . $value['product_skn'] . '.html');
$arr[$key] = $oneGoods;
}
return $arr;
}
/**
* 格式化加价购和赠品商品
*
* @param array $advanceGoods 加价购商品列表
* @param int $count 计商品件数
* @return array $arr 处理之后的加价购商品数据
*/
public static function formatAdvanceGoods($advanceGoods, &$count = 0, $isGift = false)
{
$arr = array();
$gift = array();
$oneGoods = array();
$number = 0;
foreach ($advanceGoods as $value) {
$gift = array();
$gift['promotionId'] = $value['promotion_id'];
$gift['promotionTitle'] = $value['promotion_title'];
$number = 0;
foreach ($value['goods_list'] as $single) {
$oneGoods = array();
$oneGoods['id'] = $single['product_skn'];
$oneGoods['name'] = $single['product_name'];
$oneGoods['thumb'] = !empty($single['goods_images']) ? Images::getImageUrl($single['goods_images'], 120, 160) : '';
$oneGoods['price'] = self::transPrice($single['last_price']);
$oneGoods['marketPrice'] = $isGift ? '0.00' : self::transPrice($single['market_price']);
$oneGoods['count'] = $single['storage_number'];
$gift['goods'][] = $oneGoods;
$number++;
}
$arr[] = $gift;
// 计算加价购商品数目
//$count += count($value['goods_list']);
$count += $number;
}
return $arr;
}
/**
* 订单状态,按订单支付类型和订单状态
* @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')
{
$url = '';
switch (APPLICATION_ENV) {
case 'production':
$url = 'https://login.m.yohobuy.com'; //$url = 'http://mapi.yohobuy.com';
break;
case 'preview':
$url = 'https://login.m.yohobuy.com';
break;
case 'testing':
$url = 'https://login.m.yohobuy.com'; //http://m1.yohobuy.com 没有https时
break;
default:
$url = 'https://login.m.yohobuy.com';
break;
}
$url .= '/Passport/session/index?callback=' . $callback . '&sign=' . md5(md5($uid . 'Js8Yn0!EwPM45-ws')) . '&uid=' . $uid . '&go=' . $refer;
return $url;
}
/**
* 退出清除用户的会话
*
* 转向老的PHP服务器上处理, 因购物车相关的操作会依赖SESSION
*
* @param int $token 用户ID
* @param string $refer 访问来源
* @param string $callback 回调方法名
* @return string
*/
public static function logoutSession($token, $refer = '', $callback = 'call')
{
$url = '';
switch (APPLICATION_ENV) {
case 'production':
$url = 'http://mapi.yohobuy.com';
break;
case 'preview':
$url = 'http://mapi.yohobuy.com';
break;
case 'testing':
$url = 'http://m1.yohobuy.com';
break;
default:
$url = 'http://m1.yohobuy.com';
break;
}
$url .= '/Passport/session/logout?callback=' . $callback . '&sign=' . md5(md5('Js8Yn0!EwPM45-ws')) . '&token=' . $token . '&go=' . $refer;
return $url;
}
/**
* 根据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 '//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;
}
/**
* 按照数组中指定字段排序二维数组
*
* @param array &$array 需要排序的数组
* @param string $field 字段名称
* @param boolean $desc 时候降序排列,默认为false
* @param int $sortType 排序方式
*/
public static function sortArrByField(&$array, $field, $desc = false, $sortType = SORT_REGULAR)
{
$fieldArr = array();
foreach ($array as $k => $v) {
$fieldArr[$k] = isset($v[$field]) ? $v[$field] : '';
}
$sort = $desc == false ? SORT_ASC : SORT_DESC;
array_multisort($fieldArr, $sort, $array, $sortType);
}
/**
* 将首字符为//的url转换为http://
*
* @param string $url 需要转换的url
* @return mixed
*/
public static function transHttpsUrl($url)
{
return preg_replace('/^\/\//', 'http://', $url);
}
/**
* http和https转换成//
* @param type $url 地址
* @return type string
*/
public static function getUrlSafe($url)
{
return '//' . strtr($url, array('http://' => '', 'https://' => ''));
}
/**
* 除去数组中的空值和签名参数
* @param $para 签名参数组
* return 去掉空值与签名参数后的新签名参数组
*/
public static function paraFilter(&$para)
{
$para_filter = array();
foreach ($para as $key => $val) {
if ($key === "sign" || $key === "sign_type" || $val === "") {
continue;
}
$para_filter[$key] = $val;
}
return $para_filter;
}
/**
* 判断是否是微信客户端
* @return boolean
*/
public static function is_weixin()
{
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
return true;
}
return false;
}
}