value('title'); $this->assign('countCombination', StoreActivityProductModel::where("aid={$aid} and is_del=0")->count()); $this->assign("aid", $aid); $this->assign("actName", $actName); return $this->fetch(); } public function save_excel() { $where = UtilService::getMore([ ['is_show', ''], ['title', ''], ]); StoreActivityProductModel::SaveExcel($where); } /** * 异步获取活动数据 */ public function get_combination_list() { $where = UtilService::getMore([ 'aid', ['page', 1], ['limit', 20], ['export', 0], ['is_show', ''], ['is_host', ''], ['store_name', ''] ]); $combinationList = StoreActivityProductModel::systemPage($where); if (is_object($combinationList['list'])) $combinationList['list'] = $combinationList['list']->toArray(); $data = $combinationList['list']['data']; $count = sizeof($data); return JsonService::successlayui(['data' => $data, 'count' => $count]); } public function combination($id = 0) { if (!$id) return $this->failed('数据不存在'); $product = ProductModel::get($id); if (!$product) return JsonService::fail('数据不存在!'); $f = []; $f[] = Form::hidden('product_id', $id); $f[] = Form::input('title', '活动名称', $product->getData('store_name')); $f[] = Form::input('info', '活动简介', $product->getData('store_info'))->type('textarea'); $f[] = Form::input('unit_name', '单位', $product->getData('unit_name'))->placeholder('个、位'); $f[] = Form::select('temp_id', '砍价运费模板', (string)$product->getData('temp_id'))->setOptions(function () { $list = ShippingTemplates::getList(['page' => 1, 'limit' => 20]); $menus = []; foreach ($list['data'] as $menu) { $menus[] = ['value' => $menu['id'], 'label' => $menu['name']]; } return $menus; })->filterable(1)->col(12); $f[] = Form::dateTimeRange('section_time', '活动时间'); $f[] = Form::frameImageOne('image', '商品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $product->getData('image'))->icon('image')->width('100%')->height('500px'); $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'); $f[] = Form::number('effective_time', '活动时效(单位 小时)', 24)->placeholder('请输入活动有效时间,单位:小时'); $f[] = Form::number('price', '活动价')->min(0)->col(12); $f[] = Form::number('people', '活动人数')->min(2)->col(12); $f[] = Form::number('stock', '库存', $product->getData('stock'))->min(0)->precision(0)->col(12); $f[] = Form::number('sales', '销量', $product->getData('sales'))->min(0)->precision(0)->col(12); $f[] = Form::number('sort', '排序')->col(12); $f[] = Form::number('weight', '重量', 0)->min(0)->col(12); $f[] = Form::number('volume', '体积', 0)->min(0)->col(12); $f[] = Form::radio('is_host', '热门推荐', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->col(12); $f[] = Form::radio('is_show', '活动状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->col(12); $form = Form::make_post_form('添加用户通知', $f, Url::buildUrl('save')); $this->assign(compact('form')); return $this->fetch('public/form-builder'); } /** * 显示创建资源表单页. * * @return \think\Response */ public function create(Request $request) { $aid = $request->param('aid'); $f = []; $f[] = Form::frameImageOne('product', '选择商品', Url::buildUrl('productList', array('fodder' => 'product')))->icon('plus')->width('100%')->height('500px'); $f[] = Form::hidden('product_id', ''); $f[] = Form::input('title', '活动商品名称'); // $f[] = Form::input('info', '活动简介')->type('textarea'); $f[] = Form::input('unit_name', '单位')->placeholder('个、位'); $f[] = Form::select('temp_id', '秒杀运费模板')->setOptions(function () { $list = ShippingTemplates::getList(['page' => 1, 'limit' => 20]); $menus = []; foreach ($list['data'] as $menu) { $menus[] = ['value' => $menu['id'], 'label' => $menu['name']]; } return $menus; })->filterable(1)->col(12); // $f[] = Form::dateTimeRange('section_time', '活动时间'); $f[] = Form::frameImageOne('image', '商品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')))->icon('image')->width('100%')->height('500px'); $f[] = Form::frameImages('images', '商品轮播图(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'images')))->maxLength(5)->icon('images')->width('100%')->height('500px'); // $f[] = Form::number('effective_time', '活动时效', '24')->placeholder('请输入活动订单有效时间,单位:小时')->col(12); // $f[] = Form::number('people', '活动人数', 2)->min(2)->col(12); // $f[] = Form::number('num', '单次购买商品个数', 1)->min(1)->col(12); $f[] = Form::number('sort', '排序')->col(12); $f[] = Form::radio('is_host', '热门推荐', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->col(12); $form = Form::make_post_form('添加用户通知', $f, Url::buildUrl('save', compact('aid'))); $this->assign(compact('form')); return $this->fetch('public/form-builder'); } /** * 保存新建的资源 * @param int $id */ public function save($id = 0, $aid = 0) { $data = UtilService::postMore([ 'product_id', 'title', // 'info', ['unit_name', '个'], ['image', ''], ['images', []], // ['section_time', []], // ['effective_time', 0], ['postage', 0], ['price', 0], // ['people', 0], ['sort', 0], // ['stock', 0], // ['sales', 0], ['is_show', 0], ['is_host', 0], ['is_postage', 0], ['temp_id', ''], ['weight', ''], ['volume', ''], ['num', 1], ]); $data['description'] = StoreDescription::getDescription($data['product_id']); if (!$data['title']) return JsonService::fail('请输入活动名称'); // if (!$data['info']) return JsonService::fail('请输入活动简介'); if (!$data['image']) return JsonService::fail('请上传商品图片'); if (count($data['images']) < 1) return JsonService::fail('请上传商品轮播图'); // if ($data['effective_time'] == 0 || $data['effective_time'] < 0) return JsonService::fail('请输入活动有效时间'); // if ($data['people'] == '' || $data['people'] < 1) return JsonService::fail('请输入活动人数'); // if (count($data['section_time']) < 1) return JsonService::fail('请选择活动时间'); $data['images'] = json_encode($data['images']); // $data['start_time'] = strtotime($data['section_time'][0]); // $data['stop_time'] = strtotime($data['section_time'][1]); // unset($data['section_time']); if ($id) { unset($data['description']); $product = StoreActivityProductModel::get($id); if (!$product) return JsonService::fail('数据不存在!'); $data['product_id'] = $product['product_id']; StoreActivityProductModel::edit($data, $id); return JsonService::successful('编辑成功!'); } else { $data['add_time'] = time(); $data['aid'] = $aid; $res = StoreActivityProductModel::create($data); $description['product_id'] = $res['id']; $description['description'] = htmlspecialchars_decode($data['description']); $description['type'] = 4; StoreDescription::create($description); return JsonService::successful('添加活动成功!'); } } /** * 显示编辑资源表单页. * * @param int $id * @return \think\Response */ public function edit($id) { if (!$id) return $this->failed('数据不存在'); $product = StoreActivityProductModel::get($id); if (!$product) return JsonService::fail('数据不存在!'); $f = []; $f[] = Form::input('product_id', '产品ID', $product->getData('product_id'))->disabled(true); $f[] = Form::input('title', '活动商品名称', $product->getData('title')); // $f[] = Form::input('info', '活动简介', $product->getData('info'))->type('textarea'); $f[] = Form::input('unit_name', '单位', $product->getData('unit_name'))->placeholder('个、位'); $f[] = Form::select('temp_id', '砍价运费模板', (string)$product->getData('temp_id'))->setOptions(function () { $list = ShippingTemplates::getList(['page' => 1, 'limit' => 20]); $menus = []; foreach ($list['data'] as $menu) { $menus[] = ['value' => $menu['id'], 'label' => $menu['name']]; } return $menus; })->filterable(1)->col(12); // $f[] = Form::dateTimeRange('section_time', '活动时间', date("Y-m-d H:i:s", $product->getData('start_time')), date("Y-m-d H:i:s", $product->getData('stop_time'))); $f[] = Form::frameImageOne('image', '商品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $product->getData('image'))->icon('image')->width('100%')->height('500px'); $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'); // $f[] = Form::number('effective_time', '活动时效(单位 小时)', $product->getData('effective_time'))->placeholder('请输入活动订单有效时间,单位:小时')->col(12); $f[] = Form::hidden('price', $product->getData('price')); // $f[] = Form::number('people', '活动人数', $product->getData('people'))->min(2)->col(12); // $f[] = Form::number('num', '单次购买商品个数', $product->getData('num'))->min(1)->col(12); // $f[] = Form::hidden('stock', $product->getData('stock')); // $f[] = Form::hidden('sales', $product->getData('sales')); $f[] = Form::number('sort', '排序', $product->getData('sort'))->col(12); $f[] = Form::radio('is_host', '热门推荐', $product->getData('is_host'))->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->col(12); $f[] = Form::hidden('is_show', $product->getData('is_show')); $form = Form::make_post_form('添加用户通知', $f, Url::buildUrl('save', compact('id'))); $this->assign(compact('form')); return $this->fetch('public/form-builder'); } /** * 删除指定资源 * * @param int $id * @return \think\Response */ public function delete($id) { if (!$id) return $this->failed('数据不存在'); $product = StoreActivityProductModel::get($id); if (!$product) return JsonService::fail('数据不存在!'); if ($product['is_del']) return JsonService::fail('已删除!'); $data['is_del'] = 1; if (!StoreActivityProductModel::edit($data, $id)) return JsonService::fail(StoreActivityProductModel::getErrorInfo('删除失败,请稍候再试!')); else return JsonService::successful('删除成功!'); } /** * 属性页面 * @param $id * @return mixed|void */ public function attr($id) { if (!$id) return $this->failed('数据不存在!'); $result = StoreCombinationAttrResult::getResult($id); $image = StoreActivityProductModel::where('id', $id)->value('image'); $this->assign(compact('id', 'result', 'product', 'image')); return $this->fetch(); } /** * 活动属性选择页面 * @param $id * @return string|void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function attr_list($id) { if (!$id) return $this->failed('数据不存在!'); $combinationInfo = StoreActivityProductModel::where('id', $id)->find(); $combinationResult = StoreProductAttrResult::where('product_id', $id)->where('type', 4)->value('result'); $productResult = StoreProductAttrResult::where('product_id', $combinationInfo['product_id'])->where('type', 0)->value('result'); if ($productResult) { $attr = json_decode($productResult, true)['attr']; // Log::write($attr,'error'); $productAttr = $this->get_attr($attr, $combinationInfo['product_id'], 0); $combinationAttr = $this->get_attr($attr, $id, 4); foreach ($productAttr as $pk => $pv) { foreach ($combinationAttr as $sv) { if ($pv['detail'] == $sv['detail']) { $productAttr[$pk] = $sv; } } } } else { if ($combinationResult) { $attr = json_decode($combinationResult, true)['attr']; $productAttr = $this->get_attr($attr, $id, 4); } else { $attr[0]['value'] = '默认'; $attr[0]['detailValue'] = ''; $attr[0]['attrHidden'] = ''; $attr[0]['detail'][0] = '默认'; $productAttr[0]['value1'] = '默认'; $productAttr[0]['detail'] = json_encode(['默认' => '默认']); $productAttr[0]['pic'] = $combinationInfo['image']; $productAttr[0]['price'] = $combinationInfo['price']; $productAttr[0]['cost'] = $combinationInfo['cost']; $productAttr[0]['ot_price'] = $combinationInfo['ot_price']; $productAttr[0]['stock'] = $combinationInfo['stock']; $productAttr[0]['quota'] = 0; $productAttr[0]['bar_code'] = $combinationInfo['bar_code']; $productAttr[0]['weight'] = 0; $productAttr[0]['volume'] = 0; $productAttr[0]['brokerage'] = 0; $productAttr[0]['brokerage_two'] = 0; $productAttr[0]['check'] = 0; } } $attrs['attr'] = $attr; $attrs['value'] = $productAttr; $this->assign('attr', $attrs); $this->assign('id', $id); return $this->fetch(); } /** * 活动属性添加 * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function save_attr() { $data = UtilService::postMore([ ['attr', []], ['ids', []], ['id', 0], ]); if (!$data['id']) return JsonService::fail('数据不存在!'); if (!$data['ids']) return JsonService::fail('你没有选择任何规格!'); $combinationId = StoreActivityProductModel::where('id', $data['id'])->value('product_id'); //$attr = json_decode(StoreProductAttrResult::where('product_id', $combinationId)->where('type',3)->value('result'), true)['attr']; $attr = StoreProductAttrResult::where('product_id', $combinationId)->where('type', 0)->value('result'); foreach ($data['attr'] as $k => $v) { if (in_array($k, $data['ids'])) { $v['detail'] = json_decode(htmlspecialchars_decode($v['detail']), true); $detail[$k] = $v; } } if (min(array_column($detail, 'quota')) == 0) return JsonService::fail('限购不能为0'); $price = min(array_column($detail, 'price')); $quota = array_sum(array_column($detail, 'quota')); $stock = array_sum(array_column($detail, 'stock')); if (!$attr) { $attr[0]['value'] = '规格'; $attr[0]['detailValue'] = ''; $attr[0]['attrHidden'] = ''; $attr[0]['detail'][0] = '默认'; } else { $attr = json_decode($attr, true)['attr']; } $res = StoreProductAttr::createProductAttr($attr, $detail, $data['id'], 4); if ($res === false) { return JsonService::fail(StoreProductAttr::getErrorInfo('err'), [$attr, $detail]); } StoreActivityProductModel::where('id', $data['id'])->update(['stock' => $stock, 'quota' => $quota, 'quota_show' => $quota, 'price' => $price]); return JsonService::successful('添加成功!', [$attr, $detail]); } /** * 生成属性 * @param int $id */ public function is_format_attr($id = 0) { if (!$id) return JsonService::fail('商品不存在'); list($attr, $detail) = UtilService::postMore([ ['items', []], ['attrs', []] ], $this->request, true); $product = StoreActivityProductModel::get($id); if (!$product) return JsonService::fail('商品不存在'); $attrFormat = attr_format($attr)[1]; if (count($detail)) { foreach ($attrFormat as $k => $v) { foreach ($detail as $kk => $vv) { if ($v['detail'] == $vv['detail']) { $attrFormat[$k]['price'] = $vv['price']; $attrFormat[$k]['sales'] = $vv['sales']; $attrFormat[$k]['pic'] = $vv['pic']; $attrFormat[$k]['check'] = false; break; } else { $attrFormat[$k]['price'] = ''; $attrFormat[$k]['sales'] = ''; $attrFormat[$k]['pic'] = $product['image']; $attrFormat[$k]['check'] = true; } } } } else { foreach ($attrFormat as $k => $v) { $attrFormat[$k]['price'] = $product['price']; $attrFormat[$k]['sales'] = $product['stock']; $attrFormat[$k]['pic'] = $product['image']; $attrFormat[$k]['check'] = false; } } return JsonService::successful($attrFormat); } /** * 添加 修改属性 * @param $id */ public function set_attr($id) { if (!$id) return $this->failed('商品不存在!'); list($attr, $detail) = UtilService::postMore([ ['items', []], ['attrs', []] ], $this->request, true); $res = StoreCombinationAttr::createProductAttr($attr, $detail, $id); if ($res) return $this->successful('编辑属性成功!'); else return $this->failed(StoreCombinationAttr::getErrorInfo()); } /** * 清除属性 * @param $id */ public function clear_attr($id) { if (!$id) return $this->failed('商品不存在!'); if (false !== StoreCombinationAttr::clearProductAttr($id) && false !== StoreCombinationAttrResult::clearResult($id)) return $this->successful('清空商品属性成功!'); else return $this->failed(StoreCombinationAttr::getErrorInfo('清空商品属性失败!')); } public function edit_content($id) { if (!$id) return $this->failed('数据不存在'); $product = StoreActivityProductModel::get($id); if (!$product) return JsonService::fail('数据不存在!'); $this->assign([ 'content' => htmlspecialchars_decode(StoreDescription::getDescription($id, 4)), 'field' => 'description', 'action' => Url::buildUrl('change_field', ['id' => $id, 'field' => 'description']) ]); return $this->fetch('public/edit_content'); } public function change_field($id) { if (!$id) return $this->failed('数据不存在'); $combination = StoreActivityProductModel::get($id); if (!$combination) return JsonService::fail('数据不存在!'); $data['description'] = request()->post('description'); StoreDescription::saveDescription($data['description'], $id, 4); $res = StoreActivityProductModel::edit($data, $id); if ($res) return JsonService::successful('添加成功'); else return JsonService::fail('添加失败'); } /**活动列表 * @return mixed */ public function combina_list() { $where = UtilService::getMore([ ['status', ''], ['data', ''], ], $this->request); $this->assign('where', $where); $this->assign(StorePink::systemPage($where)); return $this->fetch(); } /**活动人列表 * @return mixed */ public function order_pink($id) { if (!$id) return $this->failed('数据不存在'); $StorePink = StorePink::getPinkUserOne($id); if (!$StorePink) return $this->failed('数据不存在!'); $list = StorePink::getPinkMember($id); $list[] = $StorePink; $this->assign('list', $list); return $this->fetch(); } /** * 修改活动状态 * @param $status * @param int $idd */ public function set_combination_status($status, $id = 0) { if (!$id) return JsonService::fail('参数错误'); $res = StoreProductAttrValue::where('product_id', $id)->where('type', 4)->find(); if (!$res) return JsonService::fail('请先配置规格'); $res = StoreActivityProductModel::edit(['is_show' => $status], $id); if ($res) return JsonService::successful('修改成功'); else return JsonService::fail('修改失败'); } /** * 添加活动获取商品列表 * @return string * @throws \Exception */ public function productList() { $cate = StoreCategory::getTierList(null, 1); $this->assign('cate', $cate); return $this->fetch(); } /** * 获取活动商品规格 * @param $attr * @param $id * @param $type * @return array */ public function get_attr($attr, $id, $type) { $value = attr_format($attr)[1]; $valueNew = []; $count = 0; foreach ($value as $key => $item) { $detail = $item['detail']; sort($item['detail'], SORT_STRING); $suk = implode(',', $item['detail']); $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'); if (count($sukValue)) { foreach (array_values($detail) as $k => $v) { $valueNew[$count]['value' . ($k + 1)] = $v; } // Log::write(json_encode($sukValue), 'error'); $valueNew[$count]['detail'] = json_encode($detail); $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? ''; $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0; $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0; $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0; $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0; $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0; $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? ''; $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ?? 0; $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ?? 0; $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ?? 0; $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ?? 0; $valueNew[$count]['check'] = $type != 0 ? 1 : 0; $count++; } } return $valueNew; } public function del(Request $request, $id) { //todo 判断活动下面是否有商品 StoreActivityProductModel::update([ 'id' => $id, 'is_del' => 1 ]); return JsonService::success("删除成功"); } public function set_show(Request $request) { $param = UtilService::getMore([ 'id', 'is_show' ], $request); if (!$param['id']) return JsonService::fail('参数错误'); $res = StoreProductAttrValue::where('product_id', $param['id'])->where('type', 4)->find(); if (!$res) return JsonService::fail('请先配置规格'); StoreActivityProductModel::update([ 'id' => $param['id'], 'is_show' => $param['is_show'] ]); if ($param['is_show']) { //启用商品后,改商品所属活动也改为启用状态 $aid = StoreActivityProductModel::where("id={$param['id']}")->value('aid'); StoreActivityModel::where('id', $aid)->update([ 'is_show' => 1, ]); } JsonService::success("修改成功"); } }