Config.php 5.0 KB

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