StoreSeckill.php 26 KB

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