'' ]; // 语言标识 const LANG_CN = 1; const LANG_EN = 2; /** * 查看 */ public function index() { // 初始化中英文数据 $dataCN = $this->initServiceData(self::LANG_CN); $dataEN = $this->initServiceData(self::LANG_EN); $this->assign([ 'dataCN' => $dataCN, 'dataEN' => $dataEN ]); return $this->view->fetch(); } /** * 初始化数据 * @param int $lang 语言标识 * @return array */ protected function initServiceData($lang) { $id = $lang; // ID与语言标识相同 $data = Db::name('wwh_service')->where('id', $id)->find(); if (!$data) { $data = array_merge( ['id' => $id, 'lang' => $lang], self::DEFAULT_SERVICE ); Db::name('wwh_service')->insert($data); } return $data; } /** * 保存数据 * @param int $lang 语言标识 */ public function saveService($lang) { $id = $lang; // ID与语言标识相同 $data = [ 'id' => $id, 'content' => input('content'), 'lang' => $lang ]; $exists = Db::name('wwh_service')->where('id', $id)->find(); if ($exists) { // 检查数据变动 foreach (array_keys(self::DEFAULT_SERVICE) as $field) { if ($exists[$field] != $data[$field]) { $result = Db::name('wwh_service')->update($data); return $this->returnResult($result); } } return $this->error('未检测到数据变动', null, null, false); } $result = Db::name('wwh_service')->insert($data); return $this->returnResult($result); } /** * 返回操作结果 * @param bool $result */ protected function returnResult($result) { return $result ? $this->success('保存成功') : $this->error('保存失败'); } /** * 中文服务策略修改 */ public function ServiceCN() { return $this->saveService(self::LANG_CN); } /** * 英文服务策略修改 */ public function ServiceEN() { return $this->saveService(self::LANG_EN); } }