Team.php
102 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
<?php
/**
* tpshop
* ============================================================================
* 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
* 网站地址: http://www.tp-shop.cn
* ----------------------------------------------------------------------------
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
* 不允许对程序代码以任何形式任何目的的再发布。
* ============================================================================
* Author: IT宇宙人
* Date: 2015-09-09
*/
namespace app\mobile\controller;
use app\home\logic\UsersLogic;
use think\Cookie;
use Think\Db;
use think\Page;
class Team extends MobileBase {
public $user_id = 0;
public $user = array();
public $cartLogic; // 购物车逻辑操作类
public $redis =null;
public function __construct() {
parent::__construct();
$this->cartLogic = new \app\home\logic\CartLogic();
$nuser=array(
'Cart3_team_nosub', 'Cart3_team',
);
if(session('?user'))
{
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); //存储用户信息
}
}
// $ylpapp_list=M('store_config')->where('store_id='.getMobileStoId())->find();//app使用
// $ylpapp_list=$ylpapp_list['ylpapp_list'];
$getylpres=getylapp_res(getMobileStoId(),'3');
if ($getylpres)
{
$this->assign('ylpres',$getylpres);
}
$nologin = array(
'goodsInfo','team_info','team_fun','buy_points','team_success','teamshow','every_body_team','ajax_team_list'
);
if (!$this->user_id && !in_array(ACTION_NAME, $nologin)) {
$this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId())));
exit;
}
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;
}
}
}
/**
* 参团只针对参团,ajax将商品加入购物车
*/
public function addcart_pt()
{
//删除wxd_cart中立即购买的数量
M('cart')->where('user_id',$this->user_id)
->where('store_id',getMobileStoId())
->where('state<>0')->delete();
$this->cartLogic = new \app\home\logic\CartLogic();
$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
if ($this->user_id == 0) {
return array('status' => -101, 'msg' => '非法操作', 'result' => '');
}
//在加入购物车要判断是否满团
$teamid=I('teamid');
$team_qh=I("team_qh");
$stoid=I('stoid');
$teamact = M('teamlist')->where('id', $teamid)->where('store_id', $stoid)->find();
if($team_qh ) {
$ordract = M('order')->where('pay_time>0')->where('order_status<>3')
->where('pt_prom_id', $teamid)->where('pt_listno',$team_qh)
->where('store_id', $stoid)->count();
//如果团的人数已经满
if($ordract>=$teamact['ct_num'] && $teamact['kttype']==2){
return array('status' => -101, 'msg' => '该团人数已经满团', 'result' => '');
}
}
//判断团的时间
$tgr=M('teamgroup')->where('listno',$team_qh)->where('team_id',$teamid)->
where('store_id',$stoid)->find();
$etime=strtotime("+".$teamact['kt_time']." hour",$tgr['buystartdate']);
if($etime<=time()){
return array('status' => -101, 'msg' => '该团已经结束', 'result' => '');
}
$result =null;
$user_id = $this->user_id;
$pickup_id=$pick_up;
if ($user_id == 0)
return array('status' => -101, 'msg' => '购买商品必须先登录', 'result' => '');
if ($goods_num <= 0)
return array('status' => -2, 'msg' => '购买商品数量不能为0', 'result' => '');
if (empty($pickup_id)) {
return array('status' => -2, 'msg' => '取货门店不能为空', 'result' => '');
}
$t=time();
/*查找商品*/
$goods = M('Goods')
->where("on_time<".$t." and (down_time>".$t." or down_time=0 or down_time='' or down_time is null)")
->where("goods_id", $goods_id)->where('is_on_sale', 1)->find(); // 找出这个商品
if (empty($goods))
return array('status' => -3, 'msg' => '购买商品不存在或者已下架', 'result' => '');
//判断库存
if (($goods_num + $goods_num) > $goods['store_count']) {
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
$where = " goods_id = :goods_id";
$cart_bind['goods_id'] = $goods_id;
if ($user_id > 0) {
$where .= " and (user_id = :user_id) ";
$cart_bind['user_id'] = $user_id;
}
//核查用户购买数量
$where = "user_id = :user_id and order_status in(0,1,2,4,6) and pt_status=1 and add_time>" . $teamact['start_time'] . " and add_time<" . $teamact['end_time'];
$order_id_arr = M('order')->where($where)->bind(['user_id' => $user_id])->getField('order_id', true);
$onlybuy=$teamact['goods_num']-$teamact['buy_num'];
if ($order_id_arr) {
$goods_num1 = M('order_goods')->where("prom_id={$goods['prom_id']} and prom_type={$goods['prom_type']} and order_id in (" . implode(',', $order_id_arr) . ")")->sum('goods_num');
if ($goods_num1+$goods_num > $teamact['buy_limit']) {
return array('status' => -101, 'msg' => '超出活动限购数量', 'result' => '');
}
}
//购买数量,必须大于开团起购数
if( $goods_num<$teamact['minbuynum']
&& $goods['store_count']>$teamact['buy_limit']
&& $onlybuy>$teamact['buy_limit']
){
return array('status' => -101, 'msg' => '参团起购是'.$teamact['minbuynum'].'件', 'result' => '');
}
if ($goods_num > $teamact['goods_num']-$teamact['buy_num']) {
return array('status' => -4, 'msg' => "此商品的活动库存不足", 'result' => '');
}
$price=0;
if($teamact['kttype']==3)
$price = $teamact['yf_price']; //阶梯团是预付金额
else
$price = $teamact['price']; //阶梯团是预付金额
mlog("<会员:" . $user_id . ">" . "查找购物车", "addcart_pt/" . $goods['store_id']);
// 如果商品购物车已经存在
$data = array(
'user_id' => $this->user_id, // 用户id
'session_id' => 0, // sessionid
'goods_id' => $goods_id, // 商品id
'goods_sn' => $goods['goods_sn'], // 商品货号
'goods_name' => $goods['goods_name'], // 商品名称
'market_price' => $goods['market_price'], // 市场价
'goods_price' => $price, // 购买价
'member_goods_price' => $price, // 会员折扣价默认为购买价
'goods_num' => $goods_num, // 购买数量
'sku' => $goods['sku'], // 商品条形码
'add_time' => time(), // 加入购物车时间
'prom_type' => $goods['prom_type'], // 0 普通订单,1 限时抢购, 2 团购 ,3 促销优惠,4积分购,5优惠促销,6拼单
'prom_id' => $goods['prom_id'], // 活动id
'pick_id' => $pickup_id,
'store_id' => $goods['store_id'],
'state'=>1
);
mlog("<会员:" . $user_id . ">" . json_encode($data), "addcart_pt/" . $goods['store_id']);
$insert_id = M('Cart')->add($data);
upload_ylp_log('商品加入购物车');
if($insert_id)
exit(json_encode(array('status' => 1, 'msg' => '成功加入购物车', 'result' =>$insert_id)));
else
exit(json_encode(array('status' => -1, 'msg' => '加入购物车失败', 'result' =>$insert_id)));
}
/**
* 秒杀活动列表
*/
public function team_list()
{
$stype = I('type/d', 1);
$this->assign('type', $stype);
upload_ylp_log('秒杀活动列表');
return $this->fetch("", getMobileStoId());
}
/**
* 秒杀活动列表
*/
public function ajax_team_list()
{
$stype = I('type/d', 1);
$now_time = time(); //当前时间
$where = " and f.is_end=0";
/*---当开始时间大于当前时间,就是活动已经开始---*/
if ($stype == 1) {
$where = " and f.start_time<" . $now_time;
$fname = "ajax_team_list_1";
} else {
$where = " and f.start_time>=" . $now_time;
$fname = "ajax_team_list_2";
}
//秒杀商品
$id = getMobileStoId();
if (is_int($now_time / 7200)) { //双整点时间,如:10:00, 12:00
$start_time = $now_time;
} else {
$start_time = floor($now_time / 7200) * 7200; //取得前一个双整点时间
}
$end_time = $start_time + 7200; //结束时间
$tt=time();
/*--and f.goods_num>f.buy_num--*/
$count = DB::name('goods')
->alias('g')
->join('teamlist f', 'g.goods_id = f.goods_id', 'inner')
->where(" g.store_id=" . $id . " and g.is_on_sale=1 and f.show_time<" . $now_time . " and f.end_time>" . $now_time . $where)
->where("g.on_time<".$tt." and (g.down_time>".$tt." or g.down_time=0 ) and g.is_mainshow=1")
->where('f.is_show=1 and f.is_end=0')
->count();
//dump(" g.store_id=".$id." and g.is_on_sale=1 and f.goods_num>f.buy_num and f.show_time<".$now_time." and end_time>".$now_time);exit;
$pagesize = 10;//C('PAGESIZE'); //每页显示数
$Page = new Page($count, $pagesize); // 实例化分页类 传入总记录数和每页显示的记录数
$show = $Page->show(); // 分页显示输出
$this->assign('page', $show); // 赋值分页输出
//mlog($Page->firstRow.','.$Page->listRows,"seckill");
/*and f.goods_num>f.buy_num*/
$list = DB::name('goods')
->alias('g')
->join('teamlist f', 'g.goods_id = f.goods_id', 'inner')
->where(" g.store_id=" . $id . " and g.is_on_sale=1 and f.show_time<" . $now_time . " and f.end_time>" . $now_time . $where)
->where("g.on_time<".$tt." and (g.down_time>".$tt." or g.down_time=0 )")
->where('f.is_show=1 and f.is_end=0')
->order('f.ordid')
->limit($Page->firstRow . ',' . $Page->listRows)
->select();
foreach ($list as $item) {
$item['cur_num'] = $item['goods_num'] - $item['buy_num'];
$list1[] = $item;
}
$this->assign('list', $list1);
$p = I("p/d", 1);
if ($count <= $p * 10) {
$this->assign('mshow', 0);
} else {
$this->assign('mshow', 1);
}
upload_ylp_log('秒杀活动列表');
return $this->fetch($fname, getMobileStoId());
}
//详情
public function team_fun(){
$stoid=I('stoid/d',1);
//form表单提交
C('TOKEN_ON', true);
$goodsLogic = new \app\home\logic\GoodsLogic();
/*---查看是否有秒杀订单超时的订单,以半小时为限---*/
clear_pind_Order($stoid);
/*--更新一些订单的状态--*/
//$timer = new \app\timer\controller\Index();
//$timer->deal_jt_pt($stoid);
//$timer->claer_pt_out($stoid);
$goods_id = I("get.id/d", 0);
$tt=time();
/*--商品--*/
$goods = M('Goods')
->where("on_time<".$tt." and (down_time>".$tt." or down_time=0 ) and is_mainshow=1")
->where("goods_id", $goods_id)->where('is_on_sale', 1)->find();
if (empty($goods)) {
$this->error("此商品已经下架",U('Mobile/index/index', array('stoid' => getMobileStoId())));
}
$is_chat=tpCache("basic.is_chat",$stoid);
$rs_server=M('storage_recharge_detail')
->where('store_id',$stoid) ->where('admin_id<>0')
->where('end_time>'.$tt)
->find();
$this->assign('is_chat', $is_chat && $rs_server);
$goods_images_list = M('GoodsImages')->where("goods_id", $goods_id)->order('ismain desc')->select(); // 商品 图册
$seckill_buy_info = M('teamlist')->where('is_end', 0)->where('is_show',1)
->where(['goods_id' => $goods_id, 'show_time' => ['<=', time()], 'end_time' => ['>=', time()]])
->find(); // 找出这个商品
if (empty($seckill_buy_info)) {
$this->error("此商品没有拼团活动",
U('Mobile/Goods/goodsInfo', array('id' => $goods_id, 'stoid' => getMobileStoId())));
}
//找出正在进行的,该会员参与的团没有满团,都要查看详情
$acc=M('order')->where('pt_prom_id',$seckill_buy_info['id'])
->where('order_status=1 or order_status=0')
->where('pt_status=1')
->where('user_id',$this->user_id)
->find();
if($acc) $this->assign('ac', $acc);
$this->assign('pd_info', $seckill_buy_info);
if($seckill_buy_info['kttype']==3){
$ct_rylist=json_decode($seckill_buy_info['ct_rylist'],true);
$this->assign('ct_rylist', $ct_rylist);
$nn=count($ct_rylist)-1;
$this->assign("max_num",$ct_rylist[$nn]['rynum']);
}
/*---设置活动是秒杀销售量---*/
//$redis = new \Redis();
//$redis->connect(redisip, 6379);
$redis=get_redis_handle();
$name='pind'.$seckill_buy_info['id'].'-'.$stoid;
$namec='pind'.$seckill_buy_info['id'].'-'.$stoid.'c';
$cval=F($namec);
if(empty($cval)){
for($i=0;$i<($seckill_buy_info['goods_num']-$seckill_buy_info['buy_num']);$i++){
$redis->lPush($name,"pt".$i);
}
F($namec,1);
}
$len= $redis->lLen($name);
$this->assign("len",$len);
if ($seckill_buy_info['start_time'] > time()) { //活动进行中
$this->assign('isyure', 1);
}
$user_id = cookie('user_id');
$user=$this->user;
/*------商品规格--------*/
$sp1 = $goods["goods_spec"];
$cn1 = $goods["goods_color"];
$guige1 = "规格";
$gorder1 = 1;
$tgg1 = "";
if ($sp1 == "" && $cn1 == "") {
$tgg1 = $guige1 . $gorder1;
$gorder1++;
} else if ($sp1 != "" && $cn1 == "") {
$tgg1 = $sp1;
} else if ($sp1 == "" && $cn1 != "") {
$tgg1 = $cn1;
} else {
$tgg1 = $sp1 . "/" . $cn1;
}
$goods['guige'] = $tgg1;
if ($goods['brand_id']) {
$brnad = M('brand')->where("id", $goods['brand_id'])->find();
$goods['brand_name'] = $brnad['name'];
}
if ($goods['nation_id']) {
$nation = M('nation')->where("id", $goods['nation_id'])->find();
$goods['nation_name'] = $nation['name'];
}
if ($goods['cat_id']) {
$cat = M('goods_category')->where("id", $goods['cat_id'])->find();
$goods['cat_name'] = $cat['name'];
}
/*---规格筛选---*/
$filter_spec = M('goods')->where('sku', $goods['sku'])->where('store_id', $stoid)->where("is_on_sale", 1)
->field('goods_id,erpwareid,goods_name,brand_id,nation_id,cat_id,goods_spec,goods_color,goods_unitname,goods_sn,sku,shop_price,
market_price,sales_sum,store_count,original_img,prom_type,goods_remark,mz_price,distr_type,cardprice1,cardprice2,cardprice3,spec_img,viplimited')
->select();
foreach ($filter_spec as $k4 => $v4) {
$gidarr4[$v4['erpwareid']] = $v4['goods_id'];
}
//查看销售规则,采用线下规则要计算所有规格的商品在线下的库存
$mstore = tpCache('basic.sales_rules', $stoid);
if ($mstore == 2) {
$this->assign('is_bline', 1);
} else {
$this->assign('is_bline', 0);
}
$rank_switch = tpCache('shopping.rank_switch', $stoid);//等级价格
$mz_switch = tpCache('shopping.is_beauty', $stoid);//美妆价格
$guige = "规格";
$gorder = 1;
$ary = null;
foreach ($filter_spec as $k => $v) {
/*-----过滤掉活动的商品-----*/
$now = time();
$wh0 = [
'end_time' => ['>=', $now],
'start_time' => ['<=', $now],
'id' => $v['prom_id'],
'goods_id' => $v['goods_id'],
'is_end' => 0,
];
$rs = M('group_buy')->where($wh0)->find();
if ($rs) {
unset($filter_spec[$k]);
continue;
}
$rs = M('integral_buy')->where($wh0)->where('is_show', 1)->find();
if ($rs) {
unset($filter_spec[$k]);
continue;
}
$rs = M('flash_sale')->where($wh0)->find();
if ($rs) {
unset($filter_spec[$k]);
continue;
}
$sp = $v["goods_spec"];
$cn = $v["goods_color"];
if ($sp == "" && $cn == "") {
$tgg = $guige . $gorder;
$gorder++;
} else if ($sp != "" && $cn == "") {
$tgg = $sp;
} else if ($sp == "" && $cn != "") {
$tgg = $cn;
} else {
$tgg = $sp . "/" . $cn;
}
if (empty($v['spec_img']))
$filter_spec[$k]['spec_img'] = $v['original_img'];
/*--该商品此卖了多少件--*/
$filter_spec[$k]['guige'] = $tgg;
$filter_spec[$k]['curprice'] = $v['shop_price'];
if($v['goods_id']==$goods_id){
$filter_spec[$k]['prom_type'] = 0;
}
if ($v['prom_type'] > 0) {
//找出活动的细节
$prom = $filter_spec[$k]['flash_sale'] = get_goods_promotion($v['goods_id']);
if ($prom['is_end'] != 1 && $prom['is_end'] != 4 && $prom['is_end'] != 2) {
$filter_spec[$k]['curprice'] = $filter_spec[$k]['flash_sale']['price'];
} else {
$filter_spec[$k]['prom_type'] = 0;
}
}
if ($v['prom_type'] == 0) {
if ($user['is_mzvip'] && $v['mz_price'] > 0 && $mz_switch) {
$filter_spec[$k]['curprice'] = $v['mz_price'];
}
if ($rank_switch) {
if ($user['card_field'] && $v[$user['card_field']] >0 && strtotime($user['card_expiredate']) > time()) {
$filter_spec[$k]['curprice'] = $v[$user['card_field']];
}
}
}
/*--规格排序--*/
if ($v["goods_id"] == $goods_id) {
$filter_spec[$k]['viplimited'] = $goods['viplimited'];
$ary = $filter_spec[$k];
unset($filter_spec[$k]);
} else {
if (empty($v['viplimited'])) {
$filter_spec[$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');
$limit += M('cart')->where(['goods_id' => $v['goods_id'], 'user_id' => $user_id])->where('state',0)->sum('goods_num');
$filter_spec[$k]['viplimited'] -= $limit;
}
}
}
/*--规格排序--*/
array_unshift($filter_spec, $ary);
$this->assign('filter_spec', $filter_spec);//规格参数
if (empty($goods['viplimited'])) {
$goods['viplimited'] = 10000;
} else {
$limit = M('order')->alias('a')->join('order_goods b', 'a.order_id=b.order_id')->where(['b.goods_id' => $goods_id, 'a.user_id' => $user_id, 'a.order_status' => [['=', 4], ['<', 3], 'or']])->sum('b.goods_num');
$limit += M('cart')->where(['goods_id' => $goods_id, 'user_id' => $user_id])->where('state',0)->sum('goods_num');
if ($limit >= $goods['viplimited']) {
$goods['viplimited'] = 0;
} else {
$goods['viplimited'] -= $limit;
}
}
$prom = get_goods_promotion1($goods, $seckill_buy_info, $this->user_id);
$curprice = $prom['price'];
if ($prom['is_end'] == 2) {
$this->assign('prom_num', 0); //活动设置数量
$this->assign('onlybuy', 0); //活动商品还可以买多少件
$this->assign('limit_num', 0); //秒杀没人限购数量
} else if ($prom['is_end'] != 1 && $prom['is_end'] != 4) {
$curprice = $prom['price'];
$goods['prom_type'] = $prom['prom_type'];
$goods['prom_id'] = $prom['prom_id'];
$this->assign('prom_num', $prom['goods_num']); //活动设置数量
$this->assign('onlybuy', $prom['onlybuy']); //活动商品还可以买多少件
$this->assign('limit_num', $prom['buy_limit']); //秒杀没人限购数量
} else {
//$this->error("此商品秒杀活动的数量已经售完",
//U('Mobile/Goods/goodsInfo',array('id'=>$goods_id,'stoid'=>getMobileStoId())));
$this->assign('prom_num', 0); //活动设置数量
$this->assign('onlybuy', 0); //活动商品还可以买多少件
$this->assign('limit_num', 0); //秒杀没人限购数量
}
$t = time();
M('Goods')->where("goods_id", $goods_id)->save(array('click_count' => $goods['click_count'] + 1)); // 统计点击数
$commentStatistics = $goodsLogic->commentStatistics($goods_id);// 获取某个商品的评论统计
$goods_collect_count = M('goods_collect')->where(array("goods_id" => $goods_id))->count(); //商品收藏数
$this->assign('commentStatistics', $commentStatistics);
$this->assign('goods_images_list', $goods_images_list);
$this->assign('goods', $goods);
$this->assign('curprice', $curprice);
$this->assign('goods_collect_count', $goods_collect_count); //商品收藏人数
$this->assign('seckill_buy_info', $seckill_buy_info);
upload_ylp_log('天天拼单商品详情');
}
public function team_info(){
$this->team_fun();
return $this->fetch('', getMobileStoId());
}
//会员一键参团,开团
public function teamshow()
{
$teamid = I('teamid/d');
$qh = I('qh');
$storeid = getMobileStoId();
//判断是否成团,如果pt_status=0就是未支付,就是要跳转到支付页面
$ord=M('order')->where('user_id',$this->user['user_id'])->where('pt_listno',$qh)
->where('store_id',$storeid)->where('pt_prom_id',$teamid)->where('order_status<>3')
->field('pt_status,order_id')
->find();
if($ord['pt_status']==0 && $ord){
$this->error("您已经购买了该商品,待支付中!",U('/Mobile/user/order_detail',array('stoid' => getMobileStoId(),'id'=>$ord['order_id'])));
}
$this->assign('ord',$ord);
/*---查看是否有拼单订单超时的订单---*/
clear_pind_Order($storeid);
//$timer = new \app\timer\controller\Index();
//$timer->deal_jt_pt($storeid);
//$timer->claer_pt_out($storeid);
//判断是否已经关注
$r = M('wx_openlist')->where("wxopenid",$this->user['openid'])
->where("store_id", getMobileStoId())->find();
$this->assign('issubscribe',$r['issubscribe']);
//获取活动和商品
$pdact = M('teamlist')->where(array('store_id' => $storeid, 'id' => $teamid))->find();
if($pdact['end_time']<=time() && empty($qh)){
$this->error("此拼单活动已经过期",
U('/Mobile/Goods/goodsInfo',array('stoid' => getMobileStoId(),'id'=>$pdact['goods_id'])));
}
if($pdact['is_end']==1){
$this->error("此拼单活动已经结束",
U('/Mobile/Goods/goodsInfo',array('stoid' => getMobileStoId(),'id'=>$pdact['goods_id'])));
}
if($pdact['is_show']==0){
$this->error("此拼单活动未启用",
U('/Mobile/Goods/goodsInfo',array('stoid' => getMobileStoId(),'id'=>$pdact['goods_id'])));
}
$et=0;
$tgr=M("teamgroup")->where('listno',$qh)
->where('team_id',$teamid)->where('store_id',$storeid)
->find();
if($tgr['kt_end_time']<time()){
$this->error("此团已经结束",
U('/Mobile/Goods/goodsInfo',array('stoid' => getMobileStoId(),'id'=>$pdact['goods_id'])));
}
$this->assign('tgr',$tgr);
//会员团,如果满团,要计算下redis
if ($pdact['kttype'] == 2 && empty($ord)) {
//$redis = new \Redis();
//$redis->connect(redisip, 6379);
$redis=get_redis_handle();
$name2 = 'pind' . $teamid . '-' .$qh. $storeid;
$len = $redis->lLen($name2);
if($len==null || $len<=0 ) {
$neerodr = M("order")->where('store_id', $storeid)
->where('pt_listno', $qh)->where('pt_prom_id', $teamid)
->where('pt_status', 0)
->where('order_status=1 or order_status=0')
->field('add_time,order_id,user_id')
->find();
$us=M("users")->where('user_id',$neerodr['user_id'])->field('nickname')->find();
$tt0=(int)$neerodr['add_time'];
$tt=strtotime("+5 minute",$tt0);
$tt2=time();
$tt3=$tt-$tt2;
$this->assign('needtime',$tt3);
$this->assign('needtime_id',$neerodr['order_id']);
$this->assign('needtime_nickname',$us['nickname']);
}
}
if($tgr) {
$et =$tgr['kt_end_time'];
$this->assign('et',$et);
}
$gd=M('goods')->where('goods_id',$pdact['goods_id'])
->field('goods_id,goods_name,market_price,shop_price,original_img,store_count,prom_type,prom_id,distr_type')->find();
$this->assign('pdact',$pdact);
$this->assign('gd',$gd);
$prom=null;
//当活动还没有过期的时候
if($et>time())
$prom = get_pt_promotion($gd, $pdact, $this->user_id,$qh);
else {
//如果订单已经过期了,要进行退款处理
//pt_auto_refund($tgr,$pdact);
$prom = get_goods_promotion1($gd, $pdact, $this->user_id);
}
if ($prom['is_end'] == 2) {
$this->assign('prom_num', 0); //活动设置数量
$this->assign('onlybuy', 0); //活动商品还可以买多少件
$this->assign('limit_num', 0); //秒杀没人限购数量
} else if ($prom['is_end'] != 1 && $prom['is_end'] != 4) {
$curprice = $prom['price'];
$goods['prom_type'] = $prom['prom_type'];
$goods['prom_id'] = $prom['prom_id'];
$this->assign('prom_num', $prom['goods_num']); //活动设置数量
$this->assign('onlybuy', $prom['onlybuy']); //活动商品还可以买多少件
$this->assign('limit_num', $prom['buy_limit']); //秒杀没人限购数量
} else {
//$this->error("此商品秒杀活动的数量已经售完",
//U('Mobile/Goods/goodsInfo',array('id'=>$goods_id,'stoid'=>getMobileStoId())));
$this->assign('prom_num', 0); //活动设置数量
$this->assign('onlybuy', 0); //活动商品还可以买多少件
$this->assign('limit_num', 0); //秒杀没人限购数量
}
//已拼团人数
$ptedlist = M('order')
->field('is_pt_tz,pt_pay_time,user_id,add_time')
->where('store_id',$storeid)
->where('pt_listno', $qh)->where('pay_time>0')->order('is_pt_tz desc')->select();
//->field('a.*,b.head_pic,b.nickname')
foreach($ptedlist as $k=>$v){
$us=M('users')->where('user_id',$v['user_id'])->field('head_pic,nickname')->find();
$ptedlist[$k]['head_pic']=$us['head_pic'];
$ptedlist[$k]['nickname']=$us['nickname'];
}
$num = count($ptedlist);
$this->assign('num', $num);
$pp=null;
if($num>6){
for ($i=0;$i<6;$i++){ $pp[]=$ptedlist[$i];}
}else{
$pp=$ptedlist;
}
$this->assign('pp', $pp);
$this->assign('ptedlist', $ptedlist);
//如果是阶梯团
if($pdact['kttype']==3){
$jieti=json_decode($pdact['ct_rylist'],true);
//最大阶梯数
$nn=count($jieti)-1;
$this->assign("max_num",$jieti[$nn]['rynum']);
$curpice=0;
//计算价格
if($num>=$jieti[$nn]['rynum']){
$curpice=$jieti[$nn]['price'];
}else {
foreach ($jieti as $k => $v) {
if ($num <= $v['rynum']) {
$curpice = $v['price'];
break;
}
}
}
$this->assign('jieti',$jieti);
$this->assign('curpice',$curpice);
}
if($et>0)
//时间差
$timedif=$et-time()>0?$et-time():0;
else
$timedif=$pdact['end_time']-time()>0?$pdact['end_time']-time():0;
if(empty($tgr) && !empty($qh))
$timedif=0;
$this->assign('timedif',$timedif);
$return = M('wx_ewmlist')->where(array('store_id' => $storeid, 'scene_id' => ''))->field('ticket')->find();
if ($return) {
$this->assign('share_wximg', "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . $return['ticket']);
}
//商品图片
$imgurl = getImg($pdact['share_imgurl'],curHostURL().NOIMG);
$msg = $this->img_curl_url($imgurl, 2);
$shareimg = $msg['data'];
$this->assign('shareimg',$shareimg);
//二维码也用64位
$shareurl=curHostURL().'/index.php/mobile/team/teamshow/stoid/'.$storeid.'/teamid/'.$pdact['id'].'/qh/'.$qh;
$this->assign('shareurl',$shareurl);
$qc = new \app\home\controller\Index();
$shareurl=$qc->qr_code_64($shareurl);
$this->assign('shareurlimg',$shareurl);
//用户点击分销要用的分享要用的
$this->assign('teamshare0', 1);
//大家都在团
$p_list=M('teamlist')->alias('a')->join('goods b','a.goods_id=b.goods_id')
->where('a.buy_num>0 and a.store_id='.$storeid)
->where('a.is_show=1 and a.is_end=0 and a.end_time>'.time())
->field('a.*,b.goods_id,b.goods_name,b.market_price,b.shop_price,b.original_img')
->limit(30)
->select();
$count=count($p_list);
$as_ptedlist=null;
if($count>2){
$numbers = range (0,$count-1);
shuffle ($numbers);
$result = array_slice($numbers,0,2);
$as_ptedlist[]=$p_list[$result[0]];
$as_ptedlist[]=$p_list[$result[1]];
}else{
$as_ptedlist=$p_list;
}
$this->assign('as_ptedlist', $as_ptedlist);
return $this->fetch('', getMobileStoId());
}
//支付成功,失败
public function team_success()
{
$ordno=I('orderno');
$stoid=I('stoid');
//支付状态
$payf=I('payfail','0');
$this->assign('payf',$payf);
$p_ord=M('order')->where('store_id',$stoid)->where('order_sn',$ordno)
->field('order_amount,pt_status,pt_listno,pt_prom_id,add_time')->find();
//余额支付,就直接改订单状态
if ($p_ord['order_amount'] <=0 && empty($p_ord['pt_status'])) {
update_pay_status2($ordno, 1);
$p_ord=M('order')->where('store_id',$stoid)->where('order_sn',$ordno)->find();
}
//当如果是微信支付的时候
$wxok=I("wxok");
if($wxok==1){
//如果订单没有及时更新状态
if($p_ord['pt_status']==0) {
include_once "plugins/payment/weixin/weixin.class.php";
$wx = new \weixin();
$r = $wx->query_odr($ordno, $stoid);
if ($r){
update_pay_status2($ordno, 1);
$p_ord=M('order')->where('store_id',$stoid)->where('order_sn',$ordno)->find();
}else{
mlog('微信未找到订单','team_success/'.$stoid);
}
}
}
$this->assign('p_ord',$p_ord);
if($p_ord){
//获取活动和商品
$pdact=M('teamlist')->where('id',$p_ord['pt_prom_id'])->find();
$gd=M('goods')->where('goods_id',$pdact['goods_id'])
->field('goods_id,goods_name,market_price,shop_price,original_img,store_count,prom_type,prom_id')->find();
$this->assign('pdact',$pdact);
$this->assign('gd',$gd);
//已拼团人数
$ptedlist=null;
if($p_ord['pt_listno']) {
$qh=$p_ord['pt_listno'];
//已拼团人数
$ptedlist = M('order')
->field('is_pt_tz,pt_pay_time,user_id,add_time')
->where('store_id',$stoid)
->where('pt_listno', $qh)->where('pay_time>0')->order('is_pt_tz desc')->select();
//->field('a.*,b.head_pic,b.nickname')
foreach($ptedlist as $k=>$v){
$us=M('users')->where('user_id',$v['user_id'])->field('head_pic,nickname')->find();
$ptedlist[$k]['head_pic']=$us['head_pic'];
$ptedlist[$k]['nickname']=$us['nickname'];
}
$this->assign('num', count($ptedlist));
$this->assign('ptedlist', $ptedlist);
}else{
$p_ord['head_pic']=$this->user['head_pic'];
$p_ord['nickname']=$this->user['nickname'];
$ptedlist[]=$p_ord;$num = 1;
$this->assign('num', $num);
$this->assign('ptedlist', $ptedlist);
}
$pp=null;
if($num>6){
for ($i=0;$i<6;$i++){$pp[]=$ptedlist[$i];}
}else{
$pp=$ptedlist;
}
$this->assign('pp',$pp);
//如果是阶梯团
if($pdact['kttype']==3){
$jieti=json_decode($pdact['ct_rylist'],true);
$curpice=0;
$nn=count($jieti)-1;
foreach ($jieti as $k => $v) {
if ($num >= $v['rynum']) {
$curpice = $v['price'];break;
}
}
if($curpice==0) $curpice=$jieti[0]['price'];
$this->assign('jieti',$jieti);
$this->assign('curpice',$curpice);
$this->assign("max_num",$jieti[$nn]['rynum']);
}
$et=0;
$tgr=M("teamgroup")->where('listno',$p_ord['pt_listno'])
->where('team_id',$p_ord['pt_prom_id'])->where('store_id',getMobileStoId())
->field('buystartdate,state,kt_end_time')
->find();
if($tgr) {
$et = $tgr['kt_end_time'];
$this->assign('et',$et);
$this->assign('tgr',$tgr);
$is_succ=0;
if($p_ord['pt_status']==2 || $p_ord['pt_status']==4 || $p_ord['pt_status']==5 ){
$is_succ=1;
}
$this->assign('is_succ',$is_succ);
}else{
$this->assign('is_succ',0);
}
//时间差
if($et>0)
//时间差
$timedif=$et-time()>0?$et-time():0;
else
$timedif=$pdact['end_time']-time()>0?$pdact['end_time']-time():0;
$this->assign('timedif',$timedif);
//商品图片
$imgurl = getImg($pdact['share_imgurl'],curHostURL().NOIMG);
$msg = $this->img_curl_url($imgurl, 2);
$shareimg = $msg['data'];
$this->assign('shareimg',$shareimg);
//分享网址
//二维码也用64位
$shareurl=curHostURL().'/index.php/mobile/team/teamshow/stoid/'.$stoid.'/teamid/'.$pdact['id'].'/qh/'.$p_ord['pt_listno'];
$this->assign('shareurl',$shareurl);
$qc = new \app\home\controller\Index();
$shareurl=$qc->qr_code_64($shareurl);
$this->assign('shareurlimg',$shareurl);
}
//大家都在团
$p_list=M('teamlist')->alias('a')->join('goods b','a.goods_id=b.goods_id')
->where('a.buy_num>0 and a.store_id='.$stoid)
->where('a.is_show=1 and a.is_end=0 and a.end_time>'.time())
->field('a.*,b.goods_id,b.goods_name,b.market_price,b.shop_price,b.original_img')
->limit(30)
->select();
$count=count($p_list);
$as_ptedlist=null;
if($count>2){
$numbers = range (0,$count-1);
shuffle ($numbers);
$result = array_slice($numbers,0,2);
$as_ptedlist[]=$p_list[$result[0]];
$as_ptedlist[]=$p_list[$result[1]];
}else{
$as_ptedlist=$p_list;
}
$this->assign('as_ptedlist', $as_ptedlist);
$this->assign('teamshare0', 1);
return $this->fetch('', getMobileStoId());
}
// 网址图片生成BASE64
function img_curl_url($url,$type=0,$timeout=30)
{
$msg = ['code' => 2100, 'status' => 'error', 'msg' => '未知错误!'];
$imgs = ['image/jpeg' => 'jpeg',
'image/jpg' => 'jpg',
'image/gif' => 'gif',
'image/png' => 'png',
'text/html' => 'html',
'text/plain' => 'txt',
'image/pjpeg' => 'jpg',
'image/x-png' => 'png',
'image/x-icon' => 'ico'
];
if (!stristr($url, 'http')) {
$msg['code'] = 2101;
$msg['msg'] = 'url地址不正确!';
return $msg;
}
$dir = pathinfo($url);
//var_dump($dir);
$host = $dir['dirname'];
$refer = $host . '/';
$ch = curl_init($url);
//不验证https
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_REFERER, $refer); //伪造来源地址
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回变量内容还是直接输出字符串,0输出,1返回内容
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);//在启用CURLOPT_RETURNTRANSFER的时候,返回原生的(Raw)输出
curl_setopt($ch, CURLOPT_HEADER, 0); //是否输出HEADER头信息 0否1是
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); //超时时间
$data = curl_exec($ch);
//$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
//$httpContentType = curl_getinfo($ch,CURLINFO_CONTENT_TYPE);
$info = curl_getinfo($ch);
curl_close($ch);
$httpCode = intval($info['http_code']);
$httpContentType = $info['content_type'];
$httpSizeDownload = intval($info['size_download']);
if ($httpCode != '200') {
$msg['code'] = 2102;
$msg['msg'] = 'url返回内容不正确!';
return $msg;
}
if ($type > 0 && !isset($imgs[$httpContentType])) {
$msg['code'] = 2103;
$msg['msg'] = 'url资源类型未知!';
return $msg;
}
if ($httpSizeDownload < 1) {
$msg['code'] = 2104;
$msg['msg'] = '内容大小不正确!';
return $msg;
}
$msg['code'] = 200;
$msg['status'] = 'success';
$msg['msg'] = '资源获取成功';
if ($type == 0 or $httpContentType == 'text/html') $msg['data'] = $data;
$base_64 = base64_encode($data);
if ($type == 1) $msg['data'] = $base_64;
elseif ($type == 2) $msg['data'] = "data:{$httpContentType};base64,{$base_64}";
elseif ($type == 3) $msg['data'] = "<img src='data:{$httpContentType};base64,{$base_64}' />";
else $msg['msg'] = '未知返回需求!';
unset($info, $data, $base_64);
return $msg;
}
//查看最近24小时内,活动未结束的开单,弹出框,提示信息
public function get_buy_team(){
return -1;
//随机查找一个
$rs=M("order")->alias('a')->join('(select id from wxd_teamlist where store_id='.getMobileStoId().' and end_time>'.time().' and is_end=0) b','a.pt_prom_id=b.id')->where('a.store_id',getMobileStoId())->field('user_id,order_id,add_time')
->order('RAND()')->find();
if($rs){
$user=M('users')->where('user_id',$rs['user_id'])->find();
$gd=M('order_goods')->where('order_id',$rs['order_id'])->find();
$data['head_pic']=$user['head_pic'];
$data['nickname']=$user['nickname'];
$data['goods_id']=$gd['goods_id'];//商品id
$time=time();
$tdiff=$rs['add_time']-$time;
$str="";
if($tdiff>0) {
$remain = $tdiff%86400; $hours = intval($remain/3600);
if($hours>0){
$str=$hours."小时前";
}else{
$remain = $tdiff%3600;$mins = intval($remain/60);
if($mins>0){
$str=$mins."分钟前";
}else{
$secs = $remain%60;
$str=$secs."秒前";
}
}
$data['add_time'] = $str;
return json(['code'=>1,'data'=>$data]);
}else{
return json(['code'=>-1]);
}
}else{
return json(['code'=>-1]);
}
}
//天天拼单的参团购买界面
public function Cart2_team(){
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_pind_Order(getMobileStoId());
$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));
$is_pt=I("is_pt");
$this->assign("is_pt", $is_pt);
if($is_pt!=3) {
/*---获取地区---*/
$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;
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();//物流公司
/*----------购物车商品分组---------*/
$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('', getMobileStoId());
}
//天天拼单的参团提交
public function Cart3_team(){
if(empty($this->redis)){
//$this->redis=new \Redis();
//$this->redis->connect(redisip, 6379);
$this->redis=get_redis_handle();
}
$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();
if ($_REQUEST['act'] == 'submit_order') {
//如果是拼单
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);
}
}
/*--物流方式--*/
$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");
$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;
if($exparr) {
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))); //返回结果状态
}
$result = null;
$result = $this->cartLogic->gobuyList_cart2($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;
//订单余额判断
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' => '');
}
}
$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])->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' => '');
}
}
}
}
/*--预出库库存--*/
$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;
/*--修改获取活动的函数--*/
$prom = get_pt_promotion($ginfo, null, $this->user_id,$team_qh);
if ($prom['is_end'] == 1) {
$this->backredis($no,$notime);
return json(array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此商品活动已经结束", 'result' => ''));
}
if ($prom['is_end'] == 2) {
$this->backredis($no,$notime);
return json(array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此商品已经售完", 'result' => ''));
}
if ($prom['is_end'] == 3) {
$this->backredis($no,$notime);
return json(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'] && $prom['onlybuy']>$prom['buy_limit'] && $value['store_count']>$prom['buy_limit']) {
$this->backredis($no,$notime);
return json(array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此活动商品超出限购数量", 'result' => ''));
}
}
if ($value['member_goods_price'] != $prom['price']) {
$err_txt .= "{$ginfo['goods_name']}商品价格已经变化<br>";
}
if ($value['goods_num'] > $ginfo['store_count']) {
$this->backredis($no,$notime);
return json(array('status' => -4, 'msg' => "库存不足", 'result' => ''));
}
}
if ($err_txt != "") {
$this->backredis($no,$notime);
return json(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;
}
}
$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;
//---拼单只有一个商品---
if($ispt==3) {
$exptype = 1;$exparr[$plist[0]['pick_id']]=1;
}
//启动事务
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);
$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'],//优惠、搭配促销优惠的金额
);
/*--调用生成订单的函数--*/
$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($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;
for($i=0;$i<$pdgoodsnum;$i++) {
$this->redis->lPush($name2,time().'_'.$i);
}
}
$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();
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();
if($ispt){
$key=$no.'-6-'.$pdid.'-'.$pdgoodsnum.'-'.$notime.'-'.$team_qh;
$this->redis->sRem('redis_set_'.$stoid,$key);
$name='pind'.$pdid.'-'.$stoid;
$jsdaa['len']=$this->redis->lLen($name);
$jsdaa['num']=-$pdgoodsnum;
$jsdaa['order_sn']=$pno;
$jsdaa['type']=0; //0是买单 1是5分钟自动补回 2是手动补回
mlog(json_encode($jsdaa),'pt_log/' . $stoid . '/' . $pdid."_1");
}
exit(json_encode(array('status' => 1, 'msg' => '提交订单成功', 'result' => $pno, 'payables' => $payables)));
}
}
}catch (\Exception $e) {
// 回滚事务
Db::rollback();
if ($_REQUEST['act'] == 'submit_order') {
//如果失败就补回队列
//如果是拼单
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;
for($i=0;$i<$pdgoodsnum;$i++) {
$this->redis->lPush($name2,time().'_'.$i);
}
}
$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));
}
//天天拼单的参团点击界面的效果
public function Cart3_team_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;
$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;
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'] + $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), // 订单优惠活动优惠了多少钱
);
$return_arr = array('status' => 1, 'msg' => '计算成功', 'result' => $car_price); // 返回结果状态
exit(json_encode($return_arr));
}
//尾款支付
public function tail_pay()
{
$ord_no = I('ordsn');
//修改订单的支付方式
$order = M('order')->where("order_sn", $ord_no)->find();
if ($order['pay_status'] == 1) {
return json(['code' => -1, 'msg' => '此订单,已完成支付!']);
}
$user = $this->user;
$stoid = $order['store_id'];
//主要是要判断付尾款的时间是否到期
$wda['team_id'] = $order['pt_prom_id'];
$wda['listno'] = $order['pt_listno'];
$uu = M("teamgroup")->where($wda)->where('store_id', $stoid)->find();
if ($uu) {
$v = $order;
/*-----
//如果付尾款的时间也过期了
if ($uu['wk_end_time'] < time()) {
$refund_type = tpCache('basic.refund_type', $stoid);
//退到余额
if ($refund_type == 0) {
$user_money = $v['order_amount'] + $v['user_money'] + $v['pt_tail_money'];
$sql = "update wxd_users set user_money=user_money+" . $user_money . " where user_id=" . $v['user_id'];
$rs1 = Db::execute($sql);
if ($rs1) {
if ($user_money > 0) {
$odata['user_id'] = $v['user_id'];
$odata['create_time'] = time();
$odata['money'] = $user_money;
$odata['remark'] = "退款";
$odata['store_id'] = $v['store_id'];
$odata['order_sn'] = $v['order_sn'];
$odata['status'] = 1;
$odata['type'] = 1;
M('withdrawals')->save($odata);
}
} else {
mlog("退款到余额失败,", 'refund/' . $stoid);
}
}
//原路返回
else if ($refund_type == 1) {
$wxmoney = 0;
$usrmoney = 0;
if ($v['tail_pay_type']) {
$wxmoney = $v['order_amount'];
$usrmoney = $v['order_amount'] + $v['pt_tail_money'];
} else {
$wxmoney = $v['order_amount'] + $v['pt_tail_money'];
$usrmoney = $v['order_amount'];
}
if ($v['order_amount']) {
include_once "plugins/payment/weixin/weixin.class.php";
$wx = new \weixin();
$ordno = $v['order_sn'];
$total_fee = $wxmoney;
$refund_fee = $total_fee;
//$erpid = tpCache('shop_info.ERPId', $stoid);
$path = BASE_PATH . 'public/cert/' . $stoid . '/';
try {
$wx->setCong($stoid);
$rs = $wx->refund($ordno, $total_fee, $refund_fee, $path);
if ($rs['return_code'] == 'FAIL') {
mlog("退款失败," . $rs['return_msg'], 'refund/' . $stoid);
}
} catch (\Exception $e) {
mlog("退款签名失败," . $rs['return_msg'], 'refund/' . $stoid);
}
}
//退到余额
if ($v['user_money']) {
$user_money = $usrmoney;
$sql = "update wxd_users set user_money=user_money+" . $user_money . " where user_id=" . $v['user_id'];
$rs1 = Db::execute($sql);
if ($rs1) {
if ($user_money > 0) {
$odata['user_id'] = $v['user_id'];
$odata['create_time'] = time();
$odata['money'] = $user_money;
$odata['remark'] = "退款";
$odata['store_id'] = $v['store_id'];
$odata['order_sn'] = $v['order_sn'];
$odata['status'] = 1;
$odata['type'] = 1;
M('withdrawals')->save($odata);
}
} else {
mlog("退款到余额失败,", 'refund/' . $stoid);
}
}
}
return json(['code' => -1, 'msg' => '付尾款时间到期']);
}----*/
if($uu['wk_end_time']<time()){
$url = U("mobile/user/order_list",
array("stoid" => getMobileStoId()));
$this->error('尾款时间已到期',$url);
}
} else {
return json(['code' => -1, 'msg' => '未找到拼团']);
}
if($order['pt_tail_money']>0) {
//判断余额情况
$locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');
$u_money=$this->user['user_money']-$this->user['frozen_money']-$locking_money;
if($u_money<$order['pt_tail_money']){
return json(['code' => -1, 'msg' => '余额不足']);
}
//购买使用余额
$dr = M('users')->where('user_id', $order['user_id'])->setDec('user_money', $order['pt_tail_money']);
if ($dr) {
$odata['user_id'] = $order['user_id'];
$odata['create_time'] = time();
$odata['money'] = $order['pt_tail_money'];
$odata['remark'] = "购买使用余额";
$odata['store_id'] = $order['store_id'];
$odata['order_sn'] = $order['order_sn'];
$odata['status'] = 1;
$odata['type'] = 2;
M('withdrawals')->save($odata);
//尾款的支付更新状态
update_pt_wk($ord_no, $stoid, $order, 1);
return json(['code' => 1, 'msg' => '支付成功']);
} else {
return json(['code' => -1, 'msg' => '支付失败']);
}
}else{
update_pt_wk($ord_no, $stoid, $order, 1);
return json(['code' => 1, 'msg' => '支付成功']);
}
}
//支付失败
public function team_error(){
$ordno=I('orderno');
$stoid=I('stoid');
$now=time();
$p_ord=M('order')->where('store_id',$stoid)->where('order_sn',$ordno)->find();
$this->assign('p_ord',$p_ord);
//支付状态
$tuan_goods=M("teamlist")->alias("f")->join("goods g","g.goods_id = f.goods_id","left")
->where('g.store_id='.$stoid.' and f.show_time<='.$now.' and f.end_time>='.$now.' and g.is_on_sale=1 and f.is_end=0 and f.is_show=1 and g.on_time<'.$now.' and (g.down_time>'.$now.' or g.down_time=0)')
->field("g.goods_id,g.goods_name,g.original_img,g.prom_type,g.shop_price,g.market_price,f.goods_num,f.buy_num,f.title,f.price,f.kttype,f.start_time,f.end_time,f.show_time,f.buy_num,f.ct_num")
->order('f.ordid')->limit(2)
->select();
$this->assign('tuan_goods',$tuan_goods);
return $this->fetch("", getMobileStoId());
}
//开团列表
//开团列表
public function open_team_list(){
$teamid=I("teamid");
$type=I('team_info');
$pd_info = M('teamlist')->where('store_id', getMobileStoId())
->where('id', $teamid)->field('kttype,title,ct_rylist,ct_num,kt_time,id')->find();
$this->assign('pd_info', $pd_info);
//当type=1,表明是活动详情页的请求
if($type){
//已经开团的列表
$olist=M('order')->alias('a')
->join('teamgroup b','a.pt_listno=b.listno and a.pt_prom_id=b.team_id and a.store_id=b.store_id')
->where('a.store_id',getMobileStoId())
->where('a.is_pt_tz',1)->where('a.pt_prom_id',$teamid)
->where('a.order_status<>3 and a.pay_status<>1 and a.pay_time>0')
->where('b.state=2')
->where('b.kt_end_time>'.time())
->field('a.pt_listno,a.user_id,b.team_id,b.kt_end_time,a.store_id')
->select();
$num=count($olist);
$olist1=null;
$n=0;
foreach ($olist as $k=>$v){ $n++; if($n==4){break;}
//->join('users c','a.user_id=c.user_id')
//->join('teamlist d','a.pt_prom_id=d.id')
$usr=M("users")->where('user_id',$v['user_id'])->where('store_id',getMobileStoId())
->field('nickname,head_pic')->find();
$v['nickname']=$usr['nickname'];
$v['head_pic']=$usr['head_pic'];
$v['ct_rylist']=$pd_info['ct_rylist'];
$v['ct_num']=$pd_info['ct_num'];
$v['kt_time']=$pd_info['kt_time'];
$v['id']=$pd_info['id'];
$olist1[]=$v;
}
$this->assign('olist', $olist1);
$str=$this->fetch("ajax_open_team_list", getMobileStoId());
return json(['code'=>1,'num'=>$num,'str'=>$str]);
}else {
$p = I('p/d', 1);
$isajax = I('isajax');
if($isajax) {
//已经开团的列表
$olist = M('order')->alias('a')->join('teamgroup b', 'a.pt_listno=b.listno and a.pt_prom_id=b.team_id and a.store_id=b.store_id')
//->join('users c', 'a.user_id=c.user_id')
//->join('teamlist d', 'a.pt_prom_id=d.id')
->where('a.store_id',getMobileStoId())
->where('a.is_pt_tz', 1)->where('a.pt_prom_id', $teamid)
->where('a.order_status<>3 and a.pay_status<>1 and a.pay_time>0')
->where('b.state=2')
->where('b.kt_end_time>'.time())
->field('a.pt_listno,a.user_id,b.team_id,b.kt_end_time,a.store_id')
->page($p, 10)
->order('order_id')
->select();
if($olist){
foreach ($olist as $k=>$v){
$usr=M("users")->where('user_id',$v['user_id'])->where('store_id',getMobileStoId())
->field('nickname,head_pic')->find();
$olist[$k]['nickname']=$usr['nickname'];
$olist[$k]['head_pic']=$usr['head_pic'];
$olist[$k]['ct_rylist']=$pd_info['ct_rylist'];
$olist[$k]['ct_num']=$pd_info['ct_num'];
$olist[$k]['kt_time']=$pd_info['kt_time'];
$olist[$k]['id']=$pd_info['id'];
}
}
$this->assign('olist', $olist);
return $this->fetch("ajax_open_team_list", getMobileStoId());
}
return $this->fetch("", getMobileStoId());
}
}
//获取redis长度
public function rd_team_len(){
//循环更新订单状态,以及库存
$odr_arr=M('order')
->where('store_id',getMobileStoId())
->where('user_id',$this->user_id)
->where('order_amount',0)
->where('pt_status=0 and pt_prom_id>0')
->where('order_status=0 or order_status=1')
->field('order_sn')->select();
foreach ($odr_arr as $k=>$v) {
update_pay_status2($v['order_sn'], 1);
}
$id=I("id");
$stoid=I('stoid');
//$redis = new \Redis();
//$redis->connect(redisip, 6379);
$redis=get_redis_handle();
//判断是否成团,如果pt_status=0就是未支付,就是要跳转到支付页面
$ord=M('order')->where('user_id',$this->user['user_id'])
->where('store_id',$stoid)->where('pt_prom_id',$id)
->where('pt_status',0)->field('order_id')
->where('order_status<>3')->find();
$ordstr0=M('order')->where('user_id',$this->user['user_id'])
->where('store_id',$stoid)->where('pt_prom_id',$id)
->where('pt_status',0)->field('order_id')->fetchSql(ture)
->where('order_status<>3')->find();
mlog("1.".$ordstr0,'rd_team_len/'.$stoid);
if($ord){
mlog("2.".json_encode($ord),'rd_team_len/'.$stoid);
return json(array('code' => -2, 'msg' => "您已经购买了该商品,待支付中!", 'id'=>$ord['order_id']));
}else {
//判断是否成团,如果pt_status=0就是未支付,就是要跳转到支付页面
$ord = M('order')->where('user_id', $this->user['user_id'])
->where('store_id', $stoid)->where('pt_prom_id', $id)
->where('pt_status', 1)->field('pt_prom_id,pt_listno')
->where('order_status<>3')->find();
$ordstr1 = M('order')->where('user_id', $this->user['user_id'])
->where('store_id', $stoid)->where('pt_prom_id', $id)
->where('pt_status', 1)->field('pt_prom_id,pt_listno')->fetchSql(ture)
->where('order_status<>3')->find();
mlog("3.".$ordstr1,'rd_team_len/'.$stoid);
if ($ord) {
mlog("4.".json_encode($ord),'rd_team_len/'.$stoid);
return json(array('code' => -3, 'msg' => "您已经购买了该商品,待成团中!", 'id' => $ord['pt_prom_id'], 'qh' => $ord['pt_listno']));
}
}
$name='pind'.$id.'-'.$stoid;
$len= $redis->lLen($name);
if($len==null) $len=0;
$d['code']=1;
$d['len']=$len;
return json($d);
}
//大家都在团
public function every_body_team(){
$stoid=getMobileStoId();
//大家都在团
$p_list=M('teamlist')->alias('a')->join('goods b','a.goods_id=b.goods_id')
->where('a.buy_num>0 and a.store_id='.$stoid)
->where('a.is_show=1 and a.is_end=0 and a.end_time>'.time())
->field('a.*,b.goods_id,b.goods_name,b.market_price,b.shop_price,b.original_img')
->limit(30)
->select();
$count=count($p_list);
$as_ptedlist=null;
if($count>2){
$numbers = range (0,$count-1);
shuffle ($numbers);
$result = array_slice($numbers,0,2);
$as_ptedlist[]=$p_list[$result[0]];
$as_ptedlist[]=$p_list[$result[1]];
}else{
$as_ptedlist=$p_list;
}
$this->assign('as_ptedlist', $as_ptedlist);
return $this->fetch("", $stoid);
}
/*----相应的返回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);
//如果是拼单
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;
$this->redis->sRem('redis_set_'.$stoid,$key);
}
}
}
//取消订单,处理会员团
public function clearOrder(){
$id=I("id");
$order = M('order')->where(array('order_id'=>$id,'store_id'=>getMobileStoId()))
->find();
//检查是否未支付订单 已支付联系客服处理退款
if(empty($order))
return json(array('code'=>-1,'msg'=>'订单不存在','result'=>''));
//要计算是否满团,满团就要提示并刷新页面,团失败也要刷新页面
//如果是redis满,就要重新换个会员在等支付
$wh['store_id']=getMobileStoId();
$wh['listno']=$order['pt_listno'];
$wh['team_id']=$order['pt_prom_id'];
$tgr=M("teamgroup")->where($wh)->find();
//如果团是进行中
if($tgr['state']==2){
$user_id = $order['user_id'];
$row = M('order')->where(array('order_id' => $id, 'user_id' => $user_id))
->where('order_status=0 or order_status=1')
->where('pt_status', 0)
->save(array('order_status' => 3));
if ($row) {
//获取记录表信息
//$log = M('account_log')->where(array('order_id'=>$order_id))->find();
//有余额支付的情况
if ($order['user_money'] > 0) {
//解冻冻余额
$rs = M('Users')->where("user_id", $user_id)
->where('frozen_money>=' . $order['user_money'])->find();
if ($rs) {
M('Users')->where("user_id", $user_id)->setDec('frozen_money', $order['user_money']);
}
}
/*---优惠券冻结---*/
$quanno = $order['coupon_no'];
if (!empty($quanno)) {
$dada = ['CashRepNo' => $quanno, 'user_id' => $user_id];
//冻结优惠券
M('frozen_quan')->where($dada)->delete();
}
/*---优惠券冻结---*/
$quanno = $order['coupon_no'];
if (!empty($quanno)) {
$dada = ['CashRepNo' => $quanno, 'user_id' => $user_id];
//冻结优惠券
M('frozen_quan')->where($dada)->delete();
}
//查一下拼团
$stoid = $order['store_id'];
if ($order['is_zsorder'] >= 2) {
//$redis = new \Redis();
//$redis->connect(redisip, 6379);
$redis=get_redis_handle();
//查找会员团,要补回人数
$v = $order;
if ($v['is_zsorder'] == 3) {
$name2 = 'pind' . $v['pt_prom_id'] . '-' . $v['pt_listno'] . $stoid;
if (!doublefind($redis, $name2, $v['order_id'])) {
$redis->lPush($name2, $v['order_id'] . '_0');
}
$len = $redis->lLen($name2);
//如果这个团,全部补回,那这个单就没有意思了,要delete掉
$teamnum = M('teamlist')->field('ct_num')
->where('id', $v['pt_prom_id'])->find();
if ($len == $teamnum['ct_num']) {
//$redis->delete($name2);
del_redis($redis,$name2);
//更新团
$da['state'] = 1;
$map['team_id'] = $v['pt_prom_id'];
$map['listno'] = $v['pt_listno'];
$map['store_id'] = $stoid;
mlog("取消订单:".$v['pt_listno'].'-'.$v['order_id'],"clearOrder/".$stoid);
M('teamgroup')->where($map)->save($da);
}
}
//补回购买的数量
$hh = M('order_goods')->where('order_id', $id)
->where('prom_type', 6)->where('store_id', $stoid)
->field('order_id,goods_num,prom_id,rec_id')->select();
if ($hh) {
//增加队列
foreach ($hh as $k => $v) {
$name0 = 'pind' . $v['prom_id'] . '-' . $stoid;
if (!doublefind($redis, $name2, $v['rec_id'])) {
for ($i = 0; $i < $v['goods_num']; $i++) {
$redis->lPush($name0, $v['rec_id'] . "_" . $i);
}
}
}
}
//修改订单是状态
M('order')->where('order_id', $id)->save(['pt_status' => 3]);
}
$du['msg']='退回成功1'; $du['code']=1; return json($du);
}else{
//$redis = new \Redis();
//$redis->connect(redisip, 6379);
$redis=get_redis_handle();
$name2 = 'pind' . $order['pt_prom_id'] . '-' . $order['pt_listno'] . getMobileStoId();
$len = $redis->lLen($name2);
if ($len == null || $len <= 0) {
$neerodr = M("order")->where('store_id', getMobileStoId())
->where('pt_listno', $order['pt_listno'])->where('pt_prom_id', $order['pt_prom_id'])
->where('pt_status', 0)->field('add_time')
->where('order_status=1 or order_status=0')
->field('add_time,order_id,user_id')
->find();
$us=M("users")->where('user_id',$neerodr['user_id'])->field('nickname')->find();
$tt = strtotime("+5 minute", (int)$neerodr['add_time']);
$tt3 = $tt -time();
$du['needtime']=$tt3;
$du['needtime_id']=$neerodr['order_id'];
$du['nickname']=$us['nickname'];
$du['code']=2;
return json($du);
}else{
$du['msg']='退回成功2'; $du['code']=1; return json($du);
}
}
}else {
//拼团失败
if($tgr['state']==1){
$du['msg']='拼团失败'; $du['code']=3; return json($du);
}else{
$du['msg']='拼团成功'; $du['code']=3; return json($du);
}
}
}
//开团和参团在点击前都要判断
public function check_team_num(){
try {
//循环更新订单状态,以及库存
$odr_arr = M('order')
->where('store_id', getMobileStoId())
->where('user_id', $this->user_id)
->where('order_amount', 0)
->where('pt_status=0 and pt_prom_id>0')
->where('order_status=0 or order_status=1')
->field('order_sn')->select();
foreach ($odr_arr as $k => $v) {
update_pay_status2($v['order_sn'], 1);
}
$team_qh = I("qh");
$pdid = I("team_id");
$stoid = getMobileStoId();
$ispt = I("ispt");
//判断是否成团,如果pt_status=0就是未支付,就是要跳转到支付页面
$ord = M('order')->where('user_id', $this->user['user_id'])
->where('store_id', $stoid)->where('pt_prom_id', $pdid)
->where('pt_status', 0)->field('order_id')
->where('order_status<>3')->find();
if ($ord) {
return json(array('code' => -2, 'msg' => "您已经购买了该商品,待支付中!", 'id' => $ord['order_id']));
} else {
//判断是否成团,如果pt_status=0就是未支付,就是要跳转到支付页面
$ord = M('order')->where('user_id', $this->user['user_id'])
->where('store_id', $stoid)->where('pt_prom_id', $pdid)
->where('pt_status', 1)->field('pt_prom_id,pt_listno')
->where('order_status<>3')->find();
if ($ord) {
return json(array('code' => -3, 'msg' => "您已经购买了该商品,待成团中!", 'id' => $ord['pt_prom_id'], 'qh' => $ord['pt_listno']));
}
}
if (empty($this->redis)) {
//$this->redis=new \Redis();
//$this->redis->connect(redisip, 6379);
$this->redis = get_redis_handle();
}
$name = 'pind' . $pdid . '-' . $stoid;
$len0 = $this->redis->lLen($name);
if ($len0 == 0 || $len0 == null) {
return json(array('code' => -1, 'msg' => "您晚了一步,商品已被抢光!", 'result' => ''));
}
//如果,$ispt是会员团的时候,要判断团的缓存
if ($team_qh && $ispt == '2') {
$name2 = 'pind' . $pdid . '-' . $team_qh . $stoid;
$len1 = $this->redis->lLen($name2);
if ($len1 == 0 || $len1 == null) {
return json(array('code' => -4, 'msg' => "手慢了,您参与的团已满团!", 'result' => ''));
}
}
$da['code'] = 1;
$da['len0'] = $len0;
$da['len1'] = $len1;
return json($da);
}catch (\Exception $e){
mlog("cuowu:".$e->getMessage(),"check_team_num/".getMobileStoId());
return json(array('code' => -9001, 'msg' => $e->getMessage(), 'result' => ''));
}
}
}