123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- namespace app\admin\controller\wwh;
- use app\common\controller\Backend;
- use think\Db;
- class Home extends Base
- {
- // 表字段结构
- const DEFAULT_HOME = [
- 'about_title' => '',
- 'introduction' => '',
- 'num1' => '',
- 'unit1' => '',
- 'introduce1' => '',
- 'num2' => '',
- 'unit2' => '',
- 'introduce2' => '',
- 'num3' => '',
- 'unit3' => '',
- 'introduce3' => '',
- 'image' => ''
- ];
- // 语言标识
- const LANG_CN = 1;
- const LANG_EN = 2;
- /**
- * 查看
- */
- public function index()
- {
- // 初始化中英文数据
- $dataCN = $this->initHomeData(self::LANG_CN);
- $dataEN = $this->initHomeData(self::LANG_EN);
- $this->assign([
- 'dataCN' => $dataCN,
- 'dataEN' => $dataEN
- ]);
- return $this->view->fetch();
- }
- /**
- * 初始化数据
- * @param int $lang 语言标识
- * @return array
- */
- protected function initHomeData($lang)
- {
- $id = $lang; // ID与语言标识相同
- $data = Db::name('wwh_home')->where('id', $id)->find();
- if (!$data) {
- $data = array_merge(
- ['id' => $id, 'lang' => $lang],
- self::DEFAULT_HOME
- );
- Db::name('wwh_home')->insert($data);
- }
- return $data;
- }
- /**
- * 保存数据
- * @param int $lang 语言标识
- */
- public function saveHome($lang)
- {
- $id = $lang; // ID与语言标识相同
- $data = [
- 'id' => $id,
- 'about_title' => input('about_title'),
- 'introduction' => input('introduction','',null,'htmlspecialchars'),
- 'num1' => input('num1'),
- 'unit1' => input('unit1'),
- 'introduce1' => input('introduce1'),
- 'num2' => input('num2'),
- 'unit2' => input('unit2'),
- 'introduce2' => input('introduce2'),
- 'num3' => input('num3'),
- 'unit3' => input('unit3'),
- 'introduce3' => input('introduce3'),
- 'image' => input('image'),
- 'lang' => $lang
- ];
- $exists = Db::name('wwh_home')->where('id', $id)->find();
- if ($exists) {
- // 检查数据变动
- foreach (array_keys(self::DEFAULT_HOME) as $field) {
- if ($exists[$field] != $data[$field]) {
- $result = Db::name('wwh_home')->update($data);
- return $this->returnResult($result);
- }
- }
- return $this->error('未检测到数据变动', null, null, false);
- }
- $result = Db::name('wwh_home')->insert($data);
- return $this->returnResult($result);
- }
- /**
- * 返回操作结果
- * @param bool $result
- */
- protected function returnResult($result)
- {
- return $result ? $this->success('保存成功') : $this->error('保存失败');
- }
- /**
- * 中文首页配置修改
- */
- public function HomeCN()
- {
- return $this->saveHome(self::LANG_CN);
- }
- /**
- * 英文首页配置修改
- */
- public function HomeEN()
- {
- return $this->saveHome(self::LANG_EN);
- }
- }
|