Cart.php 71.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 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
<?php

namespace Shopping;

use LibModels\Web\Home\CartData;
use LibModels\Web\Home\UserData;
use LibModels\Web\Product\FavoriteData;
use LibModels\Web\Product\ItemData;
use WebPlugin\Helpers;
use WebPlugin\Cache;
use WebPlugin\Images;
use Configs\WebCacheConfig;
use Hood\Core\Security\AuthCode;
use WebPlugin\UdpLog;
use WebPlugin\Encryption;
use Configs\ChannelConfig;

/**
 * 购物车相关的模板数据模型
 *
 * @name CartModel
 * @package Shopping
 * @copyright yoho.inc
 * @version 1.0 (2016-2-17 15:54:56)
 * @author fei.hong <fei.hong@yoho.cn>
 */
class CartModel
{

    const ERROR_400_MESSAGE = '系统繁忙,请稍候再试!';

    /**
     * 获取我的购物车数据
     *
     * @param int $uid 当前登录用户ID
     * @param string $shoppingKey 购物车在浏览器的唯一识别码
     * @param mixed $cartDelList 删除的商品列表
     * @return array
     */
    public static function myCartData($uid, $shoppingKey, $cartDelList)
    {
        $result = array();

        // 存放分析用的数据
        $analysisData = array();

        do {
            $result['isEmpty'] = false;

            // 未登录
            if (!$uid) {
                $result['loginUrl'] = Helpers::url('/signin.html', array('refer' => Helpers::url('/shopping/cart')));
            }

            // 调用接口,获取购物车数据
            $cartData = CartData::cartData($uid, $shoppingKey);

            // 接口异常时,购物车置为空
            if (empty($cartData['data'])) {
                $result['isEmpty'] = true;
                $result['guangUrl'] = Helpers::url('', null, 'list');
                $result['viewOrderUrl'] = Helpers::url('/home/orders', array('t' => time()));
                UdpLog::info('【购物车】校验参数传递auth', 'uid:' . $uid . 'shoppingKey:' . $shoppingKey);
                break;
            }

            $ordinaryCartData = $cartData['data']['ordinary_cart_data'];
            $advanceCartData = $cartData['data']['advance_cart_data'];

            // 普通商品总数
            $ordinaryCount = intval($ordinaryCartData['shopping_cart_data']['goods_count']);
            // 预售商品总数
            $advanceCount = intval($advanceCartData['shopping_cart_data']['goods_count']);
            // 普通商品是否有售磬的
            $ordinarySoldOut = empty($ordinaryCartData['sold_out_goods_list']);
            // 预售商品是否有售磬的
            $advanceSoldOut = empty($advanceCartData['sold_out_goods_list']);
            // 普通商品是否有失效的
            $ordinaryOffShelves = empty($ordinaryCartData['off_shelves_goods_list']);
            // 预售商品是否有失效的
            $advanceOffShelves = empty($advanceCartData['off_shelves_goods_list']);

            /* 移除的商品列表 */
            if (!empty($cartDelList)) {
                $result['deleteShop'] = json_decode($cartDelList, true);
            }

            // 普通购物车和预售购物车都为空
            if ($ordinaryCount === 0 && $advanceCount === 0 && $ordinarySoldOut && $advanceSoldOut) {
                $result['isEmpty'] = true;
                $result['guangUrl'] = Helpers::url('', null, 'search');
                $result['viewOrderUrl'] = Helpers::url('/home/orders', array('t' => time()));
                break;
            }

            // 搜索链接(再逛逛)
            $result['searchUrl'] = Helpers::url('', null, 'search');

            /* 总计 */
            // 预售的
            if ($ordinaryCount === 0 && $advanceCount !== 0) {
                // 商品总价
                //$result['productAmmount'] = Helpers::transPrice($advanceCartData['shopping_cart_data']['order_amount']);
                $result['productAmmount'] = self::genProductAmount($advanceCartData['shopping_cart_data']['promotion_formula_list'], $advanceCartData['shopping_cart_data']['order_amount']);
                // 活动价
                $result['activeSale'] = Helpers::transPrice($advanceCartData['shopping_cart_data']['discount_amount']);
                // 商品金额总计
                $result['productAllA'] = Helpers::transPrice($advanceCartData['shopping_cart_data']['last_order_amount']);
                // 获赠有货币个数
                $result['getYoho'] = $advanceCartData['shopping_cart_data']['gain_yoho_coin'];
            } // 普通的
            elseif ($ordinaryCount !== 0 && $advanceCount === 0) {
                //$result['productAmmount'] = Helpers::transPrice($ordinaryCartData['shopping_cart_data']['order_amount']);
                $result['productAmmount'] = self::genProductAmount($ordinaryCartData['shopping_cart_data']['promotion_formula_list'], $ordinaryCartData['shopping_cart_data']['order_amount']);
                $result['activeSale'] = Helpers::transPrice($ordinaryCartData['shopping_cart_data']['discount_amount']);
                $result['productAllA'] = Helpers::transPrice($ordinaryCartData['shopping_cart_data']['last_order_amount']);
                $result['getYoho'] = $ordinaryCartData['shopping_cart_data']['gain_yoho_coin'];
            } // 所有的
            else {
                $result['productAmmount'] = Helpers::transPrice($ordinaryCartData['shopping_cart_data']['order_amount'] + $advanceCartData['shopping_cart_data']['order_amount']);
                $result['activeSale'] = Helpers::transPrice($ordinaryCartData['shopping_cart_data']['discount_amount'] + $advanceCartData['shopping_cart_data']['discount_amount']);
                $result['productAllA'] = Helpers::transPrice($ordinaryCartData['shopping_cart_data']['last_order_amount'] + $advanceCartData['shopping_cart_data']['last_order_amount']);
                $result['getYoho'] = $ordinaryCartData['shopping_cart_data']['gain_yoho_coin'] + $advanceCartData['shopping_cart_data']['gain_yoho_coin'];
            }

            /* 商品 */
            // 普通的
            if (!empty($ordinaryCartData['goods_list'])) {
                $result['commonSell']['cartProductNum'] = strval($ordinaryCount);
                $result['commonSell']['productItem'] = Helpers::formatCartGoods($ordinaryCartData['goods_list'], false, true, false, $analysisData);
            }
            // 预售的
            if (!empty($advanceCartData['goods_list'])) {
                $result['preSell']['cartProductNum'] = strval($advanceCount);
                $result['preSell']['productItem'] = Helpers::formatCartGoods($advanceCartData['goods_list'], true, true, false, $analysisData);
            }

            /* 已售磬失效 */
            // 普通的
            if (!$ordinarySoldOut) {
                //$result['commonSell']['cartProductNum'] += count($ordinaryCartData['sold_out_goods_list']);
                $result['commonSell']['productItem'] = isset($result['commonSell']['productItem']) ? self::appendProductItem($result['commonSell']['productItem'], Helpers::formatCartGoods($ordinaryCartData['sold_out_goods_list'], false, true), count($result['commonSell']['productItem'])) : Helpers::formatCartGoods($ordinaryCartData['sold_out_goods_list'], false, true, false, $analysisData);
            }
            // 预售的
            if (!$advanceSoldOut) {
                //$result['preSell']['cartProductNum'] += count($advanceCartData['sold_out_goods_list']);
                $result['preSell']['productItem'] = isset($result['preSell']['productItem']) ? self::appendProductItem($result['preSell']['productItem'], Helpers::formatCartGoods($advanceCartData['sold_out_goods_list'], true, true), count($result['preSell']['productItem'])) : Helpers::formatCartGoods($advanceCartData['sold_out_goods_list'], true, true, false, $analysisData);
            }

            /* 已失效的 */
            // 普通的
            if (!$ordinaryOffShelves) {
                //$result['commonSell']['cartProductNum'] += count($ordinaryCartData['off_shelves_goods_list']);
                $result['commonSell']['productItem'] = isset($result['commonSell']['productItem']) ? self::appendProductItem($result['commonSell']['productItem'], Helpers::formatCartGoods($ordinaryCartData['off_shelves_goods_list'], false, true, true), count($result['commonSell']['productItem'])) : Helpers::formatCartGoods($ordinaryCartData['off_shelves_goods_list'], false, true, true, $analysisData);
            }
            // 预售的
            if (!$advanceOffShelves) {
                //$result['preSell']['cartProductNum'] += count($advanceCartData['off_shelves_goods_list']);
                $result['preSell']['productItem'] = isset($result['preSell']['productItem']) ? self::appendProductItem($result['preSell']['productItem'], Helpers::formatCartGoods($advanceCartData['off_shelves_goods_list'], true, true, true), count($result['preSell']['productItem'])) : Helpers::formatCartGoods($advanceCartData['off_shelves_goods_list'], true, true, true, $analysisData);
            }

            /* 赠品 */
            // 预售的
            if (!empty($advanceCartData['gift_list'])) {
                $result['subjoinItem'] = Helpers::formatGiftPriceGoods($advanceCartData['gift_list'], true);
            }
            // 普通的
            if (!empty($ordinaryCartData['gift_list'])) {
                $result['subjoinItem'] = isset($result['subjoinItem']) ? self::appendProductItem($result['subjoinItem'], Helpers::formatGiftPriceGoods($ordinaryCartData['gift_list'], true), count($result['subjoinItem'])) : Helpers::formatGiftPriceGoods($ordinaryCartData['gift_list'], true);
            }

            /* 加价购 */
            // 预售的
            if (!empty($advanceCartData['price_gift'])) {
                $result['subjoinItem'] = isset($result['subjoinItem']) ? self::appendProductItem($result['subjoinItem'], Helpers::formatGiftPriceGoods($advanceCartData['price_gift'], false), count($result['subjoinItem'])) : Helpers::formatGiftPriceGoods($advanceCartData['price_gift'], false);
            }
            // 普通的
            if (!empty($ordinaryCartData['price_gift'])) {
                $result['subjoinItem'] = isset($result['subjoinItem']) ? self::appendProductItem($result['subjoinItem'], Helpers::formatGiftPriceGoods($ordinaryCartData['price_gift'], false), count($result['subjoinItem'])) : Helpers::formatGiftPriceGoods($ordinaryCartData['price_gift'], false);
            }

            /* 促销短语 */
            $result['salesPromotion'] = array();
            // 普通的
            if (!empty($ordinaryCartData['promotion_info'])) {
                $result['salesPromotion'] = self::buildPromotionData($ordinaryCartData['promotion_info']);
            } // 预售的
            elseif (!empty($advanceCartData['promotion_info'])) {
                //$result['salesPromotion'] = array_merge($result['salesPromotion'], self::buildPromotionData($advanceCartData['promotion_info']));
                $result['salesPromotion'] = self::buildPromotionData($advanceCartData['promotion_info']);
            }

            // 清空变量
            $advanceCartData = array();
            $ordinaryCartData = array();
            $cartData = array();

        } while (false);

        // 增加第三方分析用的数据
        $result['ids'] = empty($analysisData['ids']) ? '' : implode(',', $analysisData['ids']);
        $result['criteo'] = empty($analysisData['criteo']) ? "''" : json_encode($analysisData['criteo']);

        return $result;
    }

