Cart.php 142 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 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027
<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: IT宇宙人 2015-08-10 $
 */

namespace app\mobile\controller;

use think\Db;
use think\Cookie;
use app\home\logic\UsersLogic;

class Cart extends MobileBase
{
    public $cartLogic; // 购物车逻辑操作类    
    public $user_id = 0;
    public $user = array();
    public  $redis =null;
    /**
     * 析构流函数
     */
    public function __construct()
    {
        parent::__construct();
        $this->cartLogic = new \app\home\logic\CartLogic();
        if (session('?user')) {
            $nuser=array(
                'cart3', 'cart3_nosub','cart6_nosub','cart6'
            );
            if (!in_array(ACTION_NAME, $nuser)) {

                $user = session('user');
                $user = M('users')->where("user_id", $user['user_id'])->find();
                session('user', $user);  //覆盖session 中的 user
                $this->user = $user;
                $this->user_id = $user['user_id'];
                $this->assign('user', $user); //存储用户信息
            }else{
                $user = session('user');
                $this->user = $user;
                $this->user_id = $user['user_id'];
                $this->assign('user', $user); //存储用户信息
            }

            $nologin = array(
                'getNum', 'addskill', 'cart2skill', 'del_address','cart2','ajaxAddCart',
                'get_area', 'edit_address', 'add_address', 'cart3', 'reg_user','cart3_nosub','cart6_nosub',
                'cart5','cart6'
            );


            if ($this->user_id == 0 && ACTION_NAME!='getNum') {
                $stoid=getMobileStoId();
                if($stoid==1){
                    $this->user=['user_id'=>982127];
                    $this->user_id=982127;
                }
                else
                   $this->error('请先登录', U('Mobile/User/login', array('stoid' => getMobileStoId(), 'oldurl' => urlencode(curHostURL() . $_SERVER['']))));
            }
            if (!in_array(ACTION_NAME, $nologin)) {
                if (($this->pm_erpid  && (!$user || !$user['erpvipid'] || !$user['erpvipno']) || ($this->pm_erpid && (!$user || !$user['mobile'])))) {
                    $this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId())));
                    exit;
                }
            }
        } else {
            $uid = Cookie::get('user_id');
            if ($uid) {
                $user = M('users')->where("user_id", $uid)->where("store_id", getMobileStoId())->find();
                session('user', $user);  //覆盖session 中的 user
                $this->user = $user;
                $this->user_id = $user['user_id'];


                $this->assign('user', $user); //存储用户信息
            } else {
                $stoid = getMobileStoId();
                if ($stoid == 1) {
                    $this->user = ['user_id' => 982127];
                    $this->user_id = 982127;
                }
            }
        }
        /*----
        $getylpres=getylapp_res(getMobileStoId(),'2');

        if ($getylpres)
        {
            $this->assign('ylpres',$getylpres);
        }-----*/
    }

    public function cart()
    {
        $t=time();
        //获取热卖商品
        $hot_goods = M('Goods')
            ->where("on_time<".$t." and (down_time>".$t." or down_time=0 or down_time='' or down_time is null)")
            ->where('is_hot=1 and is_on_sale=1 ' . getMobileStoWhere())->limit(20)->select();
        $this->assign('hot_goods', $hot_goods);

        return $this->fetch('cart', getMobileStoId());
    }

    /**
     * 将商品加入购物车
     */
    function addCart()
    {
        $goods_id = I("goods_id/d"); // 商品id
        $goods_num = I("goods_num/d");// 商品数量
        $goods_spec = I("goods_spec"); // 商品规格                
        $goods_spec = json_decode($goods_spec, true); //app 端 json 形式传输过来
        $unique_id = I("unique_id"); // 唯一id  类似于 pc 端的session id
        $user_id = I('user_id/d', 0); // 用户id
        if (empty($this->user['erpvipid']))
            return array('status' => -101, 'msg' => '购买商品必须先绑定线下会员', 'result' => '');

        $result = $this->cartLogic->addCart($goods_id, $goods_num, $goods_spec, $unique_id, $user_id); // 将商品加入购物车
        upload_ylp_log('商品加入购物车');
        exit(json_encode($result));
    }

    /**
     * ajax 将商品加入购物车
     */
    function ajaxAddCart()
    {
        $goods_id = I("goods_id/d"); // 商品id
        $goods_num = I("goods_num/d");// 商品数量
        $goods_spec = I("goods_spec/a", array()); // 商品规格
        $pick_up = I("pickup_id/d"); //获取门店id
        $to_catr = I("to_catr/d");//1:立即购买
        if($to_catr==3) $to_catr=1;
        $goods_list = I('goods_list/s');//搭配有值,其他的值是underfined

        $isinte=I('isinte/d',0);//积分商品普通购买
        $ispd=I('ispd/d',0);//拼团商品普通购买

        if ($this->user_id == 0) {
            return array('status' => -101, 'msg' => '非法操作', 'result' => '');
        }
        $result = $this->cartLogic
            ->addCart($goods_id, $goods_num, $goods_spec, $this->session_id, $this->user, $pick_up, $to_catr, $goods_list,$isinte,$ispd); // 将商品加入购物车
        upload_ylp_log('商品加入购物车');
        exit(json_encode($result));
    }


    /**
     * 秒杀ajax将商品加入购物车
     */
    public function addskill()
    {
        $goods_id = I("goods_id/d"); // 商品id
        $goods_num = I("goods_num/d");// 商品数量
        $goods_spec = I("goods_spec/a", array()); // 商品规格
        $pick_up = I("pickup_id/d"); //获取门店id
        $to_catr = I("to_catr/d");//1:立即购买
        if ($this->user_id == 0) {
            return array('status' => -101, 'msg' => '非法操作', 'result' => '');
        }

        $result = $this->cartLogic
            ->addCart($goods_id, $goods_num, $goods_spec, $this->session_id, $this->user, $pick_up, $to_catr); // 将商品加入购物车
        upload_ylp_log('商品加入购物车');
        exit(json_encode($result));
    }


    /*
     * 请求获取购物车列表
     */
    public function cartList()
    {
        $cart_form_data = input('cart_form_data'); // goods_num 购物车商品数量
        $cart_form_data = json_decode($cart_form_data, true); //app 端 json 形式传输过来

        $unique_id = I("unique_id"); // 唯一id  类似于 pc 端的session id
        $user_id = I("user_id/d"); // 用户id
        $where['session_id'] = $unique_id; // 默认按照 $unique_id 查询
        if ($user_id) {
            $where['user_id'] = $user_id;
        }
        $cartList = M('Cart')->where($where)->getField("id,goods_num,selected,goods_id");
        /*--要剔除下架商品,活动有过期的商品--*/
        foreach ($cartList as $k => $v) {
            $good = M('goods')
                ->where('goods_id', $v['goods_id'])
                ->where('is_on_sale=1')->find();
            if ($good) {
                if ($v['prom_type'] != 0) {
                    $prom = get_goods_promotion($v["goods_id"], $user_id);
                    if ($prom['is_end'] == 2 || $prom['is_end'] == 3 || $prom['is_end'] == 1) {
                        M('Cart')->where('goods_id', $v['goods_id'])->where('user_id', $user_id)->delete();
                        unset($cartList[$k]);
                    }
                }
            } else {
                M('Cart')->where('goods_id', $v['goods_id'])->where('user_id', $user_id)->delete();
                unset($cartList[$k]);
            }
        }

        if ($cart_form_data) {
            // 修改购物车数量 和勾选状态
            foreach ($cart_form_data as $key => $val) {
                $data['goods_num'] = $val['goodsNum'];
                $data['selected'] = $val['selected'];
                $cartID = $val['cartID'];
                if (($cartList[$cartID]['goods_num'] != $data['goods_num']) || ($cartList[$cartID]['selected'] != $data['selected']))
                    M('Cart')->where("id", $cartID)->save($data);
            }
            //$this->assign('select_all', $_POST['select_all']); // 全选框
        }

        $result = $this->cartLogic->cartList($this->user, $unique_id, 0);
        upload_ylp_log('购物车列表');
        exit(json_encode($result));
    }

    /**
     * 购物车第二步确定页面
     */
    public function cart2()
    {

        $killarr = null;
        if ($this->user_id == 0)
            $this->error('请先登录', U('Mobile/User/login', array('stoid' => getMobileStoId())));

        /*--不能重复的一张表--*/
        //$userd=M("users")->where('user_id',$this->user_id)->find();
        $userd = $this->user;

        $locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');
        $this->assign('u_money', number_format($userd['user_money'] - $userd['frozen_money'] - $locking_money, 2));


        //20180717   season(增加短信随机码校验)

        $storeconfig = M('store_config')->where('store_id', getMobileStoId())->field('reg_type,reg_info,reg_default,sms_send_type,switch_list')->find();
        $sms_send_type = json_decode($storeconfig['sms_send_type'], true);
        $this->assign('s_type', $sms_send_type['type'] ? $sms_send_type['type'] : 0);
        $this->assign('s_time_out', $sms_send_type['time_out'] ? $sms_send_type['time_out'] : 60);
        $this->assign('is_verifycode', $sms_send_type['is_verifycode'] ? $sms_send_type['is_verifycode'] : 0);

        $user_openid = $this->user['openid'];
        //$user_openid="ou608v4s8TmwUsGLtJKDvdPKat5w"; //测试用
        $this->assign("user_openid", $user_openid);

        $user_openidkey = md5($user_openid . "&" . getErpKey());
        $this->assign("user_openid", $user_openid);
        $this->assign("user_openidkey", $user_openidkey);
        //

        /*---获取地区---*/
        $region_list = get_region_list();
        $addlist = M('user_address')->where(['user_id' => $this->user_id])->select();
        foreach ($addlist as $kk => $vv) {
            $vv['provicename'] = $region_list[$vv['province']]['name'];
            $vv['cityname'] = $region_list[$vv['city']]['name'];
            $vv['districtname'] = $region_list[$vv['district']]['name'];
            $vv['twonname'] = $region_list[$vv['twon']]['name'];

            //$address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
            $addrstr = $vv['provicename'] . "-" . $vv['cityname'];
            if (!empty($vv['district']))
                $addrstr .= "-" . $vv['districtname'];
            if (!empty($vv['twon']))
                $addrstr = $addrstr . "-" . $vv['twonname'];
            $vv['addrstr'] = $addrstr;

            $addlist[$kk] = $vv;
        }
        $this->assign("addlist", $addlist);
        $address_id = I('address_id/d');
        if ($address_id) {
            foreach ($addlist as $kk => $vv) {
                if ($vv['address_id'] == $address_id) {
                    $address = $vv;
                }
            }
        } else {
            foreach ($addlist as $kk => $vv) {
                if ($vv['is_default'] == 1) {
                    $address = $vv;
                }
            }
        }

        $p = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
        $this->assign('province', $p);
        if (empty($address)) {
            //header("Location: ".U('Mobile/User/add_address',array('stoid'=>getMobileStoId(),'source'=>'cart2')));
            $this->assign('setadd', 1);
        } else {
            $this->assign('address', $address);
            $this->assign('setadd', 0);
        }

        /*--是否是购物车--*/
        $tocart = I("tocart/d", 0);
        $result = null;

        if ($tocart == 0) {
            if ($this->cartLogic->cart_count($this->user_id, 1) == 0)
                $this->error('你的购物车没有选中商品', 'Index/index', array('stoid' => getMobileStoId()));
            $result = $this->cartLogic->cartList_cart2($this->user, $this->session_id, 1, 1); //获取购物车商品
        } else {
            $count = M('Cart')->where(['user_id' => $this->user_id, 'state' => 1])->count();
            if ($count == 0)
                $this->redirect('Index/index', array('stoid' => getMobileStoId()));
            $result = $this->cartLogic->gobuyList_cart2($this->user, $this->session_id); //获取购物车商品
        }

        /*--购物车商品分组--*/
        $cart_goods = $result['cartList'];
        $expty = 0;

        foreach ($cart_goods as $kp => $vp) {
            $exp2 = 0;
            if ($vp['distr_type'] != $vp['bdistr_type']) {
                $exp2 = $vp['distr_type'] == 0 ? $vp['bdistr_type'] : $vp['distr_type'];
            } else {
                $exp2 = $vp['distr_type'];
            }
            $cart_goods[$kp]['expty'] = $exp2;
            if ($vp['is_gift'] == 0) {
                //判断最后的物流方式
                if ($expty != 0) {
                    if ($expty != $exp2) {
                        if ($exp2 == 2 || $expty == 2) {
                            $expty = 2;
                        } else {
                            $expty = 0;
                        }
                    }
                } else {
                    $expty = $exp2;
                }
            }
        }
        $this->assign("expty", $expty);

        $shippingList = M('store_shipping')->alias('a')->join("shipping b", "a.shipping_id=b.shipping_id")
            ->field('b.shipping_id,b.shipping_code,b.shipping_name,b.shipping_desc,b.shipping_logo')
            ->where("a.status=1 and a.store_id=" . getMobileStoId())->cache("shippingList_" . getMobileStoId(), TPSHOP_CACHE_TIME)
            ->select();//物流公司
        /*--
        if(I('cid') != ''){
            $cid = I('cid');
            $checkconpon = M('coupon')->field('id,name,money')->where("id = $cid")->find();    //要使用的优惠券
            $checkconpon['lid'] = I('lid');
        }--*/

        /*----------购物车商品分组---------*/
        $sql = null;
        if ($tocart == 0) {
            $sql = "select pickup_id,pickup_name from __PREFIX__pick_up where pickup_id in(select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and selected=1 and state=0 GROUP BY pick_id)";
        } else if ($tocart == 1) {
            $sql = "select pickup_id,pickup_name from __PREFIX__pick_up where pickup_id in(select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=1 GROUP BY pick_id)";
        } else {
            $sql = "select pickup_id,pickup_name from __PREFIX__pick_up where pickup_id in(select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=2 GROUP BY pick_id)";
        }

        //mlog('$tocart:'.$sql,'cart');
        $plist_cart = Db::query($sql);
        foreach ($plist_cart as $k => $v) {
            $isallskill = 1;
            $expty00 = 0;
            foreach ($cart_goods as $kk => $vv) {
                if ($vv['pick_id'] == $v['pickup_id']) {
                    $list[] = $vv;
                    $expty11 = $vv['expty'];
                    if ($expty00 == 0) {
                        $expty00 = $expty11;
                    }
                }
                if ($vv['prom_type'] != 1) {
                    if ($vv['prom_type'] == 6) {
                        $isallskill = 1;
                    }else  if($vv['prom_type'] == 2 && $isallskill==1){
                            $t = time();
                            $gb = M('group_buy')->where('store_id', getMobileStoId())
                                ->where('goods_id', $vv['goods_id'])
                                ->where('start_time<' . $t)
                                ->where('end_time>' . $t)->where('is_end', 0)
                                ->field('is_quan')
                                ->find();
                            if ($gb) {
                                if ($gb['is_quan'] ==1 ) $isallskill = 0;
                            }
                    }else{
                       $isallskill = 0;
                    }
                } else {
                    $t = time();
                    $fl = M('flash_sale')->where('store_id', getMobileStoId())->where('goods_id', $vv['goods_id'])
                        ->where('start_time<' . $t)->where('end_time>' . $t)->where('is_end', 0)
                        ->find();
                    if ($fl) {
                        $killarr[] = ['goods_id' => $vv['goods_id'], 'fid' => $fl['id'], 'num' => $vv['goods_num']];
                    }
                }
            }

            if ($killarr) {
                $this->assign("killarr", json_encode($killarr));
            }
            $plist_cart[$k]["goods"] = $list;
            $plist_cart[$k]["expty"] = $expty00;
            // 获取订单商品总价,$tocart=0为购物车 1为立即购买
            //$r = $this->cartLogic->my_calculate($list,$this->user_id,$tocart);
            $r = $this->cartLogic->my_calculate_cart2($list, $this->user_id);
            unset($list);
            // 找出这个用户的优惠券没过期的 并且订单金额达到condition 优惠券指定标准的
            $fzquan = M('frozen_quan')->where('user_id', $this->user_id)->field('CashRepNo')->select();
            $snow = date("Y-m-d", time());

            $is_pt = I("is_pt");
            //--如果所有商品不全是秒杀且没有优惠商品--
            if ($isallskill == 0 && !$is_pt) {
                //如果是注册的会员
                $qlistall = "";
                if ($this->user['erpvipid']) {
                    $tk = tpCache('shop_info.api_token', getMobileStoId());
                    //$user = M("users")->where("user_id",$this->user_id)->field("erpvipid")->find();
                    $where = "VIPId='" . $this->user['erpvipid'] . "'";
                    $where .= " and isnull(useObjectID,'')='' and IsUse=0 and (ValidDate is null or ValidDate='' or ValidDate>='" . $snow . "')";
                    $where .= " and (BuySum<=" . $r." or BuySum=0 or BuySum is null)";
                    $where .= " and Sum<=" . $r;
                    $where .= " and (BeginDate='' or BeginDate is null or BeginDate<='" . $snow . "')";
                    $order['Sum'] = 'desc';
                    //$rs=getApiData("wxderp.cashreplace.list.get",$tk["api_token"],null,$where,1,1000,$order);
                    $rs = getApiData("wxderp.cashreplace.list.get", $tk, null, $where, 1, 1000, $order);

                    $qlistall = json_decode($rs, true);
                    if (!empty($qlistall)) {
                        if ($qlistall['data']) {
                            foreach ($qlistall['data'] as $k1 => $v1) {
                                $qlistall['data'][$k1]['Sum'] = number_format(empty($v1['Sum']) ? 0 : $v1['Sum'], 2);
                                $qlistall['data'][$k1]['BuySum'] = number_format(empty($v1['BuySum']) ? 0 : $v1['BuySum'], 2);
                                $qlistall['data'][$k1]['BillDate'] = date('Y-m-d', strtotime($v1['BeginDate']));
                                if (empty($v1['ValidDate'])) {
                                    $qlistall['data'][$k1]['ValidDate'] = "不限";
                                } else {
                                    $qlistall['data'][$k1]['ValidDate'] = date('Y-m-d', strtotime($v1['ValidDate']));
                                }
                                //冻结的券不能使用
                                foreach ($fzquan as $ku => $vu) {
                                    if ($qlistall['data'][$k1]['CashRepNo'] == $vu['CashRepNo']) {
                                        unset($qlistall['data'][$k1]);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    //$couponList = DB::query($sql);
                    $plist_cart[$k]["couponList"] = $qlistall['data'];
                    unset($qlistall);
                }
                //通过缓存拿tk
                $ERPId = $this->pm_erpid;
                //如果是erp用户
                if (empty($ERPId)) {
                    //非erp用户
                    $ti = time();
                    $qlistall = M('coupon_list')->where('store_id', getMobileStoId())
                        ->where('uid', $this->user_id)->where('is_user', 0)
                        ->where('begintime<' . $ti . ' and validtime>' . $ti)
                        ->where('buysum<=' . $r . ' and sum<=' . $r)
                        ->select();

                    if (!empty($qlistall)) {
                        foreach ($qlistall as $k1 => $v1) {
                            $qlistall[$k1]['CashRepNo']=$qlistall[$k1]['code'];
                            $qlistall[$k1]['Sum'] = number_format(empty($v1['sum']) ? 0 : $v1['sum'], 2);
                            $qlistall[$k1]['BuySum'] = number_format(empty($v1['buysum']) ? 0 : $v1['buysum'], 2);
                            $qlistall[$k1]['BillDate'] = date('Y-m-d', $v1['begintime']);
                            if (empty($v1['validtime'])) {
                                $qlistall[$k1]['ValidDate'] = "不限";
                            } else {
                                $qlistall[$k1]['ValidDate'] = date('Y-m-d', $v1['validtime']);
                            }
                            //冻结的券不能使用
                            foreach ($fzquan as $ku => $vu) {
                                if ($qlistall[$k1]['code'] == $vu['CashRepNo']) {
                                    unset($qlistall[$k1]);
                                    break;
                                }
                            }
                        }
                    }
                    $plist_cart[$k]["couponList"] = $qlistall;
                    unset($qlistall);
                }
            }
        }


        $this->assign('slist', $plist_cart);
        $this->assign('shippingList', $shippingList); // 物流公司
        //$this->assign('cartList', $result['cartList']); // 购物车的商品
        $this->assign('total_price', $result['total_price']); // 总计
        upload_ylp_log('购物车已选商品显示');
        return $this->fetch('', getMobileStoId());
    }


    /*---------------------------------秒杀购物详情-------------------------------------------*/
    public function cart2skill()
    {
        $this->assign("skill", 1);
        if ($this->user_id == 0)
            $this->error('请先登录', U('Mobile/User/login', array('stoid' => getMobileStoId())));

        /*--不能重复的一张表--*/
        //$userd=M("users")->where('user_id',$this->user_id)->find();
        $userd = $this->user;
        /*--清理未支付订单--*/
        clear_flash_Order(getMobileStoId());


        $storeconfig = M('store_config')->where('store_id', getMobileStoId())->field('reg_type,reg_info,reg_default,sms_send_type,switch_list')->find();
        $sms_send_type = json_decode($storeconfig['sms_send_type'], true);
        $this->assign('s_type', $sms_send_type['type'] ? $sms_send_type['type'] : 0);
        $this->assign('s_time_out', $sms_send_type['time_out'] ? $sms_send_type['time_out'] : 60);
        $this->assign('is_verifycode', $sms_send_type['is_verifycode'] ? $sms_send_type['is_verifycode'] : 0);


        $locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');
        $this->assign('u_money', number_format($userd['user_money'] - $userd['frozen_money'] - $locking_money, 2));

        $user_openid = $this->user['openid'];
        //$user_openid="ou608v4s8TmwUsGLtJKDvdPKat5w"; //测试用
        $this->assign("user_openid", $user_openid);

        $user_openidkey = md5($user_openid . "&" . getErpKey());
        $this->assign("user_openid", $user_openid);
        $this->assign("user_openidkey", $user_openidkey);
        mlog($user_openid."|".$user_openidkey,"cart2skill/".getMobileStoId());
        /*---获取地区---*/
        $region_list = get_region_list();
        $addlist = M('user_address')->where(['user_id' => $this->user_id])->select();
        foreach ($addlist as $kk => $vv) {
            $vv['provicename'] = $region_list[$vv['province']]['name'];
            $vv['cityname'] = $region_list[$vv['city']]['name'];
            $vv['districtname'] = $region_list[$vv['district']]['name'];
            $vv['twonname'] = $region_list[$vv['twon']]['name'];
            //$address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
            $addrstr = $vv['provicename'] . "-" . $vv['cityname'];
            if (!empty($vv['district']))
                $addrstr .= "-" . $vv['districtname'];
            if (!empty($vv['twon']))
                $addrstr = $addrstr . "-" . $vv['twonname'];
            $vv['addrstr'] = $addrstr;
            $addlist[$kk] = $vv;
        }
        $this->assign("addlist", $addlist);
        $address_id = I('address_id/d');

        if ($address_id) {
            foreach ($addlist as $kk => $vv) {
                if ($vv['address_id'] == $address_id) {
                    $address = $vv;
                }
            }
        } else {
            foreach ($addlist as $kk => $vv) {
                if ($vv['is_default'] == 1) {
                    $address = $vv;
                }
            }
        }

        $p = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
        $this->assign('province', $p);
        if (empty($address)) {
            //header("Location: ".U('Mobile/User/add_address',array('stoid'=>getMobileStoId(),'source'=>'cart2')));
            $this->assign('setadd', 1);
        } else {
            $this->assign('address', $address);
            $this->assign('setadd', 0);
        }

        /*--是否是购物车--*/
        $tocart = I("tocart/d", 0);
        $result = null;

        $count = M('Cart')->where(['user_id' => $this->user_id, 'state' => 1])->count();
        if ($count == 0)
            $this->redirect('Index/index', array('stoid' => getMobileStoId()));
        $result = $this->cartLogic->gobuyList_cart2($this->user, $this->session_id); //获取购物车商品

        /*--购物车商品分组--*/
        $cart_goods = $result['cartList'];
        $expty = 0;
        $killarr=null;
        foreach ($cart_goods as $kp => $vp) {
            $exp2=0;
            if($vp['distr_type']!=$vp['bdistr_type']){
                $exp2=$vp['distr_type']==0?$vp['bdistr_type']:$vp['distr_type'];
            }else{
                $exp2=$vp['distr_type'];
            }
            $cart_goods[$kp]['expty']=$exp2;
            if ($vp['is_gift'] == 0) {
                //判断最后的物流方式
                if ($expty != 0) {
                    if ($expty != $exp2) {
                        if($exp2==2 || $expty==2 ){
                            $expty=2;
                        }else{
                            $expty=0;
                        }
                    }
                } else {
                    $expty =$exp2;
                }
            }

            $t=time();
            $fl=M('flash_sale')->where('store_id',getMobileStoId())->where('goods_id',$vp['goods_id'])
                ->where('start_time<'.$t)->where('end_time>'.$t)->where('is_end',0)
                ->find();
            if($fl) {
                $killarr[] = ['goods_id' => $vp['goods_id'], 'fid' =>$fl['id'],'num'=>$vp['goods_num']];
            }
        }
        $this->assign("expty", $expty);

        if($killarr)
          $this->assign("killarr", json_encode($killarr));

        $shippingList = M('store_shipping')->alias('a')->join("shipping b", "a.shipping_id=b.shipping_id")
            ->field('b.shipping_id,b.shipping_code,b.shipping_name,b.shipping_desc,b.shipping_logo')
            ->where("a.status=1 and a.store_id=" . getMobileStoId())->cache("shippingList_" . getMobileStoId(), TPSHOP_CACHE_TIME)
            ->select();//物流公司

        /*----------购物车商品分组---------*/
        $sql = null;
        $sql = "select pickup_id,pickup_name from __PREFIX__pick_up where pickup_id in(select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=1 GROUP BY pick_id)";

        //mlog('$tocart:'.$sql,'cart');
        $plist_cart = Db::query($sql);
        foreach ($plist_cart as $k => $v) {
            $expty00 = 0;
            foreach ($cart_goods as $kk => $vv) {
                 if ($vv['pick_id'] == $v['pickup_id']) {
                    $list[] = $vv;
                    $expty11=$vv['expty'];
                    if($expty00==0){
                        $expty00=$expty11;
                    }
                }
            }
            $plist_cart[$k]["goods"] = $list;
            $plist_cart[$k]["expty"]=$expty00;
            unset($list);
        }

        $this->assign('slist', $plist_cart);
        $this->assign('shippingList', $shippingList); // 物流公司
        $this->assign('total_price', $result['total_price']); // 总计
        upload_ylp_log('购物车已选商品显示');
        return $this->fetch('cart2', getMobileStoId());
    }

    /**
     * ajax 获取订单商品价格或者提交订单
     */
    public function cart3()
    {
        if(empty($this->redis)){
            $this->redis=new \Redis();
            $this->redis->connect(redisip, 6379);
        }

        $popvalue=null;
        $popvalue2=null;
        $stoid = I("stoid");
        //拼单有关
        $ispt=I('is_pt');
        $pdid=I('pdid');
        $team_qh=I('team_qh');
        $pdgoodsnum=I('pdgoodsnum/d',0);
        $no="";
        $notime=microtime_float();

        //记录秒杀数据
        $sd=null;
        if ($_REQUEST['act'] == 'submit_order') {

            $sd=I('da');
            if(!empty($sd)){
                $no='r'.microtime_float().rand(10000, 99990);
                $skillarray=json_decode($sd,true);
                foreach ($skillarray as $lk=>$lv){
                    //判断秒杀数量是否超限
                    $name='ms'.$lv['fid'].'-'.$stoid;
                    $num0=$lv['num'];
                    if($this->redis->lLen($name)<$num0){
                        return json(array('status' => -4, 'msg' => "秒杀商品已经抢光", 'result' => ''));
                    }
                    //秒杀队列控制
                    for($i=0;$i<$num0;$i++) {
                        $value = $this->redis->lPop($name);
                        $popvalue[]=$value;
                    }

                    //redis要加入set中,编号,类型,活动id,数量,时间,stoid
                    $key=$no.'-1-'.$lv['fid'].'-'.$num0.'-'.$notime;
                    $this->redis->sAdd('redis_set_'.$stoid,$key);
                }
            }
            //如果是拼单
            if($ispt){
                $no='r'.microtime_float().rand(10000, 99990);
                $name='pind'.$pdid.'-'.$stoid;
                if($this->redis->lLen($name)<$pdgoodsnum){
                    return json(array('status' => -4, 'msg' => "您晚了一步,商品已被抢光!", 'result' => ''));
                }
                //拼单队列控制
                for($i=0;$i<$pdgoodsnum;$i++) {
                    $value = $this->redis->lPop($name);
                    $popvalue[]=$value;
                }
                //如果,$ispt是会员团的时候,要判断团的缓存
                if($team_qh && $ispt=='2'){
                    $name2='pind'.$pdid.'-'.$team_qh.$stoid;
                    $value = $this->redis->lPop($name2);
                    if (empty($value)) {
                        foreach ($popvalue as $k=>$v)
                            $this->redis->lPush($name,$v);
                        return array('status' => -4, 'msg' => "手慢了,您参与的团已满团!", 'result' => '');
                    }
                    $popvalue2[]=$value;
                }

                //redis要加入set中,编号,类型,活动id,数量,时间,stoid
                $key=$no.'-6-'.$pdid.'-'.$pdgoodsnum.'-'.$notime.'-'.$team_qh;
                $this->redis->sAdd('redis_set_'.$stoid,$key);
            }
        }

        //return;
        /*--物流方式--*/
        $exptype = I("exptype/d");
        $state = I("state/d");
        if ($this->user_id == 0) {
            $this->backredis($no,$notime);
            exit(json_encode(array('status' => -100, 'msg' => "登录超时请重新登录!", 'result' => null))); // 返回结果状态
        }
        $address_id = I("address_id/d"); //  收货地址id
        $setadd = I("setadd/d");
        $is_skill = I("skill");  //是否是秒杀

        /*--是否是编辑地址--*/
        if ($setadd == 1 && $exptype == 2 && empty($address_id)) {
            $consignee = I("consignee");
            $mobile = I("mobile");
            $province = I("province");
            $city = I("city");
            $district = I("district");
            $twon = I("twon");
            $more_address = I("more_address");
            $address = I("address");
            $data = ['consignee' => $consignee,
                'mobile' => $mobile,
                'province' => $province,
                'city' => $city,
                'district' => $district,
                'twon' => $twon,
                'more_address' => $more_address,
                'address' => $address,
            ];

            $logic = new UsersLogic();
            $data = $logic->add_address($this->user_id, 0, $data);
            $address_id = $data['result'];
        }
        $shipping_code = I("shipping_code/a"); //  物流编号
        $invoice_title = I('invoice_title/a'); // 发票
        $exparr= I('exparr/a');//获取物流配送数组
        $couponTypeSelect = I("couponTypeSelect"); //  优惠券类型  1 下拉框选择优惠券 2 输入框输入优惠券代码
        $coupon_id = I("coupon_id/a"); //  优惠券id
        $couponCode = I("couponCode"); //  优惠券代码
        $user_note = I('user_note/a');   //买家留言
        $pay_points = I("pay_points", 0); //  使用积分
        $user_money = I("user_money", 0); //  使用余额

        $user_money = str_replace(',', '', $user_money);
        $user_money = doubleval($user_money);

        $exptype=0;
        foreach ($exparr as $ko=>$vo){
           if($vo==1){ $exptype=1; break;}
        }

        if($user_money<0) {
            $this->backredis($no,$notime);
            exit(json_encode(array('status' => -2, 'msg' => '你的余额不足', 'result' => null))); //返回结果状态
        }

        //if(!empty($user_money))
        //$user_money=number_format(empty($user_money)?0:$user_money,2);  //使用俩位
        /*--购物车商品提取--*/
        /*--
        $cart_goods = M('Cart')->alias('a')
                ->join("pick_up b","a.pick_id=b.pickup_id",'left')
                ->where(['a.user_id'=>$this->user_id,'a.selected'=>1])
                ->where($where)->getField("a.*,b.pickup_name,b.pickup_id");--*/
        /*---购物车和立即购买的区分---*/
        $result = null;
        if ($state == 0) {
            $result = $this->cartLogic->cartList($this->user, $this->session_id, 1, 1); //获取购物车商品
        } else {
            $result = $this->cartLogic->gobuyList($this->user, $this->session_id); //获取购物车商品
        }
        $cart_goods = $result['cartList'];

        if (empty($cart_goods)) {
            $this->backredis($no,$notime);
            exit(json_encode(array('status' => -2, 'msg' => '你的购物车没有选中商品', 'result' => null))); //返回结果状态
        }
        $address = M('UserAddress')->where("address_id", $address_id)->find();
        $allusrnum = $user_money;
        $locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');

        $userd = $this->user;
        $salerule = tpCache('basic.sales_rules', $stoid);
        $erpid = tpCache('shop_info.ERPId', $stoid);
        $userd['sales_rules'] = $salerule;
        $userd['ERPId'] = $erpid;

//            M("users")->alias('a')
//            ->join('store_config b',' a.store_id=b.store_id','left')
//            ->join('store c',' a.store_id=c.store_id','left')
//            ->field('a.user_money,a.frozen_money,b.sales_rules,c.ERPId')
//            ->where('a.user_id',$this->user_id)->find();
        //订单余额判断
        if (!empty($allusrnum)) {
            $ye = $userd['user_money'] - $userd['frozen_money'] - $locking_money;
            if ($ye . '' < $allusrnum) {
                $this->backredis($no,$notime);
                return array('status' => -4,
                    'msg' => "您的可用余额不足", 'result' => '');
            }
        }
//        $srule=M('store_config')->where('store_id',$stoid)->find();
//        $uerp=M('store')->where('store_id',$stoid)->find();

        $count_arr = null;
        /*----如果是线下库存,获取线下库存----*/
        if ($userd['sales_rules'] == 2) {
            if ($is_skill != 1) {
                $count_arr = getcountarr($cart_goods, $stoid);
            }
        }

        $err_txt = "";
        $prom_goods = array();

        //建立优惠促销数组
        foreach ($cart_goods as $k => $value) {
            if ($value['prom_type'] == 3 && $value['is_gift'] == 0 && $value['is_collocation'] == 0) {
                $prom_goods[$value['pick_id']][$value['prom_id']]['num'] += $value['goods_num'];
                $prom_goods[$value['pick_id']][$value['prom_id']]['price'] += $value['goods_num'] * $value['goods_price'];

            }
            if ($value['is_gift'] == 1) {
                $prom_goods[$value['pick_id']][$value['prom_id']]['key'] = $k;
            }
        }


        if (!empty($prom_goods)) {
            $prom_info = M('prom_goods')->where(['start_time' => ['lt', time()], 'end_time' => ['gt', time()], 'is_end' => 0])
                ->where('store_id',$stoid)
                ->getfield('id,name');
            foreach ($prom_goods as $kk => $vv) {
                foreach ($vv as $key => $val) {
                    if (!empty($prom_info[$key])) {
                        $prom = discount($val['price'], $key, $val['num'], $this->user_id);
                        if (empty($prom['goods_id']) && !empty($val['key'])) {
                            unset($cart_goods[$val['key']]);
                        }
                        $prom_goods[$kk][$key]['int'] = $prom['int'];
                        $prom_goods[$kk][$key]['coupon'] = $prom['coupon_id'];
                        $prom_goods[$kk][$key]['is_past'] = $prom['is_past'];
                        $prom_goods[$kk][$key]['gift_id'] = $prom['gift_id'];
                        $prom_goods[$kk][$key]['price'] = $prom['price'];
                        $prom_goods[$kk][$key]['all_price'] = $val['price'];
                    } else {
                        //return array('status' => -4, 'msg' => "优惠活动已经结束", 'result' => '');
                        unset($prom_goods[$kk][$key]);
                    }
                }
            }
        }

        /*--预出库库存--*/
        $gidarr=get_arr_column($cart_goods,'goods_id');
        $fgid0=implode(',',$gidarr);
        $strq = 'select pickup_id,goods_id,sum(out_qty) as sum0 from wxd_erp_yqty where goods_id in(' . $fgid0 . ') and store_id='.$stoid.' GROUP BY pickup_id,goods_id';
        $dd = Db::query($strq);
        $ddd = [];
        //---组装数组---
        if ($dd){
            foreach ($dd as $ku => $vu) {
                $ypkid=$vu['pickup_id'];
                $ypgid=$vu['goods_id'];
                $ddd[$ypkid.".".$ypgid]=$vu['sum0'];
            }
        }

        /*--库存判断和活动判断,商品的库存和活动不能过期--*/
        foreach ($cart_goods as $k => $value) {
            $ginfo = $value;
            /*--线下库存的序列key--*/
            $bkey = $value['pick_id'] . $value['goods_id'];
            //商品参与活动查看
            if ($value['prom_type'] > 0 && $value['prom_type'] != 3 && $value['is_collocation'] == 0 && $value['is_integral_normal']!=1 && $value['is_pd_normal']!=1 ) {
                /*--修改获取活动的函数--*/
                $prom = get_goods_promotion2($ginfo, null, $this->user_id);
                if ($prom['is_end'] == 1) {
                    $this->backredis($no,$notime);
                    return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此商品活动已经结束", 'result' => '');
                }
                if ($prom['is_end'] == 2) {
                    $this->backredis($no,$notime);
                    return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此商品已经售完", 'result' => '');
                }
                if ($prom['is_end'] == 3) {
                    $this->backredis($no,$notime);
                    return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此活动商品超出限购数量", 'result' => '');
                }
                if ($value['prom_type'] == 1 || $value['prom_type'] == 2 || $value['prom_type'] == 4) {
                    if ($value['goods_num'] > $prom['buy_limit']) {
                        $this->backredis($no,$notime);
                        return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此活动商品超出限购数量", 'result' => '');
                    }
                }
                if ($value['member_goods_price'] != $prom['price']) {
                    $err_txt .= "{$ginfo['goods_name']}商品价格已经变化,是否继续购买!<br>";
                }
                if ($value['prom_type'] == 1 || $value['prom_type'] == 2 || $value['prom_type'] == 4 || $value['prom_type'] == 6) {
                    if ($value['goods_num'] > $ginfo['store_count']) {
                        $this->backredis($no,$notime);
                        return array('status' => -4, 'msg' => "库存不足", 'result' => '');
                    }
                } else {
                    if ($userd['sales_rules'] == 1) {
                        if ($value['goods_num'] > $ginfo['store_count']) {
                            $this->backredis($no,$notime);
                            return array('status' => -4, 'msg' => "库存不足", 'result' => '');
                        }
                    } else if ($userd['sales_rules'] == 2) {
                        if (empty($count_arr)) {
                            return array('status' => -4, 'msg' => "库存获取失败", 'result' => '');
                        }
                        /*--预出库判断--*/
                        $ynum=$ddd[$bkey];
                        if($ynum) {
                            if ($value['goods_num']+$ynum > $count_arr[$bkey]) {
                                return array('status' => -4, 'msg' => "库存不足", 'result' => '');
                            }
                        }else{
                            if ($value['goods_num'] > $count_arr[$bkey]) {
                                return array('status' => -4, 'msg' => "库存不足", 'result' => '');
                            }
                        }
                    }
                }
            }else{
                if ($value['is_gift'] == 0) {
                    if ($userd['sales_rules'] == 1) {
                        if ($value['goods_num'] > $ginfo['store_count']) {
                            return array('status' => -4, 'msg' => "库存不足", 'result' => '');
                        }
                    } else if ($userd['sales_rules'] == 2) {
                        if (empty($count_arr)) {
                            return array('status' => -4, 'msg' => "库存获取失败", 'result' => '');
                        }
                        if ($value['goods_num'] > $count_arr[$bkey]) {
                            return array('status' => -4, 'msg' => "库存不足", 'result' => '');
                        }
                    }
                    /*--判断价格是否发生变化 并且不是搭配商品--*/
                    if ($value['member_goods_price'] != $ginfo['shop_price'] && $value['is_collocation'] == 0 && $value['is_integral_normal']!=1 && $value['is_pd_normal']!=1) {
                        $err_txt .= "{$ginfo['goods_name']} 商品价格已经变化<br>";
                    }
                }
            }
        }
        if ($err_txt != "") {
            $this->backredis($no,$notime);
            return array('status' => -1004, 'msg' => $err_txt, 'result' => '');
        }

        /*--购物车商品分组,生成不同订单--*/
        $plist = null;
        if ($state == 0) {
            $sql = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and selected=1 and state=0 GROUP BY pick_id";
            $plist = Db::query($sql);
            foreach ($plist as $k => $v) {
                $count = array();
                foreach ($cart_goods as $kk => $vv) {
                    if ($v['pick_id'] == $vv['pickup_id']) {
                        $list[] = $vv;
                    }
                    if (!empty($prom_goods[$v['pick_id']]) && $vv['prom_type'] == 3 && $count[$vv['prom_id']] == 0) {
                        $plist[$k]['order']['int'] += $prom_goods[$v['pick_id']][$vv['prom_id']]['int'];
                        $plist[$k]['order']['coupon'][] = $prom_goods[$v['pick_id']][$vv['prom_id']]['coupon'];
                        $plist[$k]['order'][$vv['prom_id']]['gift_id'] = $prom_goods[$vv['pick_id']][$vv['prom_id']]['gift_id'];
                        $plist[$k]['discount'] = $prom_goods[$v['pick_id']];
                        $count[$vv['prom_id']] += 1;
                    }
                }
                $plist[$k]["goods"] = $list;
                unset($list);
            }
        } else {
            $sql1 = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=1 GROUP BY pick_id";
            $plist = Db::query($sql1);
            foreach ($plist as $k => $v) {
                $count = array();
                foreach ($cart_goods as $kk => $vv) {
                    if ($v['pick_id'] == $vv['pickup_id']) {
                        $list[] = $vv;
                    }
                    if (!empty($prom_goods[$v['pick_id']]) && $vv['prom_type'] == 3 && $count[$vv['prom_id']] == 0) {
                        $plist[$k]['order']['int'] += $prom_goods[$v['pick_id']][$vv['prom_id']]['int'];
                        $plist[$k]['order']['coupon'][] = $prom_goods[$v['pick_id']][$vv['prom_id']]['coupon'];
                        $plist[$k]['order'][$vv['prom_id']]['gift_id'] = $prom_goods[$vv['pick_id']][$vv['prom_id']]['gift_id'];
                        $plist[$k]['discount'] = $prom_goods[$v['pick_id']];
                        $count[$vv['prom_id']] += 1;
                    }

                    if($plist[$k]['order']) mlog(json_encode($plist[$k]['order']).$k,"cart3_yhui/".$stoid);
                }
                $plist[$k]["goods"] = $list;
                unset($list);
            }
        }

        /*--开始生成多单--*/
        //$uerp=M('store')->where('store_id',$stoid)->find();
        $pno = "";
        if (count($plist) > 1) $pno = $userd['ERPId'] . date('ymdHis') . get_total_millisecond() . rand(1000, 9999);
        $postFee = 0;$couponFee = 0;$balance = 0;$pointsFee = 0;$payables = 0;$payables1 = 0;$goodsFee = 0;$order_prom_id = 0;$order_prom_amount = 0;

        //启动事务
        Db::startTrans();
        try {
           foreach ($plist as $k => $v) {
                /*--计算订单相关--*/
                $result = calculate_price2($this->user, $v["goods"], $shipping_code[$v['pick_id']], 0, $address['province'], $address['city'], $address['district'], $pay_points, $user_money, $coupon_id[$v['pick_id']], $couponCode, $stoid, $exparr[$v['pick_id']], $v['discount']);

                if ($result['status'] < 0) {
                    $this->backredis($no,$notime);
                    exit(json_encode($result));
                }
                /*---余额使用---*/
                $user_money = round($user_money - $result['result']['user_money'], 2);

                /*----
                $order_prom = get_order_promotion($result['result']['order_amount'],$stoid);
                $result['result']['order_amount'] = $order_prom['order_amount'];
                $result['result']['order_prom_id'] = $order_prom['order_prom_id'];
                $result['result']['order_prom_amount'] = $order_prom['order_prom_amount'];
                ---*/

                $postFee += $result['result']['shipping_price']; // 物流费
                $couponFee += $result['result']['coupon_price'];// 优惠券
                $balance += $result['result']['user_money']; // 使用用户余额
                $pointsFee += $result['result']['integral_money']; // 积分支付
                $payables += $result['result']['order_amount'];// 应付金额
                $payables1 += $result['result']['order_amount1'];// 应付金额,不包含余额的那部分
                $goodsFee += $result['result']['goods_price'];// 商品价格
                $order_prom_id .= $result['result']['order_prom_id'] . ",";// 订单优惠活动id
                $order_prom_amount += $result['result']['order_prom_amount'] + $result['result']['discount'];// 订单优惠活动优惠了多少钱

                // 提交订单
                if ($_REQUEST['act'] == 'submit_order') {
                    $address_id = $_REQUEST['address_id'];
                    mlog("日志" . $address_id . "是否编辑地址" . $setadd, "cart");
                    if (!$address_id && $exptype == 0) {
                        $this->backredis($no,$notime);
                        exit(json_encode(array('status' => -3, 'msg' => '请先填写收货人信息', 'result' => null)));
                    } // 返回结果状态
                    if (!$shipping_code && $exptype == 0){
                        $this->backredis($no,$notime);
                        exit(json_encode(array('status' => -4, 'msg' => '请选择物流信息', 'result' => null)));
                    } // 返回结果状态
                    if (empty($coupon_id) && !empty($couponCode)) {
                        $coupon_id = M('CouponList')->where("code", $couponCode)->getField('id');
                    }
                    //$iii=$result['result']['user_money'];
                    $car_price0 = array(
                        'postFee' => $result['result']['shipping_price'], // 物流费
                        'couponFee' => $result['result']['coupon_price'], // 优惠券
                        'balance' => $result['result']['user_money'], // 使用用户余额
                        'pointsFee' => $result['result']['integral_money'], // 积分支付
                        'payables' => $result['result']['order_amount'], // 应付金额
                        'payables1' => $result['result']['order_amount1'], // 应付金额
                        'goodsFee' => $result['result']['goods_price'],// 商品价格
                        'order_prom_id' => $result['result']['order_prom_id'], // 订单优惠活动id
                        'order_prom_amount' => $result['result']['order_prom_amount'], // 订单优惠活动优惠了多少钱
                        'discount_amount' => $result['result']['discount'],//优惠、搭配促销优惠的金额
                        'good_prom'=>$result['result']['good_prom'],//优惠活动
                    );
                    /*--调用生成订单的函数--*/
                    $result = $this->cartLogic->addOrder($this->user_id, $address_id,
                        $shipping_code[$v['pick_id']], $invoice_title[$v['pick_id']], $car_price0,
                        $v["goods"], $coupon_id[$v['pick_id']], $user_note[$v['pick_id']], $stoid, $exparr[$v['pick_id']], $pno, $v['pick_id'], $v['order'], $this->user,$no,$ispt,$pdid,$team_qh); // 添加订单

                    if($result["status"] != 1) {
                           //如果失败就补回队列
                            if(!empty($sd)){
                                $skillarray=json_decode($sd,true);
                                foreach ($skillarray as $lk=>$lv){
                                    $name='ms'.$lv['fid'].'-'.$stoid;
                                    $num0=$lv['num'];
                                    //秒杀队列控制
                                    for($i=0;$i<$num0;$i++) {
                                        $this->redis->lPush($name,time().'_'.$i);
                                    }
                                    $key=$no.'-1-'.$lv['fid'].'-'.$num0.'-'.$notime;
                                    $this->redis->sRem('redis_set_'.$stoid,$key);
                                }
                            }
                            //如果是拼单
                            if($ispt){
                                $name='pind'.$pdid.'-'.$stoid;
                                //拼单队列控制
                                for($i=0;$i<$pdgoodsnum;$i++) {
                                    $this->redis->lPush($name,time().'_'.$i);
                                }
                                if($team_qh && $ispt=='2') {
                                    $name2 = 'pind' . $pdid . '-' . $team_qh . $stoid;
                                    $this->redis->lPush($name2,time().'_0');

                                }
                                $key=$no.'-6-'.$pdid.'-'.$pdgoodsnum.'-'.$notime.'-'.$team_qh;
                                $this->redis->sRem('redis_set_'.$stoid,$key);
                            }
                            //如果订单失败要还原数量
                            exit(json_encode($result));
                    }
                    if (empty($pno) && count($plist) == 1) {
                        $pno = $result["result"];
                    }
                }
            }
           //提交订单
           if ($_REQUEST['act'] == 'submit_order') {
              upload_ylp_log('提交订单');
              if ($state == 0) {
                // 删除购物车中已提交订单商品
                M('Cart')->where(['user_id' => $this->user_id, 'selected' => 1])->delete();
                //删除购物车的cookie
                Cookie::delete("cn");
                Db::commit();

                if(!empty($sd)) {
                      $skillarray = json_decode($sd, true);
                      foreach ($skillarray as $lk => $lv) {
                          //判断秒杀数量是否超限
                          $num0 = $lv['num'];
                          $name='ms'.$lv['fid'].'-'.$stoid;
                          $jsda['len']=$this->redis->lLen($name);
                          $jsda['num']=-$num0;
                          $jsda['order_sn']=$pno;
                          $jsda['type']=0;  //0是买单  1是5分钟自动补回  2是手动补回
                          mlog(json_encode($jsda),'flash_log/' . $stoid . '/' . $lv['fid']."_1");

                          $key=$no.'-1-'.$lv['fid'].'-'.$num0.'-'.$notime;
                          $this->redis->sRem('redis_set_'.$stoid,$key);
                      }
                  }

                exit(json_encode(array('status' => 1, 'msg' => '提交订单成功', 'result' => $pno, 'payables' => $payables)));
              } else {
                 // 删除cart表中立即购买的商品
                 M('Cart')->where(['user_id' => $this->user_id, 'state' => 1])->delete();
                 Db::commit();

                 //正确提交订单记录redis数量
                 if(!empty($sd)) {
                      $skillarray = json_decode($sd, true);
                      foreach ($skillarray as $lk => $lv) {
                          //判断秒杀数量是否超限
                          $num0 = $lv['num'];
                          $name='ms'.$lv['fid'].'-'.$stoid;
                          $jsda['len']=$this->redis->lLen($name);
                          $jsda['num']=-$num0;
                          $jsda['order_sn']=$pno;
                          $jsda['type']=0;  //0是买单  1是5分钟自动补回  2是手动补回
                          mlog(json_encode($jsda),'flash_log/' . $stoid . '/' . $lv['fid']."_1");

                          $key=$no.'-1-'.$lv['fid'].'-'.$num0.'-'.$notime;
                          $this->redis->sRem('redis_set_'.$stoid,$key);
                      }
                 }
                 if($ispt){
                     $key=$no.'-6-'.$pdid.'-'.$pdgoodsnum.'-'.$notime.'-'.$team_qh;
                     $this->redis->sRem('redis_set_'.$stoid,$key);
                 }

                 exit(json_encode(array('status' => 1, 'msg' => '提交订单成功', 'result' => $pno, 'payables' => $payables)));
              }
        }

        }catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            if ($_REQUEST['act'] == 'submit_order') {
                //如果失败就补回队列
                if(!empty($sd)){
                    $skillarray=json_decode($sd,true);
                    foreach ($skillarray as $lk=>$lv){
                        $name='ms'.$lv['fid'].'-'.$stoid;
                        $num0=$lv['num'];
                        //秒杀队列控制
                        for($i=0;$i<$num0;$i++) {
                            $this->redis->lPush($name,time().'_'.$i);
                        }

                        $key=$no.'-1-'.$lv['fid'].'-'.$num0.'-'.$notime;
                        $this->redis->sRem('redis_set_'.$stoid,$key);
                    }
                }
                //如果是拼单
                if($ispt){
                    $name='pind'.$pdid.'-'.$stoid;
                    //拼单队列控制
                    for($i=0;$i<$pdgoodsnum;$i++) {
                        $this->redis->lPush($name,time().'_'.$i);
                    }
                    if($team_qh && $ispt=='2') {
                        $name2 = 'pind' . $pdid . '-' . $team_qh . $stoid;
                        $this->redis->lPush($name2,time().'_0');
                    }

                    $key=$no.'-6-'.$pdid.'-'.$pdgoodsnum.'-'.$notime.'-'.$team_qh;
                    $this->redis->sRem('redis_set_'.$stoid,$key);
                }
            }
            exit(json_encode(array('status' => -1, 'msg' => '提交订单失败')));
        }

        $car_price = array(
            'postFee' => round($postFee, 2), // 物流费
            'couponFee' => round($couponFee, 2), // 优惠券
            'balance' => round($allusrnum - $user_money, 2), // 使用用户余额
            'pointsFee' => round($pointsFee, 2), // 积分支付
            'payables' => round($payables, 2), // 应付金额
            'payables1' => round($payables1, 2), // 应付金额,不包含使用余额的那一部分
            'goodsFee' => round($goodsFee, 2),// 商品价格
            'order_prom_id' => $order_prom_id, // 订单优惠活动id
            'order_prom_amount' => round($order_prom_amount, 2), // 订单优惠活动优惠了多少钱
        );

        $return_arr = array('status' => 1, 'msg' => '计算成功', 'result' => $car_price); // 返回结果状态
        exit(json_encode($return_arr));
    }

    /**
     * ajax 获取订单商品价格
     */
    public function cart3_nosub()
    {
        /*--物流方式--*/
        $exptype = I("exptype/d");
        $stoid = I("stoid");
        $state = I("state/d");
        if ($this->user_id == 0)
            exit(json_encode(array('status' => -100, 'msg' => "登录超时请重新登录!", 'result' => null))); // 返回结果状态
        $address_id = I("address_id/d"); //收货地址id
        $setadd = I("setadd/d");
        $is_skill = I("skill");  //是否是秒杀
        $exparr= I('exparr/a');//获取物流配送数组

        /*--是否是编辑地址--*/
        if ($setadd == 1 && $exptype == 0 && empty($address_id)) {
            $consignee = I("consignee");
            $mobile = I("mobile");
            $province = I("province");
            $city = I("city");
            $district = I("district");
            $twon = I("twon");
            $address = I("address");
            $data = ['consignee' => $consignee,
                'mobile' => $mobile,
                'province' => $province,
                'city' => $city,
                'district' => $district,
                'twon' => $twon,
                'address' => $address,
            ];

            $logic = new UsersLogic();
            $data = $logic->add_address($this->user_id, 0, $data);
            $address_id = $data['result'];
        }
        $shipping_code = I("shipping_code/a"); //  物流编号
        $invoice_title = I('invoice_title/a'); // 发票
        $couponTypeSelect = I("couponTypeSelect"); //  优惠券类型  1 下拉框选择优惠券 2 输入框输入优惠券代码
        $coupon_id = I("coupon_id/a"); //  优惠券id
        $couponCode = I("couponCode"); //  优惠券代码
        $user_note = I('user_note/a');   //买家留言
        $pay_points = I("pay_points", 0); //  使用积分
        $user_money = I("user_money", 0); //  使用余额

        $user_money = str_replace(',', '', $user_money);
        $user_money = doubleval($user_money);

        //if(!empty($user_money))
        //$user_money=number_format($user_money,2);  //使用俩位

        $result = null;
        if ($state == 0) {
            $result = $this->cartLogic->cartList_cart2($this->user, $this->session_id, 1, 1); //获取购物车商品
        } else {
            $result = $this->cartLogic->gobuyList_cart2($this->user, $this->session_id); //获取购物车商品
        }
        $cart_goods = $result['cartList'];
        if (empty($cart_goods))
            exit(json_encode(array('status' => -2, 'msg' => '你的购物车没有选中商品', 'result' => null))); //返回结果状态
        $address = M('UserAddress')->where("address_id", $address_id)->find();
        $allusrnum = $user_money;
        $locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');

        $userd = $this->user;
        $salerule = tpCache('basic.sales_rules', $stoid);
        $erpid = tpCache('shop_info.ERPId', $stoid);
        $userd['sales_rules'] = $salerule;
        $userd['ERPId'] = $erpid;

        //订单余额判断
        if (!empty($allusrnum)) {
            $ye = $userd['user_money'] - $userd['frozen_money'] - $locking_money;
            if ($ye . '' < $allusrnum) {
                return array('status' => -4,
                    'msg' => "您的可用余额不足", 'result' => '');
            }
        }

        /*----如果是线下库存,获取线下库存----*/
        $count_arr = null;
        if ($userd['sales_rules'] == 2) {
            if ($is_skill != 1)
                $count_arr = getcountarr($cart_goods, $stoid);
        }
        /*--购物车商品分组,生成不同订单--*/
        $plist = null;
        if ($state == 0) {
            $sql = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and selected=1 and state=0 GROUP BY pick_id";
            $plist = Db::query($sql);
            foreach ($plist as $k => $v) {
                foreach ($cart_goods as $kk => $vv) {
                    if ($v['pick_id'] == $vv['pickup_id']) {
                        $list[] = $vv;
                    }
                }
                $plist[$k]["goods"] = $list;
                unset($list);
            }
        } else {
            $sql1 = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=1 GROUP BY pick_id";
            $plist = Db::query($sql1);
            foreach ($plist as $k => $v) {
                foreach ($cart_goods as $kk => $vv) {
                    if ($v['pick_id'] == $vv['pickup_id']) {
                        $list[] = $vv;
                    }
                }
                $plist[$k]["goods"] = $list;
                unset($list);
            }
        }

        /*--开始生成多单--*/
        //$uerp=M('store')->where('store_id',$stoid)->find();
        $pno = "";
        if (count($plist) > 1) $pno = $userd['ERPId'] . date('ymdHis') . get_total_millisecond() . rand(1000, 9999);
        $postFee = 0;$couponFee = 0;$balance = 0;$pointsFee = 0;$payables = 0;$payables1 = 0;$goodsFee = 0;$order_prom_id = 0;$order_prom_amount = 0;$goods_prom_amount=0;
        foreach ($plist as $k => $v) {

            /*--计算订单相关--*/
            $result = calculate_price2($this->user, $v["goods"], $shipping_code[$v['pick_id']], 0, $address['province'], $address['city'], $address['district'], $pay_points, $user_money, $coupon_id[$v['pick_id']], $couponCode, $stoid, $exparr[$v['pick_id']],$v['discount']);
            if ($result['status'] < 0)
                exit(json_encode($result));
            /*---余额使用---*/
            $user_money = round($user_money - $result['result']['user_money'], 2);

            $postFee += $result['result']['shipping_price']; // 物流费
            $couponFee += $result['result']['coupon_price'];// 优惠券
            $balance += $result['result']['user_money']; // 使用用户余额
            $pointsFee += $result['result']['integral_money']; // 积分支付
            $payables += $result['result']['order_amount'];// 应付金额
            $payables1 += $result['result']['order_amount1'];// 应付金额,不包含余额的那部分
            $goodsFee += $result['result']['goods_price'];// 商品价格
            $order_prom_id .= $result['result']['order_prom_id'] . ",";// 订单优惠活动id
            $order_prom_amount += $result['result']['order_prom_amount'];//订单优惠
            $goods_prom_amount+= $result['result']['discount'];//优惠促销活动
        }
        $car_price = array(
            'postFee' => round($postFee, 2), // 物流费
            'couponFee' => round($couponFee, 2), // 优惠券
            'balance' => round($allusrnum - $user_money, 2), // 使用用户余额
            'pointsFee' => round($pointsFee, 2), // 积分支付
            'payables' => round($payables, 2), // 应付金额
            'payables1' => round($payables1, 2), // 应付金额,不包含使用余额的那一部分
            'goodsFee' => round($goodsFee, 2),// 商品价格
            'order_prom_id' => $order_prom_id, // 订单优惠活动id
            'order_prom_amount' => round($order_prom_amount, 2), // 订单优惠优惠了多少钱
            'goods_prom_amount' => round($goods_prom_amount, 2), // 优惠活动优惠了多少钱
        );

        $return_arr = array('status' => 1, 'msg' => '计算成功', 'result' => $car_price); // 返回结果状态
        exit(json_encode($return_arr));
    }


    /*
     * 订单支付页面
     */
    public function cart4()
    {
        $orderno = I('order_id');
        $order_amount = M('Order')->where("parent_sn", $orderno)->sum('order_amount');
        $order = M('Order')->where("parent_sn", $orderno)->select();

        // 如果已经支付过的订单直接到订单详情页面. 不再进入支付页面
        if ($order[0]['pay_status'] == 1) {
            $order_detail_url = U("Mobile/User/order_detail", array('stoid' => getMobileStoId(), 'id' => $order["order_id"]));
            header("Location: $order_detail_url");
            exit;
        }

        $paymentList = M('Plugin')->where("`type`='payment' and status = 1 and  scene in(0,1)")->select();
        //微信浏览器
        if (strstr($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')) {
            //$paymentList = M('Plugin')->where("`type`='payment' and status = 1 and code in('weixin','cod')")->select();
            $paymentList = M('Plugin')->where("`type`='payment' and status = 1 and code in('weixin')")->select();
        }
        $paymentList = convert_arr_key($paymentList, 'code');
        foreach ($paymentList as $key => $val) {
            $val['config_value'] = unserialize($val['config_value']);
            if ($val['config_value']['is_bank'] == 2) {
                $bankCodeList[$val['code']] = unserialize($val['bank_code']);
            }
            //判断当前浏览器显示支付方式
            if (($key == 'weixin' && !is_weixin()) || ($key == 'alipayMobile' && is_weixin())) {
                unset($paymentList[$key]);
            }
        }

        $bank_img = include APP_PATH . 'home/bank.php'; // 银行对应图片
        $payment = M('Plugin')->where("`type`='payment' and status = 1")->select();
        $this->assign('paymentList', $paymentList);
        $this->assign('bank_img', $bank_img);
        $this->assign('order_amount', $order_amount);
        $this->assign('bankCodeList', $bankCodeList);
        $this->assign('pay_date', date('Y-m-d', strtotime("+1 day")));
        upload_ylp_log('订单支付');
        return $this->fetch('', getMobileStoId());
    }


    public function cart5()
    {
        if ($this->user_id == 0)
            $this->error('请先登录', U('Mobile/User/login', array('stoid' => getMobileStoId())));
        $userd=$this->user;

        //获取积分
        $integ=get_userpoints($userd,getMobileStoId(),$this->pm_erpid);


        //20180717   season(增加短信随机码校验)

        $storeconfig = M('store_config')->where('store_id', getMobileStoId())->field('reg_type,reg_info,reg_default,sms_send_type,switch_list')->find();
        $sms_send_type = json_decode($storeconfig['sms_send_type'], true);
        $this->assign('s_type', $sms_send_type['type'] ? $sms_send_type['type'] : 0);
        $this->assign('s_time_out', $sms_send_type['time_out'] ? $sms_send_type['time_out'] : 60);
        $this->assign('is_verifycode', $sms_send_type['is_verifycode'] ? $sms_send_type['is_verifycode'] : 0);

        $user_openid = $this->user['openid'];
        //$user_openid="ou608v4s8TmwUsGLtJKDvdPKat5w"; //测试用
        $this->assign("user_openid", $user_openid);

        $user_openidkey = md5($user_openid . "&" . getErpKey());
        $this->assign("user_openid", $user_openid);
        $this->assign("user_openidkey", $user_openidkey);
        //

        $locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');
        $this->assign('u_money', number_format($userd['user_money'] - $userd['frozen_money'] - $locking_money, 2));
        $this->assign('u_integral', $integ);

        /*---获取地区---*/
        $region_list = get_region_list();
        $addlist = M('user_address')->where(['user_id' => $this->user_id])->select();
        foreach ($addlist as $kk => $vv) {
            $vv['provicename'] = $region_list[$vv['province']]['name'];
            $vv['cityname'] = $region_list[$vv['city']]['name'];
            $vv['districtname'] = $region_list[$vv['district']]['name'];
            $vv['twonname'] = $region_list[$vv['twon']]['name'];

            //$address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
            $addrstr = $vv['provicename'] . "-" . $vv['cityname'];
            if (!empty($vv['district']))
                $addrstr .= "-" . $vv['districtname'];
            if (!empty($vv['twon']))
                $addrstr = $addrstr . "-" . $vv['twonname'];
            $vv['addrstr'] = $addrstr;

            $addlist[$kk] = $vv;
        }
        $this->assign("addlist", $addlist);

        $address_id = I('address_id/d');
        if ($address_id) {
            foreach ($addlist as $kk => $vv) {
                if ($vv['address_id'] == $address_id) {
                    $address = $vv;
                }
            }
        } else {
            foreach ($addlist as $kk => $vv) {
                if ($vv['is_default'] == 1) {
                    $address = $vv;
                }
            }
        }

        $p = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
        $this->assign('province', $p);
        if (empty($address)) {
            //header("Location: ".U('Mobile/User/add_address',array('stoid'=>getMobileStoId(),'source'=>'cart2')));
            $this->assign('setadd', 1);
        } else {
            $this->assign('address', $address);
            $this->assign('setadd', 0);
        }

        /*--是否是购物车--*/
        $tocart = I("tocart/d", 0);
        $result = null;
        $count = M('Cart')->where(['user_id' => $this->user_id, 'state' => 2])->count();
        if ($count == 0)
            $this->redirect('Index/index', array('stoid' => getMobileStoId()));

        $result = $this->cartLogic->gointeList($this->user, $this->session_id); //获取购物车商品


        $cartinfo = M('cart')->where(array('state' => 2, 'user_id' => $this->user_id))->select(); //购物车积分购
        $proinfo = M('goods')->where(array('goods_id' => $cartinfo[0]['goods_id']))->select();
        $inteinfo = M('integral_buy')->where(array('goods_id' => $proinfo[0]['goods_id'], 'is_end' => 0))->select();

        mlog(json_encode($result), "cart");

        $shippingList = M('store_shipping')->alias('a')->join("shipping b", "a.shipping_id=b.shipping_id")
            ->field('b.shipping_id,b.shipping_code,b.shipping_name,b.shipping_desc,b.shipping_logo')
            ->where("a.status=1 and a.store_id=" . getMobileStoId())
            ->select();//物流公司


        /*--购物车商品分组--*/
        $cart_goods = $result['cartList'];
        $expty = 0;
        foreach ($cart_goods as $kp => $vp) {
            $exp2=0;
            if($vp['distr_type']!=$vp['bdistr_type']){
                $exp2=$vp['distr_type']==0?$vp['bdistr_type']:$vp['distr_type'];
            }else{
                $exp2=$vp['distr_type'];
            }
            $cart_goods[$kp]['expty']=$exp2;
            if ($vp['is_gift'] == 0) {
                //判断最后的物流方式
                if ($expty != 0) {
                    if ($expty != $exp2) {
                        if($exp2==2 || $expty==2 ){
                            $expty=2;
                        }else{
                            $expty=0;
                        }
                    }
                } else {
                    $expty =$exp2;
                }
            }
        }

        $this->assign("expty", $expty);
        /*----
        foreach ($cart_goods as $kp => $vp) {
            if ($vp['distr_type'] != 0) {
                if ($expty != 0) {
                    if ($expty != $vp['distr_type']) {
                        $this->error('您不能同时选择自提和物流商品', 'cart/cart', array('stoid' => getMobileStoId()));
                    }
                } else {
                    $expty = $vp['distr_type'];
                }
            }
        }
        $this->assign("expty", $expty);---*/

        $sql = null;
        //mlog('$tocart:'.$sql,'cart');
        $plist_cart = Db::query("select pickup_id,pickup_name from __PREFIX__pick_up where pickup_id in(select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=2 GROUP BY pick_id)");
        foreach ($plist_cart as $k => $v) {

            $expty00 = 0;
            foreach ($cart_goods as $kk => $vv) {
                if ($vv['pick_id'] == $v['pickup_id']) {
                    $list[] = $vv;
                    $expty11=$vv['expty'];
                    if($expty00==0){
                        $expty00=$expty11;
                    }
                }
            }

            $plist_cart[$k]["goods"] = $list;
            $plist_cart[$k]["expty"]=$expty00;
            // 获取订单商品总价,$tocart=0为购物车 1为立即购买
            //$r = $this->cartLogic->my_calculate($list,$this->user_id,$tocart);
            // 找出这个用户的优惠券没过期的 并且订单金额达到condition 优惠券指定标准的
//            $fzquan=M('frozen_quan')->where('user_id',$this->user_id)->field('CashRepNo')->select();
//            $snow=date("Y-m-d H:i:s",time());
//            $tk = M("store")->where("store_id", getMobileStoId())->field("api_token")->find();
//            $user = M("users")->where("user_id",$this->user_id)->field("erpvipid")->find();
//            $where = "VIPId='" . $user['erpvipid'] . "'";
//            $where .= " and IsUse=0 and ValidDate>='" . $snow . "'";
////            $where .= " and BuySum<=".$r;
////            $where .= " and Sum<=".$r;
//            $order['Sum']='desc';
//
//            $rs=getApiData("wxderp.cashreplace.list.get",$tk["api_token"],null,$where,1,1000,$order);
//            $qlistall = json_decode($rs, true);
//            if(!empty($qlistall)){
//                if($qlistall['data']) {
//                    foreach ($qlistall['data'] as $k1 => $v1) {
//                        foreach ($fzquan as $ku=>$vu){
//                            $qlistall['data'][$k1]['CashRepNo']=$vu['CashRepNo'];
//                            unset($qlistall['data'][$k1]);
//                            break;
//                        }
//                        $qlistall['data'][$k1]['Sum'] = number_format(empty($v1['Sum']) ? 0 : $v1['Sum'], 2);
//                        $qlistall['data'][$k1]['BuySum'] = number_format(empty($v1['BuySum']) ? 0 : $v1['BuySum'], 2);
//                        $qlistall['data'][$k1]['ValidDate']=date('Y-m-d H:i:s', strtotime($v1['ValidDate']));
//                    }
//
//                }
//            }
//            //$couponList = DB::query($sql);
//            $plist_cart[$k]["couponList"]=$qlistall['data'];
            unset($list);
            //unset($qlistall);
        }

        $this->assign('slist', $plist_cart);
        $this->assign('shippingList', $shippingList); // 物流公司
        $this->assign('total_price', $result['total_price']); // 总计
        upload_ylp_log('购买积分购商品订单页面');
        return $this->fetch('', getMobileStoId());
    }

    /**
     * ajax 获取积分购订单商品价格或者积分购提交订单
     */
    public function cart6()
    {
        /*--物流方式--*/
        $exptype = I("exptype/d");
        $stoid = I("stoid");
        $state = I("state/d");
        if ($this->user_id == 0)
            exit(json_encode(array('status' => -100, 'msg' => "登录超时请重新登录!", 'result' => null))); // 返回结果状态
        $address_id = I("address_id/d"); //  收货地址id
        $setadd = I("setadd/d");
        /*--积分购传到前台的值--*/
        $arystr = I("goodsstr");
        $ary0 = explode("-", $arystr);

        /*--是否是编辑地址--*/
        if ($setadd == 1 && $exptype == 0) {
            $consignee = I("consignee");
            $mobile = I("mobile");
            $province = I("province");
            $city = I("city");
            $district = I("district");
            $twon = I("twon");
            $address = I("address");
            $data = ['consignee' => $consignee,
                'mobile' => $mobile,
                'province' => $province,
                'city' => $city,
                'district' => $district,
                'twon' => $twon,
                'address' => $address,
            ];

            $logic = new UsersLogic();
            $data = $logic->add_address($this->user_id, 0, $data);
            $address_id = $data['result'];
        }

        $shipping_code = I("shipping_code/a"); //  物流编号
        $invoice_title = I('invoice_title/a'); // 发票
        $exparr= I('exparr/a');//获取物流配送数组
        $couponTypeSelect = I("couponTypeSelect"); //  优惠券类型  1 下拉框选择优惠券 2 输入框输入优惠券代码
        $coupon_id = I("coupon_id/a"); //  优惠券id
        $couponCode = I("couponCode"); //  优惠券代码
        $user_note = I('user_note/a');   //买家留言
        $pay_points = I("pay_points", 0); //  使用积分
        $user_money = I("user_money", 0); //  使用余额

        $user_money = str_replace(',', '', $user_money);
        $user_money = doubleval($user_money);


        $exptype=0;
        foreach ($exparr as $ko=>$vo){
            if($vo==1){ $exptype=1; break;}
        }

        /*--购物车商品提取--*/
        /*--
        $cart_goods = M('Cart')->alias('a')
                ->join("pick_up b","a.pick_id=b.pickup_id",'left')
                ->where(['a.user_id'=>$this->user_id,'a.selected'=>1])
                ->where($where)->getField("a.*,b.pickup_name,b.pickup_id");--*/
        /*---购物车和立即购买的区分---*/
        $result = null;
        $result = $this->cartLogic->gointeList($this->user, $this->session_id); //获取购物车商品
        $cart_goods = $result['cartList'];
        if (empty($cart_goods))
            exit(json_encode(array('status' => -2, 'msg' => '你的购物车没有选中商品', 'result' => null))); //返回结果状态
        $address = M('UserAddress')->where("address_id", $address_id)->find();

        $allusrnum = $user_money;
        $userd = $this->user;
        if ($state == 2) {
            $pay_points = $cart_goods[0]['prom']['integral'] * $cart_goods[0]["goods_num"];
        }

        $locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');
        //订单余额判断
        if (!empty($allusrnum)) {
            $ye = $userd['user_money'] - $userd['frozen_money'] - $locking_money;
            if ($ye . '' < $allusrnum) {
                return array('status' => -4,
                    'msg' => "您的可用余额不足", 'result' => '');
            }
        }

        $err_txt = "";
        /*--库存判断和活动判断,商品的库存和活动不能过期--*/
        foreach ($cart_goods as $k => $value) {
            $ginfo = $value;
            /*--线下库存的序列key--*/
            $bkey = $value['pick_id'] . $value['goods_id'];
            //商品参与活动查看
            if ($value['prom_type'] > 0) {
                $prom = get_goods_promotion1($value, null, $this->user_id);
                if ($prom['is_end'] == 1) {
                    return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此商品活动已经结束", 'result' => '');
                }
                if ($prom['is_end'] == 2) {
                    return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此商品已经售完", 'result' => '');
                }
                if ($prom['is_end'] == 3) {
                    return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此活动商品超出限购数量", 'result' => '');
                }
                if ($value['prom_type'] == 1 || $value['prom_type'] == 2 || $value['prom_type'] == 4) {
                    if ($value['goods_num'] > $prom['buy_limit']) {
                        return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此活动商品超出限购数量",
                            'result' => '');
                    }
                }
                if ($ary0[0] != $prom['integral'] && $ary0[0] != "" && $ary0[1] != "") {
                    $err_txt .= "商品购买积分已经变化";
                }
                if ($ary0[1] != $prom['addmoney'] && $ary0[0] != "" && $ary0[1] != "") {
                    $err_txt .= "商品价格已经变化 <br>";
                }

                if ($value['prom_type'] == 1 || $value['prom_type'] == 2 || $value['prom_type'] == 4) {
                    if ($value['goods_num'] > $ginfo['store_count']) {
                        return array('status' => -4, 'msg' => "库存不足", 'result' => '');
                    }
                } else {
                    return array('status' => -4, 'msg' => "库存不足", 'result' => '');
                }
            } else {
                return array('status' => -4, 'msg' => "库存不足", 'result' => '');
            }
        }

        if ($err_txt != "") {
            return array('status' => -1004, 'msg' => $err_txt, 'result' => '');
        }

        /*--购物车商品分组,生成不同订单--*/
        $plist = null;
        if ($state == 0) {
            $sql = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and selected=1 and state=0 GROUP BY pick_id";
            $plist = Db::query($sql);
            foreach ($plist as $k => $v) {
                foreach ($cart_goods as $kk => $vv) {
                    if ($v['pick_id'] == $vv['pickup_id']) {
                        $list[] = $vv;
                    }
                }
                $plist[$k]["goods"] = $list;
                unset($list);
            }
        } else {
            $sql1 = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=" . $state . " GROUP BY pick_id";
            $plist = Db::query($sql1);
            foreach ($plist as $k => $v) {
                foreach ($cart_goods as $kk => $vv) {
                    if ($v['pick_id'] == $vv['pickup_id']) {
                        $list[] = $vv;
                    }
                }
                $plist[$k]["goods"] = $list;
                unset($list);
            }
        }

        /*--开始生成多单--*/
        //$uerp = M('store')->where('store_id', $stoid)->find();
        $uerp =tpCache('shop_info',$stoid);
        $pno = "";
        if (count($plist) > 1) $pno = $uerp['ERPId'] . date('YmdHis') . get_total_millisecond() . rand(1000, 9999);

        $postFee = 0;
        $couponFee = 0;
        $balance = 0;$pointsFee = 0;$payables = 0;$payables1 = 0;$goodsFee = 0;$order_prom_id = 0;$order_prom_id = 0;$order_prom_amount = 0;

        // 启动事务
        Db::startTrans();
        try {
            foreach ($plist as $k => $v) {
                /*--计算订单相关--*/
                $scode = $shipping_code[$v['pick_id']];
//            if (!empty($shipping_code)){
//                if ($scode == 'mdzz'){
//                    $exptype = 1;
//                }else{
//                    $exptype = 0;
//                }
//            }

                $result = calculate_price3($this->user, $v["goods"], $scode, 0, $address['province'], $address['city'], $address['district'], $pay_points, $user_money, 0, "", $stoid, $exparr[$v['pick_id']]);

                if ($result['status'] < 0)
                    exit(json_encode($result));
                /*---余额使用---*/
                $user_money = $user_money - $result['result']['user_money'];

                $postFee += $result['result']['shipping_price']; // 物流费
                $couponFee += $result['result']['coupon_price'];// 优惠券
                $balance += $result['result']['user_money']; // 使用用户余额
                $pointsFee += $result['result']['integral_money']; // 积分支付
                $payables += $result['result']['order_amount'];// 应付金额
                $payables1 += $result['result']['order_amount1'];// 应付金额,不包含余额的那部分
                $goodsFee += $result['result']['goods_price'];// 商品价格

                // 提交订单
                if ($_REQUEST['act'] == 'submit_order') {
                    $address_id = $_REQUEST['address_id'];
                    mlog("日志" . $address_id . "是否编辑地址" . $setadd, "cart");
                    if (!$address_id && $exptype == 0) exit(json_encode(array('status' => -3, 'msg' => '请先填写收货人信息', 'result' => null))); // 返回结果状态
                    if (!$shipping_code && $exptype == 0) exit(json_encode(array('status' => -4, 'msg' => '请选择物流信息', 'result' => null))); // 返回结果状态
                    if (empty($coupon_id) && !empty($couponCode)) {
                        $coupon_id = M('CouponList')->where("code", $couponCode)->getField('id');
                    }
                    //$iii=$result['result']['user_money'];
                    $car_price0 = array(
                        'postFee' => $result['result']['shipping_price'], // 物流费
                        'couponFee' => $result['result']['coupon_price'], // 优惠券
                        'balance' => $result['result']['user_money'], // 使用用户余额
                        'pointsFee' => $result['result']['integral_money'], // 积分支付
                        'payables' => $result['result']['order_amount'], // 应付金额
                        'payables1' => $result['result']['order_amount1'], // 应付金额,不包含余额那一部分
                        'goodsFee' => $result['result']['goods_price'],// 商品价格
                        'order_prom_id' => 0, // 订单优惠活动id
                        'order_prom_amount' => 0, // 订单优惠活动优惠了多少钱
                    );
                    /*--调用生成订单的函数--*/
                    $result = $this->cartLogic->addOrder($this->user_id, $address_id,
                        $shipping_code[$v['pick_id']], $invoice_title[$v['pick_id']], $car_price0,
                        $v["goods"], $coupon_id[$v['pick_id']], $user_note[$v['pick_id']], $stoid, $exparr[$v['pick_id']], $pno, $v['pick_id'], null, $this->user); // 添加订单

                    if ($result["status"] != 1)
                        exit(json_encode($result));
                    if (empty($pno) && count($plist) == 1) {
                        $pno = $result["result"];
                    }
                }
            }
            // 提交订单
            if ($_REQUEST['act'] == 'submit_order') {
                upload_ylp_log('积分购商品提交订单');
                if ($state == 0) {
                    // 删除购物车中已提交订单商品
                    M('Cart')->where(['user_id' => $this->user_id, 'selected' => 1])->delete();
                    //删除购物车的cookie
                    Cookie::delete("cn");
                    Db::commit();


                    $odr_arr=M('order')->where('parent_sn',$pno)->where('store_id',$stoid)
                        ->field('order_amount,order_sn,order_id,user_id')->select();
                    foreach ($odr_arr as $k=>$v) {
                        //不是拼单的订单,才进行分成
                        if($data['pt_prom_id']>0){
                        }else{
                            $order=$v;
                            //分销开关全局
                            $distribut_switch = tpCache('distribut.switch', getMobileStoId());
                            mlog("分成开关-" . $order['order_id'] . $order['order_sn'] . ":" . $distribut_switch, 'rebate_log/' . getAdmStoId());
                            if ($distribut_switch == 1 && file_exists(APP_PATH . 'common/logic/DistributLogic.php')) {
                                $distributLogic = new \app\common\logic\DistributLogic();
                                $distributLogic->rebate_log($order, getMobileStoId()); // 生成分成记录
                            }
                        }

                        if ($v['order_amount'] == 0) {
                            update_pay_status2($v['order_sn'], 1);
                        }
                    }

                    exit(json_encode(array('status' => 1, 'msg' => '提交订单成功', 'result' => $pno, 'payables' => $payables)));
                } else {
                    // 删除cart表中立即购买的商品
                    M('Cart')->where(['user_id' => $this->user_id, 'state' => 1])->delete();
                    Db::commit();

                    exit(json_encode(array('status' => 1, 'msg' => '提交订单成功', 'result' => $pno, 'payables' => $payables)));
                }
            }
        }
        catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            exit(json_encode(array('status' => -1, 'msg' => '提交订单失败')));
        }

        $car_price = array(
            'postFee' => round($postFee, 2), // 物流费
            'couponFee' => round($couponFee, 2), // 优惠券
            'balance' => round($allusrnum - $user_money, 2), // 使用用户余额
            'pointsFee' => round($pointsFee, 2), // 积分支付
            'payables' => round($payables, 2), // 应付金额
            'payables1' => round($payables1, 2), // 应付金额,不包含使用余额的那一部分
            'goodsFee' => round($goodsFee, 2)// 商品价格
        );

        $return_arr = array('status' => 1, 'msg' => '计算成功', 'result' => $car_price); // 返回结果状态
        exit(json_encode($return_arr));
    }


    /**
     * ajax 获取积分购订单商品价格
     */
    public function cart6_nosub()
    {
        /*--物流方式--*/
        $exptype = I("exptype/d");
        $stoid = I("stoid");
        $state = I("state/d",0);
        if ($this->user_id == 0)
            exit(json_encode(array('status' => -100, 'msg' => "登录超时请重新登录!", 'result' => null))); // 返回结果状态

        $address_id = I("address_id/d"); //  收货地址id
        $setadd = I("setadd/d");

        /*--是否是编辑地址--*/
        if ($setadd == 1 && $exptype == 0) {
            $consignee = I("consignee");
            $mobile = I("mobile");
            $province = I("province");
            $city = I("city");
            $district = I("district");
            $twon = I("twon");
            $address = I("address");
            $data = ['consignee' => $consignee,
                'mobile' => $mobile,
                'province' => $province,
                'city' => $city,
                'district' => $district,
                'twon' => $twon,
                'address' => $address,
            ];

            $logic = new UsersLogic();
            $data = $logic->add_address($this->user_id, 0, $data);
            $address_id = $data['result'];
        }
        $shipping_code = I("shipping_code/a"); //  物流编号
        $invoice_title = I('invoice_title/a'); // 发票
        $exparr= I('exparr/a');//获取物流配送数组
        $couponTypeSelect = I("couponTypeSelect"); //  优惠券类型  1 下拉框选择优惠券 2 输入框输入优惠券代码
        $coupon_id = I("coupon_id/a"); //  优惠券id
        $couponCode = I("couponCode"); //  优惠券代码
        $user_note = I('user_note/a');   //买家留言
        $pay_points = I("pay_points", 0); //  使用积分
        $user_money = I("user_money", 0); //  使用余额

        $user_money = str_replace(',', '', $user_money);
        $user_money = doubleval($user_money);
        /*--购物车商品提取--*/
        /*--
        $cart_goods = M('Cart')->alias('a')
                ->join("pick_up b","a.pick_id=b.pickup_id",'left')
                ->where(['a.user_id'=>$this->user_id,'a.selected'=>1])
                ->where($where)->getField("a.*,b.pickup_name,b.pickup_id");--*/
        /*---购物车和立即购买的区分---*/
        $result = null;
        $result = $this->cartLogic->gointeList($this->user, $this->session_id); //获取购物车商品

        $cart_goods = $result['cartList'];
        if (empty($cart_goods))
            exit(json_encode(array('status' => -2, 'msg' => '你的购物车没有选中商品', 'result' => null))); //返回结果状态
        $address = M('UserAddress')->where("address_id", $address_id)->find();

        $allusrnum = $user_money;
        $userd = $this->user;

        $pay_points = $cart_goods[0]['prom']['integral'] * $cart_goods[0]["goods_num"];
        $locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');
        //订单余额判断
        if (!empty($allusrnum)) {
            $ye = $userd['user_money'] - $userd['frozen_money'] - $locking_money;
            if ($ye . '' < $allusrnum) {
                return array('status' => -4,
                    'msg' => "您的可用余额不足", 'result' => '');
            }
        }

        /*--购物车商品分组,生成不同订单--*/
        $plist = null;
        $sql1 = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=" . $state . " GROUP BY pick_id";
        $plist = Db::query($sql1);
        foreach ($plist as $k => $v) {
            foreach ($cart_goods as $kk => $vv) {
                if ($v['pick_id'] == $vv['pickup_id']) {
                    $list[] = $vv;
                }
            }
            $plist[$k]["goods"] = $list;
            unset($list);
        }

        /*--开始生成多单--*/
        //$uerp = M('store')->where('store_id', $stoid)->find();
        $uerp = tpCache('shop_info',$stoid);
        $pno = "";
        if (count($plist) > 1) $pno = $uerp['ERPId'] . date('YmdHis') . get_total_millisecond() . rand(1000, 9999);

        $postFee = 0;
        $couponFee = 0;
        $balance = 0;
        $pointsFee = 0;
        $payables = 0;
        $payables1 = 0;
        $goodsFee = 0;
        $order_prom_id = 0;
        $order_prom_id = 0;
        $order_prom_amount = 0;

        foreach ($plist as $k => $v) {
            /*--计算订单相关--*/
            $scode = $shipping_code[$v['pick_id']];
            $result = calculate_price3($this->user, $v["goods"], $scode, 0, $address['province'], $address['city'], $address['district'], $pay_points, $user_money, 0, "", $stoid, $exparr[$v['pick_id']]);

            if ($result['status'] < 0)
                exit(json_encode($result));
            /*---余额使用---*/
            $user_money = $user_money - $result['result']['user_money'];
            $postFee += $result['result']['shipping_price']; // 物流费
            $couponFee += $result['result']['coupon_price'];// 优惠券
            $balance += $result['result']['user_money']; // 使用用户余额
            $pointsFee += $result['result']['integral_money']; // 积分支付
            $payables += $result['result']['order_amount'];// 应付金额
            $payables1 += $result['result']['order_amount1'];// 应付金额,不包含余额的那部分
            $goodsFee += $result['result']['goods_price'];// 商品价格
        }

        $car_price = array(
            'postFee' => round($postFee, 2), // 物流费
            'couponFee' => round($couponFee, 2), // 优惠券
            'balance' => round($allusrnum - $user_money, 2), // 使用用户余额
            'pointsFee' => round($pointsFee, 2), // 积分支付
            'payables' => round($payables, 2), // 应付金额
            'payables1' => round($payables1, 2), // 应付金额,不包含使用余额的那一部分
            'goodsFee' => round($goodsFee, 2)// 商品价格
        );

        $return_arr = array('status' => 1, 'msg' => '计算成功', 'result' => $car_price); // 返回结果状态
        exit(json_encode($return_arr));
    }


    /*
    * ajax 请求获取购物车列表
    */
    public function ajaxCartList()
    {
        $post_goods_num = I("goods_num/a"); // goods_num 购物车商品数量
        $post_cart_select = I("cart_select/a"); // 购物车选中状态
        $where['session_id'] = $this->session_id; // 默认按照 session_id 查询
        // 如果这个用户已经等了则按照用户id查询
        $user_id = $this->user_id;
        $stoid = getMobileStoId();
        if ($user_id) {
            unset($where);
            $where['user_id'] = $user_id;
        }
        $where['state'] = 0;
        $where['store_id'] = $stoid;
        $where['is_gift'] = 0;

        M('Cart')->where($where)->save(['selected' => 0]);
        $cartList = M('Cart')->alias('a')->join('goods b', 'a.goods_id=b.goods_id')->where(['a.user_id' => $user_id, 'a.store_id' => $stoid, 'a.state' => 0])
            ->getfield('a.id,a.prom_type,a.selected,a.pick_id,a.goods_id,a.goods_num,a.prom_id,b.shop_price,b.store_count,b.is_on_sale,a.is_gift,b.prom_type as prom_type1,b.viplimited');

        $now0 = time();
        /*---如果是线下销售规则,获取所有购物车商品和门店的组合库存数组---*/
        $sale_r = tpCache("basic.sales_rules", $stoid);
        if ($sale_r == 2) {
            $dlinearr = getcountarr($cartList, $stoid);
        }

        /*-----------------页面一开始加载的时候更新数量,剔除下架和活动结束的商品-------------*/
        if (empty($post_goods_num) && !empty($cartList)) {
            /*--要剔除下架商品,活动有过期的商品,或者商品在参加活动之前被添加到购物车--*/
            foreach ($cartList as $k => $v) {
                if ($v['is_on_sale'] == 1  && $v['on_time']< $now0 && ($v['down_time']> $now0 || empty($v['down_time']))) {
                    if ($v['prom_type'] != 0 && $v['is_gift'] == 0) {
                        $prom = get_goods_promotion1($v, null, $user_id);
                        if ($prom['is_end'] == 2 || $prom['is_end'] == 3 || $prom['is_end'] == 1) {

                            M('Cart')->where('goods_id', $v['goods_id'])->where('user_id', $this->user_id)->delete();
                            unset($cartList[$k]);
                        }
                    } else {
                        $where = [
                            'end_time' => ['>=', $now0],
                            'start_time' => ['<=', $now0],
                            'goods_id' => $v['goods_id'],
                            'is_end' => 0
                        ];
                        if ($v['prom_type1'] == 1) {//秒杀
                            $jj = M('flash_sale')->where($where)->find();
                            if ($jj) {
                                M('Cart')->where('goods_id', $v['goods_id'])->where('user_id', $this->user_id)->delete();
                                unset($cartList[$k]);
                            }
                        }
                        if ($v['prom_type1'] == 2) {//团购
                            $jj = M('group_buy')->where($where)->find();
                            if ($jj) {
                                M('Cart')->where('goods_id', $v['goods_id'])->where('user_id', $this->user_id)->delete();
                                unset($cartList[$k]);
                            }

                        }
                        if ($v['prom_type1'] == 4) {//积分购
                            $jj = M('flash_sale')->where($where)->find();
                            if ($jj) {
                                M('Cart')->where('goods_id', $v['goods_id'])->where('user_id', $this->user_id)->delete();
                                unset($cartList[$k]);
                            }
                        }
                        if ($v['is_gift'] == 1) {
                            M('cart')->where(['id' => $v['id'], 'user_id' => $user_id])->delete();
                            unset($cartList[$k]);
                        }
                    }
                } else {
                    M('Cart')->where('id', $v['id'])->where('user_id', $user_id)->delete();
                    unset($cartList[$k]);
                }
            }
        }

        //统计赠品
//        $count=array();
//        foreach ($cartList as $val){
//            if ($val['is_gift']==1){
//                $count[$val['prom_id']]+=1;
//                $count[$val['pick_id']][$val['prom_id']]+=1;
//            }
//        }

        /*--------------改变数量和选择商品--------------*/
        if (!empty($post_goods_num)) {
            // 修改购物车数量,和勾选状态
            foreach ($post_goods_num as $key => $val) {
                /*--线下库存的序列key--*/
                $bkey = $cartList[$key]['pick_id'] . $cartList[$key]['goods_id'];
                $data['goods_num'] = $val < 1 ? 1 : $val;

                if ($cartList[$key]['prom_type'] > 0 && $cartList[$key]['prom_type'] != 3 && $cartList[$key]['prom_type'] != 5) {
                    $prom = get_goods_promotion1($cartList[$key], null, $user_id);
                    /*---如果大于线上库存--*/
                    $data['goods_num'] = $data['goods_num'] > $prom['store_count'] ? $prom['store_count'] : $data['goods_num'];
                    if ($cartList[$key]['prom_type'] == 2) {
                        $data['goods_num'] = $data['goods_num'] > $prom['onlybuy'] ? $prom['onlybuy'] : $data['goods_num'];
                    }
                    /*---如果是秒杀和积分购---*/
                    if ($cartList[$key]['prom_type'] == 1 || $cartList[$key]['prom_type'] == 4) {
                        $data['goods_num'] = $data['goods_num'] > $prom['buy_limit'] ? $prom['buy_limit'] : $data['goods_num'];
                    }
                } else {
                    /*--如果是线下规则--*/
                    if ($sale_r == 2) {
                        $dlcount = $dlinearr[$bkey];
                        if (!empty($dlcount) && $dlcount > 0) {
                            $data['goods_num'] = $data['goods_num'] > $dlcount ? $dlcount : $data['goods_num'];
                        }
                    } else if ($sale_r == 1) {
                        $data['goods_num'] = $data['goods_num'] > $cartList[$key]['store_count'] ? $cartList[$key]['store_count'] : $data['goods_num'];
                    }
                }

                $data['selected'] = $post_cart_select[$key] ? 1 : 0;
                if ($cartList[$key]['goods_num'] != $data['goods_num'] || $cartList[$key]['selected'] != $data['selected']) {
                    M('Cart')->where("id", $key)->save($data);
                }
            }
            $this->assign('select_all', input('post.select_all')); // 全选框
        }

        $prom_goods = array();
        if (!empty($post_cart_select)) {
            foreach ($post_cart_select as $kk => $vv) {
                if ($cartList[$kk]['prom_type'] == 3) {
                    $prom_goods[$cartList[$kk]['pick_id']][$cartList[$kk]['prom_id']]['num'] += $post_goods_num[$kk];
                    $prom_goods[$cartList[$kk]['pick_id']][$cartList[$kk]['prom_id']]['price'] += $cartList[$kk]['shop_price'] * $post_goods_num[$kk];
                    $prom_goods['pick'][] = $cartList[$kk]['pick_id'];//收集选中的门店
                }
            }
        }

        if (!empty($prom_goods)) {
            M('cart')->where(['is_gift' => 1, 'user_id' => $user_id, 'pick_id' => ['not in', $prom_goods['pick']]])->delete();
            unset($prom_goods['pick']);
            $prominfo = M('prom_goods')->where(['start_time' => ['lt', time()], 'end_time' => ['gt', time()], 'is_end' => 0])->getfield('id,good_object,name');
            foreach ($prom_goods as $k => $v) {
                foreach ($v as $kk => $vv) {
                    $prom_id_list[] = $kk;//收集选中门店选中优惠活动id
                    if (!empty($prominfo[$kk])) {
                        $prom = discount($vv['price'], $kk, $vv['num'], $user_id);
                        if (!empty($prom['goods_id'])) {
                            $count = M('cart')->where(['user_id' => $user_id, 'prom_id' => $kk, 'is_gift' => 1])->count();
                            $pick = M('cart')->where(['user_id' => $user_id, 'prom_id' => $kk, 'is_gift' => 1, 'pick_id' => $k])->count();
                            if ($count < $prom['limit_num'] && $pick < 1) {
                                M('cart')->add(['user_id' => $user_id, 'session_id' => $this->session_id, 'goods_id' => $prom['goods_id'], 'goods_sn' => '',
                                    'goods_name' => $prom['goods_name'], 'market_price' => 0, 'goods_price' => 0, 'member_goods_price' => 0, 'goods_num' => 1, 'sku' => '',
                                    'add_time' => time(), 'prom_type' => 3, 'prom_id' => $kk, 'pick_id' => $k, 'store_id' => $stoid, 'state' => 0, 'main_cart_id' => 1,
                                    'is_gift' => 1, 'selected' => 1]);
                            }
                        } else {
                            M('cart')->where(['user_id' => $user_id, 'is_gift' => 1, 'prom_id' => $kk, 'pick_id' => $k])->delete();
                        }
                        if ($prom['type'] == 0) {
                            if ($vv['price'] < $prom['condition']) {
                                $prom_goods[$k][$kk]['condition'] = ($prom['condition'] - $vv['price']) . '元';
                            }
                        } else {
                            if ($vv['num'] < $prom['condition']) {
                                $prom_goods[$k][$kk]['condition'] = ($prom['condition'] - $vv['num']) . '件';
                            }
                        }
                        if (!empty($prom['rule'])){
                            $prom_goods[$k][$kk]['next_mon'] = $prom['rule']['money'];
                            $prom_goods[$k][$kk]['next_sal'] = $prom['rule']['sale'];
                            $prom_goods[$k][$kk]['next_pas'] = $prom['rule']['is_past'];
                            $prom_goods[$k][$kk]['next_int'] = $prom['rule']['int'];
                            $prom_goods[$k][$kk]['next_cou'] = $prom['rule']['coupon'];
                            $prom_goods[$k][$kk]['next_gif'] = $prom['rule']['gift'];
                        }
                        $prom_goods[$k][$kk]['all'] = $prominfo[$kk]['good_object'];
                    }
                }
                M('cart')->where(['is_gift' => 1, 'user_id' => $user_id, 'pick_id' => $k, 'prom_id' => ['not in', $prom_id_list]])->delete();
            }
//            $pick = substr($pick, 0, strlen($pick) - 1);//去除最后一个字符

        } else {
            M('cart')->where(['user_id' => $user_id, 'is_gift' => 1])->delete();
        }

        /*------购物车中商品库存要判断线下库存------*/
        $result = $this->cartLogic->cartList($this->user, $this->session_id, 0, 1);
        $cart_goods = $result['cartList'];

        foreach ($cart_goods as $k=>$v){
            if (empty($v['viplimited'])){
                $cart_goods[$k]['viplimited'] = 10000;
            }else{
                $limit=M('order')->alias('a')->join('order_goods b','a.order_id=b.order_id')->where(['b.goods_id'=>$v['goods_id'],'a.user_id'=>$user_id,'a.order_status'=>[['=',4],['<',3],'or']])->sum('b.goods_num');
                $cart_goods[$k]['viplimited']-= $limit;
            }
        }

        $allprice = 0;
        if ($sale_r == 2) {
            /*---循环变更购物车商品的库存---*/
            foreach ($cart_goods as $km => $vm) {
                /*--线下库存的序列key--*/
                $bkey = $vm['pick_id'] . $vm['goods_id'];
                if ($vm['prom_type'] == 0 || $vm['prom_type'] == 3 || $vm['prom_type'] == 5) {
                    $dlcount = $dlinearr[$bkey];
                    if ($dlcount == null) $dlcount = 0;
                    $cart_goods[$km]['store_count'] = $dlcount;
                    if ($cart_goods[$km]['goods_num'] > $dlcount) {
                        $cart_goods[$km]['selected'] = 0;
                        $cart_goods[$km]['goods_num'] = $dlcount;
                        $rss = M('Cart')->where("id", $vm['id'])->save(['selected' => 0, 'goods_num' => $dlcount]);
                    } else {
                        if ($vm['selected'])
                            $allprice += $vm['goods_num'] * $vm['member_goods_price'];
                    }
                } else {
                    if ($cart_goods[$km]['goods_num'] > $vm['store_count']) {
                        $cart_goods[$km]['selected'] = 0;
                        M('Cart')->where("id", $vm['id'])->save(['selected' => 0, 'goods_num' => $vm['store_count']]);
                    } else {
                        if ($vm['selected'])
                            $allprice += $vm['goods_num'] * $vm['member_goods_price'];
                    }
                }
            }
        } else {
            /*---循环变更购物车商品的库存---*/
            foreach ($cart_goods as $km => $vm) {
                if ($cart_goods[$km]['goods_num'] > $vm['store_count']) {
                    $cart_goods[$km]['selected'] = 0;
                    M('Cart')->where("id", $vm['id'])->save(['selected' => 0, 'goods_num' => $vm['store_count']]);
                } else {
                    if ($vm['selected'])
                        $allprice += $vm['goods_num'] * $vm['member_goods_price'];
                }
            }
        }
        $this->assign("allprice", $allprice);

        $countC = M('Cart')->where('user_id', $this->user_id)->where('store_id', getMobileStoId())->where('state', '0')->where('is_gift', 0)->sum('goods_num');
        $this->assign("cart_num", $countC);

        /*---计算价格---*/
//        if (empty($result['total_price']))
//            $result['total_price'] = Array('total_fee' => 0, 'cut_fee' => 0, 'num' => 0, 'atotal_fee' => 0, 'acut_fee' => 0, 'anum' => 0);

        $cartsql = "select pickup_id,pickup_name from wxd_pick_up where pickup_id in(select pick_id from wxd_cart where user_id=" . $this->user_id . " and state=0 and store_id=" . getMobileStoId() . " GROUP BY pick_id)";
        $plist_cart = Db::query($cartsql);

        foreach ($plist_cart as $k => $v) {
            $isallsele = 1;
            foreach ($cart_goods as $kk => $vv) {
                if ($vv['pick_id'] == $v['pickup_id']) {
                    $plist_cart[$k]['goods'][] = $vv;
                    if ($vv["selected"] != 1) {
                        $isallsele = 0;
                    }
//                    else {
//                        $good_jg = 0;
//                        if ($vv['prom_type'] != 0 && $vv['prom_type'] != 3) {
//                            $prom = get_goods_promotion1($vv,null, $user_id);
//                            $good_jg = $prom['price'];
//                        } else {
//                            $good_jg = $vv['member_goods_price'];
//                        }
//                    }
                }
            }
            $plist_cart[$k]['selected'] = $isallsele;

            if (!empty($prom_goods)) {
                foreach ($prom_goods as $key => $val) {
                    if ($key == $v['pickup_id']) {
                        $plist_cart[$k]['condition'] = $val;
                    }
                }
            }

//            $freight_free = tpCache('shopping.freight_free', getMobileStoId()); // 全场满多少免运费
//            if ($selenum >= $freight_free || $selenum == 0) {
//                $plist_cart[$k]["moremoney"] = 0;
//            } else if ($selenum > 0) {
//                $plist_cart[$k]["moremoney"] = $freight_free - $selenum;
//            }
        }

        //$this->assign('cartList', $result['cartList']); // 购物车的商品
        $this->assign('cartList', $plist_cart); // 购物车的商品
//        $this->assign('total_price', $result['total_price']); // 总计
        upload_ylp_log('购物车列表');
        return $this->fetch('ajax_cart_list', getMobileStoId());
    }


    /*
 * ajax 获取用户收货地址 用于购物车确认订单页面
 */
    public function ajaxAddress()
    {

        $regionList = M('Region')->getField('id,name');

        $address_list = M('UserAddress')->where("user_id", $this->user_id)->select();
        $c = M('UserAddress')->where("user_id = {$this->user_id} and is_default = 1")->count(); // 看看有没默认收货地址
        if ((count($address_list) > 0) && ($c == 0)) // 如果没有设置默认收货地址, 则第一条设置为默认收货地址
            $address_list[0]['is_default'] = 1;

        $this->assign('regionList', $regionList);
        $this->assign('address_list', $address_list);
        return $this->fetch('ajax_address', getMobileStoId());
    }

    /**
     * ajax 删除购物车的商品
     */
    public function ajaxDelCart()
    {
        $ids = I("ids"); // 购物车ids
        M("Cart")->where("id", "in", $ids)->delete(); // 删除id为5的用户数据
        $return_arr = array('status' => 1, 'msg' => '删除成功', 'result' => ''); // 返回结果状态
        exit(json_encode($return_arr));
    }

    public function getNum()
    {
        $user_id = $this->user_id;
        $now0 = time();
        if (session('?user')) {
            /*--要剔除下架商品,活动有过期的商品,或者商品在参加活动之前被添加到购物车--*/
            $cartList = M('Cart')->alias('a')->join('goods b', 'a.goods_id=b.goods_id')->where(['a.user_id' => $user_id, 'a.store_id' => getMobileStoId(), 'a.state' => 0])
                ->field('a.id,a.prom_type,a.goods_id,a.prom_id,b.shop_price,b.store_count,b.is_on_sale,a.is_gift,b.prom_type as prom_type1')->select();

            if (!empty($cartList)) {
                foreach ($cartList as $k => $v) {
                    if ($v['is_on_sale'] == 1 && $v['on_time']< $now0 && ($v['down_time']> $now0 || empty($v['down_time'])) ) {
                        if ($v['prom_type'] != 0 && $v['is_gift'] == 0) {
                            $prom = get_goods_promotion1($v, null, $user_id);
                            if ($prom['is_end'] == 2 || $prom['is_end'] == 3 || $prom['is_end'] == 1) {
                                M('Cart')->where('id', $v['id'])->where('user_id', $user_id)->delete();
                            }
                        } else {
                            $where = [
                                'end_time' => ['>=', $now0],
                                'start_time' => ['<=', $now0],
                                'goods_id' => $v['goods_id'],
                                'is_end' => 0
                            ];
                            if ($v['prom_type1'] == 1) {//秒杀
                                $jj = M('flash_sale')->where($where)->find();
                                if ($jj) {
                                    M('Cart')->where('id', $v['id'])->where('user_id', $user_id)->delete();
                                }
                            }
                            if ($v['prom_type1'] == 2) {//团购
                                $jj = M('group_buy')->where($where)->find();
                                if ($jj) {
                                    M('Cart')->where('id', $v['id'])->where('user_id', $user_id)->delete();
                                }

                            }
                            if ($v['prom_type1'] == 4) {//积分购
                                $jj = M('flash_sale')->where($where)->find();
                                if ($jj) {
                                    M('Cart')->where('id', $v['id'])->where('user_id', $user_id)->delete();
                                }
                            }
                        }
                    } else {
                        M('Cart')->where('id', $v['id'])->where('user_id', $user_id)->delete();
                    }
                }
            }

            $num = M("Cart")->where('user_id', $user_id)->where('store_id', getMobileStoId())->where('state', 0)->where('is_gift', 0)->sum('goods_num');
            if (empty($num)) {
                return 0;
            } else {
                return $num;
            }
        } else {
            return 0;
        }
    }

    /*----------------------------------地址管理------------------------------------*/
    /*
     * 添加地址
     */
    public function add_address()
    {
        upload_ylp_log('添加地址');
        $logic = new UsersLogic();
        $id = I('address_id', 0);
        $seleid = I("seleid");
        $this->assign("seleid", $seleid);
        $data = $logic->add_address($this->user_id, $id, I('get.'));
        if ($data['status'] != 1)
            return json(['code' => -1, 'fir' => $data['fir'],]);
        else {
            /*---获取地区---*/
            $region_list = get_region_list();
            $addlist = M('user_address')->where(['user_id' => $this->user_id])->select();
            foreach ($addlist as $kk => $vv) {
                $vv['provicename'] = $region_list[$vv['province']]['name'];
                $vv['cityname'] = $region_list[$vv['city']]['name'];
                $vv['districtname'] = $region_list[$vv['district']]['name'];
                $vv['twonname'] = $region_list[$vv['twon']]['name'];

                //$address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
                $addrstr = $vv['provicename'] . "-" . $vv['cityname'];
                if (!empty($vv['district']))
                    $addrstr .= "-" . $vv['districtname'];
                if (!empty($vv['twon']))
                    $addrstr = $addrstr . "-" . $vv['twonname'];
                $vv['addrstr'] = $addrstr;
                $addlist[$kk] = $vv;
            }

            $this->assign("addlist", $addlist);
            return json(['code' => 1, 'fir' => $data['fir'], 'id' => $data['result'],
                'data' => $this->fetch('ajax_addrlist', getMobileStoId())]);
        }
    }

    /*
     * 地址编辑
     */
    public function edit_address()
    {
        upload_ylp_log('编辑地址');
        $logic = new UsersLogic();
        $data = $logic->add_address($this->user_id, 0, I('get.'));
        if ($data['status'] != 1)
            return json(['code' => -1]);
        else {
            return json(['code' => 1]);
        }
    }

    /*
     * 获取省市区
     */
    public function get_area()
    {
        $id = I('aid');
        $address = M("user_address")->where('address_id', $id)->find();
        //获取省份
        $p = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
        $c = M('region')->where(array('parent_id' => $address['province'], 'level' => 2))->select();
        $d = M('region')->where(array('parent_id' => $address['city'], 'level' => 3))->select();
        $data['province'] = $p;
        $data['city'] = $c;
        $data['district'] = $d;
        $region_list = get_region_list();

        $addrstr = $region_list[$address['province']]['name'] . "-" . $region_list[$address['city']]['name'];
        if (!empty($address['district']))
            $addrstr .= "-" . $region_list[$address['district']]['name'];
        if (!empty($address['twon']))
            $addrstr = $addrstr . "-" . $region_list[$address['twon']]['name'];
        $address['addname'] = $addrstr;

        if (empty($data['district'])) {
            $e = M('region')->where(array('parent_id' => $address['city'], 'level' => 4))->select();
            $data['twon'] = $e;
        } else {
            $e = M('region')->where(array('parent_id' => $address['district'], 'level' => 4))->select();
            $data['twon'] = $e;
        }
        $jsond = ['code' => 1, 'data' => $data, 'data2' => $address];
        return json($jsond);
    }

    /*
     * 地址删除
     */
    public function del_address()
    {
        upload_ylp_log('删除地址');
        $backaddr = null;
        $id = I('get.id');
        $seleid = I("seleid");

        $region_list = get_region_list();

        $address = M('user_address')->where("address_id", $id)->find();
        $row = M('user_address')->where(array('user_id' => $this->user_id, 'address_id' => $id))->delete();
        // 如果删除的是默认收货地址 则要把第一个地址设置为默认收货地址
        if ($address['is_default'] == 1) {
            $address2 = M('user_address')->where("user_id", $this->user_id)->find();
            $address2 && M('user_address')->where("address_id", $address2['address_id'])->save(array('is_default' => 1));
            if ($address2) {
                $provinename = M('region')->where("id", $address2['province'])->field('name')->find();
                $cityname = M('region')->where("id", $address2['city'])->field('name')->find();
                $distname = M('region')->where("id", $address2['district'])->field('name')->find();
                $twonname = M('region')->where("id", $address2['twon'])->field('name')->find();

                $addrstr = $provinename['name'] . "-" . $cityname['name'] . "-" . $distname['name'];
                if ($twonname) {
                    $addrstr = $addrstr . "-" . $twonname['name'];
                }
                $address2['addrstr'] = $addrstr . " " . $address2['address'];
                $backaddr = $address2;
                $seleid = $address2['address_id'];
            }
        } else {
            $provinename = $region_list[$address['province']]['name'];
            $cityname = $region_list[$address['city']]['name'];
            $distname = $region_list[$address['district']]['name'];
            $twonname = $region_list[$address['twon']]['name'];
            $addrstr = $provinename . "-" . $cityname;
            if ($distname) {
                $addrstr = $addrstr . "-" . $distname;
            }
            if ($twonname) {
                $addrstr = $addrstr . "-" . $twonname;
            }
            $address['addrstr'] = $addrstr . " " . $address['address'];
            $backaddr = $address;
        }
        $this->assign("seleid", $seleid);

        if (!$row)
            return json(['code' => -1]);
        else {
            /*---获取地区---*/
            $addlist = M('user_address')->where(['user_id' => $this->user_id])->select();
            foreach ($addlist as $kk => $vv) {
                $vv['provicename'] = $region_list[$vv['province']]['name'];
                $vv['cityname'] = $region_list[$vv['city']]['name'];
                $vv['districtname'] = $region_list[$vv['district']]['name'];
                $vv['twonname'] = $region_list[$vv['twon']]['name'];

                //$address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
                $addrstr = $vv['provicename'] . "-" . $vv['cityname'];
                if (!empty($vv['district']))
                    $addrstr .= "-" . $vv['districtname'];
                if (!empty($vv['twon']))
                    $addrstr = $addrstr . "-" . $vv['twonname'];
                $vv['addrstr'] = $addrstr;
                $addlist[$kk] = $vv;
            }

            $this->assign("addlist", $addlist);
            return json(['code' => 1, 'address2' => $backaddr,
                'data' => $this->fetch('ajax_addrlist', getMobileStoId())]);
        }
    }

    /*--绑定手机号--*/
    public function reg_user()
    {
        $code = I('co');
        $mobile = I('mob');
        $session_id = session_id();
        $logic = new UsersLogic();
        $stoid = getMobileStoId();

        mlog("0", 'reg_user/' . $stoid);

        if (getMobileStoId() != "1") {
            $check_code = $logic->check_validate_code($code, $mobile, $session_id);
            if ($check_code['status'] != 1) {
                return array('status' => -1, 'msg' => $check_code['msg']);
                exit;
            }
        }

        mlog("1", 'reg_user/' . $stoid);

        $map = null;
        // 成为分销商条件
        $distribut_condition = tpCache('distribut.condition', $stoid);
        $distribut_switch = tpCache('distribut.switch', $stoid);
        if ($distribut_switch === 1 && $distribut_condition === 0 && $this->user['is_distribut'] == 0)  // 本来要购买成为分销,后来商家改为直接注册就可以成为分销商进行更改
        {
            //判断分销可以注册的人数
            $distr_num = M('store_distribut')->where('store_id', $stoid)->field('distribut_num')->find();
            $count1 = M("users")->where('store_id', getMobileStoId())->where('is_distribut', 1)->count();
            if ($distr_num['distribut_num'] > $count1) {
                $map['is_distribut'] = 1;
                $map['be_distribut_time'] = time();
                $map['be_dis_condition'] = "直接购买成为分销商";
            } else {
                $is_out = M("store_module_endtime")
                    ->where('store_id', getMobileStoId())
                    ->where('type', 2)->find();
                if (empty($is_out)) {
                    $yy = M('distri_price')->where('type', 0)->where('money', 0)->find();
                    $allnum = $yy['user_num'];
                    if ($allnum > $count1) {
                        $map['is_distribut'] = 1;
                        $map['be_distribut_time'] = time();
                        $map['be_dis_condition'] = "直接购买成为分销商";
                    }
                }
            }
        }

        $map = ['mobile' => $mobile];
        $is_distri_rate = tpCache('distribut.is_distri_rate', $stoid);   //分销商奖励
        $is_invitation_rate = tpCache('distribut.is_invitation_rate', $stoid);  //是否参与邀请送积分或代金券

        //启动事务
        Db::startTrans();
        try {
            $first_leader = Cookie::get('first_leader');//上级会员ID
            if ($first_leader != "") {
                $first_info = M('users')->where(array('user_id' => $first_leader, 'store_id' => $stoid))->find();

                if ($first_info && $first_info['is_distribut'] == "1" ) {
                    $map['first_leader'] = $first_leader;
                } else {
                    //往上找会员是否的分销商,
                    //如果是分销商则注册会员的 first_leader 就是此分销商
                    $first_info0 = M('users')->where(array('user_id' => $first_info['first_leader'], 'store_id' => $stoid))->find();
                    if ($first_info0 && $first_info0['is_distribut'] == "1" ) {
                        $map['first_leader'] = $first_info0['user_id'];
                    }
                }
                if ($map['first_leader']) {
                    $first_leader = M('users')->where("user_id", $map['first_leader'])->field("first_leader,second_leader")->find();
                    $map['second_leader'] = $first_leader['first_leader']; //  第一级推荐人
                    $map['third_leader'] = $first_leader['second_leader']; // 第二级推荐人
                    //他上线分销的下线人数要加1
                    M('users')->where(array('user_id' => $map['first_leader']))->setInc('underling_number');
                    M('users')->where(array('user_id' => $map['second_leader']))->setInc('underling_number');
                    M('users')->where(array('user_id' => $map['third_leader']))->setInc('underling_number');
                } else {
                    $map['first_leader'] = 0;
                }
            }

            mlog("6", 'reg_user/' . $stoid);

            $rs = M("users")->where("user_id", $this->user_id)->save($map);
            $user_info['api_token'] = tpCache('shop_info.api_token', $stoid);

            $map['user_id']=$this->user_id;
            $news_info = $map;

            $is_distri_rate = tpCache('distribut.is_distri_rate', $stoid);   //分销商奖励
            $is_invitation_rate = tpCache('distribut.is_invitation_rate', $stoid);  //是否参与邀请送积分或代金券
            mlog("7", 'reg_user/' . $stoid);

            //邀请赠送积分
            if (!empty($first_info) &&
                !empty($first_info['mobile']) &&
                !empty($first_info['erpvipid'])
            ) {   //如果上级会员是绑定,说明是通过链接进来,给发链接的用户赠送积分、优惠券
                /*---当上级不是分销商 或者 开启分销商奖励,参与邀请送积分或代金券---*/
                if ($first_info['is_distribut'] == 0 || $is_distri_rate == 0 || ($is_invitation_rate == 1 && $is_distri_rate == 1)) {
                    mlog("701", 'reg_user/' . $stoid);
                    $inv_type = tpCache('basic.inv_type', $stoid);
                    $inv_info = tpCache('basic.invitation_rate', $stoid);
                    mlog("703" . $inv_info, 'reg_user/' . $stoid);
                    $invitation_rate = json_decode($inv_info, true);

                    /*--当上级的会员是绑定了的--*/
                    if ($inv_type == 0) { //邀请送积分
                        if ($invitation_rate['inv_jf'] != "") {
                           /*--
                            $data = array(
                                'Id' => $first_info["erpvipid"],//会员id
                                'Integral' => $invitation_rate['inv_jf'],//金额
                                'Remark' => '邀请送积分',
                            );
                            $add_rs = getApiData("wxd.vip.addreduce", $user_info['api_token'], array($data));
                            mlog("711邀请送积分:" . json_encode($data) . "返回:" . $add_rs, 'smslog/' . $stoid);
                            $add_rs = json_decode($add_rs, true);
                            if ($add_rs['code'] != 1) {
                                //  return json(array('status' => -1, 'msg' => '服务器繁忙, 请联系管理员!'));
                            }--*/
                            $accdb=tpCache("shop_info.ERPId",$stoid);
                            $res=com_update_integral($accdb,$first_info,$invitation_rate['inv_jf'],"购买时注册邀请送积分",$module="cart");
                        }
                    } else {
                        if ($invitation_rate['inv_coupon'] != "") {
                            mlog("72", 'reg_user/' . $stoid);
                            $coupon = M("coupon")->where("id", $invitation_rate['inv_coupon'])->find();
                            if ($coupon["send_end_time"] > time()) {
                                $CashRepNo = "1005" . date("ymdHis") . get_total_millisecond();
                                $data = array(
                                    'CashRepNo' => $CashRepNo,//券号
                                    'VIPId' => $first_info["erpvipid"],//会员id
                                    'Sum' => $coupon["money"],//金额
                                    'SendMan' => '邀请发放',//发放类型
                                    'Operator' => '微信商城',//来源
                                    'BuySum' => $coupon["condition"],//满多少才能使用
                                    'Remark' => '邀请送微券【消费满' . $coupon["condition"] . '元可用】',
                                );
                                if ($coupon["use_start_time"] != "") {
                                    $data['BeginDate'] = date('Y-m-d', $coupon["use_start_time"]);//使用开始日期
                                }
                                if ($coupon["use_end_time"] != "") {
                                    $data['ValidDate'] = date('Y-m-d', $coupon["use_end_time"]);//截止期限
                                }

                                $add_rs = getApiData("wxderp.cashreplace.add", $user_info['api_token'], array($data));
                                mlog("721邀请送微券:" . json_encode($data) . "返回:" . $add_rs, 'reg_user/' . $stoid);
                                $add_rs = json_decode($add_rs, true);
                                if ($add_rs['code'] != 1) {
                                    // return json(array('status' => -1, 'msg' => '服务器繁忙, 请联系管理员!'));
                                }

                                $colorlist = urlencode("#FF0000|#173177|#173177|#173177|#173177|#173177|#FF0000|#ca003a");
                                $pd['typeid'] = "1006";
                                $pd['backurl'] = curHostURL() . '/index.php/Mobile/User/coupon/stoid/' . $stoid;
                                $pd['colorlist'] = $colorlist;

                                $pd['title'] = "您好,恭喜您成功领取" . $coupon["money"] . "元优惠券";
                                $pd['key1'] = $first_info['vipname'];
                                $pd['key2'] = $coupon["money"] . "元优惠券";
                                $pd['key3'] = date("Y-m-d");
                                $pd['remark'] = "点击详情查看优惠券详情信息";

                                $Event = \think\Loader::controller('home/Api');
                                $Event->web_poswxcode($pd, $stoid, $first_info);
                            }
                        }
                    }
                }
            }

            $wx_user = M('wx_user')->where('store_id', $stoid)->find();
            $jssdk = new \app\mobile\logic\Jssdk($wx_user['appid'], $wx_user['appsecret']);

            mlog("7.1", 'reg_user/' . $stoid);
            /*---当是分销商 且 开启分销商奖励---*/
            if ($is_distri_rate == 1) {

                mlog("7.1.1", 'reg_user/' . $stoid);
                $ydd_str = tpCache('distribut.invitation_ratelist', $stoid);
                $ydd = json_decode($ydd_str, true);
                // 一级 分销商赚 的钱
                if ($ydd['first_money'] > 0 && $first_info['is_distribut'] == 1) {
                    // 微信推送消息
                    $tmp_user = $first_info;
                    $rrs1 = $this->add_user_money($ydd['first_money'], $first_info['user_id']);
                    if ($rrs1) {
                        $odata['user_id'] = $first_info['user_id'];
                        $odata['create_time'] = time();
                        $odata['money'] = $ydd['first_money'];
                        $odata['remark'] = "线下一级注册";
                        $odata['store_id'] = $stoid;
                        $odata['order_sn'] = "zc" . date("YmdHis") . rand(1000, 9999);
                        $odata['status'] = 1;
                        $odata['type'] = 4;
                        M('withdrawals')->save($odata);

                        if ($tmp_user['oauth'] == 'weixin') {
                            $wx_content = "你的一级下线刚刚绑定,你获得 ¥" . $ydd['first_money'] . "奖励 !";
                            $first_res = $jssdk->push_msg($tmp_user['openid'], $wx_content);
                            mlog($news_info['mobile'] . "的一级[" . $first_res['mobile'] . "]推送返回:" . json_encode($first_res), 'reg_user/' . $stoid);
                        }
                    }
                }
                mlog("7.1.2", 'reg_user/' . $stoid);

                // 二级 分销商赚 的钱
                if ($news_info['second_leader'] > 0 && $ydd['second_money'] > 0) {
                    $fuid2 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $news_info['second_leader'])->find();
                    if ($fuid2 && $fuid2['is_distribut'] == 1) {

                        $rrs2 = $this->add_user_money($ydd['second_money'], $fuid2['user_id']);
                        if ($rrs2) {
                            $odata['user_id'] = $fuid2['user_id'];
                            $odata['create_time'] = time();
                            $odata['money'] = $ydd['second_money'];
                            $odata['remark'] = "线下二级注册";
                            $odata['store_id'] = $stoid;
                            $odata['order_sn'] = "zc" . date("YmdHis") . rand(1000, 9999);
                            $odata['status'] = 1;
                            $odata['type'] = 4;
                            M('withdrawals')->save($odata);

                            // 微信推送消息
                            $tmp_user = $fuid2;
                            if ($tmp_user['oauth'] == 'weixin') {
                                $wx_content = "你的二级下线刚刚绑定,你获得 ¥" . $ydd['second_money'] . "奖励 !";
                                $second_res = $jssdk->push_msg($tmp_user['openid'], $wx_content);
                                mlog($news_info['mobile'] . "的二级[" . $tmp_user['mobile'] . "]推送返回:" . json_encode($second_res), 'reg_user/' . $stoid);
                            }
                        }
                    }
                }

                mlog("7.1.3", 'reg_user/' . $stoid);
                // 三级 分销商赚 的钱
                if ($news_info['third_leader'] > 0 && $ydd['third_money'] > 0) {
                    $fuid3 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $news_info['third_leader'])->find();
                    if ($fuid3 && $fuid3['is_distribut'] == 1) {
                        $rrs3 = $this->add_user_money($ydd['third_money'], $fuid3['user_id']);
                        if ($rrs3) {
                            $odata['user_id'] = $fuid3['user_id'];
                            $odata['create_time'] = time();
                            $odata['money'] = $ydd['third_money'];
                            $odata['remark'] = "线下三级注册";
                            $odata['store_id'] = $stoid;
                            $odata['order_sn'] = "zc" . date("YmdHis") . rand(1000, 9999);
                            $odata['status'] = 1;
                            $odata['type'] = 4;
                            M('withdrawals')->save($odata);

                            // 微信推送消息
                            $tmp_user = $fuid3;
                            if ($tmp_user['oauth'] == 'weixin') {
                                $wx_content = "你的三级下线刚刚绑定,你获得 ¥" . $ydd['third_money'] . "奖励 !";
                                $third_res = $jssdk->push_msg($tmp_user['openid'], $wx_content);
                                mlog($news_info['mobile'] . "的三级[" . $tmp_user['mobile'] . "]推送返回:" . json_encode($third_res), 'reg_user/' . $stoid);
                            }
                        }
                    }
                }
                mlog("7.1.4", 'reg_user/' . $stoid);
            }

            mlog("7.3", 'reg_user/' . $stoid);
            Db::commit();
            return array('status' => 1, 'msg' => "绑定成功");
        }catch (\Exception $e){
            Db::rollback();
            mlog("err-".json_encode($e), 'reg_user/' . $stoid);
            return array('status' => -1, 'msg' => "绑定失败");
        }
    }


    /*---循环更新订单的状态---*/
    public function updatecart2()
    {
        $tocart = I("state");
        if ($tocart == 0) {
            $result = $this->cartLogic->cartList($this->user, $this->session_id, 1, 1); //获取购物车商品
        } else if ($tocart == 1) {
            $result = $this->cartLogic->gobuyList($this->user, $this->session_id); //获取立即购买的商品
        } else if ($tocart == 2) {
            $result = $this->cartLogic->gointeList($this->user, $this->session_id); //获取积分购的商品
        }
        return "1";
    }

    /*----增加会员的余额----*/
    public function add_user_money($addmoney,$user_id){
        $rs=M('users')->where('user_id',$user_id)->setInc('user_money',$addmoney);
        return $rs;
    }

    /*----相应的返回redis----*/
    public function backredis($no,$notime)
    {
        if ($_REQUEST['act'] == 'submit_order') {
            $popvalue = null;
            $popvalue2 = null;
            $stoid = I("stoid");
            //拼单有关
            $ispt = I('is_pt');
            $pdid = I('pdid');
            $team_qh = I('team_qh');
            $pdgoodsnum = I('pdgoodsnum/d', 0);
            $sd = I('da');

            if (!empty($sd)) {
                $skillarray = json_decode($sd, true);
                foreach ($skillarray as $lk => $lv) {
                    $name = 'ms' . $lv['fid'] . '-' . $stoid;
                    $num0 = $lv['num'];
                    //秒杀队列控制
                    for ($i = 0; $i < $num0; $i++) {
                        $this->redis->lPush($name, time() . '_' . $i);
                    }

                    $key=$no.'-1-'.$lv['fid'].'-'.$num0.'-'.$notime;
                    $this->redis->sRem('redis_set_'.$stoid,$key);

                }
            }
            //如果是拼单
            if ($ispt) {
                $name = 'pind' . $pdid . '-' . $stoid;
                //拼单队列控制
                for ($i = 0; $i < $pdgoodsnum; $i++) {
                    $this->redis->lPush($name, time() . '_' . $i);
                }
                if ($team_qh && $ispt == '2') {
                    $name2 = 'pind' . $pdid . '-' . $team_qh . $stoid;
                    $this->redis->lPush($name2, time() . '_0' );

                }
                $key=$no.'-6-'.$pdid.'-'.$pdgoodsnum.'-'.$notime.'-'.$team_qh;
                $this->redis->sRem('redis_set_'.$stoid,$key);
            }
        }
    }
}