Config.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\wechat\controller;
  3. use app\wechat\service\WechatService;
  4. use think\admin\Controller;
  5. use think\admin\storage\LocalStorage;
  6. /**
  7. * 微信授权绑定
  8. * Class Config
  9. * @package app\wechat\controller
  10. */
  11. class Config extends Controller
  12. {
  13. /**
  14. * 微信授权配置
  15. * @auth true
  16. * @menu true
  17. * @throws \think\db\exception\DataNotFoundException
  18. * @throws \think\db\exception\DbException
  19. * @throws \think\db\exception\ModelNotFoundException
  20. */
  21. public function options()
  22. {
  23. $this->_applyFormToken();
  24. $this->thrNotify = sysuri('wechat/api.push/index', [], false, true);
  25. if ($this->request->isGet()) {
  26. try {
  27. $source = enbase64url(sysuri('admin/index/index', [], false, true) . '#' . $this->request->url());
  28. $this->authurl = "https://open.cuci.cc/service/api.push/auth?source={$source}";
  29. if (input('?appid') && input('?appkey')) {
  30. sysconf('wechat.type', 'thr');
  31. sysconf('wechat.thr_appid', input('appid'));
  32. sysconf('wechat.thr_appkey', input('appkey'));
  33. WechatService::ThinkServiceConfig()->setApiNotifyUri($this->thrNotify);
  34. }
  35. $this->wechat = WechatService::ThinkServiceConfig()->getConfig();
  36. } catch (\Exception $exception) {
  37. $this->wechat = [];
  38. $this->message = $exception->getMessage();
  39. }
  40. $this->geoip = $this->app->cache->get('mygeoip', '');
  41. if (empty($this->geoip)) {
  42. $this->geoip = gethostbyname($this->request->host());
  43. $this->app->cache->set('mygeoip', $this->geoip, 360);
  44. }
  45. $this->title = '微信授权配置';
  46. $this->fetch();
  47. } else {
  48. foreach ($this->request->post() as $k => $v) sysconf($k, $v);
  49. if ($this->request->post('wechat.type') === 'thr') {
  50. try {
  51. WechatService::ThinkServiceConfig()->setApiNotifyUri($this->thrNotify);
  52. } catch (\Exception $exception) {
  53. $this->error($exception->getMessage());
  54. }
  55. }
  56. sysoplog('微信授权配置', '修改微信授权配置成功');
  57. $location = url('wechat/config/options')->build() . '?uniqid=' . uniqid();
  58. $this->success('微信授权修改成功!', sysuri('admin/index/index') . "#{$location}");
  59. }
  60. }
  61. /**
  62. * 微信授权测试
  63. * @auth true
  64. */
  65. public function options_test()
  66. {
  67. $this->fetch();
  68. }
  69. /**
  70. * 微信支付配置
  71. * @auth true
  72. * @menu true
  73. * @throws \think\admin\Exception
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. */
  78. public function payment()
  79. {
  80. if ($this->request->isGet()) {
  81. $this->title = '微信支付配置';
  82. $local = LocalStorage::instance();
  83. $this->mch_ssl_cer = sysconf('wechat.mch_ssl_cer');
  84. $this->mch_ssl_key = sysconf('wechat.mch_ssl_key');
  85. $this->mch_ssl_p12 = sysconf('wechat.mch_ssl_p12');
  86. if (!$local->has($this->mch_ssl_cer, true)) $this->mch_ssl_cer = '';
  87. if (!$local->has($this->mch_ssl_key, true)) $this->mch_ssl_key = '';
  88. if (!$local->has($this->mch_ssl_p12, true)) $this->mch_ssl_p12 = '';
  89. $this->fetch();
  90. } else {
  91. $this->error('抱歉,数据提交地址错误!');
  92. }
  93. }
  94. /**
  95. * 微信支付测试
  96. * @auth true
  97. */
  98. public function payment_test()
  99. {
  100. $this->fetch();
  101. }
  102. /**
  103. * 微信支付修改
  104. * @auth true
  105. * @throws \think\admin\Exception
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. */
  110. public function payment_save()
  111. {
  112. if ($this->request->isPost()) {
  113. if ($this->request->post('wechat.mch_ssl_type') === 'p12') {
  114. if (!LocalStorage::instance()->has(input('wechat.mch_ssl_p12', '-'), true)) {
  115. $this->error('商户证书 P12 证书不能为空!');
  116. }
  117. $content = LocalStorage::instance()->get(input('wechat.mch_ssl_p12', '-'), true);
  118. if (!openssl_pkcs12_read($content, $certs, input('wechat.mch_id'))) {
  119. $this->error('商户账号与 P12 证书不匹配!');
  120. }
  121. } elseif ($this->request->post('wechat.mch_ssl_type') === 'pem') {
  122. if (!LocalStorage::instance()->has(input('wechat.mch_ssl_key', '-'), true)) {
  123. $this->error('商户证书 KEY 不能为空!');
  124. }
  125. if (!LocalStorage::instance()->has(input('wechat.mch_ssl_cer', '-'), true)) {
  126. $this->error('商户证书 CERT 不能为空!');
  127. }
  128. }
  129. foreach ($this->request->post() as $k => $v) sysconf($k, $v);
  130. sysoplog('微信授权配置', '修改微信支付配置成功');
  131. $this->success('微信支付配置成功!');
  132. } else {
  133. $this->error('抱歉,访问方式错误!');
  134. }
  135. }
  136. }