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' => '操作成功']; } }