TopicForm.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models;
  8. use app\utils\GetInfo;
  9. use app\models\Topic;
  10. use app\models\TopicFavorite;
  11. use yii\base\Model;
  12. class TopicForm extends Model
  13. {
  14. public $store_id;
  15. public $user_id;
  16. public $id;
  17. public function rules()
  18. {
  19. return [
  20. ['id', 'required'],
  21. ];
  22. }
  23. public function search()
  24. {
  25. if (!$this->validate()) {
  26. return [
  27. 'code' => 1,
  28. 'msg' => $this->getErrorSummary(false)[0],
  29. ];
  30. }
  31. $model = Topic::find()->where(['store_id' => $this->store_id, 'id' => $this->id, 'is_delete' => 0])
  32. ->select('id,title,read_count,virtual_read_count,content,created_at')->asArray()->one();
  33. if (empty($model)) {
  34. // return new ApiResponse(1, '内容不存在');
  35. return [
  36. 'code' => 1,
  37. 'msg' => '内容不存在'
  38. ];
  39. }
  40. Topic::updateAll(['read_count' => $model['read_count'] + 1], ['id' => $model['id']]);
  41. $model['read_count'] = intval($model['read_count']) + intval($model['virtual_read_count']);
  42. unset($model['virtual_read_count']);
  43. if ($model['read_count'] < 10000) {
  44. $model['read_count'] = $model['read_count'] . '人浏览';
  45. }
  46. if ($model['read_count'] >= 10000) {
  47. $model['read_count'] = intval($model['read_count'] / 10000) . '万+人浏览';
  48. }
  49. $model['addtime'] = date('Y-m-d', $model['created_at']);
  50. $favorite = TopicFavorite::findOne(['user_id' => $this->user_id, 'topic_id' => $model['id'], 'is_delete' => 0]);
  51. $model['is_favorite'] = $favorite ? 1 : 0;
  52. $model['content'] = $this->transTxvideo($model['content']);
  53. // return new ApiResponse(0, 'success', $model);
  54. return [
  55. 'code' => 0,
  56. 'msg' => 'success',
  57. 'data' => $model
  58. ];
  59. }
  60. private function transTxvideo($content)
  61. {
  62. preg_match_all("/https\:\/\/v\.qq\.com[^ '\"]+\.html[^ '\"]*/i", $content, $match_list);
  63. if (!is_array($match_list) || count($match_list) == 0) {
  64. return $content;
  65. }
  66. $url_list = $match_list[0];
  67. foreach ($url_list as $url) {
  68. $res = GetInfo::getVideoInfo($url);
  69. if ($res['code'] == 0) {
  70. $new_url = $res['url'];
  71. $content = str_replace('src="' . $url . '"', 'src="' . $new_url . '"', $content);
  72. }
  73. }
  74. return $content;
  75. }
  76. }