TopicForm.php 2.8 KB

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