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
<?php
 
namespace app\agent\model\app;
 
use app\common\model\app\App as AppModel;
use think\facade\Cache;
 
/**
 * 应用模型
 */
class App extends AppModel
{
    /**
     * 更新应用设置
     */
    public function edit($data)
    {
        $this->startTrans();
        try {
            // 删除app缓存
            self::deleteCache();
            $where['agent_id'] = self::$agent_id;
 
            $count = $this->count($where);
            // 更新小程序设置
            if ($count > 0) {
                self::update($data, $where);
            }
            if ($count == 0) {
                $data['agent_id'] = self::$agent_id;
                self::create($data);
            }
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    public function count($where)
    {
        return $this->where($where)->count();
    }
 
    /**
     * 删除app缓存
     */
    public static function deleteCache()
    {
        return Cache::delete('agent_' . self::$agent_id);
    }
 
}