TopicForm.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Topic;
  9. use app\models\TopicType;
  10. use yii\base\Model;
  11. use yii\data\ActiveDataProvider;
  12. /**
  13. * TopicSearch represents the model behind the search form of `app\models\Topic`.
  14. */
  15. class TopicForm extends Model
  16. {
  17. public $id;
  18. public $store_id;
  19. public $title;
  20. public $sub_title;
  21. public $cover_pic;
  22. public $content;
  23. public $read_count;
  24. public $virtual_read_count;
  25. public $layout;
  26. public $sort;
  27. public $agree_count;
  28. public $virtual_agree_count;
  29. public $virtual_favorite_count;
  30. public $is_delete;
  31. public $is_chosen;
  32. public $type;
  33. public $qrcode_pic;
  34. public $is_show;
  35. //搜索
  36. public $search_key;
  37. public $search_is_show;
  38. const SCENARIO_ADD = 'add';
  39. const SCENARIO_EDIT = 'edit';
  40. const SCENARIO_DEL = 'del';
  41. const SCENARIO_LIST = 'list';
  42. const SCENARIO_EDIT_STATUS = 'is_show';
  43. const IS_SHOW = [
  44. Topic::IS_SHOW_YES => '显示',
  45. Topic::IS_SHOW_NO => '不显示'
  46. ];
  47. const IS_CHOSEN = [
  48. Topic::IS_CHOSEN_YES => '精选',
  49. Topic::IS_CHOSEN_NO => '不精选'
  50. ];
  51. const LAYOUT = [
  52. Topic::LAYOUT_LARGE => '大图模式',
  53. Topic::LAYOUT_SMALL => '小图模式'
  54. ];
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function rules()
  59. {
  60. return [
  61. [['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'],
  62. [['cover_pic', 'content', 'qrcode_pic'], 'string'],
  63. [['title', 'sub_title'], 'string', 'max' => 255],
  64. [['title', 'cover_pic', 'content', 'sort'], 'required', 'on' => self::SCENARIO_ADD],
  65. [['title', 'cover_pic', 'content', 'sort', 'id'], 'required', 'on' => self::SCENARIO_EDIT],
  66. [['id'], 'required', 'on' => self::SCENARIO_DEL],
  67. [['id', 'is_show'], 'required', 'on' => self::SCENARIO_EDIT_STATUS],
  68. ['search_key', 'string', 'on' => self::SCENARIO_LIST],
  69. ['search_is_show', 'integer', 'on' => self::SCENARIO_LIST]
  70. ];
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function attributeLabels()
  76. {
  77. return [
  78. 'id' => 'ID',
  79. 'store_id' => 'Store ID',
  80. 'title' => '标题',
  81. 'sub_title' => '副标题',
  82. 'cover_pic' => '封面图片',
  83. 'content' => '专题内容',
  84. 'read_count' => '阅读量',
  85. 'virtual_read_count' => '虚拟阅读量',
  86. 'layout' => '布局方式:0=小图,1=大图模式',
  87. 'sort' => '排序:升序',
  88. 'agree_count' => '点赞数',
  89. 'virtual_agree_count' => '虚拟点赞数',
  90. 'virtual_favorite_count' => '虚拟收藏量',
  91. 'is_delete' => '删除',
  92. 'is_chosen' => '精选(0、否;1、是)',
  93. 'type' => '专题分类',
  94. 'qrcode_pic' => '海报分享图',
  95. 'is_show' => '显示1'
  96. ];
  97. }
  98. /**
  99. * Undocumented function
  100. *
  101. * @Author LGL 24963@qq.com
  102. * @DateTime 2021-01-06
  103. * @desc: model场景
  104. * @return void
  105. */
  106. public function scenarios()
  107. {
  108. $scenarios = parent::scenarios();
  109. return $scenarios;
  110. }
  111. /**
  112. * Undocumented function
  113. *
  114. * @Author LGL 24963@qq.com
  115. * @DateTime 2021-01-06
  116. * @desc: 保存,包括新建以及编辑专题model
  117. * @return void
  118. */
  119. public function saveTopic()
  120. {
  121. if ($this->validate()) {
  122. $t = \Yii::$app->db->beginTransaction();
  123. if ($this->scenario == self::SCENARIO_EDIT) {
  124. $model = Topic::findOne(['id' => $this->id]);
  125. } else {
  126. $model = new Topic();
  127. }
  128. $model->attributes = $this->attributes;
  129. if (!$model->save()) {
  130. $t->rollBack();
  131. return [
  132. 'code' => 1,
  133. 'msg' => '保存失败'
  134. ];
  135. }
  136. $t->commit();
  137. return [
  138. 'code' => 0,
  139. 'msg' => '保存成功'
  140. ];
  141. } else {
  142. // 验证失败:$errors 是一个包含错误信息的数组
  143. return [
  144. 'code' => 1,
  145. "msg" => $this->getErrorSummary(false)[0]
  146. ];
  147. }
  148. }
  149. /**
  150. * Undocumented function
  151. *
  152. * @Author LGL 24963@qq.com
  153. * @DateTime 2021-01-06
  154. * @desc: 搜索专题model
  155. *
  156. * @return void
  157. */
  158. public function searchForm()
  159. {
  160. $query = Topic::find()->alias("t")->leftJoin(['tc' => TopicType::tableName()], 'tc.id=t.type');
  161. $query->where(['t.is_delete' => Topic::IS_DELETE_NO, 't.store_id' => $this->store_id])->orderBy("t.sort desc,t.id desc");
  162. // 搜索
  163. if ($this->search_key) {
  164. $query->andWhere(['like', 't.title', trim($this->search_key)]);
  165. }
  166. // 搜索是否显示
  167. if (isset($this->search_is_show) && ((int)$this->search_is_show === Topic::IS_SHOW_YES || (int)$this->search_is_show === Topic::IS_SHOW_NO)) {
  168. $query->andWhere(['t.is_show' => $this->search_is_show]);
  169. }
  170. $query->select("t.*, tc.name type_name");
  171. $list = pagination_make($query);
  172. foreach ($list['list'] as &$val) {
  173. $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
  174. $val['is_chosen_text'] = self::IS_CHOSEN[$val['is_chosen']];
  175. $val['layout_text'] = self::LAYOUT[$val['layout']];
  176. }
  177. $type_list = TopicTypeForm::getList();
  178. return [
  179. 'code' => 0,
  180. 'msg' => 'success',
  181. 'data' => [
  182. 'data' => $list['list'],
  183. 'pageNo' => $list['pageNo'],
  184. 'totalCount' => $list['totalCount'],
  185. 'type_list' => $type_list
  186. ]
  187. ];
  188. }
  189. /**
  190. * Undocumented function
  191. *
  192. * @Author LGL 24963@qq.com
  193. * @DateTime 2021-01-06
  194. * @desc: 删除专题model
  195. * @return void
  196. */
  197. public function delTopic()
  198. {
  199. if ($this->validate()) {
  200. $t = \Yii::$app->db->beginTransaction();
  201. if ($this->scenario !== self::SCENARIO_DEL) {
  202. return [
  203. 'code' => 1,
  204. 'msg' => '删除失败'
  205. ];
  206. }
  207. $model = Topic::findOne(['id' => $this->id]);
  208. $model->is_delete = Topic::IS_DELETE_YES;
  209. if (!$model || !$model->save()) {
  210. $t->rollBack();
  211. return [
  212. 'code' => 1,
  213. 'msg' => '删除失败'
  214. ];
  215. }
  216. $t->commit();
  217. return [
  218. 'code' => 0,
  219. 'msg' => '删除成功'
  220. ];
  221. // 所有输入数据都有效 all inputs are valid
  222. } else {
  223. // 验证失败:$errors 是一个包含错误信息的数组
  224. return [
  225. 'code' => 1,
  226. "msg" => $this->getErrorSummary(false)[0]
  227. ];
  228. }
  229. }
  230. public function editTopicIsShow()
  231. {
  232. if ($this->validate()) {
  233. $t = \Yii::$app->db->beginTransaction();
  234. if ($this->scenario !== self::SCENARIO_EDIT_STATUS) {
  235. return [
  236. 'code' => 1,
  237. 'msg' => '操作失败'
  238. ];
  239. }
  240. $model = Topic::findOne(['id' => $this->id]);
  241. $model->is_show = $this->is_show == Topic::IS_DELETE_YES ? Topic::IS_SHOW_NO : Topic::IS_DELETE_YES;
  242. if (!$model || !$model->save()) {
  243. $t->rollBack();
  244. return [
  245. 'code' => 1,
  246. 'msg' => '操作失败'
  247. ];
  248. }
  249. $t->commit();
  250. return [
  251. 'code' => 0,
  252. 'msg' => '操作成功'
  253. ];
  254. // 所有输入数据都有效 all inputs are valid
  255. } else {
  256. // 验证失败:$errors 是一个包含错误信息的数组
  257. return [
  258. 'code' => 1,
  259. "msg" => $this->getErrorSummary(false)[0]
  260. ];
  261. }
  262. }
  263. }