quanwei
2026-01-17 e1e2fe5710a5b5cd9c19bd3aa99c998a1a613ca8
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
 
namespace app\admin\model;
 
use app\common\model\admin\Application as ApplicationModel;
use app\admin\model\Access as AccessModel;
use app\common\model\admin\RoleAccess as RoleAccess;
/**
 * 应用表模型
 */
class Application extends ApplicationModel
{
    /**
     * 获取应用表数据
     */
    public static function getAll()
    {
        $model = new static;
        $list = $model->where('is_delete', '=', 0)->select();
        // 查询分类下的应用
        foreach ($list as $category){
            $category['children'] = AccessModel::getListByPlusCategoryId($category['plus_category_id']);
        }
        return $list;
    }
 
 
    /**
     * 软删除
     */
    public function delete_category()
    {
        return $this->save(['category_id' => 0]);
    }
    public function add($data)
    {
        $this->startTrans();
        try {
            $arr = [
                'category_id' => $data['category_id'],
                'app_icon' => $data['app_icon'],
                'app_name' => $data['app_name'],
                'app_use' => $data['app_use'],
                'admin_money' => $data['admin_money'],
                'supplier_money' => $data['supplier_money'],
                'shop' => $data['shop'],
            ];
            $res = self::create($arr);
            $arr1 = [];
            foreach ($data['access_id'] as $val) {
                $arr1[] = [
                    'role_id' => $res['id'],
                    'access_id' => $val,
                    'app_id' => self::$app_id
                ];
            }
            $model = new RoleAccess();
            $model->saveAll($arr1);
            // 事务提交
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 软删除
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }
 
    /**
     * 编辑
     * @param $data
     * @return bool
     */
    public function edit($data)
    {
        $this->startTrans();
        try {
            $this->save([
                'category_id' => $data['category_id'],
                'app_icon' => $data['app_icon'],
                'app_name' => $data['app_name'],
                'app_use' => $data['app_use'],
                'admin_money' => $data['admin_money'],
                'supplier_money' => $data['supplier_money'],
                'shop' => $data['shop'],
            ]);
            if (!isset($data['access_id'])) {
                $this->commit();
                return true;
            }
    
            $access_list = [];
            $access_model = new RoleAccess();
            $access_model->where(['role_id' => $data['id']])->delete();
    
            foreach ($data['access_id'] as $val) {
                $access_list[] = [
                    'role_id' => $data['id'],
                    'access_id' => $val,
                    'app_id' => self::$app_id
                ];
            }
    
            $access_model->saveAll($access_list);
            // 事务提交
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 获取所有插件
     */
    public static function getAllPlus(){
        $model = new static();
        return $model->where('is_delete', '=', 0)
            ->where('category_id', '=', 0)
            ->select();
    }
 
    /**
     * 新增应用
     */
    public function addApplication($data)
    {
        $model = new self();
        return $model->where('id', '=', $data['id'])->save(
            [
                'category_id' => $data['category_id'],
                'is_delete' => 0
            ]
        );
    }
}