    /**
     * 加入购物车
     *
     * @param int $productSku 商品SKU
     * @param int $buyNumber 购买数量
     * @param int $goodsType 商品类型,0表示普通商品,1表示加价购商品
     * @param int $isEdit 是否是编辑商品SKU,0表示不是编辑
     * @param null|int $promotionId 促销id,默认null(加价购有关)
     * @param null|int $uid 用户UID,可以不传
     * @param string $shoppingKey 购物车在浏览器的唯一识别码,可以不传
     * @return array 加入购物车接口返回的数据
     */
    public static function addToCart($productSku, $buyNumber, $goodsType, $isEdit, $promotionId, $uid, $shoppingKey)
    {
        $result = array('code' => 400, 'message' => self::ERROR_400_MESSAGE);

        if (empty($productSku)) {
            return $result;
        }

        $addCart = CartData::addToCart($productSku, $buyNumber, $goodsType, $isEdit, $promotionId, $uid, $shoppingKey);
        if ($addCart && isset($addCart['code'])) {
            $result = $addCart;
        }

        if (empty($addCart) || (isset($addCart['code']) && $addCart['code'] !== 200) ) {
            UdpLog::info('【购物车】校验参数传递auth','productSku:'.$productSku.'buyNumber:'.$buyNumber.'goodsType:'.$goodsType,'isEdit:'.$isEdit.'promotionId:'.$promotionId.'uid:'.$uid.'shoppingKey:'.$shoppingKey);
        }

        return $result;
    }

    /**
     * 购物车商品选择与取消接口返回的数据处理
     *
     * @param int $uid 用户ID
     * @param string $skuList 商品sku列表json格式, 如{"744403":1}
     * @param string $shoppingKey 未登录用户唯一识别码
     * @param bool hasPromotion 标识是不是有promotion_id参数, 后端会去调用不同的接口
     * @return array 处理之后的数据的数据
     */
    public static function selectGoods($uid, $skuList, $shoppingKey, $hasPromotion = false)
    {
        $result = array('code' => 400, 'message' => self::ERROR_400_MESSAGE);

        do {
            if (empty($skuList)) {
                UdpLog::info('【购物车】校验参数传递auth', 'skuList:' . $skuList);
                break;
            }

            $select = CartData::selectGoods($uid, $skuList, $shoppingKey, $hasPromotion);
            if ($select && isset($select['code'])) {
                $result['code'] = $select['code'];
                $result['message'] = $select['message'];
            } else {
                UdpLog::info('【购物车】校验参数传递auth', 'uid:' . $uid . 'skuList:' . $skuList . 'shoppingKey:' . $shoppingKey . 'hasPromotion:' . $hasPromotion);
            }
        } while (false);

        return $result;
    }

    /**
     * 移出购物车
     *
     * @param int $uid 用户ID
     * @param string $skuList 商品sku列表json格式, 如{"744403":1}
     * @param string $count 要删除的数目
     * @param string $shoppingKey 未登录用户唯一识别码
     * @param bool hasPromotion 标识是不是有promotion_id参数, 后端会去调用不同的接口
     * @return array 接口返回的数据
     */
    public static function removeFromCart($uid, $skuList, $shoppingKey, $hasPromotion = false)
    {
        $result = array('code' => 400, 'message' => self::ERROR_400_MESSAGE);

        do {
            if (empty($skuList)) {
                UdpLog::info('【购物车】校验参数传递auth', 'skuList:' . $skuList);
                break;
            }

            $remove = CartData::removeFromCart($uid, $skuList, $shoppingKey, $hasPromotion);
            if ($remove && isset($remove['code'])) {
                $result['code'] = $remove['code'];
                $result['message'] = $remove['message'];
                if (isset($remove['data']['goods_count'])) {
                    $result['total_goods_num'] = $remove['data']['goods_count'];
                }
            } else {
                UdpLog::info('【购物车】校验参数传递auth', 'uid:' . $uid . 'skuList:' . $skuList . 'shoppingKey:' . $shoppingKey . 'hasPromotion:' . $hasPromotion);
            }
        } while (false);

        return $result;
    }

    /**
     * 移入收藏夹
     *
     * @param int $uid 用户ID
     * @param string $skuList 商品sku列表
     * @param bool hasPromotion 标识是不是有promotion_id参数, 后端会去调用不同的接口
     * @return array 接口返回的数据
     */
    public static function addToFav($uid, $skuList, $hasPromotion = false)
    {
        $result = array('code' => 400, 'message' => self::ERROR_400_MESSAGE);

        do {
            if (empty($uid)) {
                $result['code'] = 403;
                $result['message'] = '请先登录!';
                $result['data']['url'] = Helpers::url('/signin.html', array('refer' => Helpers::url('/shopping/cart')));
                break;
            }

            if (empty($skuList)) {
                UdpLog::info('【购物车】校验参数传递auth', 'skuList:' . $skuList);
                break;
            }

            $add = CartData::addToFav($uid, $skuList, $hasPromotion);
            if ($add && isset($add['code'])) {
                $result['code'] = $add['code'];
                $result['message'] = $add['message'];
            } else {
                UdpLog::info('【购物车】校验参数传递auth', 'uid:' . $uid . 'skuList:' . $skuList . 'hasPromotion:' . $hasPromotion);
            }
        } while (false);

        return $result;
    }

