Config.php 5.1 KB

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