| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models;
- use app\utils\GetInfo;
- use app\models\Topic;
- use app\models\TopicFavorite;
- use yii\base\Model;
- class TopicForm extends Model
- {
- public $store_id;
- public $user_id;
- public $id;
- public function rules()
- {
- return [
- ['id', 'required'],
- ];
- }
- public function search()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- $model = Topic::find()->where(['store_id' => $this->store_id, 'id' => $this->id, 'is_delete' => 0])
- ->select('id,title,read_count,virtual_read_count,content,created_at')->asArray()->one();
- if (empty($model)) {
- // return new ApiResponse(1, '内容不存在');
- return [
- 'code' => 1,
- 'msg' => '内容不存在'
- ];
- }
- Topic::updateAll(['read_count' => $model['read_count'] + 1], ['id' => $model['id']]);
- $model['read_count'] = intval($model['read_count']) + intval($model['virtual_read_count']);
- unset($model['virtual_read_count']);
- if ($model['read_count'] < 10000) {
- $model['read_count'] = $model['read_count'] . '人浏览';
- }
- if ($model['read_count'] >= 10000) {
- $model['read_count'] = intval($model['read_count'] / 10000) . '万+人浏览';
- }
- $model['addtime'] = date('Y-m-d', $model['created_at']);
- $favorite = TopicFavorite::findOne(['user_id' => $this->user_id, 'topic_id' => $model['id'], 'is_delete' => 0]);
- $model['is_favorite'] = $favorite ? 1 : 0;
- $model['content'] = $this->transTxvideo($model['content']);
- // return new ApiResponse(0, 'success', $model);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $model
- ];
- }
- private function transTxvideo($content)
- {
- preg_match_all("/https\:\/\/v\.qq\.com[^ '\"]+\.html[^ '\"]*/i", $content, $match_list);
- if (!is_array($match_list) || count($match_list) == 0) {
- return $content;
- }
- $url_list = $match_list[0];
- foreach ($url_list as $url) {
- $res = GetInfo::getVideoInfo($url);
- if ($res['code'] == 0) {
- $new_url = $res['url'];
- $content = str_replace('src="' . $url . '"', 'src="' . $new_url . '"', $content);
- }
- }
- return $content;
- }
- }
|