    /**
     * 修改购物车商品数量
     *
     * @param int $uid 用户ID
     * @param string $sku 商品SKU
     * @param int $increaseNum 增加的数目
     * @param int $decreaseNum 减少的数目
     * @param string $shoppingKey 未登录用户唯一识别码
     * @return array 接口返回的数据
     */
    public static function modifyProductNum($uid, $sku, $increaseNum, $decreaseNum, $shoppingKey)
    {
        $result = array('code' => 400, 'message' => self::ERROR_400_MESSAGE);

        do {
            if (empty($sku)) {
                UdpLog::info('【购物车】校验参数传递auth', 'sku:' . $sku);
                break;
            }

            $modify = CartData::modifyProductNum($uid, $sku, $increaseNum, $decreaseNum, $shoppingKey);
            if ($modify && isset($modify['code'])) {
                $result['code'] = $modify['code'];
                $result['message'] = $modify['message'];
            } else {
                UdpLog::info('【购物车】校验参数传递auth', 'uid:' . $uid . 'sku:' . $sku, 'increaseNum:' . $increaseNum . 'decreaseNum:' . $decreaseNum . 'shoppingKey:' . $shoppingKey);
            }
        } while (false);

        return $result;
    }

    /**
     * 获取凑单商品
     *
     * @param int $page 分页页码
     * @return array
     */
    public static function getTogetherProduct($page)
    {
        $result = array('code' => 200, 'message' => '', 'data' => array('header' => '凑单商品', 'hasPrev' => false, 'hasNext' => false, 'item' => array()));

        do {
            if (!is_numeric($page)) {
                break;
            }

            $together = CartData::togetherProduct($page);
            if (empty($together['data']['goods'])) {
                break;
            }

            $result['data']['hasNext'] = true;
            $result['data']['hasPrev'] = true;

            $build = array();
            foreach ($together['data']['goods'] as $value) {
                $build = array();
                $build['id'] = $value['id'];
                $build['skn'] = $value['product_skn'];
                $build['href'] = Helpers::getUrlSafe($value['url']);
                $build['title'] = $value['product_name'];
                $build['img'] = Helpers::getImageUrl($value['default_pic'], 100, 100);
                $build['alt'] = $value['product_name'];
                $build['price'] = $value['price']['sales_price'];
                if ($value['price']['sales_price'] !== $value['price']['market_price']) {
                    $build['marketPrice'] = $value['price']['market_price'];
                }

                $result['data']['item'][] = $build;
            }

            // 当数据量不足6个时,判定为没有下一页
            if ($page == 1 && !isset($result['data']['item'][5])) {
                $result['data']['hasPrev'] = false;
                $result['data']['hasNext'] = false;
            }
        } while (false);

        return $result;
    }

    /**
     * 获取为你优选商品 待处理
     *
     * @param $channel 频道
     * @param $uid 用户id
     * @param $udid 设备id
     * @param int $page 分页页码
     * @return array
     * @internal param $rec_pos
     * @internal param $limit
     */
    public static function getRecommendProduct($channel, $uid, $udid, $page)
    {
        $result = array('code' => 200, 'message' => '', 'data' => array('header' => '为您优选', 'hasPrev' => false, 'hasNext' => false, 'item' => array()));

        do {
            if (!is_numeric($page)) {
                break;
            }

            $channelNum = 1;
            switch ($channel) {
                case 'boys':
                    $channelNum = 1;
                    break;
                case 'girls':
                    $channelNum = 2;
                    break;
                case 'kids':
                    $channelNum = 3;
                    break;
                case 'lifestyle':
                    $channelNum = 4;
                    break;
                default:
                    break;
            }

            $together = UserData::newPreference($channelNum, $uid, $udid, '100003', 30);
            if (empty($together['data']['product_list'])) {
                break;
            }

            $result['data']['hasNext'] = true;
            $result['data']['hasPrev'] = true;

            $build = array();
            $begin = 0;
            $end = 0;
            switch ($page) {
                case 1:
                    $begin = 0;
                    $end = 5;
                    break;
                case 2:
                    $begin = 6;
                    $end = 11;
                    break;
                case 3:
                    $begin = 12;
                    $end = 17;
                    break;
                case 4:
                    $begin = 18;
                    $end = 23;
                    break;
                case 5:
                    $begin = 24;
                    $end = 30;
                    break;
                default:
                    $begin = 0;
                    $end = 5;
                    break;
            }
            foreach ($together['data']['product_list'] as $key => $value) {
                if ($key >= $begin && $key <= $end) {
                    $build = array();
                    $build['id'] = $value['product_id'];
                    $build['skn'] = $value['product_skn'];
                    $build['href'] = Helpers::getUrlBySkc($value['product_id'], $value['goods_list'][0]['product_skc'], $value['cn_alphabet']);;
                    $build['title'] = $value['product_name'];
                    $build['img'] = Helpers::getImageUrl($value['default_images'], 100, 100);
                    $build['alt'] = $value['product_name'];
                    $build['price'] = strstr(strval($value['sales_price']), '.') ? $value['sales_price'] : $value['sales_price'] . '.00';
                    if ($value['sales_price'] !== $value['market_price']) {
                        $build['marketPrice'] = strstr(strval($value['market_price']), '.') ? $value['market_price'] : $value['market_price'] . '.00';
                    }

                    $result['data']['item'][] = $build;
                }
            }

            // 当数据量不足6个时,判定为没有下一页
            if ($page == 1 && !isset($result['data']['item'][5])) {
                $result['data']['hasPrev'] = false;
                $result['data']['hasNext'] = false;
            }

            // 到达第六页,没有下一页
            if ($page == 5) {
                $result['data']['hasNext'] = false;
            }

            // 第-页,没有上一页
            if ($page == 1) {
                $result['data']['hasPrev'] = false;
            }

        } while (false);

        return $result;
    }

    /**
     * 获取最近浏览商品
     *
     * @param int $page 分页页码
     * @return array
     */
    public static function getBrowseProduct($uid, $udid, $page)
    {
        $result = array('code' => 200, 'message' => '', 'data' => array('header' => '最近浏览的商品', 'hasPrev' => false, 'hasNext' => false, 'item' => array()));

        do {
            if (!is_numeric($page) || empty($udid)) {
                break;
            }

            $browse = CartData::browseRecord($uid, $udid, $page, 6);
            if (empty($browse['data']['product_list'])) {
                UdpLog::info('【购物车】校验参数传递auth', 'uid:' . $uid . 'udid' . $udid . 'page' . $page);
                break;
            }


            $result['data']['hasPrev'] = false;
            $result['data']['hasNext'] = true;
            if (intval($page) > 1) {
                $result['data']['hasPrev'] = true;
                $result['data']['hasNext'] = false;
            }

            $build = array();
            foreach ($browse['data']['product_list'] as $value) {
                $build = array();
                $build['id'] = $value['product_id'];
                $build['skn'] = $value['product_skn'];
                $build['href'] = Helpers::url('/product/show_' . $value['product_id'] . '_' . $value['product_skn'] . '.html', null, 'item');
                $build['title'] = $value['product_name'];
                $build['img'] = Helpers::getImageUrl($value['image'], 100, 100);
                $build['alt'] = $value['product_name'];
                $build['price'] = Helpers::transPrice($value['sales_price']);
                if ($value['sales_price'] !== $value['market_price']) {
                    $build['marketPrice'] = Helpers::transPrice($value['market_price']);
                }

                $result['data']['item'][] = $build;
            }

            // 当数据量不足6个时,判定为没有下一页
            if (!isset($result['data']['item'][5])) {
                $result['data']['hasNext'] = false;
            }
        } while (false);

        return $result;
    }

