StoreExchange.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <?php
  2. namespace app\admin\controller\ump;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\store\{StoreDescription,
  5. StoreProductAttr,
  6. StoreProductAttrResult,
  7. StoreProduct as ProductModel,
  8. StoreProductAttrValue
  9. };
  10. use crmeb\traits\CurdControllerTrait;
  11. use think\Exception;
  12. use think\exception\ErrorException;
  13. use think\exception\ValidateException;
  14. use think\facade\Route as Url;
  15. use app\admin\model\system\{SystemAttachment, SystemGroupData, ShippingTemplates};
  16. use app\admin\model\ump\{StoreExchange as StoreExchangeModel};
  17. use crmeb\services\{
  18. FormBuilder as Form, UtilService as Util, JsonService as Json
  19. };
  20. use app\admin\model\store\StoreCategory;
  21. /**
  22. * 限时秒杀 控制器
  23. * Class StoreSeckill
  24. * @package app\admin\controller\store
  25. */
  26. class StoreExchange extends AuthController
  27. {
  28. use CurdControllerTrait;
  29. protected $bindModel = StoreExchangeModel::class;
  30. /**
  31. * 显示资源列表
  32. *
  33. * @return \think\Response
  34. */
  35. public function index()
  36. {
  37. $this->assign('countSeckill', StoreExchangeModel::getExchangeCount());
  38. $this->assign('seckillId', StoreExchangeModel::getExchangeIdAll());
  39. return $this->fetch();
  40. }
  41. public function save_excel()
  42. {
  43. $where = Util::getMore([
  44. ['status', ''],
  45. ['store_name', '']
  46. ]);
  47. StoreExchangeModel::SaveExcel($where);
  48. }
  49. /**
  50. * 异步获取砍价数据
  51. */
  52. public function get_exchange_list()
  53. {
  54. $where = Util::getMore([
  55. ['page', 1],
  56. ['limit', 20],
  57. ['status', ''],
  58. ['store_name', '']
  59. ]);
  60. $seckillList = StoreExchangeModel::systemPage($where);
  61. if (is_object($seckillList['list'])) $seckillList['list'] = $seckillList['list']->toArray();
  62. $data = $seckillList['list']['data'];
  63. return Json::successlayui(['count' => $seckillList['list']['total'], 'data' => $data]);
  64. }
  65. public function create()
  66. {
  67. $f = array();
  68. $f[] = Form::frameImageOne('product', '选择商品', Url::buildUrl('productList', array('fodder' => 'product')))->icon('plus')->width('100%')->height('500px');
  69. $f[] = Form::hidden('product_id', '');
  70. $f[] = Form::hidden('description', '');
  71. $f[] = Form::hidden('store_id', '');
  72. $f[] = Form::input('title', '商品标题');
  73. $f[] = Form::input('info', '活动简介')->type('textarea');
  74. $f[] = Form::input('unit_name', '单位')->placeholder('个、位');
  75. $f[] = Form::select('temp_id', '运费模板')->setOptions(function () {
  76. $list = ShippingTemplates::getList(['page' => 1, 'limit' => 20]);
  77. $menus = [];
  78. foreach ($list['data'] as $menu) {
  79. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  80. }
  81. return $menus;
  82. })->filterable(1)->col(12);
  83. $f[] = Form::frameImageOne('image', '商品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')))->icon('image')->width('100%')->height('500px');
  84. $f[] = Form::frameImages('images', '商品轮播图(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'images')))->maxLength(5)->icon('images')->width('100%')->height('500px');
  85. $f[] = Form::number('sort', '排序')->col(12);
  86. $f[] = Form::number('num', '单次购买商品个数')->precision(0)->col(12);
  87. $f[] = Form::number('give_integral', '赠送积分')->min(0)->precision(0)->col(12);
  88. // $f[] = Form::radio('is_hot', '热门推荐', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->col(12);
  89. $form = Form::make_post_form('添加商品', $f, Url::buildUrl('save'));
  90. $this->assign(compact('form'));
  91. return $this->fetch('public/form-builder');
  92. }
  93. /**
  94. * 保存秒杀商品
  95. * @param int $id
  96. */
  97. public function save($id = 0)
  98. {
  99. $data = Util::postMore([
  100. 'title',
  101. 'product_id',
  102. 'info',
  103. 'unit_name',
  104. ['image', ''],
  105. ['images', []],
  106. ['price', 0],
  107. ['ot_price', 0],
  108. ['cost', 0],
  109. ['sales', 0],
  110. ['stock', 0],
  111. ['sort', 0],
  112. ['give_integral', 0],
  113. ['postage', 0],
  114. ['is_postage', 0],
  115. ['cost', 0],
  116. ['is_hot', 0],
  117. ['status', 0],
  118. ['num', 0],
  119. 'temp_id',
  120. ['weight', 0],
  121. ['volume', 0],
  122. ]);
  123. $data['description'] = StoreDescription::getDescription($data['product_id']);
  124. if (!$data['title']) return Json::fail('请输入商品标题');
  125. if (!$data['unit_name']) return Json::fail('请输入商品单位');
  126. if (!$data['product_id']) return Json::fail('商品ID不能为空');
  127. if (!$data['image']) return Json::fail('请选择推荐图');
  128. if (count($data['images']) < 1) return Json::fail('请选择轮播图');
  129. $data['images'] = json_encode($data['images']);
  130. if ($data['num'] < 1) return Json::fail('请输入单次兑换个数');
  131. if ($id) {
  132. unset($data['description']);
  133. $product = StoreExchangeModel::get($id);
  134. if (!$product) return Json::fail('数据不存在!');
  135. StoreExchangeModel::edit($data, $id);
  136. return Json::successful('编辑成功!');
  137. } else {
  138. $data['add_time'] = time();
  139. $res = StoreExchangeModel::create($data);
  140. $description['product_id'] = $res['id'];
  141. $description['description'] = htmlspecialchars_decode($data['description']);
  142. $description['type'] = 4;
  143. StoreDescription::create($description);
  144. return Json::successful('添加成功!');
  145. }
  146. }
  147. /**
  148. * 显示编辑资源表单页.
  149. *
  150. * @param int $id
  151. * @return \think\Response
  152. */
  153. public function edit($id)
  154. {
  155. if (!$id) return $this->failed('数据不存在');
  156. $product = StoreExchangeModel::get($id);
  157. // $time = StoreSeckillTime::getSeckillTime($id);
  158. if (!$product) return Json::fail('数据不存在!');
  159. $f = array();
  160. $f[] = Form::input('product_id', '产品ID', $product->getData('product_id'))->disabled(true);
  161. $f[] = Form::input('title', '商品标题', $product->getData('title'));
  162. $f[] = Form::input('info', '活动简介', $product->getData('info'))->type('textarea');
  163. $f[] = Form::input('unit_name', '单位', $product->getData('unit_name'))->placeholder('个、位');
  164. $f[] = Form::select('temp_id', '运费模板', (string)$product->getData('temp_id'))->setOptions(function () {
  165. $list = ShippingTemplates::getList(['page' => 1, 'limit' => 20]);
  166. $menus = [];
  167. foreach ($list['data'] as $menu) {
  168. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  169. }
  170. return $menus;
  171. })->filterable(1)->col(12);
  172. $f[] = Form::frameImageOne('image', '商品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $product->getData('image'))->icon('image')->width('100%')->height('500px');
  173. $f[] = Form::frameImages('images', '商品轮播图(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'images')), json_decode($product->getData('images')))->maxLength(5)->icon('images')->width('100%')->height('500px');
  174. $f[] = Form::number('sort', '排序', $product->getData('sort'))->col(12);
  175. $f[] = Form::hidden('stock', $product->getData('stock'));
  176. $f[] = Form::hidden('price', $product->getData('price'));
  177. $f[] = Form::hidden('ot_price', $product->getData('ot_price'));
  178. $f[] = Form::hidden('money', $product->getData('money'));
  179. $f[] = Form::hidden('money_type', $product->getData('money_type'));
  180. $f[] = Form::hidden('sales', $product->getData('sales'));
  181. $f[] = Form::number('num', '单次购买商品个数', $product->getData('num'))->precision(0)->col(12);
  182. $f[] = Form::number('give_integral', '赠送积分', $product->getData('give_integral'))->min(0)->precision(0)->col(12);
  183. // $f[] = Form::radio('is_hot', '热门推荐', $product->getData('is_hot'))->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->col(12);
  184. $f[] = Form::hidden('status', $product->getData('status'));
  185. $form = Form::make_post_form('添加商品', $f, Url::buildUrl('save', compact('id')));
  186. $this->assign(compact('form'));
  187. return $this->fetch('public/form-builder');
  188. }
  189. /**
  190. * 删除指定资源
  191. *
  192. * @param int $id
  193. * @return \think\Response
  194. */
  195. public function delete($id)
  196. {
  197. if (!$id) return $this->failed('数据不存在');
  198. $product = StoreExchangeModel::get($id);
  199. if (!$product) return Json::fail('数据不存在!');
  200. if ($product['is_del']) return Json::fail('已删除!');
  201. $data['is_del'] = 1;
  202. if (!StoreExchangeModel::edit($data, $id))
  203. return Json::fail(StoreSeckillModel::getErrorInfo('删除失败,请稍候再试!'));
  204. else
  205. return Json::successful('删除成功!');
  206. }
  207. public function edit_content($id)
  208. {
  209. if (!$id) return $this->failed('数据不存在');
  210. $seckill = StoreExchangeModel::get($id);
  211. if (!$seckill) return Json::fail('数据不存在!');
  212. $this->assign([
  213. 'content' => htmlspecialchars_decode(StoreDescription::getDescription($id, 4)),
  214. 'field' => 'description',
  215. 'action' => Url::buildUrl('change_field', ['id' => $id, 'field' => 'description'])
  216. ]);
  217. return $this->fetch('public/edit_content');
  218. }
  219. public function change_field($id)
  220. {
  221. if (!$id) return $this->failed('数据不存在');
  222. $seckill = StoreExchangeModel::get($id);
  223. if (!$seckill) return Json::fail('数据不存在!');
  224. $data['description'] = request()->post('description');
  225. StoreDescription::saveDescription($data['description'], $id, 4);
  226. $res = StoreExchangeModel::edit($data, $id);
  227. if ($res)
  228. return Json::successful('添加成功');
  229. else
  230. return Json::fail('添加失败');
  231. }
  232. /**
  233. * 属性页面
  234. * @param $id
  235. * @return mixed|void
  236. */
  237. public function attr($id)
  238. {
  239. if (!$id) return $this->failed('数据不存在!');
  240. $result = StoreProductAttrResult::getResult($id, 4);
  241. $image = StoreExchangeModel::where('id', $id)->value('image');
  242. $this->assign(compact('id', 'result', 'image'));
  243. return $this->fetch();
  244. }
  245. /**
  246. * 秒杀属性选择页面
  247. * @param $id
  248. * @return string|void
  249. * @throws \think\db\exception\DataNotFoundException
  250. * @throws \think\db\exception\DbException
  251. * @throws \think\db\exception\ModelNotFoundException
  252. */
  253. public function attr_list($id)
  254. {
  255. if (!$id) return $this->failed('数据不存在!');
  256. $seckillInfo = StoreExchangeModel::where('id', $id)->find();
  257. $seckillResult = StoreProductAttrResult::where('product_id', $id)->where('type', 4)->value('result');
  258. $productResult = StoreProductAttrResult::where('product_id', $seckillInfo['product_id'])->where('type', 0)->value('result');
  259. if ($productResult) {
  260. $attr = json_decode($productResult, true)['attr'];
  261. $productAttr = $this->get_attr($attr, $seckillInfo['product_id'], 0);
  262. $seckillAttr = $this->get_attr($attr, $id, 4);
  263. foreach ($productAttr as $pk => $pv) {
  264. foreach ($seckillAttr as $sv) {
  265. if ($pv['detail'] == $sv['detail']) {
  266. $productAttr[$pk] = $sv;
  267. }
  268. }
  269. }
  270. } else {
  271. if ($seckillResult) {
  272. $attr = json_decode($seckillResult, true)['attr'];
  273. $productAttr = $this->get_attr($attr, $id, 4);
  274. } else {
  275. $attr[0]['value'] = '默认';
  276. $attr[0]['detailValue'] = '';
  277. $attr[0]['attrHidden'] = '';
  278. $attr[0]['detail'][0] = '默认';
  279. $productAttr[0]['value1'] = '默认';
  280. $productAttr[0]['detail'] = json_encode(['默认' => '默认']);
  281. $productAttr[0]['pic'] = $seckillInfo['image'];
  282. $productAttr[0]['price'] = $seckillInfo['price'];
  283. $productAttr[0]['cost'] = $seckillInfo['cost'];
  284. $productAttr[0]['ot_price'] = $seckillInfo['ot_price'];
  285. $productAttr[0]['stock'] = $seckillInfo['stock'];
  286. $productAttr[0]['quota'] = 0;
  287. $productAttr[0]['bar_code'] = $seckillInfo['bar_code'];
  288. $productAttr[0]['weight'] = 0;
  289. $productAttr[0]['volume'] = 0;
  290. $productAttr[0]['brokerage'] = 0;
  291. $productAttr[0]['brokerage_two'] = 0;
  292. $productAttr[0]['money'] = 0;
  293. $productAttr[0]['money_type'] = '';
  294. $productAttr[0]['check'] = 0;
  295. }
  296. }
  297. $attrs['attr'] = $attr;
  298. $attrs['value'] = $productAttr;
  299. $this->assign('attr', $attrs);
  300. $this->assign('money_type', sys_data('money_type'));
  301. $this->assign('id', $id);
  302. return $this->fetch();
  303. }
  304. /**
  305. * 秒杀属性保存页面
  306. * @throws \think\db\exception\DataNotFoundException
  307. * @throws \think\db\exception\DbException
  308. * @throws \think\db\exception\ModelNotFoundException
  309. */
  310. public function save_attr()
  311. {
  312. $data = Util::postMore([
  313. ['attr', []],
  314. ['ids', []],
  315. ['id', 0],
  316. ]);
  317. if (!$data['id']) return Json::fail('数据不存在!');
  318. if (!$data['ids']) return Json::fail('你没有选择任何规格!');
  319. $productId = StoreExchangeModel::where('id', $data['id'])->value('product_id');
  320. $attr = json_decode(StoreProductAttrResult::where('product_id', $productId)->where('type', 0)->value('result'), true)['attr'];
  321. foreach ($data['attr'] as $k => $v) {
  322. if (in_array($k, $data['ids'])) {
  323. $v['detail'] = json_decode(htmlspecialchars_decode($v['detail']), true);
  324. $detail[$k] = $v;
  325. }
  326. }
  327. if (min(array_column($detail, 'quota')) == 0) return Json::fail('限购不能为0');
  328. $price = min(array_column($detail, 'price'));
  329. $otPrice = min(array_column($detail, 'ot_price'));
  330. $money = $detail[1]['money'];
  331. $money_type = $detail[1]['money_type'];
  332. $quota = array_sum(array_column($detail, 'quota'));
  333. $stock = array_sum(array_column($detail, 'stock'));
  334. if (!$attr) {
  335. $attr[0]['value'] = '默认';
  336. $attr[0]['detailValue'] = '';
  337. $attr[0]['attrHidden'] = '';
  338. $attr[0]['detail'][0] = '默认';
  339. }
  340. StoreProductAttr::createProductAttr($attr, $detail, $data['id'], 4);
  341. StoreExchangeModel::where('id', $data['id'])->update(['stock' => $stock, 'quota' => $quota, 'quota_show' => $quota, 'price' => $price, 'ot_price' => $otPrice, 'money' => $money, 'money_type' => $money_type]);
  342. return Json::successful('修改成功!');
  343. }
  344. /**
  345. * 生成属性
  346. * @param int $id
  347. */
  348. public function is_format_attr($id = 0)
  349. {
  350. if (!$id) return Json::fail('商品不存在');
  351. list($attr, $detail) = Util::postMore([
  352. ['items', []],
  353. ['attrs', []]
  354. ], $this->request, true);
  355. $product = StoreExchangeModel::get($id);
  356. if (!$product) return Json::fail('商品不存在');
  357. $attrFormat = attr_format($attr)[1];
  358. if (count($detail)) {
  359. foreach ($attrFormat as $k => $v) {
  360. foreach ($detail as $kk => $vv) {
  361. if ($v['detail'] == $vv['detail']) {
  362. $attrFormat[$k]['price'] = $vv['price'];
  363. $attrFormat[$k]['sales'] = $vv['sales'];
  364. $attrFormat[$k]['money'] = $vv['money'] ?? 0;
  365. $attrFormat[$k]['money_type'] = $vv['money_type'] ?? '';
  366. $attrFormat[$k]['pic'] = $vv['pic'];
  367. $attrFormat[$k]['check'] = false;
  368. break;
  369. } else {
  370. $attrFormat[$k]['price'] = '';
  371. $attrFormat[$k]['sales'] = '';
  372. $attrFormat[$k]['money'] = 0;
  373. $attrFormat[$k]['money_type'] = '';
  374. $attrFormat[$k]['pic'] = $product['image'];
  375. $attrFormat[$k]['check'] = true;
  376. }
  377. }
  378. }
  379. } else {
  380. foreach ($attrFormat as $k => $v) {
  381. $attrFormat[$k]['price'] = $product['price'];
  382. $attrFormat[$k]['sales'] = $product['stock'];
  383. $attrFormat[$k]['money'] = $product['money'] ?? 0;
  384. $attrFormat[$k]['money_type'] = $product['money_type'] ?? '';
  385. $attrFormat[$k]['pic'] = $product['image'];
  386. $attrFormat[$k]['check'] = false;
  387. }
  388. }
  389. return Json::successful($attrFormat);
  390. }
  391. /**
  392. * 修改秒杀商品状态
  393. * @param $status
  394. * @param int $id
  395. */
  396. public function set_exchange_status($status, $id = 0)
  397. {
  398. if (!$id) return Json::fail('参数错误');
  399. $res = StoreProductAttrValue::where('product_id', $id)->where('type', 4)->find();
  400. if (!$res) return Json::fail('请先配置规格');
  401. $res = StoreExchangeModel::edit(['status' => $status], $id);
  402. if ($res) return Json::successful('修改成功');
  403. else return Json::fail('修改失败');
  404. }
  405. /**
  406. * 获取秒杀商品规格
  407. * @param $attr
  408. * @param $id
  409. * @param $type
  410. * @return array
  411. */
  412. public function get_attr($attr, $id, $type)
  413. {
  414. $value = attr_format($attr)[1];
  415. $valueNew = [];
  416. $count = 0;
  417. foreach ($value as $key => $item) {
  418. $detail = $item['detail'];
  419. // sort($item['detail'], SORT_STRING);
  420. $suk = implode(',', $item['detail']);
  421. $sukValue = StoreProductAttrValue::where('product_id', $id)->where('type', $type)->where('suk', $suk)->column('bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,quota,money,money_type', 'suk');
  422. if (count($sukValue)) {
  423. foreach (array_values($detail) as $k => $v) {
  424. $valueNew[$count]['value' . ($k + 1)] = $v;
  425. }
  426. $valueNew[$count]['detail'] = json_encode($detail);
  427. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  428. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  429. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  430. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  431. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  432. $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
  433. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  434. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ?? 0;
  435. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ?? 0;
  436. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ?? 0;
  437. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ?? 0;
  438. $valueNew[$count]['money'] = $sukValue[$suk]['money'] ?? 0;
  439. $valueNew[$count]['money_type'] = $sukValue[$suk]['money_type'] ?? '';
  440. $valueNew[$count]['check'] = $type != 0 ? 1 : 0;
  441. $count++;
  442. }
  443. }
  444. return $valueNew;
  445. }
  446. /**
  447. * 秒杀获取商品列表
  448. * @return string
  449. * @throws \Exception
  450. */
  451. public function productList()
  452. {
  453. $cate = StoreCategory::getTierList(null, 1);
  454. $this->assign('cate', $cate);
  455. return $this->fetch();
  456. }
  457. }