| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\VerifyCard;
- use app\models\Video;
- use app\models\VideoCat;
- use yii\base\Model;
- class VideoForm extends Model
- {
- public $id;
- public $title;
- public $url;
- public $sort;
- public $is_delete;
- public $created_at;
- public $store_id;
- public $pic_url;
- public $content;
- public $type;
- public $urls = "";
- public $updated_at;
- //搜索
- public $search_key;
- public $search_is_show;
- public $cat_id;
- public $select = 0;
- const SCENARIO_ADD = 'add';
- const SCENARIO_EDIT = 'edit';
- const SCENARIO_DEL = 'del';
- const SCENARIO_LIST = 'list';
- public $keyword = '';
- public $name;
- public $status;
- public $ids = [];
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['is_delete', 'store_id', 'type', 'sort', 'cat_id', 'select'], 'integer'],
- [['content'], 'string'],
- [['title', 'search_key'], 'string', 'max' => 255],
- [['url', 'urls', 'pic_url'], 'string', 'max' => 2048],
- ['is_delete', 'default', 'value' => 0],
- [['created_at', 'updated_at'], 'safe'],
- [['id'], 'required', 'on' => self::SCENARIO_DEL]
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'title' => '标题',
- 'url' => 'Url',
- 'sort' => '排序 升序',
- 'is_delete' => 'Is Delete',
- 'created_at' => 'Add Time',
- 'store_id' => '商城id',
- 'pic_url' => 'Pic Url',
- 'content' => '详情介绍',
- 'type' => '视频来源 0--源地址 1--腾讯视频',
- 'updated_at' => '更新时间',
- 'urls' => '腾讯视频地址'
- ];
- }
- public function scenarios()
- {
- $scenarios = parent::scenarios();
- $scenarios[self::SCENARIO_ADD] = ['title', 'url', 'sort', 'store_id', 'pic_url', 'content', 'type', 'urls', 'cat_id'];
- $scenarios[self::SCENARIO_EDIT] = ['id', 'title', 'url', 'sort', 'store_id', 'pic_url', 'content', 'type', 'urls', 'cat_id'];
- $scenarios[self::SCENARIO_DEL] = ['id', 'is_delete'];
- $scenarios[self::SCENARIO_LIST] = ['search_key', 'cat_id', 'select'];
- return $scenarios;
- }
- /**
- * Undocumented function
- * @Author LGL 24963@qq.com
- * @DateTime 2021-01-06
- * @desc: 保存视频
- */
- public function saveVideo()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- if ($this->scenario == self::SCENARIO_EDIT) {
- $model = Video::findOne(['id' => $this->id]);
- } else {
- $model = new Video();
- }
- $model->attributes = $this->attributes;
- $model->type = 0;
- 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-07
- * @desc: 搜索视频
- */
- public function searchVideo()
- {
- $query = Video::find()->with(['videoCat']);
- $query->where(['is_delete' => Video::IS_DELETE_NO, 'store_id' => $this->store_id])->orderBy("sort desc,id desc");
- // 搜索
- if ($this->search_key) {
- $query->andWhere(['like', 'title', $this->search_key]);
- }
- if ($this->cat_id) {
- $query->andWhere(['cat_id' => $this->cat_id]);
- }
- $list = pagination_make($query);
- if ($this->select == 1) {
- // 如果是选择视频
- $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();
- $ids = array_map(function ($id) {
- return json_decode($id, TRUE);
- }, $useVideoId);
- $ids = array_merge(...$ids);
- foreach ($list['list'] as &$item) {
- if (in_array($item['id'], $ids)) {
- $item['is_add'] = TRUE;
- } else {
- $item['is_add'] = FALSE;
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount']
- ]
- ];
- }
- /**
- * Undocumented function
- * @Author LGL 24963@qq.com
- * @DateTime 2021-01-07
- * @desc: 删除视频
- */
- public function delVideo()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- if ($this->scenario !== self::SCENARIO_DEL) {
- return [
- 'code' => 1,
- 'msg' => '删除失败'
- ];
- }
- $model = Video::findOne(['id' => $this->id]);
- $model->is_delete = Video::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]
- ];
- }
- }
- /**
- * Undocumented function
- * @Author LGL 24963@qq.com
- * @DateTime 2021-01-07
- * @desc: 获取视频列表,不分页
- */
- public static function getVideoList()
- {
- return Video::find()->where(['is_delete' => Video::IS_DELETE_NO])->orderBy(['sort desc, id desc'])->select('*')->asArray()->all();
- }
- public function getVideoCatList()
- {
- $query = VideoCat::find()->where(['is_delete' => VideoCat::IS_NOT_DELETE])->andWhere(['store_id' => $this->store_id])->orderBy('created_at desc');
- if ($this->keyword) {
- $query->andWhere(['like', 'name', $this->keyword]);
- }
- if ($this->status >= 0) {
- $query->andWhere(['status' => $this->status]);
- }
- return pagination_make($query);
- }
- public function saveVideoCat()
- {
- if ($this->id > 0) {
- $model = VideoCat::findOne(['id' => $this->id]);
- } else {
- $model = new VideoCat();
- }
- $model->name = $this->name;
- $model->store_id = $this->store_id;
- $model->status = $this->status;
- if (!$model->save()) {
- return [
- 'code' => 1,
- 'msg' => '保存失败'
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- }
- public function changeVideoCatStatus()
- {
- if (empty($this->ids)) {
- return ['code' => 1, 'msg' => '请求选择操作数据'];
- }
- VideoCat::updateAll(['status' => $this->status], ['id' => $this->ids]);
- return ['code' => 0, 'msg' => '操作成功'];
- }
- public function delVideoCat()
- {
- if (empty($this->ids)) {
- return ['code' => 1, 'msg' => '请求选择操作数据'];
- }
- if (Video::find()->where(['cat_id' => $this->ids, 'is_delete' => Video::IS_DELETE_NO])->exists()) {
- return ['code' => 1, 'msg' => '请先删除该分类下的视频'];
- }
- VideoCat::updateAll(['is_delete' => VideoCat::IS_DELETE], ['id' => $this->ids]);
- return ['code' => 0, 'msg' => '操作成功'];
- }
- }
|