    /**
     * 基于本地浏览器Cookie的指定浏览记录
     *
     * @param int $page 页码
     * @param int $limit 限制数
     * @return array
     */
    public static function getNamedBrowseProduct($page, $limit, $sknList)
    {
        $result = array('code' => 200, 'message' => '', 'data' => array('header' => '最近浏览的商品', 'hasPrev' => false, 'hasNext' => false, 'item' => array()));

        do {
            $page = intval($page);
            $offset = ($page - 1) * $limit;
            $hasNext = isset($sknList[$offset + $limit]);
            $sknList = array_slice($sknList, $offset, $limit);
            if (empty($sknList)) {
                break;
            }

            // 组装 skn => goodsId 对应关系
            $sknArr = array();
            foreach ($sknList as $value) {
                $value = explode('-', $value);
                if (isset($value[1])) {
                    $sknArr[$value[0]] = $value[1];
                }
            }

            if (array() === $sknArr) {
                break;
            }

            // 通过SKN列表搜索调用商品信息
            $browse = CartData::browseRecordFromSearch(implode(' ', array_keys($sknArr)));
            if (empty($browse['data']['product_list'])) {
                break;
            }

            $result['data']['hasPrev'] = false;
            $result['data']['hasNext'] = $hasNext;
            if ($page > 1) {
                $result['data']['hasPrev'] = true;
            }

            // 按照浏览记录顺序返回数据
            $build = array();
            foreach ($sknArr as $skn => $goodsId) {
                foreach ($browse['data']['product_list'] as $value) {
                    if ($skn != $value['product_skn'] || empty($value['goods_list'])) {
                        continue;
                    }

                    $build = array();
                    $build['id'] = $value['product_id'];
                    $build['title'] = $value['product_name'];
                    $build['img'] = empty($value['default_images']) ? Images::getSourceUrl($value['goods_list'][0]['images_url'], 'goodsimg') : Images::getSourceUrl($value['default_images'], 'goodsimg');
                    $build['alt'] = $value['product_name'];
                    $build['price'] = Helpers::transPrice($value['sales_price']);
                    if ($value['sales_price'] !== $value['market_price']) {
                        $build['marketPrice'] = Helpers::transPrice($value['market_price']);
                    }
                    foreach ($value['goods_list'] as $goods) {
                        if ($goodsId != $goods['goods_id']) {
                            continue;
                        }
                        if (!empty($goods['images_url'])) {
                            $build['img'] = Images::getSourceUrl($goods['images_url'], 'goodsimg');
                        }
                        break;
                    }
                    $build['img'] .= '?imageMogr2/thumbnail/100x100/extent/100x100/background/d2hpdGU=/position/center/quality/90';
                    $build['href'] = Helpers::url('/product/pro_' . $value['product_id'] . '_' . $goodsId . '/' . $value['cn_alphabet'] . '.html', null, 'item');

                    $result['data']['item'][] = $build;

                    break;
                }
            }

            // 当数据量不足6个时,判定为没有下一页
            if (!isset($result['data']['item'][5])) {
                $result['data']['hasNext'] = false;
            }
        } while (false);

        return $result;
    }

    /**
     * 调用购物车订单确认接口返回的数据处理
     *
     * @param int $uid 用户ID
     * @param string $cartType 购物车类型,ordinary表示普通购物车
     * @param bool $isAdvanceCart 是否是预售商品列表
     * @return array 接口返回的数据
     */
    public static function cartPay($uid, $cartType, $isAdvanceCart)
    {
        $result = [];
        /* 调接口订单确认接口 */
        $pay = CartData::cartPay($uid, $cartType);
        $result = self::filterCartPay($pay, $cartType, $isAdvanceCart, $uid);

        //有货币
        $result['yohoCoinCompute'] = self::yohoCoinCompute($pay['data']);

        return $result;
    }
    /**
     * 处理购物车返回
     * @param type $pay 获取商品数据
     * @param string $cartType 购物车类型,ordinary表示普通购物车
     * @param bool $isAdvanceCart 是否是预售商品列表
     * @return type []
     */
    private static function filterCartPay($pay, $cartType, $isAdvanceCart, $uid)
    {
        // 存放分析用的数据
        $analysisData = array();
        $result = array();

        do {
            //获取用户手机号码用于发票接收人
            $userInfo = UserData::getUserInfo($uid);

            if (!$pay || empty($pay['data']['goods_list'])) {
                break;
            }

            /* 支付方式及送货时间 */
            if (!empty($pay['data']['payment_way'])) {
                $isDefault = false;
                foreach ($pay['data']['payment_way'] as $value) {
                    $isDefault = $value['default'] === 'Y';
                    // 在线支付
                    if ($value['payment_type'] == 1 && $value['is_support'] === 'Y') {
                        $result['onlinePay']['checked'] = $isDefault ? true : false;
                        $result['onlinePay']['paymentId'] = $value['payment_id'];
                    } // 货到付款
                    elseif ($value['payment_type'] == 2 && $value['is_support'] === 'Y') {
                        $result['deliveryPay']['checked'] = $isDefault ? true : false;
                        $result['deliveryPay']['paymentId'] = $value['payment_id'];
                        $result['supportDeliveryPay'] = true;
                    }
                    // 默认
                    if ($isDefault) {
                        $result['defaultPayWay'] = $value['payment_type_name'];
                    }
                }
            }
            // tar add 1605061351 货到付款提示信息处理
            if (isset($pay['data']['payment_way'])) {
                foreach ($pay['data']['payment_way'] as $item) {
                    if ($item['payment_type'] === 2) {
                        $result['paymentInCashInfo'] = $item['is_support_message'];
                    }
                }
            }
            // 未设置时,设置默认
            if (!isset($result['defaultPayWay'])) {
                $result['onlinePay']['checked'] = true;
                $result['onlinePay']['paymentId'] = 15;
                $result['deliveryPay']['checked'] = false;
                $result['deliveryPay']['paymentId'] = 0;
                $result['defaultPayWay'] = '在线支付';
                $result['supportDeliveryPay'] = true;
            }

            if (!empty($pay['data']['delivery_time'])) {
                $build = array();
                foreach ($pay['data']['delivery_time'] as $value) {
                    $build = array();
                    $build['id'] = $value['delivery_time_id'];
                    $build['desc'] = $value['delivery_time_string'];
                    $build['checked'] = $value['default'] === 'Y';
                    if ($build['checked']) {
                        $result['defaultDelivery'] = $value['delivery_time_string'];
                    }
                    $result['delivery'][] = $build;
                }
            }

            // 默认
            if (!isset($result['defaultDelivery'])) {
                $result['defaultDelivery'] = '送货时间不限';
                if (isset($result['delivery'][0])) {
                    $result['delivery'][0]['checked'] = true;
                }
            }

            // 支付支持的平台
            $result['supportLine'] = array(
                array('src' => '//static.yohobuy.com/images/pay/icon/zhifubao.png'),
                array('src' => '//static.yohobuy.com/images/pay/icon/zaixianzhifu.png'),
                array('src' => '//static.yohobuy.com/images/pay/icon/weixinzhifu.png'),
                array('src' => '//static.yohobuy.com/images/pay/icon/wangyinzaixian.png'),
                array('src' => '//static.yohobuy.com/images/pay/icon/caifutong.png'),
                array('src' => '//static.yohobuy.com/images/pay/icon/shengfutong.png'),
                array('src' => '//static.yohobuy.com/images/pay/icon/tonglianzhifu.png'),
            );
            // 支付支持的银行
            $result['supportBank'] = array(
                array('src' => '//static.yohobuy.com/images/bankico/BOC.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/ICBC.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/CMB.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/CCB.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/ABC.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/SPDB.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/CIB.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/GDB.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/SDB.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/CMBC.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/COMM.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/CITIC.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/HZCBB2C.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/CEB.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/SHBANK.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/NBBANK.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/SZPAB.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/BJRCB.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/FDB.gif'),
                array('src' => '//static.yohobuy.com/images/bankico/PSBC.gif'),
            );

            /* 快递方式 */
            if (!empty($pay['data']['delivery_way'])) {
                $build = array();
                foreach ($pay['data']['delivery_way'] as $value) {
                    $build = array();
                    $build['id'] = $value['delivery_way_id'];
                    $build['name'] = $value['delivery_way_name'];
                    $build['value'] = $value['delivery_way_cost'] . '.00';
                    $build['checked'] = $value['default'] === 'Y';
                    if ($value['delivery_way_id'] == 2) {
                        $build['desc'] = '(仅支持顺丰可配送的地区)';
                    }
                    $result['carriageList'][] = $build;
                }
            }

            /* 发票内容 */
            if (!empty($pay['data']['invoices']['invoiceContentList'])) {
                foreach ($pay['data']['invoices']['invoiceContentList'] as $value) {
                    $result['piaoTypes'][] = array(
                        'id' => $value['invoices_type_id'],
                        'name' => $value['invoices_type_name']
                    );
                }
                $result['receiverMobile'] = isset($userInfo['data']['mobile']) && $userInfo['data']['mobile'] ? $userInfo['data']['mobile'] : '';
                $result['invoiceUrl'] = 'http://www.yohobuy.com/help?category_id=99';
            }

            /* 需购买的商品 */
            if (!empty($pay['data']['goods_list'])) {
                $result['orderProducts'] = Helpers::formatCartGoods($pay['data']['goods_list'], $isAdvanceCart, false, false, $analysisData);
            }

            /* 商品价格明细 */
            if (!empty($pay['data']['shopping_cart_data']['promotion_formula_list'])) {
                $build = array();
                $miniAmount = null;
                $vipAmount = null;
                foreach ($pay['data']['shopping_cart_data']['promotion_formula_list'] as $value) {
                    $build = array();
                    if ($value['promotion'] === '运费') {
                        $build['isExpress'] = true;
                    } elseif ($value['promotion'] === '商品金额') {
                        $value['promotion'] = '商品总价';
                        $value['promotion_amount'] = '+' . $value['promotion_amount'];
                        $miniAmount = strtr($value['promotion_amount'], array('¥' => '', '+' => '', '-' => ''));
                    } elseif ($value['promotion'] === '红包') {
                        continue;
                    } elseif ($value['promotion'] === 'VIP优惠') {
                        $vipAmount = strtr($value['promotion_amount'], array('¥' => '', '+' => '', '-' => ''));
                        continue;
                    }
                    $build['promotion'] = $value['promotion'] . ':';
                    $build['promotionAmount'] = strtr($value['promotion_amount'], array('+' => '+ ', '-' => '- '));
                    $result['promotionFormulaList'][] = $build;
                }
                // 商品总价 = 商品金额 - VIP价格
                if (null !== $vipAmount && null !== $miniAmount) {
                    $miniAmount = floatval($miniAmount) - floatval($vipAmount);
                    $result['promotionFormulaList'][0]['promotionAmount'] = '+ ¥' . Helpers::transPrice($miniAmount);
                }
            }

            /* 商品实付总金额 */
            if (!empty($pay['data']['shopping_cart_data'])) {
                $result['lastOrderAmount'] = Helpers::transPrice($pay['data']['shopping_cart_data']['last_order_amount']);
            }
            // 返还的 YOHO 币
            $result['totalYoho'] = $pay['data']['shopping_cart_data']['gain_yoho_coin'];

            // 红包总金额
            $result['redEnvelopes'] = empty($pay['data']['red_envelopes']) ? false : Helpers::transPrice($pay['data']['red_envelopes']);
            // 使用的红包金额
            $result['useRedEnvelopes'] = empty($pay['data']['use_red_envelopes']) ? false : Helpers::transPrice($pay['data']['use_red_envelopes']);
            // 购物车类型, 调接口需要的
            $result['cartType'] = $cartType;
            // 购物车链接
            $result['cartUrl'] = Helpers::url('/shopping/cart');
            // 是否要显示优惠券支付
            $result['showCouponPay'] = $isAdvanceCart ? false : true;
            // 是否是预售
            $result['isPreSell'] = $isAdvanceCart ? true : false;
            // 顺丰查看详情
            $result['sfUrl'] = Helpers::url('/help/index/logisticsarea', null, 'default');

            // 是否是新客访问, 控制显示新手引导
            $isNewUser = Helpers::isNewUser();
            // 新用户(未下单)且未设置收货地址的,提示引导
            if ($isNewUser && !empty($_COOKIE['_newAddress'])) {
                $isNewUser = false;
            }
            $result['isNewUser'] = $isNewUser ? true : false;
            //JIT拆单需求 package
            $result['packages'] = self::getPackageInfo($pay['data']['shopping_cart_data']);
        } while (false);

        return $result;
    }

