Config.php 4.2 KB

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