Config.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace app\admin\controller\wwh;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. class Config extends Base
  6. {
  7. // 表字段结构
  8. const DEFAULT_CONFIG = [
  9. 'template' => 'materials',
  10. 'site_name' => '',
  11. 'keywords' => '',
  12. 'description' => '',
  13. 'logo' => '',
  14. 'logo1' => '',
  15. 'footer_logo' => '',
  16. 'tel' => '',
  17. 'facsimile' => '',
  18. 'email' => '',
  19. 'address' => '',
  20. 'gongwang' => '',
  21. 'link1' => '',
  22. 'beian' => '',
  23. 'link2' => '',
  24. 'copyright' => '',
  25. 'weibo' => '',
  26. 'wechat' => '',
  27. 'tencent_map_key' =>'',
  28. 'douyin' => '',
  29. 'content' => '',
  30. 'strength' => ''
  31. ];
  32. // 语言标识
  33. const LANG_CN = 1;
  34. const LANG_EN = 2;
  35. /**
  36. * 查看
  37. */
  38. public function index()
  39. {
  40. // 初始化中英文数据
  41. $dataCN = $this->initConfigData(self::LANG_CN);
  42. $dataEN = $this->initConfigData(self::LANG_EN);
  43. //获取模板名称
  44. $directory = ADDON_PATH . 'wwh' . DS . 'view' . DS;
  45. $files = scandir($directory);
  46. $folders = [];
  47. foreach ($files as $file) {
  48. if ($file !== '.' && $file !== '..' && is_dir($directory . '/' . $file)) {
  49. $folders[] = $file;
  50. }
  51. }
  52. $this->assign([
  53. 'dataCN' => $dataCN,
  54. 'dataEN' => $dataEN,
  55. 'folders' => $folders
  56. ]);
  57. return $this->view->fetch();
  58. }
  59. /**
  60. * 初始化数据
  61. * @param int $lang 语言标识
  62. * @return array
  63. */
  64. protected function initConfigData($lang)
  65. {
  66. $id = $lang; // ID与语言标识相同
  67. $data = Db::name('wwh_config')->where('id', $id)->find();
  68. if (!$data) {
  69. $data = array_merge(
  70. ['id' => $id, 'lang' => $lang],
  71. self::DEFAULT_CONFIG
  72. );
  73. Db::name('wwh_config')->insert($data);
  74. }
  75. return $data;
  76. }
  77. /**
  78. * 保存数据
  79. * @param int $lang 语言标识
  80. */
  81. public function saveConfig($lang)
  82. {
  83. $id = $lang; // ID与语言标识相同
  84. $data = [
  85. 'id' => $id,
  86. 'template' => input('template'),
  87. 'site_name' => input('site_name'),
  88. 'keywords' => input('keywords'),
  89. 'description' => input('description'),
  90. 'logo' => input('logo'),
  91. 'logo1' => input('logo1'),
  92. 'footer_logo' => input('footer_logo'),
  93. 'tel' => input('tel'),
  94. 'facsimile' => input('facsimile'),
  95. 'email' => input('email'),
  96. 'address' => input('address'),
  97. 'gongwang' => input('gongwang'),
  98. 'link1' => input('link1'),
  99. 'beian' => input('beian'),
  100. 'link2' => input('link2'),
  101. 'copyright' => input('copyright'),
  102. 'weibo' => input('weibo'),
  103. 'wechat' => input('wechat'),
  104. 'douyin' => input('douyin'),
  105. 'tencent_map_key' => input('tencent_map_key'),
  106. 'content' => input('content'),
  107. 'strength' => input('strength'),
  108. 'lang' => $lang
  109. ];
  110. $exists = Db::name('wwh_config')->where('id', $id)->find();
  111. if ($exists) {
  112. // 检查数据变动
  113. foreach (array_keys(self::DEFAULT_CONFIG) as $field) {
  114. if ($exists[$field] != $data[$field]) {
  115. $result = Db::name('wwh_config')->update($data);
  116. return $this->returnResult($result);
  117. }
  118. }
  119. return $this->error('未检测到数据变动', null, null, false);
  120. }
  121. $result = Db::name('wwh_config')->insert($data);
  122. return $this->returnResult($result);
  123. }
  124. /**
  125. * 返回操作结果
  126. * @param bool $result
  127. */
  128. protected function returnResult($result)
  129. {
  130. return $result ? $this->success('保存成功') : $this->error('保存失败');
  131. }
  132. /**
  133. * 中文站点配置修改
  134. */
  135. public function ConfigCN()
  136. {
  137. return $this->saveConfig(self::LANG_CN);
  138. }
  139. /**
  140. * 英文站点配置修改
  141. */
  142. public function ConfigEN()
  143. {
  144. return $this->saveConfig(self::LANG_EN);
  145. }
  146. }