Home.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\admin\controller\wwh;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. class Home extends Base
  6. {
  7. // 表字段结构
  8. const DEFAULT_HOME = [
  9. 'about_title' => '',
  10. 'introduction' => '',
  11. 'num1' => '',
  12. 'unit1' => '',
  13. 'introduce1' => '',
  14. 'num2' => '',
  15. 'unit2' => '',
  16. 'introduce2' => '',
  17. 'num3' => '',
  18. 'unit3' => '',
  19. 'introduce3' => '',
  20. 'image' => ''
  21. ];
  22. // 语言标识
  23. const LANG_CN = 1;
  24. const LANG_EN = 2;
  25. /**
  26. * 查看
  27. */
  28. public function index()
  29. {
  30. // 初始化中英文数据
  31. $dataCN = $this->initHomeData(self::LANG_CN);
  32. $dataEN = $this->initHomeData(self::LANG_EN);
  33. $this->assign([
  34. 'dataCN' => $dataCN,
  35. 'dataEN' => $dataEN
  36. ]);
  37. return $this->view->fetch();
  38. }
  39. /**
  40. * 初始化数据
  41. * @param int $lang 语言标识
  42. * @return array
  43. */
  44. protected function initHomeData($lang)
  45. {
  46. $id = $lang; // ID与语言标识相同
  47. $data = Db::name('wwh_home')->where('id', $id)->find();
  48. if (!$data) {
  49. $data = array_merge(
  50. ['id' => $id, 'lang' => $lang],
  51. self::DEFAULT_HOME
  52. );
  53. Db::name('wwh_home')->insert($data);
  54. }
  55. return $data;
  56. }
  57. /**
  58. * 保存数据
  59. * @param int $lang 语言标识
  60. */
  61. public function saveHome($lang)
  62. {
  63. $id = $lang; // ID与语言标识相同
  64. $data = [
  65. 'id' => $id,
  66. 'about_title' => input('about_title'),
  67. 'introduction' => input('introduction'),
  68. 'num1' => input('num1'),
  69. 'unit1' => input('unit1'),
  70. 'introduce1' => input('introduce1'),
  71. 'num2' => input('num2'),
  72. 'unit2' => input('unit2'),
  73. 'introduce2' => input('introduce2'),
  74. 'num3' => input('num3'),
  75. 'unit3' => input('unit3'),
  76. 'introduce3' => input('introduce3'),
  77. 'image' => input('image'),
  78. 'lang' => $lang
  79. ];
  80. $exists = Db::name('wwh_home')->where('id', $id)->find();
  81. if ($exists) {
  82. // 检查数据变动
  83. foreach (array_keys(self::DEFAULT_HOME) as $field) {
  84. if ($exists[$field] != $data[$field]) {
  85. $result = Db::name('wwh_home')->update($data);
  86. return $this->returnResult($result);
  87. }
  88. }
  89. return $this->error('未检测到数据变动', null, null, false);
  90. }
  91. $result = Db::name('wwh_home')->insert($data);
  92. return $this->returnResult($result);
  93. }
  94. /**
  95. * 返回操作结果
  96. * @param bool $result
  97. */
  98. protected function returnResult($result)
  99. {
  100. return $result ? $this->success('保存成功') : $this->error('保存失败');
  101. }
  102. /**
  103. * 中文首页配置修改
  104. */
  105. public function HomeCN()
  106. {
  107. return $this->saveHome(self::LANG_CN);
  108. }
  109. /**
  110. * 英文首页配置修改
  111. */
  112. public function HomeEN()
  113. {
  114. return $this->saveHome(self::LANG_EN);
  115. }
  116. }