Config.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace app\admin\controller\xmwechat\offiaccount;
  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. $offiAccount = $this->xmwechat['offiAccount'];
  20. $this->success('', [
  21. 'offiaccount' => $offiAccount,
  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['offiAccount']) || !is_array($xmwechat['offiAccount'])) {
  39. $this->error(__('Parameter error'));
  40. }
  41. $offiAccount = $xmwechat['offiAccount'];
  42. if ($type == 'offiaccount') {
  43. $offi_qr_code = str_replace('/', '\/', $offiAccount['offi_qr_code']);
  44. $configContent = preg_replace("/'offi_name'(\s+)=>(\s+)'{$offiAccount['offi_name']}'/", "'offi_name'\$1=>\$2'{$data['offi_name']}'", $configContent);
  45. $configContent = preg_replace("/'original_id'(\s+)=>(\s+)'{$offiAccount['original_id']}'/", "'original_id'\$1=>\$2'{$data['original_id']}'", $configContent);
  46. $configContent = preg_replace("/'offi_qr_code'(\s+)=>(\s+)'{$offi_qr_code}'/", "'offi_qr_code'\$1=>\$2'{$data['offi_qr_code']}'", $configContent);
  47. } elseif ($type == 'developerInfo') {
  48. $redirect_url = str_replace('/', '\/', $offiAccount['redirect_url']);
  49. $configContent = preg_replace("/'app_id'(\s+)=>(\s+)'{$offiAccount['app_id']}'/", "'app_id'\$1=>\$2'{$data['app_id']}'", $configContent);
  50. $configContent = preg_replace("/'secret'(\s+)=>(\s+)'{$offiAccount['secret']}'/", "'secret'\$1=>\$2'{$data['secret']}'", $configContent);
  51. $configContent = preg_replace("/'redirect_url'(\s+)=>(\s+)'{$redirect_url}'/", "'redirect_url'\$1=>\$2'{$data['redirect_url']}'", $configContent);
  52. } elseif ($type == 'server') {
  53. $server_url = str_replace('/', '\/', $offiAccount['server_url']);
  54. $configContent = preg_replace("/'server_url'(\s+)=>(\s+)'{$server_url}'/", "'server_url'\$1=>\$2'{$data['server_url']}'", $configContent);
  55. $configContent = preg_replace("/'server_token'(\s+)=>(\s+)'{$offiAccount['server_token']}'/", "'server_token'\$1=>\$2'{$data['server_token']}'", $configContent);
  56. $configContent = preg_replace("/'encoding_aes_key'(\s+)=>(\s+)'{$offiAccount['encoding_aes_key']}'/", "'encoding_aes_key'\$1=>\$2'{$data['encoding_aes_key']}'", $configContent);
  57. $configContent = preg_replace("/'message_encryption_method'(\s+)=>(\s+)'{$offiAccount['message_encryption_method']}'/", "'message_encryption_method'\$1=>\$2'{$data['message_encryption_method']}'", $configContent);
  58. } else {
  59. $this->error(__('Parameter error'));
  60. }
  61. $result = @file_put_contents($configPath, $configContent);
  62. if (!$result) {
  63. $this->error(__('Configuration write failed: %s', ['config/' . self::$configFile]));
  64. }
  65. $this->success();
  66. }
  67. }