123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace app\admin\controller\wwh;
- use app\common\controller\Backend;
- use think\Db;
- class Config extends Base
- {
- // 表字段结构
- const DEFAULT_CONFIG = [
- 'template' => 'materials',
- 'site_name' => '',
- 'keywords' => '',
- 'description' => '',
- 'logo' => '',
- 'logo1' => '',
- 'footer_logo' => '',
- 'tel' => '',
- 'facsimile' => '',
- 'email' => '',
- 'address' => '',
- 'gongwang' => '',
- 'link1' => '',
- 'beian' => '',
- 'link2' => '',
- 'copyright' => '',
- 'weibo' => '',
- 'wechat' => '',
- 'tencent_map_key' =>'',
- 'douyin' => '',
- 'content' => '',
- 'strength' => ''
- ];
- // 语言标识
- const LANG_CN = 1;
- const LANG_EN = 2;
- /**
- * 查看
- */
- public function index()
- {
- // 初始化中英文数据
- $dataCN = $this->initConfigData(self::LANG_CN);
- $dataEN = $this->initConfigData(self::LANG_EN);
- //获取模板名称
- $directory = ADDON_PATH . 'wwh' . DS . 'view' . DS;
- $files = scandir($directory);
- $folders = [];
- foreach ($files as $file) {
- if ($file !== '.' && $file !== '..' && is_dir($directory . '/' . $file)) {
- $folders[] = $file;
- }
- }
- $this->assign([
- 'dataCN' => $dataCN,
- 'dataEN' => $dataEN,
- 'folders' => $folders
- ]);
- return $this->view->fetch();
- }
- /**
- * 初始化数据
- * @param int $lang 语言标识
- * @return array
- */
- protected function initConfigData($lang)
- {
- $id = $lang; // ID与语言标识相同
- $data = Db::name('wwh_config')->where('id', $id)->find();
- if (!$data) {
- $data = array_merge(
- ['id' => $id, 'lang' => $lang],
- self::DEFAULT_CONFIG
- );
- Db::name('wwh_config')->insert($data);
- }
- return $data;
- }
- /**
- * 保存数据
- * @param int $lang 语言标识
- */
- public function saveConfig($lang)
- {
- $id = $lang; // ID与语言标识相同
- $data = [
- 'id' => $id,
- 'template' => input('template'),
- 'site_name' => input('site_name'),
- 'keywords' => input('keywords'),
- 'description' => input('description'),
- 'logo' => input('logo'),
- 'logo1' => input('logo1'),
- 'footer_logo' => input('footer_logo'),
- 'tel' => input('tel'),
- 'facsimile' => input('facsimile'),
- 'email' => input('email'),
- 'address' => input('address'),
- 'gongwang' => input('gongwang'),
- 'link1' => input('link1'),
- 'beian' => input('beian'),
- 'link2' => input('link2'),
- 'copyright' => input('copyright'),
- 'weibo' => input('weibo'),
- 'wechat' => input('wechat'),
- 'douyin' => input('douyin'),
- 'tencent_map_key' => input('tencent_map_key'),
- 'content' => input('content'),
- 'strength' => input('strength'),
- 'lang' => $lang
- ];
- $exists = Db::name('wwh_config')->where('id', $id)->find();
- if ($exists) {
- // 检查数据变动
- foreach (array_keys(self::DEFAULT_CONFIG) as $field) {
- if ($exists[$field] != $data[$field]) {
- $result = Db::name('wwh_config')->update($data);
- return $this->returnResult($result);
- }
- }
- return $this->error('未检测到数据变动', null, null, false);
- }
- $result = Db::name('wwh_config')->insert($data);
- return $this->returnResult($result);
- }
- /**
- * 返回操作结果
- * @param bool $result
- */
- protected function returnResult($result)
- {
- return $result ? $this->success('保存成功') : $this->error('保存失败');
- }
- /**
- * 中文站点配置修改
- */
- public function ConfigCN()
- {
- return $this->saveConfig(self::LANG_CN);
- }
- /**
- * 英文站点配置修改
- */
- public function ConfigEN()
- {
- return $this->saveConfig(self::LANG_EN);
- }
- }
|