Config.php 5.2 KB

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