Procházet zdrojové kódy

新增微信小程序设置模块

Yahoo před 4 roky
rodič
revize
875a8daa76

+ 25 - 0
app/api/model/wxapp/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\wxapp;
+
+use app\common\model\wxapp\Setting as SettingModel;
+
+/**
+ * 微信小程序设置模型
+ * Class Setting
+ * @package app\api\model\wxapp
+ */
+class Setting extends SettingModel
+{
+
+}

+ 3 - 4
app/common/library/helper.php

@@ -189,7 +189,7 @@ class helper
      * @param int $options
      * @return string
      */
-    public static function jsonEncode($data, int $options = JSON_UNESCAPED_UNICODE)
+    public static function jsonEncode($data, int $options = JSON_UNESCAPED_UNICODE): string
     {
         return json_encode($data, $options);
     }
@@ -199,7 +199,7 @@ class helper
      * @param $json
      * @return array
      */
-    public static function jsonDecode($json)
+    public static function jsonDecode($json): array
     {
         return json_decode($json, true);
     }
@@ -209,7 +209,7 @@ class helper
      * @param $path
      * @return bool
      */
-    public static function checkWriteable($path)
+    public static function checkWriteable($path): bool
     {
         try {
             !is_dir($path) && mkdir($path, 0755);
@@ -223,5 +223,4 @@ class helper
         }
         return false;
     }
-
 }

+ 1 - 2
app/common/model/Wxapp.php

@@ -13,12 +13,11 @@ declare (strict_types=1);
 namespace app\common\model;
 
 use cores\BaseModel;
-
 use think\facade\Cache;
 use app\common\exception\BaseException;
 
 /**
- * 微信小程序模型
+ * 微信小程序模型 (当前类已废弃,请勿使用)
  * Class Wxapp
  * @package app\common\model
  */

+ 139 - 0
app/common/model/wxapp/Setting.php

@@ -0,0 +1,139 @@
+<?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\wxapp;
+
+use think\facade\Cache;
+use cores\BaseModel;
+use app\common\library\helper;
+
+/**
+ * 微信小程序设置模型
+ * Class Setting
+ * @package app\common\model\wxapp
+ */
+class Setting extends BaseModel
+{
+    // 定义表名
+    protected $name = 'wxapp_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' => [
+                    // 小程序AppID
+                    'app_id' => '',
+                    // 小程序AppSecret
+                    'app_secret' => '',
+                    // 微信支付商户号ID
+                    'mchid' => '',
+                    // 微信支付密钥
+                    'apikey' => '',
+                    // 证书文件cert
+                    'cert_pem' => '',
+                    // 证书文件key
+                    'key_pem' => '',
+                ]
+            ]
+        ];
+    }
+}

+ 13 - 12
app/store/controller/Controller.php

@@ -16,6 +16,7 @@ use cores\BaseController;
 use app\store\service\Auth as AuthService;
 use app\common\service\store\User as StoreUserService;
 use app\common\exception\BaseException;
+use think\response\Json;
 
 /**
  * 商户后台控制器基类
@@ -94,7 +95,7 @@ class Controller extends BaseController
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    private function checkPrivilege()
+    private function checkPrivilege(): bool
     {
         // 在开发模式下, 建议把此处代码暂时屏蔽, 开发完成后在超管后台新增菜单和api
         if (!AuthService::getInstance()->checkPrivilege('/' . $this->routeUri)) {
@@ -124,7 +125,7 @@ class Controller extends BaseController
      * @return bool
      * @throws BaseException
      */
