Epay.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\exception\UploadException;
  5. use app\common\library\Upload;
  6. use think\Config;
  7. class Epay extends Backend
  8. {
  9. protected $noNeedRight = ['upload'];
  10. /**
  11. * 上传本地证书
  12. * @return void
  13. */
  14. public function upload()
  15. {
  16. Config::set('default_return_type', 'json');
  17. //检测是否有addon/config的权限
  18. if (!$this->auth->check('addon/config')) {
  19. $this->error('暂无权限');
  20. }
  21. $certname = $this->request->post('certname', '');
  22. $certPathArr = [
  23. 'cert_client' => '/addons/epay/certs/apiclient_cert.pem', //微信支付api
  24. 'cert_key' => '/addons/epay/certs/apiclient_key.pem', //微信支付api
  25. 'public_key' => '/addons/epay/certs/public_key.pem', //微信公钥证书
  26. 'app_cert_public_key' => '/addons/epay/certs/appCertPublicKey.crt',//应用公钥证书路径
  27. 'alipay_root_cert' => '/addons/epay/certs/alipayRootCert.crt', //支付宝根证书路径
  28. 'ali_public_key' => '/addons/epay/certs/alipayCertPublicKey.crt', //支付宝公钥证书路径
  29. ];
  30. if (!isset($certPathArr[$certname])) {
  31. $this->error("证书错误");
  32. }
  33. $url = $certPathArr[$certname];
  34. $file = $this->request->file('file');
  35. if (!$file) {
  36. $this->error("未上传文件");
  37. }
  38. //验证文件大小限制和后缀限制
  39. if (!$file->check([
  40. 'size' => $this->convertToBytes(config('upload.maxsize')),
  41. 'ext' => 'pem,crt',
  42. ])) {
  43. $this->error($file->getError());
  44. }
  45. //验证上传的文件内容是否符合pem,crt格式内容
  46. $fileContent = file_get_contents($file->getInfo('tmp_name'));
  47. if (!preg_match('/-----BEGIN(.*)-----[\s\S]+-----END(.*)-----/', $fileContent)) {
  48. $this->error("文件内容错误");
  49. }
  50. $file->move(dirname(ROOT_PATH . $url), basename(ROOT_PATH . $url), true);
  51. $this->success(__('上传成功'), '', ['url' => $url]);
  52. }
  53. protected function convertToBytes($size)
  54. {
  55. $units = ['b' => 1, 'k' => 1024, 'kb' => 1024, 'm' => 1048576, 'mb' => 1048576, 'g' => 1073741824, 'gb' => 1073741824];
  56. preg_match('/^(\d+(?:\.\d+)?)\s*([a-z]+)$/i', strtolower(trim($size)), $matches);
  57. return intval($matches[1] * $units[$matches[2]]);
  58. }
  59. }