VideoForm.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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\VerifyCard;
  9. use app\models\Video;
  10. use app\models\VideoCat;
  11. use yii\base\Model;
  12. class VideoForm extends Model
  13. {
  14. public $id;
  15. public $title;
  16. public $url;
  17. public $sort;
  18. public $is_delete;
  19. public $created_at;
  20. public $store_id;
  21. public $pic_url;
  22. public $content;
  23. public $type;
  24. public $urls = "";
  25. public $updated_at;
  26. //搜索
  27. public $search_key;
  28. public $search_is_show;
  29. public $cat_id;
  30. public $select = 0;
  31. const SCENARIO_ADD = 'add';
  32. const SCENARIO_EDIT = 'edit';
  33. const SCENARIO_DEL = 'del';
  34. const SCENARIO_LIST = 'list';
  35. public $keyword = '';
  36. public $name;
  37. public $status;
  38. public $ids = [];
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function rules()
  43. {
  44. return [
  45. [['is_delete', 'store_id', 'type', 'sort', 'cat_id', 'select'], 'integer'],
  46. [['content'], 'string'],
  47. [['title', 'search_key'], 'string', 'max' => 255],
  48. [['url', 'urls', 'pic_url'], 'string', 'max' => 2048],
  49. ['is_delete', 'default', 'value' => 0],
  50. [['created_at', 'updated_at'], 'safe'],
  51. [['id'], 'required', 'on' => self::SCENARIO_DEL]
  52. ];
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function attributeLabels()
  58. {
  59. return [
  60. 'id' => 'ID',
  61. 'title' => '标题',
  62. 'url' => 'Url',
  63. 'sort' => '排序 升序',
  64. 'is_delete' => 'Is Delete',
  65. 'created_at' => 'Add Time',
  66. 'store_id' => '商城id',
  67. 'pic_url' => 'Pic Url',
  68. 'content' => '详情介绍',
  69. 'type' => '视频来源 0--源地址 1--腾讯视频',
  70. 'updated_at' => '更新时间',
  71. 'urls' => '腾讯视频地址'
  72. ];
  73. }
  74. public function scenarios()
  75. {
  76. $scenarios = parent::scenarios();
  77. $scenarios[self::SCENARIO_ADD] = ['title', 'url', 'sort', 'store_id', 'pic_url', 'content', 'type', 'urls', 'cat_id'];
  78. $scenarios[self::SCENARIO_EDIT] = ['id', 'title', 'url', 'sort', 'store_id', 'pic_url', 'content', 'type', 'urls', 'cat_id'];
  79. $scenarios[self::SCENARIO_DEL] = ['id', 'is_delete'];
  80. $scenarios[self::SCENARIO_LIST] = ['search_key', 'cat_id', 'select'];
  81. return $scenarios;
  82. }
  83. /**
  84. * Undocumented function
  85. * @Author LGL 24963@qq.com
  86. * @DateTime 2021-01-06
  87. * @desc: 保存视频
  88. */
  89. public function saveVideo()
  90. {
  91. if ($this->validate()) {
  92. $t = \Yii::$app->db->beginTransaction();
  93. if ($this->scenario == self::SCENARIO_EDIT) {
  94. $model = Video::findOne(['id' => $this->id]);
  95. } else {
  96. $model = new Video();
  97. }
  98. $model->attributes = $this->attributes;
  99. $model->type = 0;
  100. if (!$model->save()) {
  101. $t->rollBack();
  102. return [
  103. 'code' => 1,
  104. 'msg' => '保存失败'
  105. ];
  106. }
  107. $t->commit();
  108. return [
  109. 'code' => 0,
  110. 'msg' => '保存成功'
  111. ];
  112. } else {
  113. // 验证失败:$errors 是一个包含错误信息的数组
  114. return [
  115. 'code' => 1,
  116. "msg" => $this->getErrorSummary(FALSE)[0]
  117. ];
  118. }
  119. }
  120. /**
  121. * Undocumented function
  122. * @Author LGL 24963@qq.com
  123. * @DateTime 2021-01-07
  124. * @desc: 搜索视频
  125. */
  126. public function searchVideo()
  127. {
  128. $query = Video::find()->with(['videoCat']);
  129. $query->where(['is_delete' => Video::IS_DELETE_NO, 'store_id' => $this->store_id])->orderBy("sort desc,id desc");
  130. // 搜索
  131. if ($this->search_key) {
  132. $query->andWhere(['like', 'title', $this->search_key]);
  133. }
  134. if ($this->cat_id) {
  135. $query->andWhere(['cat_id' => $this->cat_id]);
  136. }
  137. $list = pagination_make($query);
  138. if ($this->select == 1) {
  139. // 如果是选择视频
  140. $useVideoId = VerifyCard::find()->where(['store_id' => $this->store_id, 'is_delete' => VerifyCard::IS_DELETE_NO, 'type' => 5])->andWhere('video_ids IS NOT NULL')->select('video_ids')->column();
  141. $ids = array_map(function ($id) {
  142. return json_decode($id, TRUE);
  143. }, $useVideoId);
  144. $ids = array_merge(...$ids);
  145. foreach ($list['list'] as &$item) {
  146. if (in_array($item['id'], $ids)) {
  147. $item['is_add'] = TRUE;
  148. } else {
  149. $item['is_add'] = FALSE;
  150. }
  151. }
  152. }
  153. return [
  154. 'code' => 0,
  155. 'msg' => 'success',
  156. 'data' => [
  157. 'data' => $list['list'],
  158. 'pageNo' => $list['pageNo'],
  159. 'totalCount' => $list['totalCount']
  160. ]
  161. ];
  162. }
  163. /**
  164. * Undocumented function
  165. * @Author LGL 24963@qq.com
  166. * @DateTime 2021-01-07
  167. * @desc: 删除视频
  168. */
  169. public function delVideo()
  170. {
  171. if ($this->validate()) {
  172. $t = \Yii::$app->db->beginTransaction();
  173. if ($this->scenario !== self::SCENARIO_DEL) {
  174. return [
  175. 'code' => 1,
  176. 'msg' => '删除失败'
  177. ];
  178. }
  179. $model = Video::findOne(['id' => $this->id]);
  180. $model->is_delete = Video::IS_DELETE_YES;
  181. if (!$model || !$model->save()) {
  182. $t->rollBack();
  183. return [
  184. 'code' => 1,
  185. 'msg' => '删除失败'
  186. ];
  187. }
  188. $t->commit();
  189. return [
  190. 'code' => 0,
  191. 'msg' => '删除成功'
  192. ];
  193. // 所有输入数据都有效 all inputs are valid
  194. } else {
  195. // 验证失败:$errors 是一个包含错误信息的数组
  196. return [
  197. 'code' => 1,
  198. "msg" => $this->getErrorSummary(FALSE)[0]
  199. ];
  200. }
  201. }
  202. /**
  203. * Undocumented function
  204. * @Author LGL 24963@qq.com
  205. * @DateTime 2021-01-07
  206. * @desc: 获取视频列表,不分页
  207. */
  208. public static function getVideoList()
  209. {
  210. return Video::find()->where(['is_delete' => Video::IS_DELETE_NO])->orderBy(['sort desc, id desc'])->select('*')->asArray()->all();
  211. }
  212. public function getVideoCatList()
  213. {
  214. $query = VideoCat::find()->where(['is_delete' => VideoCat::IS_NOT_DELETE])->andWhere(['store_id' => $this->store_id])->orderBy('created_at desc');
  215. if ($this->keyword) {
  216. $query->andWhere(['like', 'name', $this->keyword]);
  217. }
  218. if ($this->status >= 0) {
  219. $query->andWhere(['status' => $this->status]);
  220. }
  221. return pagination_make($query);
  222. }
  223. public function saveVideoCat()
  224. {
  225. if ($this->id > 0) {
  226. $model = VideoCat::findOne(['id' => $this->id]);
  227. } else {
  228. $model = new VideoCat();
  229. }
  230. $model->name = $this->name;
  231. $model->store_id = $this->store_id;
  232. $model->status = $this->status;
  233. if (!$model->save()) {
  234. return [
  235. 'code' => 1,
  236. 'msg' => '保存失败'
  237. ];
  238. }
  239. return [
  240. 'code' => 0,
  241. 'msg' => '保存成功'
  242. ];
  243. }
  244. public function changeVideoCatStatus()
  245. {
  246. if (empty($this->ids)) {
  247. return ['code' => 1, 'msg' => '请求选择操作数据'];
  248. }
  249. VideoCat::updateAll(['status' => $this->status], ['id' => $this->ids]);
  250. return ['code' => 0, 'msg' => '操作成功'];
  251. }
  252. public function delVideoCat()
  253. {
  254. if (empty($this->ids)) {
  255. return ['code' => 1, 'msg' => '请求选择操作数据'];
  256. }
  257. if (Video::find()->where(['cat_id' => $this->ids, 'is_delete' => Video::IS_DELETE_NO])->exists()) {
  258. return ['code' => 1, 'msg' => '请先删除该分类下的视频'];
  259. }
  260. VideoCat::updateAll(['is_delete' => VideoCat::IS_DELETE], ['id' => $this->ids]);
  261. return ['code' => 0, 'msg' => '操作成功'];
  262. }
  263. }