    /**
     * JIT拆单-结算页
     * @param array $cartInfo
     * @param bool $isOrder 处理订单接口和购物车接口返回差异性
     * @return type
     */
    public static function getPackageInfo($cartInfo)
    {
        $packages = false;
        $isMulti = ($cartInfo['is_multi_package'] === 'Y'); //是否拆单
        if ($isMulti) {
            $packageList = $cartInfo['package_list'];
            foreach ($packageList as $pk => $pv) {
                $pIndex = $pk + 1;
                $packages[$pk]['title'] = ($pv['supplier_id'] == 0) ? '包裹' . $pIndex . ':总仓发货' : '包裹' . $pIndex . ':异地调拨'; //仓库
                $goodList = $pv['goods_list'];
                foreach ($goodList as $glk => $glv) {
                    $packages[$pk]['goodlist'][$glk]['src'] = Images::getImageUrl($glv['goods_images'], 90, 90);
                    $tagInfo = isset(ChannelConfig::$orderTagArr[$glv['goods_type']]) ? ChannelConfig::$orderTagArr[$glv['goods_type']] : '';
                    $packages[$pk]['goodlist'][$glk]['goodsType'] = !empty($tagInfo['name']) ? $tagInfo['name'] : false;
                    $packages[$pk]['goodlist'][$glk]['classname'] = !empty($tagInfo['classname']) ? $tagInfo['classname'] : false;
                    $packages[$pk]['goodlist'][$glk]['link'] = 'javascript:void(0)';
                }
                $packages[$pk]['fee'] = ($pv['shopping_cost'] == '0.00') ? false : $pv['shopping_cost'];
                $packages[$pk]['orign'] = $pv['shopping_orig_cost'];
                $packages[$pk]['count'] = $pv['shopping_cut_cost'];
            }
        }
        return $packages;
    }

    /**
     * 购物车结算--支付方式和配送方式选择以及是否使用有货币接口返回的数据处理
     *
     * @param int $uid 用户ID
     * @param string $cartType 购物车类型,ordinary表示普通购物车
     * @param int $deliveryWay 配送方式,1表示普通快递,2表示顺丰速运
     * @param int $paymentType 支付方式,1表示在线支付,2表示货到付款
     * @param string $couponCode 优惠券码
     * @param string $promotionCode 优惠码
     * @param mixed $yohoCoin 使用的有货币数量
     * @param int $redEnvelopes 红包
     * @return array 接口返回的数据
     */
    public static function orderCompute($uid, $cartType, $deliveryWay, $paymentType, $couponCode, $promotionCode, $yohoCoin, $redEnvelopes)
    {
        $result = array('code' => 400, 'message' => self::ERROR_400_MESSAGE, 'data' => array());

        // 有货币稀释,换成钱
        if (!empty($yohoCoin)) {
            $yohoCoin = intval($yohoCoin) / 100;
        }
        $compute = CartData::orderCompute($uid, $cartType, $deliveryWay, $paymentType, $couponCode, $promotionCode, $yohoCoin, $redEnvelopes);
        if ($compute && isset($compute['code'])) {
            /* 商品价格明细 */
            if (!empty($compute['data']['promotion_formula_list'])) {
                $promotionFormulaList = array();
                $build = array();
                $vipAmount = null;
                $miniAmount = null;
                foreach ($compute['data']['promotion_formula_list'] as $value) {
                    $build = array();
                    if ($value['promotion'] === '运费') {
                        $build['isExpress'] = true;
                    } elseif ($value['promotion'] === '商品金额') {
                        $value['promotion'] = '商品总价';
                        $value['promotion_amount'] = '+' . $value['promotion_amount'];
                        $miniAmount = strtr($value['promotion_amount'], array('¥' => '', '+' => '', '-' => ''));
                    } elseif ($value['promotion'] === '红包') {
                        continue;
                    } elseif ($value['promotion'] === 'VIP优惠') {
                        $vipAmount = strtr($value['promotion_amount'], array('¥' => '', '+' => '', '-' => ''));
                        continue;
                    }
                    $build['promotion'] = $value['promotion'] . ':';
                    $build['promotion_amount'] = strtr($value['promotion_amount'], array('+' => '+ ', '-' => '- '));
                    $promotionFormulaList[] = $build;
                }
                // 商品总价 = 商品金额 - VIP价格
                if (null !== $vipAmount && null !== $miniAmount) {
                    $miniAmount = floatval($miniAmount) - floatval($vipAmount);
                    $promotionFormulaList[0]['promotion_amount'] = '+ ¥' . Helpers::transPrice($miniAmount);
                }
                $compute['data']['promotion_formula_list'] = $promotionFormulaList;
                //JIT拆单
                $compute['packages'] = self::getPackageInfo($compute['data']);
                //有货币
                $compute['data']['yohoCoinCompute'] = self::yohoCoinCompute($compute['data']);
            }
            $result = $compute;
        }

        return $result;
    }

