StoreExchange.php 21 KB

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