Просмотр исходного кода

修复:保存小程序设置未写入证书

Yahoo 4 лет назад
Родитель
Сommit
08fa8d2fdd
2 измененных файлов с 36 добавлено и 6 удалено
  1. 5 5
      app/store/controller/Setting.php
  2. 31 1
      app/store/model/wxapp/Setting.php

+ 5 - 5
app/store/controller/Setting.php

@@ -14,6 +14,7 @@ namespace app\store\controller;
 
 
 use app\store\model\Setting as SettingModel;
 use app\store\model\Setting as SettingModel;
 use app\common\library\sms\Driver as SmsDriver;
 use app\common\library\sms\Driver as SmsDriver;
+use think\response\Json;
 
 
 /**
 /**
  * 系统设置
  * 系统设置
@@ -25,12 +26,12 @@ class Setting extends Controller
     /**
     /**
      * 获取设置项
      * 获取设置项
      * @param string $key
      * @param string $key
-     * @return array
+     * @return Json
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      * @throws \think\db\exception\ModelNotFoundException
      */
      */
-    public function detail(string $key)
+    public function detail(string $key): Json
     {
     {
         $values = SettingModel::getItem($key);
         $values = SettingModel::getItem($key);
         return $this->renderSuccess(compact('values'));
         return $this->renderSuccess(compact('values'));
@@ -39,12 +40,12 @@ class Setting extends Controller
     /**
     /**
      * 更新系统设置
      * 更新系统设置
      * @param string $key
      * @param string $key
-     * @return array
+     * @return Json
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      * @throws \think\db\exception\ModelNotFoundException
      */
      */
-    public function update(string $key)
+    public function update(string $key): Json
     {
     {
         // 保存商城设置
         // 保存商城设置
         $model = new SettingModel;
         $model = new SettingModel;
@@ -53,5 +54,4 @@ class Setting extends Controller
         }
         }
         return $this->renderError($model->getError() ?: '操作失败');
         return $this->renderError($model->getError() ?: '操作失败');
     }
     }
-
 }
 }

+ 31 - 1
app/store/model/wxapp/Setting.php

@@ -39,8 +39,11 @@ class Setting extends SettingModel
     public function edit(string $key, array $values): bool
     public function edit(string $key, array $values): bool
     {
     {
         $model = self::detail($key) ?: $this;
         $model = self::detail($key) ?: $this;
-        // 删除系统设置缓存
+        // 删除小程序设置缓存
         Cache::delete('wxapp_setting_' . self::$storeId);
         Cache::delete('wxapp_setting_' . self::$storeId);
+        // 写入cert证书文件
+        $this->writeCertPemFiles($values['cert_pem'], $values['key_pem']);
+        // 保存设置
         return $model->save([
         return $model->save([
                 'key' => $key,
                 'key' => $key,
                 'describe' => $this->describe[$key],
                 'describe' => $this->describe[$key],
@@ -49,4 +52,31 @@ class Setting extends SettingModel
                 'store_id' => self::$storeId,
                 'store_id' => self::$storeId,
             ]) !== false;
             ]) !== false;
     }
     }
+
+    /**
+     * 写入cert证书文件
+     * @param string $certPem
+     * @param string $keyPem
+     * @return void
+     */
+    private function writeCertPemFiles(string $certPem, string $keyPem): void
+    {
+        if (empty($certPem) && empty($keyPem)) {
+            return;
+        }
+        // 证书目录
+        $filePath = base_path() . 'common/library/wechat/cert/' . self::$storeId . '/';
+        // 目录不存在则自动创建
+        if (!is_dir($filePath)) {
+            mkdir($filePath, 0755, true);
+        }
+        // 写入cert.pem文件
+        if (!empty($certPem)) {
+            file_put_contents($filePath . 'cert.pem', $certPem);
+        }
+        // 写入key.pem文件
+        if (!empty($keyPem)) {
+            file_put_contents($filePath . 'key.pem', $keyPem);
+        }
+    }
 }
 }