    /**
     * 购物车结算--提交结算信息
     *
     * @param int $uid 用户ID
     * @param int $addressId 地址ID
     * @param string $cartType 购物车类型
     * @param int $deliveryTimeId 寄送时间ID
     * @param int $deliveryWayId 寄送方式ID
     * @param string $invoiceTitle 发票抬头
     * @param int $invoiceId 发票类型ID
     * @param int $paymentId 支付方式ID
     * @param int $paymentType 支付类型ID
     * @param string $remark 留言
     * @param string $couponCode 优惠券码
     * @param string $promotionCode 优惠码
     * @param mixed $yohoCoin 使用的有货币数量或为空
     * @param int $isPreContact 送货前是否联系
     * @param int $isPrintPrice 是否打印价格
     * @param int $redEnvelopes 红包
     * @return array 接口返回的数据
     */
    public static function orderSub($uid, $addressId, $cartType, $deliveryTimeId, $deliveryWayId, $invoiceType, $invoiceTitle, $invoiceContent, $receiverMobile, $paymentId, $paymentType, $remark, $couponCode, $promotionCode, $yohoCoin, $isPreContact, $isPrintPrice, $redEnvelopes, $isContinueBuy)
    {
        $result = array('code' => 400, 'message' => self::ERROR_400_MESSAGE);

        do {
            if (empty($addressId)) {
                UdpLog::info('【结算信息】配送地址参数校验', 'addressId为空');
                $result['code'] = 401;
                $result['message'] = '配送地址不能为空';
                break;
            }
            if (empty($deliveryTimeId)) {
                UdpLog::info('【结算信息】配送时间参数校验', 'deliveryTime为空');
                $result['code'] = 402;
                $result['message'] = '请选择配送时间';
                break;
            }
            if (empty($deliveryWayId)) {
                UdpLog::info('【结算信息】配送方式参数校验', 'deliveryWay为空');
                $result['code'] = 403;
                $result['message'] = '请选择配送方式';
                break;
            }

            // 有货币稀释,换成钱
            if (!empty($yohoCoin)) {
                $yohoCoin = intval($yohoCoin) / 100;
            }

            /* 判断是否是友盟过来的用户 */
            $userAgent = null;
            $unionKey = '';
            if (!empty($_COOKIE['_QYH_UNION'])) {
                // 新平台统一来源
                $unionKey = trim(Encryption::decrypt(urldecode($_COOKIE['_QYH_UNION'])));
                $extraIndex  = strrpos($unionKey, '}') + 1;
                $unionKey = $encryObject = substr($unionKey, 0, $extraIndex);
                $unionClient = json_decode($encryObject, true);
                if (!isset($unionClient['client_id']) || empty($unionClient['client_id'])) {
                    /* 解密客户端联盟信息(老逻辑) */
                    $unionKey = AuthCode::decode($_COOKIE['_QYH_UNION'], 'q_union_yohobuy');
                }
                /* 检查联盟参数是否有效 */
                $unionInfo = empty($unionKey) ? array() : json_decode($unionKey, true);
                /* 模拟APP的User-Agent */
                $userAgent = isset($unionInfo['client_id']) ? 'YOHO!Buy/3.8.2.259(Model/PC;Channel/' . $unionInfo['client_id'] . ';uid/' . $uid . ')' : null;
            }

            $orderSubRes = CartData::orderSub($uid, $addressId, $cartType, $deliveryTimeId, $deliveryWayId, $invoiceType, $invoiceTitle, $invoiceContent, $receiverMobile, $paymentId, $paymentType, $remark, $couponCode, $promotionCode, $yohoCoin, $isPreContact, $isPrintPrice, $unionKey, $userAgent, $redEnvelopes, $isContinueBuy);
            if ($orderSubRes && isset($orderSubRes['code'])) {
                $orderSubRes['data']['unionKey'] = $unionKey;
                $result = $orderSubRes;
            }
        } while (false);

        return $result;
    }

    /**
     * 用户的收货地址列表
     *
     * @param int $uid 用户ID
     * @return array
     */
    public static function userAddressList($uid)
    {
        $result = array();

        do {
            if (!$uid || !is_numeric($uid)) {
                break;
            }

            $addressList = UserData::addressData($uid);
            if (empty($addressList['data'])) {
                break;
            }

            $build = array();
            $phone = '';
            $mobile = '';
            foreach ($addressList['data'] as $value) {
                $phone = explode('-', strtr($value['phone'], array('null' => '')));
                $mobile = strtr($value['mobile'], array('null' => ''));
                $mobile = substr($mobile, 0, 3) . '****' . substr($mobile, 7);

                $build = array();
                $build['id'] = Encryption::encrypt($value['address_id']);
                $build['user'] = $value['consignee'];
                $build['address'] = $value['area'] . $value['address'] . ' ' . $value['zip_code'] . ' ' . $mobile . ' ' . $value['phone'];
                $build['checked'] = $value['is_default'] === 'Y';
                $build['areaCode'] = $value['area_code'];
                $build['addressDesc'] = $value['address'];
                $build['isDelivery'] = $value['is_delivery'];
                $build['isSupport'] = $value['is_support'];
                $build['zipCode'] = $value['zip_code'];
                $build['mobile'] = $mobile;
                $build['completeMobile'] = $value['mobile'];
                $build['phoneCode'] = isset($phone[1]) ? $phone[0] : '';
                $build['phoneNum'] = isset($phone[1]) ? $phone[1] : '';
                $build['phone'] = $value['phone'];
                $build['email'] = strtr($value['email'], array('null' => ''));

                $result['list'][] = $build;
            }
        } while (false);

        return $result;
    }

    /**
     * 设置默认的收货地址
     *
     * @param int $uid 用户ID
     * @param int $addressId 地址ID
     * @return array
     */
    public static function setDefaultAddress($uid, $addressId)
    {
        $result = array('code' => 400, 'data' => '', 'message' => '设为默认地址失败!');

        if ($uid && is_numeric($addressId) && $addressId) {
            $result = UserData::setDefaultAddress($uid, $addressId);
        }

        return $result;
    }

    /**
     * 删除收货地址
     *
     * @param int $uid 用户ID
     * @param int $addressId 地址ID
     * @return array
     */
    public static function delAddress($uid, $addressId)
    {
        $result = array('code' => 400, 'data' => '', 'message' => '删除失败!');

        if ($uid && is_numeric($addressId) && $addressId) {
            $result = UserData::deleteAddress($uid, $addressId);
        }

        return $result;
    }

