| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <?php
- namespace backend\modules\mall\controllers;
- use common\enums\DiyEnum;
- use common\enums\StatusEnum;
- use common\models\diy\DiyComponents;
- use backend\controllers\MallController;
- use common\models\diy\DiyPage;
- use common\models\goods\Goods;
- use common\models\mall\MallAddons;
- use common\models\user\UserLevel;
- use common\models\goods\template\GoodsDecorationTemplate;
- use common\models\goods\template\GoodsDecorationTemplateContent;
- use common\models\goods\template\GoodsTemplateRelation;
- use Yii;
- use yii\db\Exception;
- use yii\helpers\Json;
- class GoodsTemplateController extends MallController
- {
- public function actionIndex()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $params = Yii::$app->request->get();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($params, [
- [['name'], 'string'],
- [['id', 'status'], 'integer'],
- [['page', 'limit'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [];
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- // 根据名称搜索
- if (!empty($params['name'])) {
- $where[] = ['like', 'name', $params['name']];
- }
- if (!empty($params['id'])) {
- $where[] = ['=', 'id', $params['id']];
- }
- if ($params['status'] != '') {
- $where[] = ['=', 'status', $params['status']];
- }
- $order = 'id desc';
- $select = ['id', 'name', 'mall_id', 'is_default', 'status', 'created_at'];
- $params = ['select' => $select, 'where' => $where, 'order' => $order, 'limit' => $params['limit']];
- $data = GoodsDecorationTemplate::listPage($params, false, true);
- if (empty($data)) return $this->success('获取成功');
- $this->success('获取成功', $data);
- }
- } else {
- return $this->render($this->action->id);
- }
- }
- public function actionDiy()
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isPost) {
- $params = $request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'integer'],
- [['content'], 'string'],
- [['content', 'title'], 'required'],
- [['id'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- // 将数组转为json字符串
- if (is_array($params['content'])) $params['content'] = Json::encode($params['content'], JSON_UNESCAPED_SLASHES);
- $params['mall_id'] = $this->mall_id;
- // 保存数据
- $res = GoodsDecorationTemplateContent::setData($params);
- if ($res == false) throw new Exception(GoodsDecorationTemplateContent::getStaticError());
- return $this->success('保存成功', ['id' => $res->id]);
- } catch (\Exception $e) {
- \Yii::error(self::class . '->' . __FUNCTION__ . ', error:' . $e->getMessage());
- return $this->error('编辑失败,msg:' . $e->getMessage());
- }
- } else {
- $params = $request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'integer'],
- [['id'], 'safe']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- $diypage = (object)[];
- $template = DiyEnum::GOODS_TEMPLATE;
- if (isset($params['id'])) {
- // 查询页面
- $diypage = GoodsDecorationTemplateContent::find()->where(['id' => $params['id'], 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])->select(['id', 'title', 'content', 'created_at'])->asArray()->one();
- if (empty($diypage)) throw new \Exception("页面不存在");
- } else {
- // 查询页面
- $diypage = DiyPage::find()->where(['template' => DiyEnum::GOODS_DETAIL, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])->select(['title', 'template', 'content'])->asArray()->one();
- if (empty($diypage)) throw new \Exception("页面不存在");
- }
- // 获取商城有效的插件
- $valid_addons = MallAddons::getValidAddons($this->mall_id);
- // 根据分组获取组件
- $components = DiyComponents::getComponent(DiyEnum::getGroup($template), [], $valid_addons, $this->mall_id);
- foreach ($components as $key => &$component) {
- if ($component['title'] == '商品组件') unset($components[$key]);
- if ($component['title'] == '图文类') {
- foreach ($component['list'] as $k => $v) {
- if (!in_array($v['type'], ['carousel-img', 'title-block'])) {
- unset($component['list'][$k]);
- }
- }
- $component['list'] = array_values($component['list']);
- }
- }
- unset($component);
- $components = array_values($components);
- // 获取会员等级
- $default_level_list[] = ['level' => 0, 'name' => '普通用户'];
- $member_level_list = UserLevel::getUserLevel([['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]], [], 1, 'level,name');
- $auth[] = [
- 'name' => 'member',
- 'title' => '会员等级',
- 'list' => array_merge($default_level_list, $member_level_list)
- ];
- return $this->success('获取成功', compact('diypage', 'components', 'auth', 'capital', 'valid_addons'));
- } catch (\Exception $e) {
- return $this->error('获取失败,msg:' . $e->getMessage());
- }
- }
- } else {
- // 获取所有插件组件
- $addons_components = DiyComponents::find()->select('code,title,group,source')->where(['status' => StatusEnum::ENABLED])->andWhere(['not', ['source' => '']])->asArray()->all();
- return $this->render($this->action->id, ['addons_components' => $addons_components]);
- }
- }
- /**
- * @Description: 保存模板
- * @Author: zhngsan
- * @DateTime 2023-01-17 17:09:32
- * @return mixed
- */
- public function actionEdit()
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isPost) {
- $params = $request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id', 'template_id', 'is_default', 'status'], 'integer'],
- [['name'], 'string'],
- [['name'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- // 保存数据
- $res = GoodsDecorationTemplate::setData($params);
- if ($res == false) return $this->error(GoodsDecorationTemplate::getStaticError());
- return $this->success('保存成功');
- } else {
- $params = $request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'integer'],
- [['id'], 'safe']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $data = GoodsDecorationTemplate::getOne([['id' => $params['id']], ['mall_id' => $this->mall_id]], true);
- return $this->success('获取成功', $data ?? []);
- }
- } else {
- return $this->render($this->action->id);
- }
- }
- /**
- * 复制页面
- * @return mixed|void
- */
- public function actionCopyTemplate()
- {
- $request = Yii::$app->request;
- if ($request->isPost) {
- $trans = Yii::$app->db->beginTransaction();
- try {
- $params = $request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $detail = GoodsDecorationTemplate::findOne(['id' => $params['id'], 'mall_id' => $this->mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($detail)) throw new \Exception("广告模板不存在");
- $template = GoodsDecorationTemplateContent::findOne(['id' => $detail->template_id, 'mall_id' => $this->mall_id]);
- if (!$template) throw new \Exception("模板样式不存在");
- $new_template = new GoodsDecorationTemplateContent();
- $new_template->attributes = $template->attributes;
- if (!$new_template->save()) throw new \Exception("复制失败:" . $new_template->getErrorMessage());
- $name = $detail->name;
- $detail->name = $name . '-copy';
- $diy = new GoodsDecorationTemplate();
- $diy->attributes = $detail->attributes;
- $diy->template_id = $new_template->id;
- if (!$diy->save()) throw new \Exception("复制失败:" . $diy->getErrorMessage());
- if ($detail->is_default == StatusEnum::ENABLED) {
- $detail->name = $name;
- $detail->is_default = StatusEnum::DISABLED;
- if (!$detail->save()) throw new \Exception("复制失败:" . $detail->getErrorMessage());
- }
- $trans->commit();
- return $this->success('复制成功');
- } catch (\Exception $e) {
- $trans->rollBack();
- return $this->error($e->getMessage());
- }
- }
- }
- /**
- * 装修删除
- * @return mixed|void
- */
- public function actionDelete()
- {
- $request = Yii::$app->request;
- if ($request->isPost) {
- $trans = Yii::$app->db->beginTransaction();
- try {
- $params = $request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $detail = GoodsDecorationTemplate::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($detail)) throw new \Exception("广告模板不存在");
- $template = GoodsDecorationTemplateContent::findOne(['id' => $detail->template_id, 'mall_id' => $this->mall_id]);
- if (!$template) throw new \Exception("模板样式不存在");
- $detail->status = StatusEnum::DELETE;
- if (!$detail->save()) throw new \Exception($detail->getErrorMessage());
- $template->status = StatusEnum::DELETE;
- if (!$template->save()) throw new \Exception($template->getErrorMessage());
- $relations = GoodsTemplateRelation::findAll(['mall_id' => $this->mall_id, 'template_id' => $detail->id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- foreach ($relations as $relation) {
- $relation->status = StatusEnum::DELETE;
- $relation->save();
- }
- $trans->commit();
- return $this->success('删除成功');
- } catch (\Exception $e) {
- $trans->rollBack();
- return $this->error($e->getMessage());
- }
- }
- }
- /**
- *
- * @return mixed|void
- */
- public function actionSwitch()
- {
- $request = Yii::$app->request;
- if ($request->isPost) {
- try {
- $params = $request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $detail = GoodsDecorationTemplate::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($detail)) throw new \Exception("模板不存在");
- $detail->status = $detail->status == StatusEnum::ENABLED ? StatusEnum::DISABLED : StatusEnum::ENABLED;
- if (!$detail->save()) throw new \Exception("操作失败");
- if ($detail->status == StatusEnum::DISABLED) {
- $relations = GoodsTemplateRelation::findAll(['mall_id' => $this->mall_id, 'template_id' => $detail->id, 'status' => StatusEnum::ENABLED]);
- foreach ($relations as $relation) {
- $relation->status = StatusEnum::DISABLED;
- $relation->save();
- }
- }
- return $this->success('操作成功');
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
- /**
- * 根据标识获取对应组件的内容
- * @Author: lun
- * @DateTime: 2021/11/11 0011 14:52
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionGetComponentsData()
- {
- $params = Yii::$app->request->post('form');
- $list = Json::decode($params, true);
- if (empty($list)) return $this->error('请求数据为空');
- $data = GoodsDecorationTemplate::getGoodsListByStrategy($this->mall_id, $list);
- return $this->success('操作成功', $data);
- }
- public function actionBatchSet()
- {
- $request = Yii::$app->request;
- if ($request->isPost) {
- try {
- $params = $request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['chooseList'], 'safe'],
- [['template_id'], 'integer'],
- [['template_id'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $goods_list = Goods::findAll(['mall_id' => $this->mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED], 'id' => $params['chooseList']]);
- foreach ($goods_list as $goods) {
- $res = GoodsTemplateRelation::setData(['mall_id' => $this->mall_id, 'template_id' => $params['template_id'], 'goods_id' => $goods->id, 'status' => StatusEnum::ENABLED]);
- if ($res === false) throw new Exception('goods_id: ' . $goods->id . ' 广告模板设置失败:' . GoodsTemplateRelation::getStaticError());
- }
- return $this->success('操作成功');
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
- }
|