| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 | <?phpnamespace app\admin\controller;use app\common\controller\Backend;use app\common\Service\PlatformService;use think\Exception;use think\exception\PDOException;use think\exception\ValidateException;/** * 平台配置管理 */class Platform extends Backend{    /**     * 支持的平台配置     * @var array     */    protected $platforms = [        'WechatMiniProgram' => '微信小程序',        'DouyinMiniProgram' => '抖音小程序',    ];        public function _initialize()    {        parent::_initialize();    }        /**     * 查看平台配置     */    public function index()    {        //设置过滤方法        $this->request->filter(['strip_tags', 'trim']);                if ($this->request->isAjax()) {            $list = [];                        // 获取所有平台配置            $configs = PlatformService::getAllPlatformConfigs();                        foreach ($configs as $platform => $config) {                $list[] = [                    'platform' => $platform,                    'name' => $config['title'],                    'status' => $config['status'] ? '启用' : '禁用',                    'count' => count($config['config']),                    'created_at' => date('Y-m-d H:i:s'),                    'updated_at' => date('Y-m-d H:i:s')                ];            }                        $result = array(                "total" => count($list),                "rows" => $list            );                        return json($result);        }                return $this->view->fetch();    }        /**     * 微信小程序配置     */    public function wechat_mini_program()    {        $group = 'shop.platform.WechatMiniProgram';        return $this->platformConfig($group, '微信小程序配置');    }    /**     * 抖音小程序配置     */    public function douyin_mini_program()    {        $group = 'shop.platform.DouyinMiniProgram';        return $this->platformConfig($group, '抖音小程序配置');    }    /**     * 平台配置通用方法     */    private function platformConfig($group, $title)    {        if ($this->request->isPost()) {            $params = $this->request->post();                        // 验证参数            if (!isset($params['config']) || !is_array($params['config'])) {                $this->error('参数错误');            }                        try {                $result = PlatformService::setConfigsByGroup($params['config'], $group);                                if ($result) {                    $this->success('配置保存成功');                } else {                    $this->error('配置保存失败');                }            } catch (Exception $e) {                $this->error($e->getMessage());            }        }                // 获取当前平台配置        $configs = PlatformService::getConfigByGroup($group);                $this->view->assign('configs', $configs);        $this->view->assign('group', $group);        $this->view->assign('title', $title);                return $this->view->fetch('config');    }        /**     * 初始化平台配置     */    public function init_config()    {        if ($this->request->isPost()) {            $platform = $this->request->post('platform');                        if (!$platform) {                $this->error('请选择平台');            }                        try {                $result = PlatformService::initPlatformConfig($platform);                                if ($result) {                    $this->success('平台配置初始化成功');                } else {                    $this->error('平台配置初始化失败');                }            } catch (Exception $e) {                $this->error($e->getMessage());            }        }                return $this->view->fetch();    }        /**     * 清除配置缓存     */    public function clear()    {        PlatformService::clearConfigCache();        $this->success('缓存清除成功');    }        /**     * 获取配置信息     */    public function get_config()    {        $name = $this->request->param('name');                if (!$name) {            $this->error('参数错误');        }                $config = PlatformService::getConfigValue($name);                return json([            'code' => 1,            'msg' => '获取成功',            'data' => $config        ]);    }        /**     * 设置配置信息     */    public function set_config()    {        if ($this->request->isPost()) {            $name = $this->request->post('name');            $value = $this->request->post('value');                        if (!$name) {                $this->error('参数错误');            }                        try {                $result = PlatformService::setConfigValue($name, $value);                                if ($result) {                    $this->success('配置设置成功');                } else {                    $this->error('配置设置失败');                }            } catch (Exception $e) {                $this->error($e->getMessage());            }        }    }} 
 |