RageFrameService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace services\common;
  3. use Yii;
  4. use yii\helpers\Json;
  5. use linslin\yii2\curl\Curl;
  6. use common\helpers\ResultHelper;
  7. use common\helpers\FileHelper;
  8. use common\helpers\ZipHelper;
  9. use yii\web\NotFoundHttpException;
  10. use yii\web\UnprocessableEntityHttpException;
  11. use Exception;
  12. /**
  13. * Class RageFrameService
  14. * @package services\common
  15. * @author jianyan74 <751393839@qq.com>
  16. */
  17. class RageFrameService
  18. {
  19. /**
  20. * 更新地址
  21. */
  22. const URL = 'http://authority.rageframe.com/api';
  23. /**
  24. * @param $addonName
  25. * @return array|mixed
  26. * @throws Exception
  27. */
  28. public function update($addonName = 'Authority')
  29. {
  30. $data = $this->getVersion($addonName);
  31. if (empty($data)) {
  32. throw new UnprocessableEntityHttpException('当前已是最新版本');
  33. }
  34. // 更新包信息
  35. $fileUrl = $data['download_url']; //更新包的下载地址
  36. $fileName = basename($fileUrl); //更新包文件名称
  37. // 检查和创建文件夹
  38. $dir = Yii::getAlias('@runtime').'/version-update/'.$addonName.'/';
  39. FileHelper::mkdirs($dir);
  40. // 下载更新包到本地并赋值文件路径变量
  41. $path = file_exists($dir.$fileName) ? $dir.$fileName : $this->downloadFile($fileUrl, $dir, $fileName);
  42. // 如果下载没成功就返回报错
  43. if (!file_exists($dir.$fileName)) {
  44. throw new UnprocessableEntityHttpException('文件下载失败!');
  45. }
  46. // 解压文件夹
  47. $toPath = Yii::getAlias('@root');
  48. // 插件
  49. $addonName != 'Authority' && $toPath .= '/addons/'.$addonName;
  50. try {
  51. ZipHelper::unZip($path, $toPath, true);
  52. } catch (Exception $e) {
  53. throw new NotFoundHttpException($e->getMessage());
  54. }
  55. // 更新数据库
  56. Yii::$app->services->addons->upgradeSql($addonName);
  57. return $data;
  58. }
  59. /**
  60. * 批量查询插件最新版本
  61. *
  62. * @param $addons
  63. * @return mixed|array
  64. * @throws UnprocessableEntityHttpException
  65. */
  66. public function queryNewestByNames($addons)
  67. {
  68. return $this->httpPost('/authority/v1/accredit/batch-inspect', [
  69. 'addons' => $addons,
  70. ]);
  71. }
  72. /**
  73. * 查询单个最新版本
  74. *
  75. * @param $addonName
  76. * @return array|mixed|void
  77. * @throws Exception
  78. */
  79. public function queryNewest($addonName = 'Authority')
  80. {
  81. return $this->getVersion($addonName);
  82. }
  83. /**
  84. * 获取版本
  85. *
  86. * @param $addonName
  87. * @return mixed
  88. * @throws UnprocessableEntityHttpException
  89. */
  90. protected function getVersion($addonName)
  91. {
  92. $addon = Yii::$app->services->addons->findByName($addonName);
  93. if (empty($addon)) {
  94. throw new UnprocessableEntityHttpException('找不到插件['.$addonName.']更新失败');
  95. }
  96. return $this->httpPost('/authority/v1/accredit/inspect', [
  97. 'version' => $addon->version,
  98. 'name' => $addon->name,
  99. ]);
  100. }
  101. /**
  102. * @param $uri
  103. * @param array $params
  104. * @return mixed
  105. * @throws UnprocessableEntityHttpException
  106. */
  107. protected function httpPost($uri, array $params)
  108. {
  109. $curl = new Curl();
  110. $result = $curl->setHeaders([
  111. 'secret-key' => Yii::$app->params['secret_key'],
  112. 'domain-name' => Yii::$app->request->hostInfo,
  113. 'ip' => Yii::$app->request->userIP,
  114. 'timestamp' => time(),
  115. ])->setPostParams($params)->post(self::URL.$uri);
  116. try {
  117. // 回调解析失败
  118. $result = Json::decode($result);
  119. } catch (\Exception $e) {
  120. throw new UnprocessableEntityHttpException('已经是最新版本');
  121. }
  122. if ($result['code'] != 200) {
  123. throw new UnprocessableEntityHttpException($result['message']);
  124. }
  125. return $result['data'];
  126. }
  127. /**
  128. * 文件下载方法
  129. *
  130. * @param string $url 文件下载地址
  131. * @param string $dir 存储的文件夹
  132. * @param string $fileName 文件名字
  133. * @return array|false|mixed|string
  134. */
  135. protected function downloadFile($url, $dir, $fileName = '')
  136. {
  137. if (empty($url)) {
  138. return false;
  139. }
  140. $ext = strrchr($url, '.');
  141. $dir = realpath($dir);
  142. //目录+文件
  143. $fileName = (empty($fileName) ? '/'.time().''.$ext : '/'.$fileName);
  144. $fileName = $dir.$fileName;
  145. //开始捕捉
  146. ob_start();
  147. try {
  148. readfile($url);
  149. } catch (Exception $e) {
  150. throw new NotFoundHttpException('文件下载失败, ' . $e->getMessage());
  151. }
  152. $file = ob_get_contents();
  153. ob_end_clean();
  154. $size = strlen($file);
  155. $fp2 = fopen($fileName, "a");
  156. fwrite($fp2, $file);
  157. fclose($fp2);
  158. return $fileName;
  159. }
  160. }
粤ICP备19079148号