GoodsController.php 16 KB

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