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() ]); } } }