Setting.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\store\model\wxapp;
  13. use think\facade\Cache;
  14. use app\common\model\wxapp\Setting as SettingModel;
  15. /**
  16. * 微信小程序设置模型
  17. * Class Setting
  18. * @package app\store\model\wxapp
  19. */
  20. class Setting extends SettingModel
  21. {
  22. /**
  23. * 设置项描述
  24. * @var array
  25. */
  26. private $describe = [
  27. 'basic' => '基础设置',
  28. ];
  29. /**
  30. * 更新系统设置
  31. * @param string $key
  32. * @param array $values
  33. * @return bool
  34. */
  35. public function edit(string $key, array $values): bool
  36. {
  37. $model = self::detail($key) ?: $this;
  38. // 删除小程序设置缓存
  39. Cache::delete('wxapp_setting_' . self::$storeId);
  40. // 写入cert证书文件
  41. $this->writeCertPemFiles($values['cert_pem'], $values['key_pem']);
  42. // 保存设置
  43. return $model->save([
  44. 'key' => $key,
  45. 'describe' => $this->describe[$key],
  46. 'values' => $values,
  47. 'update_time' => time(),
  48. 'store_id' => self::$storeId,
  49. ]) !== false;
  50. }
  51. /**
  52. * 写入cert证书文件
  53. * @param string $certPem
  54. * @param string $keyPem
  55. * @return void
  56. */
  57. private function writeCertPemFiles(string $certPem, string $keyPem): void
  58. {
  59. if (empty($certPem) && empty($keyPem)) {
  60. return;
  61. }
  62. // 证书目录
  63. $filePath = base_path() . 'common/library/wechat/cert/' . self::$storeId . '/';
  64. // 目录不存在则自动创建
  65. if (!is_dir($filePath)) {
  66. mkdir($filePath, 0755, true);
  67. }
  68. // 写入cert.pem文件
  69. if (!empty($certPem)) {
  70. file_put_contents($filePath . 'cert.pem', $certPem);
  71. }
  72. // 写入key.pem文件
  73. if (!empty($keyPem)) {
  74. file_put_contents($filePath . 'key.pem', $keyPem);
  75. }
  76. }
  77. }