Common.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\Method;
  4. use app\common\controller\Api;
  5. use app\common\exception\UploadException;
  6. use app\common\library\Upload;
  7. use app\common\model\Area;
  8. use app\common\model\Version;
  9. use fast\Random;
  10. use think\Config;
  11. use think\Db;
  12. use think\Hook;
  13. use EasyWeChat\Factory;
  14. /**
  15. * 公共接口
  16. */
  17. class Common extends Api
  18. {
  19. protected $noNeedLogin = ['init','wechat','alipay','method','getAllArea'];
  20. protected $noNeedRight = '*';
  21. /**
  22. * 加载初始化
  23. *
  24. * @param string $version 版本号
  25. * @param string $lng 经度
  26. * @param string $lat 纬度
  27. */
  28. public function init()
  29. {
  30. if ($version = $this->request->request('version')) {
  31. $lng = $this->request->request('lng');
  32. $lat = $this->request->request('lat');
  33. //配置信息
  34. $upload = Config::get('upload');
  35. //如果非服务端中转模式需要修改为中转
  36. if ($upload['storage'] != 'local' && isset($upload['uploadmode']) && $upload['uploadmode'] != 'server') {
  37. //临时修改上传模式为服务端中转
  38. set_addon_config($upload['storage'], ["uploadmode" => "server"], false);
  39. $upload = \app\common\model\Config::upload();
  40. // 上传信息配置后
  41. Hook::listen("upload_config_init", $upload);
  42. $upload = Config::set('upload', array_merge(Config::get('upload'), $upload));
  43. }
  44. $upload['cdnurl'] = $upload['cdnurl'] ? $upload['cdnurl'] : cdnurl('', true);
  45. $upload['uploadurl'] = preg_match("/^((?:[a-z]+:)?\/\/)(.*)/i", $upload['uploadurl']) ? $upload['uploadurl'] : url($upload['storage'] == 'local' ? '/api/common/upload' : $upload['uploadurl'], '', false, true);
  46. $content = [
  47. 'citydata' => Area::getCityFromLngLat($lng, $lat),
  48. 'versiondata' => Version::check($version),
  49. 'uploaddata' => $upload,
  50. 'coverdata' => Config::get("cover"),
  51. ];
  52. $this->success('', $content);
  53. } else {
  54. $this->error(__('Invalid parameters'));
  55. }
  56. }
  57. /**
  58. * 上传文件
  59. * @ApiMethod (POST)
  60. * @param File $file 文件流
  61. */
  62. public function upload()
  63. {
  64. Config::set('default_return_type', 'json');
  65. //必须设定cdnurl为空,否则cdnurl函数计算错误
  66. Config::set('upload.cdnurl', '');
  67. $chunkid = $this->request->post("chunkid");
  68. if ($chunkid) {
  69. if (!Config::get('upload.chunking')) {
  70. $this->error(__('Chunk file disabled'));
  71. }
  72. $action = $this->request->post("action");
  73. $chunkindex = $this->request->post("chunkindex/d");
  74. $chunkcount = $this->request->post("chunkcount/d");
  75. $filename = $this->request->post("filename");
  76. $method = $this->request->method(true);
  77. if ($action == 'merge') {
  78. $attachment = null;
  79. //合并分片文件
  80. try {
  81. $upload = new Upload();
  82. $attachment = $upload->merge($chunkid, $chunkcount, $filename);
  83. } catch (UploadException $e) {
  84. $this->error($e->getMessage());
  85. }
  86. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  87. } elseif ($method == 'clean') {
  88. //删除冗余的分片文件
  89. try {
  90. $upload = new Upload();
  91. $upload->clean($chunkid);
  92. } catch (UploadException $e) {
  93. $this->error($e->getMessage());
  94. }
  95. $this->success();
  96. } else {
  97. //上传分片文件
  98. //默认普通上传文件
  99. $file = $this->request->file('file');
  100. try {
  101. $upload = new Upload($file);
  102. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  103. } catch (UploadException $e) {
  104. $this->error($e->getMessage());
  105. }
  106. $this->success();
  107. }
  108. } else {
  109. $attachment = null;
  110. //默认普通上传文件
  111. $file = $this->request->file('file');
  112. try {
  113. $upload = new Upload($file);
  114. $attachment = $upload->upload();
  115. } catch (UploadException $e) {
  116. $this->error($e->getMessage());
  117. }
  118. $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  119. }
  120. }
  121. public function getAllArea(){
  122. $field=['id','pid','shortname','name','pinyin'];
  123. $list=Db::table('yt_area')->where('pid',0)->field($field)->select();
  124. foreach ($list as $k=>&$v){
  125. $v['children']= Db::table('yt_area')->where('pid',$v['id'])->field($field)->select();
  126. if(!empty($v['children'])){
  127. foreach ($v['children'] as $kk=>&$vv){
  128. $vv['children']=Db::table('yt_area')->where('pid',$vv['id'])->field($field)->select();
  129. }
  130. }
  131. }
  132. $this->success('获取成功',$list);
  133. }
  134. public function wechat(){
  135. $config = [
  136. // 必要配置
  137. 'app_id' => 'xxxx',
  138. 'mch_id' => '1636074236',
  139. 'key' => 'Yubobao0000000000000202301040907', // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
  140. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  141. 'cert_path' => 'path/to/your/cert.pem', // XXX: 绝对路径!!!!
  142. 'key_path' => 'path/to/your/key', // XXX: 绝对路径!!!!
  143. 'notify_url' => '默认的订单回调地址', // 你也可以在下单时单独设置来想覆盖它
  144. ];
  145. return $config;
  146. }
  147. public function alipay(){
  148. }
  149. public function method(){
  150. $method_model = new Method();
  151. $time =time();
  152. $list = $method_model->where('status',1)->select();
  153. foreach ($list as &$v){
  154. if($time>strtotime($v['yingye'])){
  155. $method_model->isUpdate('true',['id'=>$v['id']])->save(['status'=>2]);
  156. }
  157. }
  158. }
  159. }