StoreSeckill.php 26 KB

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