    /**
     * 保存地址数据
     *
     * @param int $uid 用户ID
     * @param string $address 地址信息
     * @param int $areaCode 城市码
     * @param string $consignee 收货人
     * @param string $email 邮箱地址
     * @param int $id 地址唯一标识符id
     * @param string $mobile 手机号码
     * @param string $zipCode 邮编
     * @param string $phone 固定电话
     * @return array | mixed 处理之后的地址列表数据
     */
    public static function saveAddressData($uid, $address, $areaCode, $consignee, $email, $id, $mobile, $zipCode, $phoneCode, $phoneNum)
    {
        $result = array();

        // 参数验证
        if (empty($uid)) {
            $result['code'] = 400;
            $result['message'] = '请先登录';
        } else if (empty($address)) {
            $result['code'] = 401;
            $result['message'] = '请输入可用的地址信息';
        } else if (empty($areaCode)) {
            $result['code'] = 402;
            $result['message'] = '请先选择省市';
        } else if (empty($consignee)) {
            $result['code'] = 403;
            $result['message'] = '请输入收货人姓名';
        } else if (!empty($email) && !Helpers::verifyEmail($email)) {
            $result['code'] = 404;
            $result['message'] = '输入的电子邮件格式不正确';
        } else if (!empty($mobile) && !Helpers::verifyMobile($mobile)) {
            $result['code'] = 404;
            $result['message'] = '输入的手机号码格式不正确';
        } else if (!empty($phoneCode) && !is_numeric($phoneCode)) {
            $result['code'] = 405;
            $result['message'] = '输入的固定电话格式不正确';
        } else if (!empty($phoneNum) && !is_numeric($phoneNum)) {
            $result['code'] = 406;
            $result['message'] = '输入的固定电话格式不正确';
        } else if ((!empty($phoneNum) && empty($phoneCode)) || (!empty($phoneCode) && empty($phoneNum))) {
            $result['code'] = 407;
            $result['message'] = '输入的固定电话格式不正确';
        } else {
            $phone = '';
            if (!empty($phoneCode)) {
                $phone = $phoneCode . '-' . $phoneNum;
            }
            $result = array('code' => 408, 'message' => self::ERROR_400_MESSAGE);
            // 调用接口保存地址数据
            $address = UserData::saveAddressData($uid, $address, $areaCode, $consignee, $email, $id, $mobile, $zipCode, $phone);
            // 处理返回结果
            if (isset($address['code']) && $address['code'] == 200) {
                $result = $address;
                if ($result['data']['address_id']) {
                    $result['data']['address_id'] = Encryption::encrypt($result['data']['address_id']);
                    $result['data']['id'] = Encryption::encrypt($result['data']['id']);
                }
            }
        }

        return $result;
    }

