WechatReplyRepository.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CatchAdmin [Just Like ~ ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
  8. // +----------------------------------------------------------------------
  9. // | Author: JaguarJack [ njphper@gmail.com ]
  10. // +----------------------------------------------------------------------
  11. namespace catchAdmin\wechat\repository;
  12. use catchAdmin\wechat\model\WechatReply;
  13. use catcher\base\CatchRepository;
  14. use catcher\library\WeChat;
  15. class WechatReplyRepository extends CatchRepository
  16. {
  17. protected $reply;
  18. public function __construct(WechatReply $reply)
  19. {
  20. $this->reply = $reply;
  21. }
  22. public function model()
  23. {
  24. return $this->reply;
  25. }
  26. /**
  27. * 存储
  28. *
  29. * @time 2020年09月13日
  30. * @param array $data
  31. * @return mixed
  32. */
  33. public function storeBy(array $data)
  34. {
  35. $material = WeChat::officialAccount()->material;
  36. $mediaUrl = $this->getLocalPath($data['media_url'] ?? '');
  37. $imageUrl = $this->getLocalPath($data['image_url'] ?? '');
  38. if ($imageUrl) {
  39. // 音乐
  40. if ($data['type'] == WechatReply::MUSIC) {
  41. $data['media_id'] = $material->uploadThumb($imageUrl)['media_id'];
  42. } else {
  43. $data['media_id'] = $material->uploadImage($imageUrl)['media_id'];
  44. }
  45. }
  46. // 语音
  47. if ($data['type'] == WechatReply::VOICE) {
  48. $data['media_id'] = $material->uploadVoice($mediaUrl)['media_id'];
  49. }
  50. // 视频
  51. if ($data['type'] == WechatReply::VIDEO) {
  52. $data['media_id'] = $material->uploadVideo($mediaUrl, $data['title'], $data['content'])['media_id'];
  53. }
  54. return parent::storeBy($data); // TODO: Change the autogenerated stub
  55. }
  56. /**
  57. * 更新
  58. *
  59. * @time 2020年09月13日
  60. * @param int $id
  61. * @param array $data
  62. * @return mixed
  63. */
  64. public function updateBy(int $id, array $data)
  65. {
  66. $material = WeChat::officialAccount()->material;
  67. $mediaUrl = $this->getLocalPath($data['media_url'] ?? '');
  68. $imageUrl = $this->getLocalPath($data['image_url'] ?? '');
  69. if ($imageUrl) {
  70. // 音乐
  71. if ($data['type'] == WechatReply::MUSIC) {
  72. $data['media_id'] = $material->uploadThumb($imageUrl)['media_id'];
  73. } else {
  74. // $data['media_id'] = $material->uploadImage($imageUrl)['media_id'];
  75. }
  76. }
  77. // 语音
  78. if ($data['type'] == WechatReply::VOICE) {
  79. $data['media_id'] = $material->uploadVoice($mediaUrl)['media_id'];
  80. }
  81. // 视频
  82. if ($data['type'] == WechatReply::VIDEO) {
  83. $data['media_id'] = $material->uploadVideo($mediaUrl, $data['title'], $data['content'])['media_id'];
  84. }
  85. return parent::updateBy($id, $data); // TODO: Change the autogenerated stub
  86. }
  87. /**
  88. * 获取本地地址
  89. *
  90. * @time 2020年06月29日
  91. * @param $url
  92. * @return string
  93. */
  94. protected function getLocalPath($url)
  95. {
  96. return $url ? '.' . trim(str_replace(config('filesystem.disks.local.domain'), '', $url)) : '';
  97. }
  98. }