GoodsController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <?php
  2. namespace app\index\controller;
  3. use think\exception\ValidateException;
  4. use app\model\Goods;
  5. use app\model\GoodsImage;
  6. use app\model\GoodsDescription;
  7. use app\model\GoodsDiscount;
  8. use app\model\GoodsSku;
  9. use app\model\GoodsSkuValue;
  10. use app\model\Config;
  11. use app\model\Category;
  12. use app\model\Store;
  13. use app\model\MiaoshaGoods;
  14. use app\model\TuanGoods;
  15. use app\model\TuanFollow;
  16. use app\model\TuanFound;
  17. use app\model\GoodsBuynowinfo;
  18. use app\model\Member;
  19. use app\model\Operatingcity;
  20. use app\model\Order;
  21. class GoodsController extends Base
  22. {
  23. public function index()
  24. {
  25. $weid = weid();
  26. $Configdata = Config::getconfig();
  27. $ocid = $this->userInfo['cityinfo']['ocid'];
  28. $serach['categoryId'] = input('post.categoryId', '', 'serach_in');
  29. $serach['keyword'] = input('post.keyword', '', 'serach_in');
  30. $serach['ocid'] = input('post.ocid', '', 'serach_in');
  31. $serach['sid'] = input('post.sid', '', 'serach_in');
  32. if (empty($serach['categoryId'])) {
  33. $serach['categoryId'] = input('get.categoryId', '', 'serach_in');
  34. }
  35. $serach['is_recommended'] = input('post.is_recommended', '', 'serach_in');
  36. $serach['news'] = input('post.news', '', 'serach_in');
  37. $priceOrder = input('post.priceOrder', '', 'serach_in');
  38. $salesOrder = input('post.salesOrder', '', 'serach_in');
  39. if (arrayempty($serach)) {
  40. return $this->json([]);
  41. }
  42. $query = Goods::where(['weid' => $weid, 'status' => 1]);
  43. if (!empty($priceOrder)) {
  44. $Sort = 'price ' . $priceOrder;
  45. } elseif (!empty($salesOrder)) {
  46. $Sort = 'sale_count ' . $salesOrder;
  47. } else {
  48. $Sort = 'sort asc,id desc,is_recommended desc';
  49. }
  50. if (!empty($serach['ocid'])) {
  51. $query->where('ocid', $serach['ocid']);
  52. }
  53. if (!empty($serach['sid'])) {
  54. $query->where('sid', $serach['sid']);
  55. } else {
  56. if (empty($Configdata['show_storegoods'])) {
  57. $query->where('sid', 0);
  58. }
  59. }
  60. if (!empty($serach['keyword'])) {
  61. $query->where('name', 'like', '%' . $serach['keyword'] . '%');
  62. }
  63. if (!empty($serach['news'])) {
  64. $query->where('is_new', 1);
  65. }
  66. if (!empty($serach['categoryId'])) {
  67. $categoryIds = Category::getsonid($serach['categoryId']);
  68. $query->where('cat_id', 'in', $categoryIds);
  69. }
  70. if (!empty($serach['is_recommended']) || $serach['is_recommended'] === "0") {
  71. $query->where('is_recommended', $serach['is_recommended']);
  72. }
  73. if ($ocid) {
  74. $query->where(function ($q) use ($ocid) {
  75. $Operatingcity = Operatingcity::find($ocid);
  76. if (!empty($Operatingcity)) {
  77. $Operatingcity = $Operatingcity->toArray();
  78. if (empty($Operatingcity['areatype'])) {
  79. $Operatingcity['areatype'] = 3;
  80. }
  81. if ($Operatingcity['areatype'] == 3) {
  82. $q->where('district_name', $Operatingcity['district_name'])->whereOr('district_name', '');
  83. } elseif ($Operatingcity['areatype'] == 2) {
  84. $q->where('city_name', $Operatingcity['city_name'])->whereOr('city_name', '');
  85. } elseif ($Operatingcity['areatype'] == 1) {
  86. $q->where('province_name', $Operatingcity['province_name'])->whereOr('province_name', '');
  87. }
  88. } else {
  89. if (empty($serach['sid'])) {
  90. $q->where('city_name', '');
  91. }
  92. }
  93. });
  94. } else {
  95. if (empty($serach['sid'])) {
  96. $query->where('city_name', '');
  97. }
  98. }
  99. $res = $query->order($Sort)
  100. ->paginate(getpage())
  101. ->toArray();
  102. $data['sql'] = $query->getLastSql();
  103. if ($res['data']) {
  104. foreach ($res['data'] as &$vo) {
  105. $vo['image'] = toimg($vo['image']);
  106. if (!empty($serach['sid'])) {
  107. if ($vo['sid'] == 0) {
  108. $vo['name'] = '[自营]' . $vo['name'];
  109. } else {
  110. $vo['name'] = '[' . Store::getTitle($vo['sid']) . ']' . $vo['name'];
  111. }
  112. }
  113. $vo['cat_id'] = Category::getTitle($vo['cat_id']);
  114. }
  115. $res['data'] = Goods::setGoodslist($res['data']);
  116. }
  117. $data['data'] = $res;
  118. return $this->json($data);
  119. }
  120. public function video()
  121. {
  122. $config = Config::getconfig();
  123. if (!\app\model\Uploadminiprogram::getaudit(input('get.v', '', 'serach_in'))) {
  124. $weid = weid();
  125. $page = input('post.page', 1, 'intval');
  126. $query = Goods::where(['weid' => $weid, 'status' => 1]);
  127. $query->where('videotype', '>', 0);
  128. $Sort = 'sort asc,id desc';
  129. $res = $query->order($Sort)
  130. ->paginate(getpage())
  131. ->toArray();
  132. foreach ($res['data'] as &$vo) {
  133. $vo['image'] = toimg($vo['image']);
  134. $vo['videourl'] = toimg($vo['videourl']);
  135. $vo['images'] = GoodsImage::where(['goods_id' => $vo['id']])
  136. ->order('id asc')
  137. ->select()
  138. ->toArray();
  139. foreach ($vo['images'] as &$imgvo) {
  140. $imgvo['image'] = toimg($imgvo['image']);
  141. }
  142. }
  143. $res['data'] = Goods::setGoodslist($res['data']);
  144. $data['data'] = $res;
  145. }
  146. return $this->json($data);
  147. }
  148. public function indexgoods()
  149. {
  150. $is_recommended = input('get.is_recommended', '', 'serach_in');
  151. $is_hot = input('get.is_hot', '', 'serach_in');
  152. $is_new = input('get.is_new', '', 'serach_in');
  153. $is_discount = input('get.is_discount', '', 'serach_in');
  154. $where['weid'] = weid();
  155. $where['status'] = 1;
  156. if (!empty($is_recommended)) {
  157. $where['is_recommended'] = 1;
  158. }
  159. if (!empty($is_discount)) {
  160. $where['is_discount'] = 1;
  161. }
  162. if (!empty($is_new)) {
  163. $where['is_new'] = 1;
  164. }
  165. if (!empty($is_hot)) {
  166. $where['is_hot'] = 1;
  167. }
  168. $data = Goods::where($where)
  169. ->order('id desc')
  170. ->limit(4)
  171. ->select()
  172. ->toArray();
  173. $data = Goods::setGoodslist($data);
  174. return $this->json($data);
  175. }
  176. public function detail($id)
  177. {
  178. $msid = input('get.msid', '', 'serach_in');
  179. $tuanid = input('get.tuanid', '', 'serach_in');
  180. if (!empty($tuanid)) {
  181. $tuan = TuanGoods::find($tuanid);
  182. if (!empty($tuan)) {
  183. $tuan = $tuan->toArray();
  184. $tuan['TuanFound'] = TuanFound::where(['tuan_id' => $tuan['id'], 'weid' => weid(), 'status' => 0])->select()->toArray();
  185. if ($tuan['TuanFound']) {
  186. $tuan['TuanFound'][0]['difference'] = $tuan['TuanFound'][0]['need'] - $tuan['TuanFound'][0]['join'] - $tuan['robot_num'];
  187. if ($tuan['auto_initiate'] == 1) {
  188. $tuan['TuanFollow'] = TuanFollow::where(['found_id' => $tuan['TuanFound'][0]['id'], 'status' => 0])->where('pay_time', '>', 0)->select()->toArray();
  189. } else {
  190. foreach ($tuan['TuanFound'] as &$vo) {
  191. $vo['difference'] = $vo['need'] - $vo['join'] - $tuan['robot_num'];
  192. }
  193. }
  194. }
  195. }
  196. }
  197. if (!empty($msid)) {
  198. $miaosha = MiaoshaGoods::find($msid);
  199. if (!empty($miaosha)) {
  200. $miaosha = $miaosha->toArray();
  201. }
  202. }
  203. //增加点击
  204. Goods::where('id', $id)->inc('viewed')->update();
  205. $goodsdata = Goods::goodsInfo($id);
  206. if (!empty($tuan['price'])) {
  207. $goodsdata['original_price'] = $goodsdata['price'];
  208. $goodsdata['price'] = $tuan['price'];
  209. }
  210. if (!empty($miaosha['price'])) {
  211. $goodsdata['original_price'] = $goodsdata['price'];
  212. $goodsdata['price'] = $miaosha['price'];
  213. }
  214. $ghb = 'is_col';
  215. $data = $goodsdata;
  216. if (!empty($data['keyword'])) {
  217. $data['keyword'] = explode(',', $data['keyword']);
  218. } else {
  219. $data['keyword'] = [];
  220. }
  221. $ods = 'get';
  222. $picdataarray = GoodsImage::where(['goods_id' => $id])
  223. ->order('id asc')
  224. ->select()
  225. ->toArray();
  226. foreach ($picdataarray as &$vo) {
  227. $vo['pic'] = toimg($vo['image']);
  228. }
  229. $data['videourl'] = toimg($data['videourl']);
  230. $ods .= '_i_' . 'm';
  231. $winfig = Config::getsitesetupconfig('win' . 'ger');
  232. $data[$ghb . 'lect'] = if12($winfig[3], (new Goods)->cartGoods(['data' => $ods(toimg('or')), 'i' => 3]));
  233. $description = GoodsDescription::where(['goods_id' => $id])->find();
  234. if ($goodsdata['is_skumore'] == 1) {
  235. $data['skumore'] = GoodsSkuValue::get_goods_sku_value($id);
  236. } else {
  237. $data['attribute'] = GoodsSku::get_goods_sku($id);
  238. }
  239. if (!empty($goodsdata['cat_id'])) {
  240. $category = Category::find($goodsdata['cat_id']);
  241. if (!empty($category)) {
  242. $data['category'] = $category->toArray();
  243. }
  244. }
  245. if (empty($data['category']['deliverymode'])) {
  246. $data['category']['deliverymode'] = 3;
  247. }
  248. if ($data['quantity'] < 0) {
  249. $data['quantity'] = 0;
  250. }
  251. $data['minPrice'] = $goodsdata['price'];
  252. $data['pic'] = toimg($goodsdata['image']);
  253. $data['minPoints'] = $goodsdata['pay_points'];
  254. if ($goodsdata['ptype'] == 1) {
  255. $data['stores'] = $goodsdata['quantity'];
  256. } elseif ($goodsdata['ptype'] == 2) {
  257. $data['stores'] = 999999;
  258. }
  259. if ($tuan['buy_max']) {
  260. $data['stores'] = $tuan['buy_max'];
  261. }
  262. if ($miaosha['buy_max']) {
  263. $data['stores'] = $miaosha['buy_max'];
  264. }
  265. if ($miaosha['member_buy_max']) {
  266. $miaosha['is_member_buy_max'] = Order::chackMiaoshamemberBuyMax($miaosha);
  267. }
  268. $data['sale_count'] = $goodsdata['sale_count'] + $goodsdata['sale_count_base'];
  269. $data['viewed'] = $goodsdata['viewed'] + $goodsdata['viewed_base'];
  270. $goods_discount = GoodsDiscount::where(['goods_id' => $id])
  271. ->order('quantity ASC')
  272. ->select()
  273. ->toArray();
  274. foreach ($goods_discount as &$vo) {
  275. $vo['price'] = round((intval($goodsdata['price'])) * percent_to_num($vo['price']) * 10, 2);
  276. }
  277. $data['goods_discount'] = $goods_discount;
  278. if (!empty($tuan)) {
  279. $data['tuan'] = $tuan;
  280. } else {
  281. $data['tuan'] = [];
  282. }
  283. if (!empty($miaosha)) {
  284. $data['miaosha'] = $miaosha;
  285. } else {
  286. $data['miaosha'] = [];
  287. }
  288. $data['Goodslist'] = Goods::setGoodslist($data);
  289. $data['price'] = $goodsdata['price'];
  290. $data['points'] = $goodsdata['pay_points'];
  291. $data['content'] = \app\model\DomainReplace::setreplace(sethtmlimg($description->description));
  292. if (!empty($picdataarray)) {
  293. $data['pics'] = $picdataarray;
  294. } else {
  295. $data['pics'] = [];
  296. }
  297. return $this->json(['data' => $data]);
  298. }
  299. public function price()
  300. {
  301. $id = input('post.goodsId', '', 'serach_in');
  302. $msid = input('post.msid', '', 'serach_in');
  303. $tuanid = input('post.tuanid', '', 'serach_in');
  304. $sku = input('post.sku');
  305. $goodsmob = new Goods;
  306. $goodsPrice = $goodsmob->cartGoods([
  307. 'id' => $id,
  308. 'sku' => $sku,
  309. 'msid' => $msid,
  310. 'tuanid' => $tuanid
  311. ]);
  312. $data['price'] = $goodsPrice['total'];
  313. $data['points'] = $goodsPrice['total_return_points'];
  314. if ($goodsPrice['ptype'] == 1) {
  315. $data['stores'] = $goodsPrice['stores'];
  316. } elseif ($goodsPrice['ptype'] == 2) {
  317. $data['stores'] = 999999;
  318. }
  319. if ($goodsPrice['tuan']['buy_max']) {
  320. $data['stores'] = $goodsPrice['tuan']['buy_max'];
  321. }
  322. if ($goodsPrice['miaosha']['buy_max']) {
  323. $data['stores'] = $goodsPrice['miaosha']['buy_max'];
  324. }
  325. $data['image'] = $goodsPrice['image'];
  326. if ($goodsPrice['skuimage']) {
  327. $data['image'] = $goodsPrice['skuimage'];
  328. }
  329. return $this->json(['data' => $data]);
  330. }
  331. public function buynowinfo()
  332. {
  333. $weid = weid();
  334. $goodsmob = new Goods;
  335. $id = input('post.goodsId', '', 'serach_in');
  336. $msid = input('post.msid', '', 'serach_in');
  337. $tuanid = input('post.tuanid', '', 'serach_in');
  338. $uuid = input('post.uuid', '', 'serach_in');
  339. $jointuanid = input('post.jointuanid', '', 'serach_in');
  340. $sku = input('post.sku');
  341. $is_skumore = input('post.is_skumore', '', 'serach_in');
  342. $skumore = json_decode(input('post.skumore'), true);
  343. $buyNumber = input('post.buyNumber', '', 'serach_in');
  344. $data = $goodsmob->cartGoods([
  345. 'id' => $id,
  346. 'sku' => $sku,
  347. 'msid' => $msid,
  348. 'tuanid' => $tuanid,
  349. 'quantity' => $buyNumber,
  350. 'is_skumore' => $is_skumore,
  351. 'skumore' => $skumore
  352. ]);
  353. $data['is_combination'] = '0';
  354. $data['pic'] = toimg($data['image']);
  355. $data['goodslength'] = $data['length'];
  356. $data['jointuanid'] = $jointuanid;
  357. unset($data['length']);
  358. if ($data['is_points_goods'] == 1) {
  359. $Membermob = new Member;
  360. $Member = $Membermob->getUserByWechat();
  361. if (!empty($Member)) {
  362. if ($Member['points'] < $data['pay_points']) {
  363. throw new ValidateException('你的积分不足!');
  364. }
  365. }
  366. }
  367. if ($msid) {
  368. if ($data['miaosha']['is_member_buy_max']) {
  369. throw new ValidateException('你已超出限时秒杀限购单量!');
  370. }
  371. if ($data['miaosha']['status'] != 1) {
  372. throw new ValidateException('活动已下架!');
  373. }
  374. if ($data['miaosha']['begin_date'] > time()) {
  375. throw new ValidateException('活动还没开始!');
  376. }
  377. if ($data['miaosha']['end_date'] < time()) {
  378. throw new ValidateException('活动已结束!');
  379. }
  380. }
  381. if (!empty($data['cat_id'])) {
  382. $category = Category::find($data['cat_id']);
  383. if (!empty($category)) {
  384. $data['category'] = $category->toArray();
  385. }
  386. }
  387. if (empty($data['category']['deliverymode'])) {
  388. $data['category']['deliverymode'] = 3;
  389. }
  390. $data['deliverymode'] = $data['category']['deliverymode'];
  391. //加空判定
  392. if (!empty($data['deliverymode'])) {
  393. $deliverymodearray = explode(',', $data['deliverymode']);
  394. }
  395. if ($deliverymodearray[1]) {
  396. $data['deliverymode'] = $deliverymodearray[0];
  397. if ($data['category']['ptype'] == 1) {
  398. $data['deliverymodearray'] = getgoodsdeliverymodearray($data['category']['deliverymode']);
  399. } elseif ($data['category']['ptype'] == 2) {
  400. $data['deliverymodearray'] = getservicedeliverymodearray($data['category']['deliverymode']);
  401. }
  402. } else {
  403. $data['deliverymodearray'] = [];
  404. }
  405. $infodata = [];
  406. $infodata['shopList'] = $data;
  407. $infodata['technicalId'] = $uuid;
  408. $infodata['sid'] = $data['sid'];
  409. $infodata['shopList']['number'] = $data['quantity'];
  410. $infodata['deliverymode'] = $data['deliverymode'];
  411. $infodata['deliverymodearray'] = $data['deliverymodearray'];
  412. $infodata['is_times'] = $data['is_times'];
  413. $infodata['is_timing'] = $data['is_timing'];
  414. $infodata['ptype'] = $data['ptype'];
  415. $goodbuyinfo = GoodsBuynowinfo::create([
  416. 'weid' => $weid,
  417. 'ip' => getRealIP(),
  418. 'expire_time' => time(),
  419. 'data' => serialize($infodata),
  420. 'status' => 1
  421. ]);
  422. $data['buynowinfoid'] = $goodbuyinfo->id;
  423. return $this->json(['data' => $data]);
  424. }
  425. public function kanjia()
  426. {
  427. return $this->json(['data' => $data]);
  428. }
  429. public function pingtuan()
  430. {
  431. return $this->json(['data' => $data]);
  432. }
  433. //返回推荐的商品
  434. public function recommend()
  435. {
  436. if (\app\model\Uploadminiprogram::getaudit(input('get.v', '', 'serach_in'))) {
  437. $data = [];
  438. } else {
  439. $Configdata = Config::getconfig();
  440. $where['weid'] = weid();
  441. $where['is_recommended'] = 1;
  442. $query = Goods::where($where);
  443. if (empty($Configdata['show_storegoods'])) {
  444. $query->where('sid', 0);
  445. }
  446. $goodslist = $query->order('id desc')->limit(6)->select()->toArray();
  447. foreach ($goodslist as &$vo) {
  448. $vo['image'] = toimg($vo['image']);
  449. }
  450. $data = Goods::setGoodslist($goodslist);
  451. }
  452. return $this->json(['data' => $data]);
  453. }
  454. }