| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\Topic;
- use app\models\TopicType;
- use yii\base\Model;
- use yii\data\ActiveDataProvider;
- /**
- * TopicSearch represents the model behind the search form of `app\models\Topic`.
- */
- class TopicForm extends Model
- {
- public $id;
- public $store_id;
- public $title;
- public $sub_title;
- public $cover_pic;
- public $content;
- public $read_count;
- public $virtual_read_count;
- public $layout;
- public $sort;
- public $agree_count;
- public $virtual_agree_count;
- public $virtual_favorite_count;
- public $is_delete;
- public $is_chosen;
- public $type;
- public $qrcode_pic;
- public $is_show;
- //搜索
- public $search_key;
- public $search_is_show;
-
- const SCENARIO_ADD = 'add';
- const SCENARIO_EDIT = 'edit';
- const SCENARIO_DEL = 'del';
- const SCENARIO_LIST = 'list';
- const SCENARIO_EDIT_STATUS = 'is_show';
- const IS_SHOW = [
- Topic::IS_SHOW_YES => '显示',
- Topic::IS_SHOW_NO => '不显示'
- ];
- const IS_CHOSEN = [
- Topic::IS_CHOSEN_YES => '精选',
- Topic::IS_CHOSEN_NO => '不精选'
- ];
- const LAYOUT = [
- Topic::LAYOUT_LARGE => '大图模式',
- Topic::LAYOUT_SMALL => '小图模式'
- ];
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['store_id', 'read_count', 'virtual_read_count', 'layout', 'sort', 'agree_count', 'virtual_agree_count', 'virtual_favorite_count', 'is_delete', 'is_chosen', 'type', 'is_show'], 'integer'],
- [['cover_pic', 'content', 'qrcode_pic'], 'string'],
- [['title', 'sub_title'], 'string', 'max' => 255],
- [['title', 'cover_pic', 'content', 'sort'], 'required', 'on' => self::SCENARIO_ADD],
- [['title', 'cover_pic', 'content', 'sort', 'id'], 'required', 'on' => self::SCENARIO_EDIT],
- [['id'], 'required', 'on' => self::SCENARIO_DEL],
- [['id', 'is_show'], 'required', 'on' => self::SCENARIO_EDIT_STATUS],
- ['search_key', 'string', 'on' => self::SCENARIO_LIST],
- ['search_is_show', 'integer', 'on' => self::SCENARIO_LIST]
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'store_id' => 'Store ID',
- 'title' => '标题',
- 'sub_title' => '副标题',
- 'cover_pic' => '封面图片',
- 'content' => '专题内容',
- 'read_count' => '阅读量',
- 'virtual_read_count' => '虚拟阅读量',
- 'layout' => '布局方式:0=小图,1=大图模式',
- 'sort' => '排序:升序',
- 'agree_count' => '点赞数',
- 'virtual_agree_count' => '虚拟点赞数',
- 'virtual_favorite_count' => '虚拟收藏量',
- 'is_delete' => '删除',
- 'is_chosen' => '精选(0、否;1、是)',
- 'type' => '专题分类',
- 'qrcode_pic' => '海报分享图',
- 'is_show' => '显示1'
- ];
- }
- /**
- * Undocumented function
- *
- * @Author LGL 24963@qq.com
- * @DateTime 2021-01-06
- * @desc: model场景
- * @return void
- */
- public function scenarios()
- {
- $scenarios = parent::scenarios();
- return $scenarios;
- }
- /**
- * Undocumented function
- *
- * @Author LGL 24963@qq.com
- * @DateTime 2021-01-06
- * @desc: 保存,包括新建以及编辑专题model
- * @return void
- */
- public function saveTopic()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- if ($this->scenario == self::SCENARIO_EDIT) {
- $model = Topic::findOne(['id' => $this->id]);
- } else {
- $model = new Topic();
- }
- $model->attributes = $this->attributes;
- if (!$model->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '保存失败'
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- "msg" => $this->getErrorSummary(false)[0]
- ];
- }
- }
- /**
- * Undocumented function
- *
- * @Author LGL 24963@qq.com
- * @DateTime 2021-01-06
- * @desc: 搜索专题model
- *
- * @return void
- */
- public function searchForm()
- {
- $query = Topic::find()->alias("t")->leftJoin(['tc' => TopicType::tableName()], 'tc.id=t.type');
- $query->where(['t.is_delete' => Topic::IS_DELETE_NO, 't.store_id' => $this->store_id])->orderBy("t.sort desc,t.id desc");
- // 搜索
- if ($this->search_key) {
- $query->andWhere(['like', 't.title', trim($this->search_key)]);
- }
- // 搜索是否显示
- if (isset($this->search_is_show) && ((int)$this->search_is_show === Topic::IS_SHOW_YES || (int)$this->search_is_show === Topic::IS_SHOW_NO)) {
- $query->andWhere(['t.is_show' => $this->search_is_show]);
- }
- $query->select("t.*, tc.name type_name");
- $list = pagination_make($query);
- foreach ($list['list'] as &$val) {
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- $val['is_chosen_text'] = self::IS_CHOSEN[$val['is_chosen']];
- $val['layout_text'] = self::LAYOUT[$val['layout']];
- }
- $type_list = TopicTypeForm::getList();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount'],
- 'type_list' => $type_list
- ]
- ];
- }
- /**
- * Undocumented function
- *
- * @Author LGL 24963@qq.com
- * @DateTime 2021-01-06
- * @desc: 删除专题model
- * @return void
- */
- public function delTopic()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- if ($this->scenario !== self::SCENARIO_DEL) {
- return [
- 'code' => 1,
- 'msg' => '删除失败'
- ];
- }
- $model = Topic::findOne(['id' => $this->id]);
- $model->is_delete = Topic::IS_DELETE_YES;
- if (!$model || !$model->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '删除失败'
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '删除成功'
- ];
- // 所有输入数据都有效 all inputs are valid
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- "msg" => $this->getErrorSummary(false)[0]
- ];
- }
- }
-
- public function editTopicIsShow()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- if ($this->scenario !== self::SCENARIO_EDIT_STATUS) {
- return [
- 'code' => 1,
- 'msg' => '操作失败'
- ];
- }
- $model = Topic::findOne(['id' => $this->id]);
- $model->is_show = $this->is_show == Topic::IS_DELETE_YES ? Topic::IS_SHOW_NO : Topic::IS_DELETE_YES;
- if (!$model || !$model->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '操作失败'
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- // 所有输入数据都有效 all inputs are valid
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- "msg" => $this->getErrorSummary(false)[0]
- ];
- }
- }
- }
|