Common.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\Text;
  4. use app\common\controller\Api;
  5. use app\common\exception\UploadException;
  6. use app\common\library\Upload;
  7. use fuyelk\yapi\Yapi;
  8. use think\Config;
  9. /**
  10. * 公共接口
  11. */
  12. class Common extends Api
  13. {
  14. protected $noNeedLogin = ['agreement', 'updateApiDoc', 'gitpull'];
  15. protected $noNeedRight = '*';
  16. /**
  17. * 上传文件
  18. * @ApiMethod (POST)
  19. * @param \think\File $file 文件流
  20. */
  21. public function upload()
  22. {
  23. Config::set('default_return_type', 'json');
  24. //必须设定cdnurl为空,否则cdnurl函数计算错误
  25. Config::set('upload.cdnurl', '');
  26. // 切片
  27. $chunkid = $this->request->post("chunkid");
  28. if ($chunkid) {
  29. if (!Config::get('upload.chunking')) {
  30. $this->error(__('Chunk file disabled'));
  31. }
  32. $action = $this->request->post("action");
  33. $chunkindex = $this->request->post("chunkindex/d");
  34. $chunkcount = $this->request->post("chunkcount/d");
  35. $filename = $this->request->post("filename");
  36. $method = $this->request->method(true);
  37. if ($action == 'merge') {
  38. $attachment = null;
  39. //合并分片文件
  40. try {
  41. $upload = new Upload();
  42. $attachment = $upload->merge($chunkid, $chunkcount, $filename);
  43. } catch (UploadException $e) {
  44. $this->error($e->getMessage());
  45. }
  46. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  47. }
  48. if ($method == 'clean') {
  49. //删除冗余的分片文件
  50. try {
  51. $upload = new Upload();
  52. $upload->clean($chunkid);
  53. } catch (UploadException $e) {
  54. $this->error($e->getMessage());
  55. }
  56. $this->success();
  57. }
  58. //上传分片文件
  59. //默认普通上传文件
  60. $file = $this->request->file('file');
  61. try {
  62. $upload = new Upload($file);
  63. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  64. } catch (UploadException $e) {
  65. $this->error($e->getMessage());
  66. }
  67. $this->success();
  68. }
  69. $attachment = null;
  70. //默认普通上传文件
  71. $file = $this->request->file('file');
  72. try {
  73. $upload = new Upload($file);
  74. $attachment = $upload->upload();
  75. } catch (UploadException $e) {
  76. $this->error($e->getMessage());
  77. }
  78. $this->success('上传成功', ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  79. }
  80. /**
  81. * 协议、规则
  82. * @param string $name 协议名称
  83. * @author fuyelk <fuyelk@fuyelk.com>
  84. * @date 2021/07/05 20:48
  85. */
  86. public function agreement()
  87. {
  88. $name = input('name');
  89. if (empty($name)) {
  90. $this->error('协议名称不能为空');
  91. }
  92. $content = Text::getText($name);
  93. $this->success('查询成功', ['content' => $content]);
  94. }
  95. /**
  96. * 更新接口文档
  97. */
  98. public function updateApiDoc()
  99. {
  100. $fileName = '/doc/rVs4ULys17.html';
  101. $yapi = new Yapi('h5box@h5box.com', 'eeAW3ECjRB551vwO', 'http://api.fuyelk.com:3000');
  102. $res = $yapi->export(227, 'html', ROOT_PATH . 'public' . $fileName);
  103. if (!$res) {
  104. $this->error($yapi->getError());
  105. }
  106. $this->success('文档更新成功', ['url' => $this->request->domain() . $fileName]);
  107. }
  108. /**
  109. * 码云git钩子
  110. * @author fuyelk <fuyelk@fuyelk.com>
  111. * @date 2021/07/08 11:15
  112. */
  113. public function gitpull()
  114. {
  115. $token = $this->request->server('HTTP_X_GITEE_TOKEN');
  116. if ('LUY})!1@#M,StanM\DR\}AKz*@dMZ[5*' != $token) {
  117. $this->error('验权失败', [], '401');
  118. }
  119. try {
  120. $path = ROOT_PATH;
  121. exec("cd {$path} && git pull 2>&1");
  122. // 注意:windows服务器可能需要将.ssh下的文件拷贝到C:\Windows\System32\config\systemprofile\.ssh\下
  123. } catch (\Exception $e) {
  124. dta('码云代码拉取失败');
  125. $this->error('代码拉取失败', ['error' => $e->getMessage()]);
  126. }
  127. $this->success('成功');
  128. }
  129. }