| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\controllers;
- use app\constants\OptionSetting;
- use app\jobs\storeSync\DiyCommon;
- use app\models\Attr;
- use app\models\AttrGroup;
- use app\models\Cat;
- use app\models\CloudGoodsBind;
- use app\models\CloudGoodsUpdateLog;
- use app\models\common\CommonGoodsAttr;
- use app\models\Goods;
- use app\models\GoodsBrand;
- use app\models\GoodsCat;
- use app\models\GoodsChainLevel;
- use app\models\GoodsFullMinus;
- use app\models\Level;
- use app\models\MchCat;
- use app\models\Option;
- use app\models\SaasUser;
- use app\models\Store;
- use app\models\StoreImageSearchSetting;
- use app\models\StoreSyncExtLog;
- use app\models\TopicType;
- use app\models\UploadGoodsImage;
- use app\models\Worker;
- use app\models\WorkerCat;
- use app\models\WorkerGoodsCat;
- use app\models\WorkerLevel;
- use app\modules\admin\models\CatForm;
- use app\modules\admin\models\CopyForm;
- use app\modules\admin\models\GoodsForm;
- use app\modules\admin\models\GoodsFullMinusForm;
- use app\modules\admin\models\GoodsSearchForm;
- use app\modules\admin\models\mch\GoodsListForm;
- use app\modules\admin\models\Taobaocsv;
- use app\utils\ShareQrcode;
- use app\utils\TiiaHelper;
- use yii\helpers\Json;
- use app\models\WorkerGoods;
- use app\modules\admin\models\MchForm;
- /**
- * Class GoodsController
- * @package app\modules\admin\controllers
- */
- class GoodsController extends BaseController
- {
- /**
- * 平台获取商品列表
- * @return \yii\web\Response
- */
- public function actionList()
- {
- $arr = get_params();
- if(get_mch_id()){
- $arr['mch'] = 1;
- $arr['mch_id'] = get_mch_id();
- }
- $groupActivity = get_params('group_activity');
- if($groupActivity == 1){
- return $this->asJson(Goods::getActivityList($arr));
- }else{
- return $this->asJson(Goods::getList($arr));
- }
- }
- public function actionWarnGoodsTimeoutSet()
- {
- return $this->asJson(Goods::warn_goods_timeout_save(get_store_id(), intval(input_params('warn_goods_timeout'))));
- }
- public function actionWarnGoodsNum()
- {
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => Goods::warn_goods_num(get_store_id()),
- ]);
- }
- public function actionWarnGoodsNumSave()
- {
- return $this->asJson(Goods::warn_goods_num_save(get_store_id(), intval(input_params('warn_goods_num'))));
- }
- public function actionGenGoodsNo()
- {
- checkReplay([get_store_id()], 1);
- return $this->asJson(Goods::genGoodsNo(get_store_id(), intval(input_params('goods_id', 0)), intval(input_params('num', 1))));
- }
- public function actionBatchUpGoodsNum() {
- $store_id = get_store_id();
- $admin = get_admin();
- debug_log([__FUNCTION__, __LINE__, $admin->id, $admin->username, $store_id], __CLASS__);
- $batchUpGoodsNum = Goods::batchUpGoodsNum($store_id, post_params());
- debug_log([__FUNCTION__, __LINE__, $admin->id, $admin->username, $store_id], __CLASS__);
- return $this->asJson($batchUpGoodsNum);
- }
- public function actionGetGoodsByIds()
- {
- $ids = get_params('ids');
- $list = Goods::find()->where([
- 'id' => $ids,
- 'is_delete' => 0,
- 'store_id' => get_store_id(),
- ])->select('id, name, cover_pic, price, goods_num')->orderBy('id asc')->all();
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $list,
- ]);
- }
- /**
- * 修改商品状态
- * @return \yii\web\Response
- */
- public function actionStatus()
- {
- $goods = Goods::findOne(get_params('id'));
- if (empty($goods)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '查无此商品'
- ]);
- }
- $type = get_params('type','status');
- if ($goods->product_type == Goods::GOODS_TYPE_NORMAL) {
- if ($type == 'status' && empty($goods->goods_num) && $goods->status == 0) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '请添加库存'
- ]);
- }
- if ($type == 'status' && $goods->status == 0 && $goods->md_food_id !== -1) {
- if(!$goods->catList1){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '请编辑商品分类'
- ]);
- }
- }
- if (!empty($goods->cloud_goods_id) && (int)$goods->status === 0) {
-
- $goods_url = "/goods/getAttrParams";
- $param = [
- 'id' => $goods->cloud_goods_id
- ];
- //请求接口
- $domain = (new OptionSetting)->getCloudDomainName();
- $goodsInfo = cloud_post($domain.$goods_url,$param);
- $goodsInfo = json_decode($goodsInfo, true);
- $goodsInfo = $goodsInfo['data'];
- //如果没有上架或审核未通过则不允许购买
- if ((int)$goodsInfo['status'] === 0 || (int)$goodsInfo['is_audit'] !== 2) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作失败,供货商商品状态异常'
- ]);
- }
-
- //判断是否可以上架
- $CloudGoodsUpdateLog = CloudGoodsUpdateLog::findOne(['cloud_goods_id' => $goods->cloud_goods_id, 'store_id' => $goods->store_id]);
- //存在记录并且不为批发订单
- debug_log($CloudGoodsUpdateLog->id, 'cloud.log');
- if ($CloudGoodsUpdateLog && !$goods->is_wholesale) {
- // if (!$CloudGoodsUpdateLog->is_audit) {
- // return $this->asJson([
- // 'code' => 1,
- // 'msg' => '供货商商品暂未审核通过'
- // ]);
- // }
- $CloudGoodsUpdateLog->is_audit = 1;
- if (!$CloudGoodsUpdateLog->save()) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '商品同步状态失败'
- ]);
- };
- if (!$CloudGoodsUpdateLog->is_update) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '商品信息需要更新'
- ]);
- }
- }
- }
- }
- $value = get_params('value',-1);
- $goods->$type = $value == -1 ? ($goods->$type ? 0 : 1) : $value;
- if($goods->mch_id && $type == 'status' && $goods->$type == 1){
- $mchSetting = (new MchForm(['store_id' => $goods->store_id]))->mchSetting();
- if($mchSetting['mch_goods_audit']){
- $goods->mch_audit = 1;
- $goods->mch_audit_time = time();
- $goods->status = Goods::STATUS_DISABLE;
- $goods->save();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '已提交审核,请等待管理员审核'
- ]);
- }
- }
- if ($goods->save()) {
- // 这时候商品是下架的 下架的商品要伤处腾讯云的图片
- if($goods->status == 0){
- // 要判断是否开启以图搜图 以及 是否创建图库
- $imageSearchStatus = Option::get('tencent_image_setting_status',0,'saas')['value'];
- $storeSetting = StoreImageSearchSetting::findOne(['store_id' => get_store_id()]);
- if($imageSearchStatus == 1 && $storeSetting){
- // 商品删除的时候 要同步删除腾讯云的图片
- $item = UploadGoodsImage::find()->where(['goods_id'=>$goods->id,'is_delete'=>0])->one();
- $tiia = new TiiaHelper();
- // 查询图库是否存在 返回code==0的时候标识图库已存在
- $delImageResult = $tiia::deleteImages($storeSetting->group_id,$item['goods_id'],$item['image_name']);
- debug_log(json_encode($delImageResult,JSON_UNESCAPED_UNICODE));
- if($delImageResult['code'] == 0){
- $item['is_delete'] = 1;
- $item->save();
- $storeSetting->image_num = $storeSetting->image_num - 1;
- $storeSetting->save();
- }
- }
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '编辑成功'
- ]);
- }else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '编辑失败'
- ]);
- }
- }
- /**
- * 批量修改商品
- * @return \yii\web\Response
- */
- public function actionUpdateAll()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $updateData = [
- get_params('type') => get_params('status')
- ];
- $updateCount = Goods::updateAll($updateData,['id' => get_params('goods_id')]);
- if (get_params('type') === 'is_delete') {
- foreach (get_params('goods_id') as $param) {
- $goods = Goods::findOne($param);
- if ($goods->cloud_goods_id && !intval($goods->is_wholesale)) {
- $message = null;
- $cloud_store_token = get_merchant_token(0, 0, $message);
- if (!$cloud_store_token && $message) {
- throw new \Exception($message['msg']);
- }
- $mch_set_submit_order_url = "/cloud/mch/delTransGoods";
- $mch_set_submit_order_data = [];
- $mch_set_submit_order_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- $mch_set_submit_order_data['goods_id'] = $goods->cloud_goods_id;
- $domain = (new OptionSetting)->getCloudDomainName();
- $mchSetSubmitOrderInfo = cloud_post( $domain . $mch_set_submit_order_url, $mch_set_submit_order_data);
- $mchSetSubmitOrderInfo = json_decode($mchSetSubmitOrderInfo,true);
- if($mchSetSubmitOrderInfo['code'] != 0){
- debug_log('删除转单商品' . $mchSetSubmitOrderInfo['msg']);
- throw new \Exception('删除云仓商品失败' . $mchSetSubmitOrderInfo['msg']);
- } else {
- CloudGoodsBind::updateAll(['is_delete' => 1], ['store_id' => get_store_id(), 'cloud_goods_id' => $goods->cloud_goods_id, 'is_delete' => 0]);
- }
- }
- }
- }
- if ($updateCount) {
- $t->commit();
- // 要判断是否开启以图搜图 以及 是否创建图库
- $imageSearchStatus = Option::get('tencent_image_setting_status',0,'saas')['value'];
- $storeSetting = StoreImageSearchSetting::findOne(['store_id' => get_store_id()]);
- if($imageSearchStatus == 1 && $storeSetting){
- // 商品删除的时候 要同步删除腾讯云的图片
- if((get_params('type') == 'is_delete' && get_params('status') == 1) || (get_params('type') == 'status' && get_params('status') == 0)){
- $list = UploadGoodsImage::find()->where(['goods_id'=>get_params('goods_id'),'is_delete'=>0])->all();
- $tiia = new TiiaHelper();
- foreach($list as $item){
- // 查询图库是否存在 返回code==0的时候标识图库已存在
- $delImageResult = $tiia::deleteImages($storeSetting->group_id,$item['goods_id'],$item['image_name']);
- debug_log(json_encode($delImageResult,JSON_UNESCAPED_UNICODE));
- if($delImageResult['code'] == 0){
- $item['is_delete'] = 1;
- $item->save();
- $storeSetting->image_num = $storeSetting->image_num - 1;
- $storeSetting->save();
- }
- }
- }
- }
- if (count(get_params('goods_id')) === 1) {
- (new DiyCommon)->JobBehaviors(get_store_id(), StoreSyncExtLog::TYPE_PRODUCT, get_params('goods_id'));
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '共修改'.$updateCount
- ]);
- }else {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '修改数据为0'
- ]);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage()
- ]);
- }
- }
- /**
- * 批量修改价格
- */
- public function actionUpPriceBatch()
- {
- $goods_id = input_params('goods_id');
- $num = input_params('num');
- $type = input_params('type', 'add');
- $form = new GoodsForm();
- $res = $form->upPriceBatch($goods_id, $num, $type);
- return $this->asJson($res);
- }
- /**
- * 商品编辑
- * @param $id
- * @return \yii\web\Response
- */
- public function actionEdit($id)
- {
- $id = $id ?: 0;
- $goodsFrom = new GoodsForm();
- $goodsFrom->store_id = get_store_id();
- if(get_mch_id()){
- $goodsFrom->mch = 1;
- $goodsFrom->mch_id = get_mch_id();
- }
- return $this->asJson($goodsFrom->getEdit($id));
- }
- /**
- * 保存商品
- */
- public function actionSave()
- {
- $form = new GoodsForm();
- $form->attributes = post_params();
- $form->store_id = get_store_id();
- if(get_mch_id()){
- $form->mch = 1;
- $form->mch_id = get_mch_id();
- }
- return $this->asJson($form->save());
- }
- /**
- * 保存商品联盟券信息
- */
- public function actionSaveLeague()
- {
- $form = new GoodsForm();
- $form->store_id = get_store_id();
- $form->attributes = post_params();
- return $this->asJson($form->saveLeague());
- }
- /**
- * 保存分类
- * @return \yii\web\Response
- */
- public function actionCatSave()
- {
- $data = post_params();
- $form = new CatForm();
- $cat = $data['id'] ? (Cat::findOne($data['id']) ?: new Cat()) : new Cat();
- $form->model = $cat;
- $form->attributes = $data;
- $form->store_id = get_store_id();
- return $this->asJson($form->save());
- }
- /**
- * 获取分类列表
- * @return \yii\web\Response
- */
- public function actionCatList()
- {
- $is_show = get_params('is_show',Cat::IS_SHOW_TRUE);
- $is_show_goods = get_params('is_show_goods', 1);
- $list = Cat::getCatList(get_store_id(), $is_show, 0, $is_show_goods);
- $is_add_goods = Option::get('is_add_goods', get_store_id(), 'store', 1)['value'];
-
- return $this->asJson([
- 'code' => 0,
- 'data' => [
- 'list' => $list,
- 'is_add_goods' => (int)$is_add_goods
- ]
- ]);
- }
- /**
- * 获取分类列表
- * @return \yii\web\Response
- */
- public function actionWorkerGoodsCatList()
- {
- $is_show = get_params('is_show',Cat::IS_SHOW_TRUE);
- $store_id = get_store_id();
- $list = WorkerGoodsCat::find()->where(['store_id' => $store_id, 'is_show' => $is_show, 'is_delete' => 0])
- ->select('id, name')->orderBy('sort desc')->asArray()->all();
- return $this->asJson([
- 'code' => 0,
- 'data' => [
- 'list' => $list
- ]
- ]);
- }
- /**
- * 获取分类列表
- * @return \yii\web\Response
- */
- public function actionGetBrandList()
- {
- return $this->asJson([
- 'code' => 0,
- 'data' => [
- 'list' => array_merge([
- ['id' => '0', 'brand_name' => '不选择品牌']
- ], GoodsBrand::getList(get_store_id()))
- ]
- ]);
- }
- /**
- * 获取服务人员列表
- * @return \yii\web\Response
- */
- public function actionWorkerList()
- {
- $store_id = get_store_id();
- $worker_ids = get_params('worker_ids');
- $name = get_params('name');
- $query = Worker::find()->alias('w')->where(['w.store_id' => $store_id, 'w.status' => 1])
- ->leftJoin(['su' => SaasUser::tableName()], 'w.saas_user_id = su.id')
- ->leftJoin(['wg' => WorkerGoods::tableName()], 'wg.worker_id = w.id')
- ->select('w.id, w.name, w.logo, w.tel, su.name nickname, w.level, wg.public')->orderBy('id desc');
- if ($name) {
- $query->andWhere(['LIKE', 'w.name', $name]);
- }
- if ($worker_ids) {
- $worker_ids = explode(',', $worker_ids);
- $query->andWhere(['w.id' => $worker_ids]);
- $data = $query->asArray()->all();
- } else {
- $list = pagination_make($query);
- $data = $list['list'];
- }
- foreach ($data as &$item) {
- $item['cat_id'] = '';
- $workerGoodsExtCatId = \app\models\WorkerCatExt::find()->where(['worker_id' => $item['id'], 'is_delete' => 0])
- ->select('cat_id')->column();
- if ($workerGoodsExtCatId) {
- $catNameArr = WorkerCat::find()->where(['id' => $workerGoodsExtCatId, 'is_delete' => 0])->select('name')
- ->column();
- $item['cat_id'] = implode(';', $catNameArr);
- }
- $item['level'] = WorkerLevel::findOne(['level' => $item['level'], 'store_id' => $store_id])->name ?? '';
- }
- return $this->asJson([
- 'code' => 0,
- 'data' => [
- 'data' => $data,
- 'pageNo' => $list['pageNo'] ?? '',
- 'totalCount' => $list['totalCount'] ?? ''
- ]
- ]);
- }
- /**
- * 批量修改分类状态
- * @return \yii\web\Response
- */
- public function actionCatStatus()
- {
- $status = get_params('status',1);
- $attributes = $status == 0 || $status == 1 ? ['is_show' => $status] : ['is_delete' => 1];
- $res = Cat::updateAll($attributes, ['id' => get_params('id',[])]);
- if ($res) {
- \queue_push(new \app\jobs\SyncDiyClassifyJob(['store_id' => get_store_id()]), 0, 1);
- $cat_id = get_params('id',[]);
- if (!is_array($cat_id)) {
- $cat_id = explode(',', $cat_id);
- }
- (new DiyCommon)->JobBehaviors(get_store_id(), StoreSyncExtLog::TYPE_PRODUCT_CAT, $cat_id);
- }
- return $this->asJson([
- 'code' => 0,
- 'data' => [
- 'updateCount' => $res
- ]
- ]);
- }
- /**
- * 批量修改分类状态
- * @return \yii\web\Response
- */
- public function actionCatChange()
- {
- $id = get_params('id');
- $list = get_params('list');
- $parent_id = get_params('parent_id');
- if ($list) {
- $list = json_decode($list, true);
- if (!empty($list)) {
- $this->menu($list);
- }
- $res = 1;
- } else {
- $res = Cat::updateAll(['parent_id' => $parent_id], ['id' => $id]);
- }
- return $this->asJson([
- 'code' => 0,
- 'data' => [
- 'updateCount' => $res
- ]
- ]);
- }
- public function menu($list, $parent_id = 0) {
- // $list = array_reverse($list);
- foreach ($list as $index => $item) {
- $cat = Cat::findOne($item['id']);
- $cat->parent_id = $parent_id;
- $cat->sort = $index;
- $cat->save();
- if ($item['children']) {
- $this->menu($item['children'], $item['id']);
- }
- }
- }
- /**
- * 删除分类
- * @return \yii\web\Response
- */
- public function actionCatDel()
- {
- $cat = Cat::findOne(get_params('id'));
- if (empty($cat)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '查无此分类'
- ]);
- }
- $cat->is_delete = 1;
- if ($cat->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '删除成功'
- ]);
- }else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '删除失败'
- ]);
- }
- }
- /**
- * 修改商品库存
- * @return \yii\web\Response
- */
- public function actionSetAttrNum()
- {
- $form = new GoodsForm();
- return $this->asJson($form->setAttrNum());
- }
- public function actionGoodsCopy()
- {
- $copy = new CopyForm();
- $copy->attributes = get_params();
- return $this->asJson($copy->copy());
- }
- /**
- * 编辑商品数据
- */
- public function actionSetAttribute()
- {
- $form = new GoodsForm();
- return $this->asJson($form->setType());
- }
- /**
- * 获取小程序二维码
- */
- public function actionGetQr ()
- {
- $id = get_params('id', 0);
- $md_id = get_md_id();
- $store_id = get_store_id();
- $scene = "id:{$id},md_id:{$md_id},store:{$store_id}";
- $res = ShareQrcode::wxQrcode('goods/goods/goods', $scene);
- if (isset($res['code']) && $res['code'] == 1) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $res['response']['errmsg'],
- ]);
- }
- return $this->asJson([
- 'code' => 0,
- 'data' => [
- 'qr_url' => $res['url_path']
- ]
- ]);
- }
- /**
- * 满减活动列表
- */
- public function actionFullMinus()
- {
- $form = new GoodsSearchForm();
- $form->attributes = get_params();
- $res = $form->fullMinusSearch();
- return $this->asJson($res);
- }
- /**
- * 满减活动删除
- */
- public function actionFullMinusDel()
- {
- $id = get_params('id', 0);
- $model = GoodsFullMinus::findOne([
- 'id' => $id,
- 'store_id' => get_store_id(),
- ]);
- $model->is_delete = 1;
- if ($model->save()){
- return $this->asJson([
- 'code' => 0,
- 'msg' => '删除成功',
- ]);
- }else{
- return $this->asJson([
- 'code' => 1,
- 'msg' => $model->errors[0],
- ]);
- }
- }
- /**
- * 满减活动编辑
- */
- public function actionFullMinusEdit()
- {
- $id = input_params('id');
- $goods_id = input_params('goods_id');
- $GoodsFullMinus = GoodsFullMinus::findOne(['id' => $id, 'store_id' => get_store_id()]);
- if (\Yii::$app->request->isPost) {
- $form = new GoodsFullMinusForm();
- $form->attributes = post_params();
- $form->store_id = get_store_id();
- $goods_id = post_params('goods_id');
- $goods = Goods::findOne(['id' => $goods_id, 'store_id' => get_store_id(), 'mch_id' => 0]);
- $form->attr = post_params('attr');
- if (!$GoodsFullMinus) {
- $GoodsFullMinus = new GoodsFullMinus();
- $form->goods_id = post_params('goods_id');
- }
- $form->model = $GoodsFullMinus;
- $form->goods = $goods;
- return $this->asJson($form->save());
- } else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '传参方式错误'
- ]);
- }
- }
- /**
- * 淘宝CSV上传
- */
- public function actionTaobaoCopy()
- {
- if (\Yii::$app->request->isPost) {
- $form = new Taobaocsv();
- $form->attributes = \Yii::$app->request->post();
- $form->store_id = $this->store->id;
- $res = $form->search();
- return $res;
- }
- return $this->render('taobao-copy');
- }
- /**
- * 商品推荐关键词
- * @return \yii\web\Response
- */
- public function actionRecommendKeyword() {
- $pageNo = get_params('pageNo', 1);
- $pageSize = get_params('pageSize', \Yii::$app->params['pageSize']);
- $recommend_keyword = Store::findOne(get_store_id())->recommend_keyword;
- $keyword = Json::decode($recommend_keyword);
- if (empty($keyword)) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => [],
- 'pageNo' => intval($pageNo),
- 'totalCount' => 0,
- ]
- ]);
- }
- $count = count($keyword);
- $data = array_slice($keyword, ($pageNo - 1) * $pageSize, $pageSize);
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $data,
- 'pageNo' => intval($pageNo),
- 'totalCount' => $count,
- ]
- ]);
- }
- /**
- * 商品推荐关键词删除
- * @return \yii\web\Response
- */
- public function actionRecommendKeywordDelete() {
- $key = post_params('key');
- $store_id = get_store_id();
- $store = Store::findOne($store_id);
- $arr = Json::decode($store->recommend_keyword);
- \Yii::error($arr);
- unset($arr[$key]);
- $store->recommend_keyword = Json::encode($arr);
- if ($store->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '删除成功'
- ]);
- } else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $store->errors[0]
- ]);
- }
- }
- /**
- * 商品推荐关键词编辑保存
- * @return \yii\web\Response
- */
- public function actionRecommendKeywordSave() {
- $type = post_params('type');
- $key = post_params('key');
- $keyword = post_params('keyword');
- if (empty(trim($keyword))) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数错误'
- ]);
- }
- $store_id = get_store_id();
- $store = Store::findOne($store_id);
- $arr = Json::decode($store->recommend_keyword);
- if (!$arr) {
- $arr = [];
- }
- if ($type == 'edit') {
- foreach ($arr as &$value) {
- if ($value['id'] == $key) {
- $value['value'] = trim($keyword);
- }
- }
- } else {
- $arr[] = [
- 'id' => count($arr),
- 'value' => trim($keyword)
- ];
- }
- $store->recommend_keyword = Json::encode($arr);
- if ($store->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '保存成功'
- ]);
- } else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $store->errors[0]
- ]);
- }
- }
- public function actionRecommendKeyBatch() {
- $id = post_params('id');
- $store_id = get_store_id();
- $store = Store::findOne($store_id);
- $recommend_key = Json::decode($store->recommend_keyword);
- if (empty($recommend_key) || (empty($id) || !is_array($id))) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '数据为空'
- ]);
- }
- foreach ($recommend_key as $key => $value) {
- if (in_array($value['id'], $id)) {
- unset($recommend_key[$key]);
- }
- }
- $store->recommend_keyword = Json::encode($recommend_key);
- if ($store->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '处理成功'
- ]);
- } else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $store->errors[0]
- ]);
- }
- }
- /**
- * 批量操作
- * @return \yii\web\Response
- */
- public function actionFullMinusBatch()
- {
- $id = post_params('id');
- $status = post_params('status');
- if (!is_array($id)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数不正确'
- ]);
- }
- GoodsFullMinus::updateAll(['is_delete' => intval($status)], ['in', 'id', $id]);
- return $this->asJson([
- 'code' => 0,
- 'msg' => '更新成功'
- ]);
- }
- /**
- * 检测商品风险
- */
- public function actionCheckRisk()
- {
- $form = new GoodsForm();
- $form->attributes = post_params();
- $form->store_id = get_store_id();
- return $this->asJson([
- 'code' => 0,
- 'data'=> $form->checkRisk(),
- 'msg' => 'success',
- ]);
- }
- public function actionCheckRiskToList()
- {
- $id = post_params('id');
- $price = post_params('price');
- $attr = post_params('attr');
- $form = new GoodsForm();
- $goods = Goods::find()->where(['id' => $id])->asArray()->one();
- if($attr) {
- $goods['attr'] = $attr;
- } else {
- $goods['attr'] = json_decode($goods['attr'],true);
- }
- $goodsChainLevel = GoodsChainLevel::findOne(['goods_id' => $id]);
- $goods['chain_level_value'] = $goodsChainLevel->value ? json_decode($goodsChainLevel->value,true) : [];
- $goods['is_open'] = $goodsChainLevel->is_open;
- $goods['chain_type'] = $goodsChainLevel->type;
- if($price > 0) {
- $goods['price'] = $price;
- }
- $form->attributes = $goods;
- $form->store_id = get_store_id();
- return $this->asJson([
- 'code' => 0,
- 'data'=> $form->checkRisk(),
- 'msg' => 'success',
- ]);
- }
- /**
- * 批量修改商品分类
- * @return \yii\web\Response
- */
- public function actionBatchUpdateCat()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $goods_ids = post_params('ids', []);
- $cat_id = post_params('cat_id', 0);
- if (empty($goods_ids)) {
- throw new \Exception('请选择要修改的商品');
- }
- if (empty($cat_id)) {
- throw new \Exception('请选择分类');
- }
- $cat = Cat::findOne(['id' => $cat_id, 'store_id' => get_store_id(), 'is_delete' => 0]);
- if (!$cat) {
- throw new \Exception('分类不存在或已被删除');
- }
- $success_count = 0;
- foreach ($goods_ids as $goods_id) {
- GoodsCat::deleteAll(['goods_id' => $goods_id]);
- $goodsCat = new GoodsCat();
- $goodsCat->goods_id = $goods_id;
- $goodsCat->cat_id = $cat_id;
- $goodsCat->store_id = get_store_id();
- if (!$goodsCat->save()) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '保存失败: ' . current($goodsCat->getFirstErrors())
- ]);
- }
- $success_count++;
- }
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '成功修改'.$success_count.'个商品的分类'
- ]);
- } catch (\Exception $e) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage()
- ]);
- }
- }
- }
|