MediaService.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2022/1/7
  8. * Time: 11:55
  9. */
  10. namespace app\frontend\modules\wechat\services;
  11. use app\common\models\AccountWechats;
  12. use app\platform\modules\system\models\SystemSetting;
  13. class MediaService
  14. {
  15. private $mediaId;
  16. private $plugin;
  17. private $uploadPath;
  18. private $remote;
  19. public function __set($name,$value)
  20. {
  21. $this->$name = $value;
  22. }
  23. private function easyWechat()
  24. {
  25. return \app\common\facades\EasyWeChat::officialAccount($this->wechatConfig());
  26. }
  27. /**
  28. * 获取公众号的 appID 和 appsecret
  29. * @return array
  30. */
  31. private function wechatConfig()
  32. {
  33. $account = AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid);
  34. $options = [
  35. 'app_id' => $account->key,
  36. 'secret' => $account->secret,
  37. ];
  38. return $options;
  39. }
  40. /**
  41. * 获取临时素材并上传本地
  42. * @return array
  43. */
  44. public function getTemporaryFile()
  45. {
  46. try {
  47. if (!$this->mediaId) {
  48. throw new \Exception('媒体ID参数错误');
  49. }
  50. $filename = md5($this->mediaId) . '_' . \YunShop::app()->uniacid;
  51. $stream = $this->easyWechat()->media->get($this->mediaId);
  52. if (!$stream instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  53. throw new \Exception('资源错误');
  54. }
  55. $filename = $stream->saveAs($this->uploadPath(), $filename);
  56. $relative_path = $this->plugin.'/'.$filename;
  57. $this->remoteUpload($relative_path);
  58. return ['code' => 0 ,'file' => $this->filePath($relative_path)];
  59. } catch (\GuzzleHttp\Exception\GuzzleException $e) {
  60. return ['code' => 1 ,'message' => $e->getMessage()];
  61. } catch (\Exception $e) {
  62. return ['code' => 1 ,'message' => $e->getMessage()];
  63. }
  64. }
  65. /**
  66. * 获取 JSSDK 上传的高清语音
  67. * @return array
  68. */
  69. public function getJsSdkMedia()
  70. {
  71. try {
  72. if (!$this->mediaId) {
  73. throw new \Exception('媒体ID参数错误');
  74. }
  75. $filename = md5($this->mediaId) . '_' . \YunShop::app()->uniacid.'.speex';
  76. $stream = $this->easyWechat()->media->getJssdkMedia($this->mediaId);
  77. if (!$stream instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  78. throw new \Exception('资源错误');
  79. }
  80. $filename = $stream->saveAs($this->uploadPath(), $filename);
  81. $relative_path = $this->plugin.'/'.$filename;
  82. $this->remoteUpload($relative_path);
  83. return ['code' => 0 ,'file' => $this->filePath($relative_path)];
  84. } catch (\GuzzleHttp\Exception\GuzzleException $e) {
  85. return ['code' => 1 ,'message' => $e->getMessage()];
  86. } catch (\Exception $e) {
  87. return ['code' => 1 ,'message' => $e->getMessage()];
  88. }
  89. }
  90. private function uploadPath()
  91. {
  92. $this->plugin = $this->plugin?str_replace('-','_',$this->plugin):'shop';
  93. if (!isset($this->uploadPath)) {
  94. if (config('app.framework') == 'platform') {
  95. $this->uploadPath = base_path('static/upload/'.$this->plugin);
  96. } else {
  97. $this->uploadPath = dirname(dirname(base_path())).'/attachment/'.$this->plugin;
  98. }
  99. }
  100. return $this->uploadPath;
  101. }
  102. private function filePath($filename)
  103. {
  104. if ($this->remote['type']) {
  105. switch ($this->remote['type']) {
  106. case 1 :
  107. $attach_url_remote = $this->remote['ftp']['url'];
  108. break;
  109. case 2 :
  110. $attach_url_remote = $this->remote['alioss']['url'];
  111. break;
  112. case 3 :
  113. $attach_url_remote = $this->remote['qiniu']['url'];
  114. break;
  115. case 4 :
  116. $attach_url_remote = $this->remote['cos']['url'];
  117. break;
  118. }
  119. $file_url = $attach_url_remote . DIRECTORY_SEPARATOR . $filename;
  120. } else {
  121. if (config('app.framework') == 'platform') {
  122. $file_url = request()->getSchemeAndHttpHost() . DIRECTORY_SEPARATOR . 'static/upload/' . $filename;
  123. } else {
  124. $file_url = request()->getSchemeAndHttpHost() . DIRECTORY_SEPARATOR . 'attachment/' . $filename;
  125. }
  126. }
  127. return [
  128. 'file' => $filename,
  129. 'file_url' => $file_url,
  130. ];
  131. }
  132. private function remoteUpload($filename)
  133. {
  134. if ($this->getRemote()['type']) {
  135. if (config('app.framework') == 'platform') {
  136. file_remote_upload($filename, true, $this->getRemote());
  137. } else {
  138. file_remote_upload_wq($filename, true, $this->getRemote());
  139. }
  140. }
  141. }
  142. private function getRemote()
  143. {
  144. if (!isset($this->remote)) {
  145. if (config('app.framework') == 'platform') {
  146. $this->remote = SystemSetting::settingLoad('remote', 'system_remote');
  147. } else {
  148. //全局配置
  149. global $_W;
  150. //公众号独立配置信息 优先使用公众号独立配置
  151. $uni_setting = app('WqUniSetting')->get()->toArray();
  152. if (!empty($uni_setting['remote']) && iunserializer($uni_setting['remote'])['type'] != 0) {
  153. $setting['remote'] = iunserializer($uni_setting['remote']);
  154. $this->remote = $setting['remote'];
  155. } else {
  156. $this->remote = $_W['setting']['remote'];
  157. }
  158. }
  159. }
  160. return $this->remote;
  161. }
  162. }