AttachmentNewsService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace addons\Wechat\services;
  3. use common\enums\StatusEnum;
  4. use yii\data\Pagination;
  5. use yii\helpers\Json;
  6. use common\components\Service;
  7. use common\helpers\Url;
  8. use addons\Wechat\common\models\AttachmentNews;
  9. use addons\Wechat\common\enums\AttachmentTypeEnum;
  10. /**
  11. * Class AttachmentNewsService
  12. * @package addons\Wechat\services
  13. * @author jianyan74 <751393839@qq.com>
  14. */
  15. class AttachmentNewsService extends Service
  16. {
  17. /**
  18. * 获取图文
  19. *
  20. * @param $keyword
  21. * @return array
  22. */
  23. public function getFirstListPage($year = '', $month = '', $keyword = '')
  24. {
  25. $data = AttachmentNews::find()
  26. ->where(['sort' => 0, 'status' => StatusEnum::ENABLED])
  27. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  28. ->andFilterWhere(['year' => $year])
  29. ->andFilterWhere(['month' => $month])
  30. ->andFilterWhere(['like', 'title', $keyword]);
  31. $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => 10, 'validatePage' => false]);
  32. $models = $data->offset($pages->offset)
  33. ->orderBy('id desc')
  34. ->with('attachment')
  35. ->limit($pages->limit)
  36. ->select('id, sort, status, thumb_url, title, attachment_id')
  37. ->asArray()
  38. ->all();
  39. $list = [];
  40. foreach ($models as $model) {
  41. $listTmp = [];
  42. $listTmp['key'] = $model['attachment_id'];
  43. $listTmp['title'] = $model['title'];
  44. $listTmp['type'] = AttachmentTypeEnum::IMAGE;
  45. $listTmp['imgUrl'] = Url::to(['analysis/image', 'attach' => $model['thumb_url']]);
  46. $list[] = $listTmp;
  47. unset($listTmp);
  48. }
  49. return $list;
  50. }
  51. /**
  52. * 格式化
  53. *
  54. * @param $attachment_id
  55. * @return string
  56. */
  57. public function formattingList($attachment_id)
  58. {
  59. $list = $this->findByAttachmentId($attachment_id);
  60. foreach ($list as &$item) {
  61. $item['thumb_url'] = urldecode(Url::to(['analysis/image', 'attach' => $item['thumb_url']]));
  62. preg_match_all('/<img[^>]*src\s*=\s*([\'"]?)([^\'" >]*)\1/isu', $item['content'], $match);
  63. $match_arr = [];
  64. foreach ($match[2] as $vo) {
  65. $match_arr[$vo] = $vo;
  66. }
  67. foreach ($match_arr as $src) {
  68. $url = Url::to(['analysis/image', 'attach' => $src]);
  69. $url = urldecode($url);
  70. $item['content'] = str_replace($src, $url, $item['content']);
  71. }
  72. }
  73. return Json::encode($list);
  74. }
  75. /**
  76. * @param $attachment_id
  77. * @return array|\yii\db\ActiveRecord|null
  78. */
  79. public function firstByAttachmentId($attachment_id)
  80. {
  81. return AttachmentNews::find()
  82. ->where(['sort' => 0, 'attachment_id' => $attachment_id])
  83. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  84. ->one();
  85. }
  86. /**
  87. * @param $attachment_id
  88. * @return array|\yii\db\ActiveRecord[]
  89. */
  90. public function findByAttachmentId($attachment_id)
  91. {
  92. return AttachmentNews::find()
  93. ->where(['attachment_id' => $attachment_id])
  94. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  95. ->orderBy('sort asc')
  96. ->asArray()
  97. ->all();
  98. }
  99. /**
  100. * @param $attachment_id
  101. * @return AttachmentNews|null
  102. */
  103. public function findModel($attachment_id)
  104. {
  105. if (empty($attachment_id) || empty(($model = AttachmentNews::findOne($attachment_id)))) {
  106. $model = new AttachmentNews();
  107. return $model->loadDefaultValues();
  108. }
  109. return $model;
  110. }
  111. }
粤ICP备19079148号