Yahoo 4 лет назад
Родитель
Сommit
ade736d221

+ 25 - 0
app/api/model/h5/Setting.php

@@ -0,0 +1,25 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace app\api\model\h5;
+
+use app\common\model\h5\Setting as SettingModel;
+
+/**
+ * H5设置模型
+ * Class Setting
+ * @package app\api\model\h5
+ */
+class Setting extends SettingModel
+{
+
+}

+ 131 - 0
app/common/model/h5/Setting.php

@@ -0,0 +1,131 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace app\common\model\h5;
+
+use cores\BaseModel;
+use think\facade\Cache;
+use app\common\library\helper;
+
+/**
+ * H5设置模型
+ * Class Setting
+ * @package app\common\model\h5
+ */
+class Setting extends BaseModel
+{
+    // 定义表名
+    protected $name = 'h5_setting';
+
+    protected $createTime = false;
+
+    /**
+     * 获取器: 转义数组格式
+     * @param $value
+     * @return array
+     */
+    public function getValuesAttr($value): array
+    {
+        return helper::jsonDecode($value);
+    }
+
+    /**
+     * 修改器: 转义成json格式
+     * @param $value
+     * @return string
+     */
+    public function setValuesAttr($value): string
+    {
+        return helper::jsonEncode($value);
+    }
+
+    /**
+     * 获取指定项设置
+     * @param string $key
+     * @param int|null $storeId
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function getItem(string $key, int $storeId = null): array
+    {
+        $data = static::getAll($storeId);
+        return isset($data[$key]) ? $data[$key]['values'] : [];
+    }
+
+    /**
+     * 获取全部设置
+     * @param int|null $storeId
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function getAll(int $storeId = null): array
+    {
+        $model = new static;
+        is_null($storeId) && $storeId = $model::$storeId;
+        if (!$data = Cache::get("wxapp_setting_{$storeId}")) {
+            // 获取全部设置
+            $setting = $model->getList($storeId);
+            $data = $setting->isEmpty() ? [] : helper::arrayColumn2Key($setting->toArray(), 'key');
+            // 写入缓存中
+            Cache::tag('cache')->set("wxapp_setting_{$storeId}", $data);
+        }
+        // 重组setting缓存数据 (多维)
+        return static::reorganize($model->defaultData(), $data, $type = 'cache');
+    }
+
+    /**
+     * 获取商城设置列表
+     * @param int $storeId
+     * @return \think\Collection
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    private function getList(int $storeId): \think\Collection
+    {
+        return $this->where('store_id', '=', $storeId)->select();
+    }
+
+    /**
+     * 获取设置项信息
+     * @param string $key
+     * @return array|\think\Model|null
+     */
+    public static function detail(string $key)
+    {
+        return static::get(compact('key'));
+    }
+
+    /**
+     * 默认配置
+     * @return array
+     */
+    public function defaultData(): array
+    {
+        return [
+            'basic' => [
+                'key' => 'basic',
+                'describe' => '基础设置',
+                'values' => [
+                    // 是否启用h5端访问  0=>关闭  1=>开启
+                    'enabled' => 1,
+                    // h5站点url [默认是当前访问的域名]
+                    'baseUrl' => base_url(),
+                ]
+            ]
+        ];
+    }
+}

+ 1 - 1
app/common/model/wxapp/Setting.php

@@ -12,8 +12,8 @@ declare (strict_types=1);
 
 namespace app\common\model\wxapp;
 
-use think\facade\Cache;
 use cores\BaseModel;
+use think\facade\Cache;
 use app\common\library\helper;
 
 /**

+ 52 - 0
app/store/controller/client/h5/Setting.php

@@ -0,0 +1,52 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace app\store\controller\client\h5;
+
+use app\store\controller\Controller;
+use app\store\model\h5\Setting as SettingModel;
+
+/**
+ * H5端基本设置
+ * Class Setting
+ * @package app\store\controller\apps\wxapp
+ */
+class Setting extends Controller
+{
+    /**
+     * 获取H5端设置 (指定)
+     * @param string $key
+     * @return \think\response\Json
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function detail(string $key): \think\response\Json
+    {
+        $detail = SettingModel::getItem($key);
+        return $this->renderSuccess(compact('detail'));
+    }
+
+    /**
+     * 更新设置项
+     * @param string $key
+     * @return \think\response\Json
+     */
+    public function update(string $key): \think\response\Json
+    {
+        $model = new SettingModel;
+        if ($model->edit($key, $this->postForm())) {
+            return $this->renderSuccess('更新成功');
+        }
+        return $this->renderError($model->getError() ?: '更新失败');
+    }
+}

+ 52 - 0
app/store/model/h5/Setting.php

@@ -0,0 +1,52 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace app\store\model\h5;
+
+use think\facade\Cache;
+use app\common\model\h5\Setting as SettingModel;
+
+/**
+ * H5设置模型
+ * Class Setting
+ * @package app\store\model\wxapp
+ */
+class Setting extends SettingModel
+{
+    /**
+     * 设置项描述
+     * @var array
+     */
+    private $describe = [
+        'basic' => '基础设置',
+    ];
+
+    /**
+     * 更新系统设置
+     * @param string $key
+     * @param array $values
+     * @return bool
+     */
+    public function edit(string $key, array $values): bool
+    {
+        $model = self::detail($key) ?: $this;
+        // 删除系统设置缓存
+        Cache::delete('h5_setting_' . self::$storeId);
+        return $model->save([
+                'key' => $key,
+                'describe' => $this->describe[$key],
+                'values' => $values,
+                'update_time' => time(),
+                'store_id' => self::$storeId,
+            ]) !== false;
+    }
+}