quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
 
namespace app\operations\model\plus\business;
 
use think\facade\Cache;
use app\common\model\plus\business\Setting as SettingModel;
 
/**
 * 名片设置模型
 */
class Setting extends SettingModel
{
    /**
     * 设置项描述
     * @var array
     */
    private $describe = [
        'basic' => '基础设置',
        'background' => '页面背景图',
    ];
 
    /**
     * 更新系统设置
     */
    public function edit($data)
    {
        $this->startTrans();
        try {
            foreach ($data as $key => $values){
                $this->saveValues($key, $values);
            }
            $this->commit();
            // 删除系统设置缓存
            Cache::delete('business_setting_' . self::$app_id);
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 保存设置项
     */
    private function saveValues($key, $values)
    {
        $where['key'] = $key;
        $where['app_id'] = self::$app_id;
        $data = [
            'key' => $key,
            'describe' => $this->describe[$key],
            'values' => $values,
            'app_id' => self::$app_id,
        ];
        // 判断是否存在记录,存在则更新,否则新增
        $info = self::detail($key);
        if (empty($info)) {
            (new self)->save($data);
        } else {
            self::update($data, $where);
        }
    }
}