Config.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace app\admin\controller\xmwechat\miniprogram;
  3. use app\common\controller\Backend;
  4. class Config extends Backend
  5. {
  6. static string $configFile = 'xmwechat.php';
  7. protected array $xmwechat;
  8. public function initialize(): void
  9. {
  10. parent::initialize();
  11. $this->xmwechat = config('xmwechat');
  12. }
  13. /**
  14. * @return void
  15. * @author jsjxsz
  16. */
  17. public function getConfig(): void
  18. {
  19. $miniProgram = $this->xmwechat['miniProgram'];
  20. $this->success('', [
  21. 'miniprogram' => $miniProgram,
  22. ]);
  23. }
  24. /**
  25. * @return void
  26. * @author jsjxsz
  27. */
  28. public function saveConfig(): void
  29. {
  30. $type = $this->request->get('type', '');
  31. $data = $this->request->post();
  32. if (!$type) {
  33. $this->error(__('Parameter error'));
  34. }
  35. $xmwechat = $this->xmwechat;
  36. $configPath = config_path() . self::$configFile;
  37. $configContent = @file_get_contents($configPath);
  38. if (!isset($xmwechat['miniProgram']) || !is_array($xmwechat['miniProgram'])) {
  39. $this->error(__('Parameter error'));
  40. }
  41. $miniProgram = $xmwechat['miniProgram'];
  42. if ($type == 'miniprogram') {
  43. $qr_code = str_replace('/', '\/', $miniProgram['qr_code']);
  44. $configContent = preg_replace("/'name'(\s+)=>(\s+)'{$miniProgram['name']}'/", "'name'\$1=>\$2'{$data['name']}'", $configContent);
  45. $configContent = preg_replace("/'original_id'(\s+)=>(\s+)'{$miniProgram['original_id']}'/", "'original_id'\$1=>\$2'{$data['original_id']}'", $configContent);
  46. $configContent = preg_replace("/'qr_code'(\s+)=>(\s+)'{$qr_code}'/", "'qr_code'\$1=>\$2'{$data['qr_code']}'", $configContent);
  47. } elseif ($type == 'developerInfo') {
  48. $configContent = preg_replace("/'app_id'(\s+)=>(\s+)'{$miniProgram['app_id']}'/", "'app_id'\$1=>\$2'{$data['app_id']}'", $configContent);
  49. $configContent = preg_replace("/'secret'(\s+)=>(\s+)'{$miniProgram['secret']}'/", "'secret'\$1=>\$2'{$data['secret']}'", $configContent);
  50. } elseif ($type == 'messagePush') {
  51. $server_url = str_replace('/', '\/', $miniProgram['server_url']);
  52. $configContent = preg_replace("/'server_url'(\s+)=>(\s+)'{$server_url}'/", "'server_url'\$1=>\$2'{$data['server_url']}'", $configContent);
  53. $configContent = preg_replace("/'server_token'(\s+)=>(\s+)'{$miniProgram['server_token']}'/", "'server_token'\$1=>\$2'{$data['server_token']}'", $configContent);
  54. $configContent = preg_replace("/'encoding_aes_key'(\s+)=>(\s+)'{$miniProgram['encoding_aes_key']}'/", "'encoding_aes_key'\$1=>\$2'{$data['encoding_aes_key']}'", $configContent);
  55. $configContent = preg_replace("/'message_encryption_method'(\s+)=>(\s+)'{$miniProgram['message_encryption_method']}'/", "'message_encryption_method'\$1=>\$2'{$data['message_encryption_method']}'", $configContent);
  56. } else {
  57. $this->error(__('Parameter error'));
  58. }
  59. $result = @file_put_contents($configPath, $configContent);
  60. if (!$result) {
  61. $this->error(__('Configuration write failed: %s', ['config/' . self::$configFile]));
  62. }
  63. $this->success();
  64. }
  65. }