    /**
     * 获取省市区列表数据
     *
     * @param int $id ID编号
     * @return array
     */
    public static function getAreaList($id)
    {
        $result = array();

        if (!is_numeric($id)) {
            return $result;
        }

        if (USE_CACHE) {
            $key = WebCacheConfig::KEY_WEB_ADDRESS_LIST_DATA . strval($id);
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get($key, 'master');
            if (!empty($result)) {
                return $result;
            }
        }

        // 调用接口获取地址列表数据
        $province = UserData::provinceData($id);
        // 处理地址数据
        if (!empty($province['data'])) {
            $result = $province['data'];
        }

        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
            if (empty($result)) {
                $result = Cache::get($key, 'slave');
            } // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            else {
                Cache::set($key, $result, 3600); // 缓存1小时
            }
        }

        return $result;
    }

    /**
     * 获取用户的优惠券列表
     *
     * @param int $uid 用户ID
     * @param int $valid 控制是否返回已失效的券, 为true时不返回, false时返回
     * @return array
     */
    public static function getCouponList($uid)
    {
        $result = array();

        do {
            if (!is_numeric($uid)) {
                break;
            }

            /* 调用接口, 获取优惠券 */
            $couponList = CartData::getListCoupon($uid);
            $build = array();

            //不可用优惠券
            if (isset($couponList['data']['unusable_coupons'])) {
                foreach ($couponList['data']['unusable_coupons'] as $value) {
                    $build['code'] = $value['coupon_code'];
                    $build['price'] = $value['coupon_value'];
                    $build['desc'] = $value['coupon_name'];
                    $build['valid'] = false;
                    $result[] = $build;
                }
            }
            //可用优惠券
            if (isset($couponList['data']['usable_coupons'])) {
                foreach ($couponList['data']['usable_coupons'] as $value) {
                    $build['code'] = $value['coupon_code'];
                    $build['price'] = $value['coupon_value'];
                    $build['desc'] = $value['coupon_name'];
                    $build['valid'] = true;
                    $result[] = $build;
                }
            }
            $couponList = array();
        } while (false);

        return $result;
    }

    /**
     * 检查用户是否收藏这一批商品
     *
     * @param int $uid 用户ID
     * @param array $skuList 商品SKU列表
     * @return array
     */
    public static function checkUserIsFav($uid, $skuList)
    {
        $result = array();

        do {
            $skuList = json_decode($skuList, true);
            if (empty($skuList)) {
                break;
            }

            if (!$uid || !is_numeric($uid)) {
                foreach ($skuList as $sku) {
                    $result[$sku] = false;
                }
                break;
            }

            $result = FavoriteData::getUidProductFavList($uid, $skuList);
        } while (false);

        return $result;
    }

    /**
     * 获取购物车总数
     *
     * @param int $uid 用户ID
     * @param string $shoppingKey 客户端购物标识
     * @return array
     */
    public static function getCartCount($uid, $shoppingKey)
    {
        return CartData::cartCount($uid, $shoppingKey);
    }

    /**
     * 追加商品
     *
     * @param array $source
     * @param array $data
     * @param int $index
     * @return array
     */
    private static function appendProductItem($source, $data, $index)
    {
        foreach ($data as $value) {
            $source[$index] = $value;
            ++$index;
        }

        return $source;
    }

    /**
     * 获取商品总价
     *
     * @param array $promotionList 短语信息
     * @param int $default 默认值
     * @return float
     */
    private static function genProductAmount($promotionList, $default)
    {
        $result = $default;
        // VIP价格
        $vipAmount = null;
        // 学生价
        $stuAmount = null;
        foreach ($promotionList as $value) {
            if ($value['promotion'] === 'VIP优惠') {
                $vipAmount = strtr($value['promotion_amount'], array('¥' => '', '-' => '', '+' => ''));
            } elseif ($value['promotion'] === '学生优惠') {
                $stuAmount = strtr($value['promotion_amount'], array('¥' => '', '-' => '', '+' => ''));
            }
        }
        if (null !== $vipAmount) {
            $result = floatval($default) - floatval($vipAmount);
        }
        if (null !== $stuAmount) {
            $result = floatval($default) - floatval($stuAmount);
        }
        $result = Helpers::transPrice($result);

        return $result;
    }

    /**
     * 构建促销短语数据
     *
     * @param array $promotionInfo 促销信息
     * @return array
     */
    private static function buildPromotionData($promotionInfo)
    {
        $result = array();

        $types = array(
            'Changeshippingfee' => '免运费',
            'FreeShippingCost' => '免运费',
            'Cashreduce' => '现金折扣',
            'Discount' => '商品打折',
            'Gift' => '买送礼品',
            'Needpaygift' => '买送礼品',
            'Givecoupon' => '买送优惠券',
            'Degressdiscount' => '商品打折',
            'Cheapestfree' => '买送礼品'
        );
        foreach ($promotionInfo as $value) {
            $result[] = array(
                'salesMessage' => $value['promotion_title'],
                'salesTitle' => isset($types[$value['promotion_type']]) ? $types[$value['promotion_type']] : '',
            );
        }

        return $result;
    }

    /**
     * 页面顶部购物车数据
     * @param int $uid
     * @param string $shoppingKey 客户端购物标识
     * @author sefon 2016-4-20 15:22:28
     * @return array
     */
    public static function shoppingCart($uid, $shoppingKey)
    {
        $result = $goods = array();
        $cartType = array('advance_cart_data', 'ordinary_cart_data');
        $cartData = CartData::cartData($uid, $shoppingKey);
        foreach ($cartType as $dataKey) {
            //购物车商品
            if (isset($cartData['data'][$dataKey]['goods_list']) && !empty($cartData['data'][$dataKey]['goods_list'])) {
                foreach ($cartData['data'][$dataKey]['goods_list'] as $val) {
                    $goods['product_url'] = Helpers::getUrlBySkc($val['product_id'], $val['goods_id'], $val['cn_alphabet']);
                    $goods['is_advance'] = $val['is_advance'];
                    $goods['default_img'] = Helpers::getImageUrl($val['goods_images'], 46, 62);
                    $goods['product_name'] = $val['product_name'];
                    $goods['factory_goods_name'] = $val['factory_goods_name'];
                    $goods['size_name'] = $val['size_name'];
                    $goods['show_price'] = $val['real_price'];
                    $goods['buy_number'] = $val['buy_number'];
                    $goods['goods_incart_id'] = $val['shopping_cart_id'];
                    $goods['product_sku'] = $val['product_sku'];
                    $goods['promotion_id'] = $val['promotion_id'];
                    $result['main_goods'][] = $goods;
                }
            }
            //下架购物车商品
            if (isset($cartData['data'][$dataKey]['off_shelves_goods_list']) && !empty($cartData['data'][$dataKey]['off_shelves_goods_list'])) {
                foreach ($cartData['data'][$dataKey]['off_shelves_goods_list'] as $val) {
                    $goods['product_url'] = Helpers::getUrlBySkc($val['product_id'], $val['goods_id'], $val['cn_alphabet']);
                    $goods['is_advance'] = $val['is_advance'];
                    $goods['default_img'] = Helpers::getImageUrl($val['goods_images'], 46, 62);
                    $goods['product_name'] = $val['product_name'];
                    $goods['color_name'] = $val['color_name'];
                    $goods['size_name'] = $val['size_name'];
                    $goods['show_price'] = $val['real_price'];
                    $goods['buy_number'] = $val['buy_number'];
                    $goods['goods_incart_id'] = $val['shopping_cart_id'];
                    $goods['product_sku'] = $val['product_sku'];
                    $goods['promotion_id'] = $val['promotion_id'];
                    $result['main_goods'][] = $goods;
                }
            }
            //售罄购物车商品
            if (isset($cartData['data'][$dataKey]['sold_out_goods_list']) && !empty($cartData['data'][$dataKey]['sold_out_goods_list'])) {
                foreach ($cartData['data'][$dataKey]['sold_out_goods_list'] as $val) {
                    $goods['product_url'] = Helpers::getUrlBySkc($val['product_id'], $val['goods_id'], $val['cn_alphabet']);
                    $goods['is_advance'] = $val['is_advance'];
                    $goods['default_img'] = Helpers::getImageUrl($val['goods_images'], 46, 62);
                    $goods['product_name'] = $val['product_name'];
                    $goods['color_name'] = $val['color_name'];
                    $goods['size_name'] = $val['size_name'];
                    $goods['show_price'] = $val['real_price'];
                    $goods['buy_number'] = $val['buy_number'];
                    $goods['goods_incart_id'] = $val['shopping_cart_id'];
                    $goods['product_sku'] = $val['product_sku'];
                    $goods['promotion_id'] = $val['promotion_id'];
                    $result['main_goods'][] = $goods;
                }
            }

            //活动
            if (isset($cartData['data'][$dataKey]['promotion_info']) && !empty($cartData['data'][$dataKey]['promotion_info'])) {
                foreach ($cartData['data'][$dataKey]['promotion_info'] as $key => $val) {
                    //包邮
                    if ($val['promotion_type'] == 'FreeShippingCost') {
                        preg_match('/\d+/', $val['promotion_title'], $arr);
                        $result['fit_free_shipping'] = $arr[0];
                        continue;
                    }
                    //打折
                    if (!isset($result['first_promotions'])) {
                        $result['has_promotion'] = true;
                        $result['has_first_promotion'] = true;
                        $result['first_promotions']['promotion_id'] = $val['promotion_id'];
                        $result['first_promotions']['promotion_title'] = $val['promotion_title'];
                    } else {
                        $result['has_other_promotion'] = true;
                        $result['other_promotions'][$key]['promotion_id'] = $val['promotion_id'];
                        $result['other_promotions'][$key]['promotion_title'] = $val['promotion_title'];
                    }
                }
            }
        }
        return $result;
    }

    /**
     * 立即购买
     * @param int $uid 用户ID
     * @param type int $productSku 产品sku
     * @param type int $buyNumber 购买数量,范围1-4
     * @param type int $yohoCoin 有货币
     * @return type []
     */
    public function addTicket($uid, $productSku, $buyNumber, $yohoCoin = 0)
    {
        $result = [];
        $result = CartData::addTicket($uid, $productSku, $buyNumber, $yohoCoin);
        //有货币
        if (isset($result['data']['shopping_cart_data'])) {
            $result['data']['yohoCoinCompute'] = self::yohoCoinCompute($result['data']['shopping_cart_data']);
        }
        return $result;
    }
    
    /**
     * 订单确认-处理添加商品
     * @param int $uid 用户ID
     * @param type int $productSku 产品sku
     * @param type int $buyNumber 购买数量,范围1-4
     * @param type int $yohoCoin 有货币
     * @return type
     */
    public function filterTicket($uid, $productSku, $buyNumber, $yohoCoin = 0)
    {
        $result = [];
        $cartType = 'advance';

        $data = self::addTicket($uid, $productSku, $buyNumber, $yohoCoin);
        $result = self::filterCartPay($data, $cartType, false);
        //有货币
        if (isset($data['data']['shopping_cart_data'])) {
            $result['yohoCoinCompute'] = self::yohoCoinCompute($data['data']['shopping_cart_data']);
        }

        return $result;
    }

    /**
     * 电子票下单
     * @param type int $uid 用户ID
     * @param type int $productSku 产品sku
     * @param type int $buyNumber 购买数量,范围1-4
     * @param type string $mobile 手机号码
     * @param type int $yohoCoin 有货币
     * @return type []
     */
    public function submitTicket($uid, $productSku, $buyNumber, $mobile, $yohoCoin = 0)
    {
        return CartData::submitTicket($uid, $productSku, $buyNumber, $mobile, $yohoCoin);
    }

    /**
    *有货币使用前端方案显示及是否可单击判断
    */
    public static function yohoCoinCompute($orderCompute) {
        $yohoCoinData = ['totalYohoCoinNum' => 0, 'yohoCoin' => 0, 'yohoCoinNum' =>0, 'useYohoCoin' => 0, 'yohoCoinClick' => 0, 'yohoCoinMsg' => ''];

        if (empty($orderCompute) || empty($orderCompute['yoho_coin_pay_rule'])) {
            return $yohoCoinData;
        }

        $yohoCoinData = [
            'totalYohoCoinNum' => isset($orderCompute['total_yoho_coin_num']) ? intval($orderCompute['total_yoho_coin_num']) : 0,
            'yohoCoin' => isset($orderCompute['yoho_coin']) ? Helpers::transPrice($orderCompute['yoho_coin']) : 0,
            'yohoCoinNum' => isset($orderCompute['yoho_coin']) ? $orderCompute['yoho_coin'] * 100 : 0,
            'useYohoCoin' => isset($orderCompute['use_yoho_coin']) ? Helpers::transPrice($orderCompute['use_yoho_coin']) : 0,
            'yohoCoinClick' => 0,
            'yohoCoinMsg' => '',
            'yoho_coin_pay_rule' => $orderCompute['yoho_coin_pay_rule']
        ];

        if ($yohoCoinData['totalYohoCoinNum'] < 100) {
            $yohoCoinData['yohoCoinMsg'] = "抱歉,您的有货币不足,有货币满{$orderCompute['yoho_coin_pay_rule']['num_limit']}个方可使用";
        } else if ($yohoCoinData['useYohoCoin'] > 0 || $yohoCoinData['yohoCoin'] > 0) {
            $yohoCoinData['yohoCoinMsg'] = '可抵¥' . ($yohoCoinData['useYohoCoin'] > 0 ? $yohoCoinData['useYohoCoin'] : $yohoCoinData['yohoCoin']);
            $yohoCoinData['yohoCoinClick'] = 1;
        } else {
            $yohoCoinData['yohoCoinMsg'] = "抱歉,您的订单实付款不满足有货币使用条件";
        }
        return $yohoCoinData;
    }
}