-    private function checkLogin()
+    private function checkLogin(): bool
     {
         // 验证当前请求是否在白名单
         if (in_array($this->routeUri, $this->allowAllAction)) {
@@ -141,7 +142,7 @@ class Controller extends BaseController
      * 获取当前store_id
      * @return int|null
      */
-    protected function getStoreId()
+    protected function getStoreId(): ?int
     {
         // app/store/common.php
         return getStoreId();
@@ -152,9 +153,9 @@ class Controller extends BaseController
      * @param int|null $status 状态码
      * @param string $message
      * @param array $data
-     * @return array
+     * @return Json
      */
-    protected function renderData(int $status = null, string $message = '', array $data = [])
+    protected function renderData(int $status = null, string $message = '', array $data = []): Json
     {
         is_null($status) && $status = config('status.success');
         return json(compact('status', 'message', 'data'));
@@ -164,9 +165,9 @@ class Controller extends BaseController
      * 返回操作成功json
      * @param array|string $data
      * @param string $message
-     * @return array
+     * @return Json
      */
-    protected function renderSuccess($data = [], string $message = 'success')
+    protected function renderSuccess($data = [], string $message = 'success'): Json
     {
         if (is_string($data)) {
             $message = $data;
@@ -179,9 +180,9 @@ class Controller extends BaseController
      * 返回操作失败json
      * @param string $message
      * @param array $data
-     * @return array
+     * @return Json
      */
-    protected function renderError(string $message = 'error', array $data = [])
+    protected function renderError(string $message = 'error', array $data = []): Json
     {
         return $this->renderData(config('status.error'), $message, $data);
     }
@@ -198,10 +199,10 @@ class Controller extends BaseController
 
     /**
      * 获取post数据 (数组)
-     * @param $key
+     * @param string $key
      * @return mixed
      */
-    protected function postForm($key = 'form')
+    protected function postForm(string $key = 'form')
     {
         return $this->postData($key);
     }
@@ -221,7 +222,7 @@ class Controller extends BaseController
      * @return bool
      * @throws BaseException
      */
-    private function checkMethodRules()
+    private function checkMethodRules(): bool
     {
         if (!isset($this->methodRules[$this->action])) {
             return true;

+ 20 - 24
app/store/controller/client/Wxapp.php → app/store/controller/client/wxapp/Setting.php

@@ -8,50 +8,46 @@
 // +----------------------------------------------------------------------
 // | Author: 萤火科技 <admin@yiovo.com>
 // +----------------------------------------------------------------------
-declare (strict_types = 1);
+declare (strict_types=1);
 
-namespace app\store\controller\client;
+namespace app\store\controller\client\wxapp;
 
 use app\store\controller\Controller;
-use app\store\model\Wxapp as WxappModel;
+use app\store\model\wxapp\Setting as SettingModel;
+use think\response\Json;
 
 /**
- * 微信小程序管理
- * Class Wxapp
- * @package app\store\controller
+ * 微信小程序设置
+ * Class Setting
+ * @package app\store\controller\apps\wxapp
  */
-class Wxapp extends Controller
+class Setting extends Controller
 {
     /**
-     * 微信小程序详情
-     * @return array
+     * 获取微信小程序设置 (指定)
+     * @param string $key
+     * @return Json
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public function detail()
+    public function detail(string $key): Json
     {
-        // 当前小程序信息
-        $detail = WxappModel::detail($this->storeId);
+        $detail = SettingModel::getItem($key);
         return $this->renderSuccess(compact('detail'));
     }
 
     /**
-     * 微信小程序设置
-     * @return array
-     * @throws \think\db\exception\DataNotFoundException
-     * @throws \think\db\exception\DbException
-     * @throws \think\db\exception\ModelNotFoundException
+     * 更新设置项
+     * @param string $key
+     * @return Json
      */
-    public function setting()
+    public function update(string $key): Json
     {
-        // 当前小程序信息
-        $model = WxappModel::detail($this->storeId);
-        // 更新小程序设置
-        if ($model->edit($this->postForm())) {
+        $model = new SettingModel;
+        if ($model->edit($key, $this->postForm())) {
             return $this->renderSuccess('更新成功');
         }
         return $this->renderError($model->getError() ?: '更新失败');
     }
-
-}
+}

+ 52 - 0
app/store/model/wxapp/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\wxapp;
+
+use think\facade\Cache;
+use app\common\model\wxapp\Setting as SettingModel;
+
+/**
+ * 微信小程序设置模型
+ * 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('wxapp_setting_' . self::$storeId);
+        return $model->save([
+                'key' => $key,
+                'describe' => $this->describe[$key],
+                'values' => $values,
+                'update_time' => time(),
+                'store_id' => self::$storeId,
+            ]) !== false;
+    }
+}

+ 33 - 12
数据库修改记录.txt

@@ -1,7 +1,32 @@
 
-# 订单导出Excel记录表
+# v2.0.4
+# 修改时间:2021-11-29
+ALTER TABLE `yoshop_wxapp` COMMENT='微信小程序记录表(已废弃)';
+
+CREATE TABLE `yoshop_wxapp_setting` (
+  `key` varchar(30) NOT NULL DEFAULT '' COMMENT '设置项标示',
+  `describe` varchar(255) NOT NULL DEFAULT '' COMMENT '设置项描述',
+  `values` mediumtext NOT NULL COMMENT '设置内容(json格式)',
+  `store_id` int unsigned NOT NULL DEFAULT '0' COMMENT '商城ID',
+  `update_time` int unsigned NOT NULL DEFAULT '0' COMMENT '更新时间',
+  UNIQUE KEY `unique_key` (`key`,`store_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='微信小程序设置表';
+
+
+CREATE TABLE `yoshop_h5_setting` (
+  `key` varchar(30) CHARACTER SET utf8 NOT NULL DEFAULT '' COMMENT '设置项标示',
+  `describe` varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT '' COMMENT '设置项描述',
+  `values` mediumtext CHARACTER SET utf8 NOT NULL COMMENT '设置内容(json格式)',
+  `store_id` int unsigned NOT NULL DEFAULT '0' COMMENT '商城ID',
+  `update_time` int unsigned NOT NULL DEFAULT '0' COMMENT '更新时间',
+  UNIQUE KEY `unique_key` (`key`,`store_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='H5端设置表';
+
+
+--------------------------------------------
+
+# v2.0.3
 # 修改时间:2021-10-19
-## v2.0.3
 DROP TABLE IF EXISTS `yoshop_order_export`;
 CREATE TABLE `yoshop_order_export` (
   `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
@@ -16,9 +41,8 @@ CREATE TABLE `yoshop_order_export` (
 ) ENGINE=InnoDB AUTO_INCREMENT=10001 DEFAULT CHARSET=utf8 COMMENT='订单导出Excel记录表';
 
 
-# 后台删除订单权限
+# v2.0.3
 # 修改时间:2021-10-19
-## v2.0.3
 INSERT INTO `yoshop_store_api` VALUES ('11185', '删除订单', '/order.event/delete', '11132', '135', '1614556800', '1614556800');
 INSERT INTO `yoshop_store_menu` VALUES ('10141', '20', '删除订单', '', 'delete', '10051', '115', '1614556800', '1614556800');
 INSERT INTO `yoshop_store_menu_api` VALUES ('10661', '10141', '11185', '1614556800');
@@ -26,30 +50,27 @@ INSERT INTO `yoshop_store_menu_api` VALUES ('10662', '10141', '11132', '16145568
 INSERT INTO `yoshop_store_menu_api` VALUES ('10663', '10141', '11076', '1614556800');
 
 
-# 下单来源客户端
+# v2.0.3
 # 修改时间:2021-10-19
-## v2.0.3
 ALTER TABLE `yoshop_order`
 ADD COLUMN `platform`  varchar(20) NOT NULL DEFAULT '' COMMENT '来源客户端 (APP、H5、小程序等)' AFTER `order_source_id`;
 
 --------------------------------------------
 
-# 第三方用户信息表 - 添加索引
+
+# v2.0.1
 # 修改时间:2021-3-26
-# 当前版本号:v2.0.1
 ALTER TABLE `yoshop_user_oauth` ADD INDEX `oauth_type_2` (`oauth_type`, `oauth_id`) USING BTREE ;
 
 
-# 第三方用户信息表 - 添加字段:是否删除
+# v2.0.1
 # 修改时间:2021-3-26
-# 当前版本号:v2.0.1
 ALTER TABLE `yoshop_user_oauth`
 ADD COLUMN `is_delete`  tinyint UNSIGNED NOT NULL DEFAULT 0 COMMENT '是否删除' AFTER `store_id` ;
 
 
-# 省市区数据表 - 补充部分城市缺失的3级区划 (东莞市、中山市、三沙市、儋州市、嘉峪关市)
+# v2.0.1
 # 修改时间:2021-6-2
-# 当前版本号:v2.0.1
 INSERT INTO `yoshop_region` VALUES ('3620', '东城街道', '2051', '44190', '3');
 INSERT INTO `yoshop_region` VALUES ('3621', '南城街道', '2051', '441900', '3');
 INSERT INTO `yoshop_region` VALUES ('3622', '万江街道', '2051', '441900', '3');