Service.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\admin\controller\wwh;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. class Service extends Base
  6. {
  7. // 表字段结构
  8. const DEFAULT_SERVICE = [
  9. 'content' => ''
  10. ];
  11. // 语言标识
  12. const LANG_CN = 1;
  13. const LANG_EN = 2;
  14. /**
  15. * 查看
  16. */
  17. public function index()
  18. {
  19. // 初始化中英文数据
  20. $dataCN = $this->initServiceData(self::LANG_CN);
  21. $dataEN = $this->initServiceData(self::LANG_EN);
  22. $this->assign([
  23. 'dataCN' => $dataCN,
  24. 'dataEN' => $dataEN
  25. ]);
  26. return $this->view->fetch();
  27. }
  28. /**
  29. * 初始化数据
  30. * @param int $lang 语言标识
  31. * @return array
  32. */
  33. protected function initServiceData($lang)
  34. {
  35. $id = $lang; // ID与语言标识相同
  36. $data = Db::name('wwh_service')->where('id', $id)->find();
  37. if (!$data) {
  38. $data = array_merge(
  39. ['id' => $id, 'lang' => $lang],
  40. self::DEFAULT_SERVICE
  41. );
  42. Db::name('wwh_service')->insert($data);
  43. }
  44. return $data;
  45. }
  46. /**
  47. * 保存数据
  48. * @param int $lang 语言标识
  49. */
  50. public function saveService($lang)
  51. {
  52. $id = $lang; // ID与语言标识相同
  53. $data = [
  54. 'id' => $id,
  55. 'content' => input('content'),
  56. 'lang' => $lang
  57. ];
  58. $exists = Db::name('wwh_service')->where('id', $id)->find();
  59. if ($exists) {
  60. // 检查数据变动
  61. foreach (array_keys(self::DEFAULT_SERVICE) as $field) {
  62. if ($exists[$field] != $data[$field]) {
  63. $result = Db::name('wwh_service')->update($data);
  64. return $this->returnResult($result);
  65. }
  66. }
  67. return $this->error('未检测到数据变动', null, null, false);
  68. }
  69. $result = Db::name('wwh_service')->insert($data);
  70. return $this->returnResult($result);
  71. }
  72. /**
  73. * 返回操作结果
  74. * @param bool $result
  75. */
  76. protected function returnResult($result)
  77. {
  78. return $result ? $this->success('保存成功') : $this->error('保存失败');
  79. }
  80. /**
  81. * 中文服务策略修改
  82. */
  83. public function ServiceCN()
  84. {
  85. return $this->saveService(self::LANG_CN);
  86. }
  87. /**
  88. * 英文服务策略修改
  89. */
  90. public function ServiceEN()
  91. {
  92. return $this->saveService(self::LANG_EN);
  93. }
  94. }