quanwei
2025-12-25 a47b138c7455dee981af9b4fac431a16c0eee675
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
 
namespace app\agent\controller;
 
use app\admin\model\Application_category as ModelApplication_category;
use app\admin\model\Application as ApplicationModel;
use app\common\model\admin\Client_applic as ApplicModel;
use app\agent\model\Agent as AgentModel;
use app\admin\model\app\App as AppModel;
use app\common\model\shop\RoleAccess as RoleAccessModel;
use app\agent\model\shop\Access as AccessModel;
use app\agent\model\Client as ClientModel;
 
use app\admin\model\Setting as SettingModel;
class Application extends Controller
{
    /**
     * 获取分类应用
     */
    public function index()
    {
        $data=$this->postData();
        $accessList = ModelApplication_category::getAll();
        $applicList = ApplicModel::getListByClientId($data['client_id']);
        return $this->renderSuccess('', compact('accessList','applicList'));
    }
    /**
     * 绑定应用
     */
    public function bangApplic()
    {
        $data = json_decode($this->postData()['params'], true);
        //代理商扣款
        // 代理商详情
        $AgentModel = AgentModel::detail($data['agent_id']);
        if($AgentModel['balance']<$data['applic_info']['supplier_money']){
             return $this->renderError($AgentModel->getError() ?: '代理商余额不足');
        }
        $money=$data['applic_info']['shop'];
        if($AgentModel['level']==1){
            $money=$data['applic_info']['supplier_money'];
        }else if($AgentModel['level']==2){
            $money=$data['applic_info']['admin_money'];
        }
        $source=0;
        $params=array(
            "balance"=>array(
                "mode"=> 'dec',
                "money"=>$money,
                "remark"=> '绑定应用;应用名称:'.$data['applic_info']['app_name'].';客户名称:'.$data['client_name'],
            )
        );
        if ($AgentModel->recharge($AgentModel['agent_name'], $source, $params)) {
           $model = new ApplicModel;
           // 新增记录
           $i=$model->save([
                'client_id' => $data['client_id'],
                'applic_id' => $data['applic_info']['id'],
                'agent_id' => $data['agent_id']
           ]);
           // 新增权限
           $RoleAccessModel = new RoleAccessModel();
           $arr=[];
           foreach($data['applic_info']['access'] as $key=>$value){
               $Model=$RoleAccessModel;
               $item=$Model->where('client_id','=',$data['client_id'])->where('access_id','=',$value['access_id'])->find();
               if(empty($item)){
                   $arr[]=[
                       'role_id'=>$data['role']['role_id'],
                       'access_id'=>$value['access_id'],
                       'app_id'=>$data['app']['app_id'],
                       'client_id'=>$data['client_id']
                   ];
               }
           }
           $RoleAccessModel->saveAll($arr);
           if($i){
                return $this->renderSuccess('操作成功');
           } 
        }
        
        return $this->renderError($model->getError() ?: '操作失败');
        
    }
    /**
     * 获取角色菜单信息
     */
    public function getRoleList()
    {
        $user_info = $this->postData();
         
        if ($user_info['is_super'] == 1) {
            $model = new AccessModel();
            $menus = $model->getList();
        } else {
            $model = new AccessModel();
            $menus = $model->getListByUser($user_info['shop_user_id']);
    
            foreach ($menus as $key => $val) {
                if ($val['redirect_name'] != $val['children'][0]['path']) {
                    $menus[$key]['redirect_name'] = $menus[$key]['children'][0]['path'];
                }
            }
        }
        
        return $this->renderSuccess('', compact('menus'));
    }
    public function getApplicRole($id){
        
        $model =ApplicationModel::detail($id);
        
        $select_menu = array_column($model->toArray()['access'], 'access_id');
        
        $AccessModel = new AccessModel();
        $menus = $AccessModel->getListByApplic($select_menu);
        return $this->renderSuccess('', compact('menus'));
    }
    /**
     * 添加到分类
     */
    public function editApplic()
    {
        $data=$this->postData();
        $applicList = ApplicModel::getListByClientId($data['client_id']);
        $n=0;
        if(!empty($data['applic_arr'])){
            $applic_arr=$data['applic_arr'];
            foreach ($applicList as $item) {
                 if(!in_array($item['applic_id'],$data['applic_arr'])){
                     $model = ApplicModel::detail($item['id']);
                     $model->setDelete();
                     $n=$n+1;
                 }else{
                    
                 }
            }
            
            foreach($applic_arr as $k=>$v) {
                
             $detail = ApplicModel::getListByApplicId($data['client_id'],$v);
            
             if(count($detail)==0){
                 $model = new ApplicModel;
                 // 新增记录
                 $model->save([
                     'client_id' => $data['client_id'],
                     'applic_id' => $v,
                     'agent_id' => $data['agent_id']
                 ]);
                 $n=$n+1;
             }
             
            }
        }
        
        return $this->renderSuccess('保存成功');
    }
    /**
     * 添加商城
     */
    public function addShop()
    {
        $data=$this->postData();
        $arr = [
            'app_name' => $data['app_name'],
            'client_id' => $data['client_id'],
            'no_expire' => true,
            'weixin_service' => false
        ];
        $arr['password']=$this->make_coupon_card();
        $arr['password_confirm']=$arr['password'];
        $arr['user_name']=$arr['app_name'];
        
        $model = new AppModel;
        // 新增商城,主管理员记录
        if ($model->add($arr,$data['client_id'])) {
           return $this->renderSuccess('添加成功');
        }
        
        return $this->renderError($model->getError() ?: '添加失败');
    }
    //生成8位随机码 :
    public function make_coupon_card() {
        $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $rand = $code[rand(0,25)]
            .strtoupper(dechex(date('m')))
            .date('d').substr(time(),-5)
            .substr(microtime(),2,5)
            .sprintf('%02d',rand(0,99));
        for(
            $a = md5( $rand, true ),
            $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
            $d = '',
            $f = 0;
            $f < 8;
            $g = ord( $a[ $f ] ),
            $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
            $f++
        );
        return  $d;
    }
    /**
     * 客户详情
     */
    public function getClientDetail($client_id)
    {
        $data = ClientModel::detail($client_id);
        $agentInfo = AgentModel::detail($data['agent_id']);
        return $this->renderSuccess('', compact('data','agentInfo'));
    }
}