StoreSeckill.php 26 KB

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