zhaojs
2023-07-17 e86f2a9f0cdb291dee8f2a89d368854f4f0452fb
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
<?php
 
namespace app\admin\controller\general;
 
use app\common\controller\Backend;
use app\common\library\Email;
use app\common\model\Config as ConfigModel;
use think\Cache;
use think\Db;
use think\Exception;
use think\Validate;
 
 
/**
 * 代理配置
 *
 */
class Agent extends Backend
{
 
    protected $model = null;
    protected $noNeedRight = [];
 
    public function _initialize()
    {
        parent::_initialize();
        // $this->model = model('Config');
        $this->model = new ConfigModel;
    }
 
    /**
     * 查看
     */
    public function index()
    {
        $agent_mode_list = ConfigModel::where(['name'=>'agent_mode'])->value('content');
        $agent_mode_list = json_decode($agent_mode_list,true);
        $this->assign('agent_mode_list',$agent_mode_list);
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a", [], 'trim');
            if ($params) {
                if($params['agent_mode'] == "") $this->error('代理模式不能为空');
                $data = $params['data'];
                if(empty($data))$this->error('数据错误');
                foreach($data as $key => $item){
                    switch($key){
                        case 0:
                            if($item['level_name'] == "") $this->error('低级消费者名称未配置');
                            break;
                        case 1:
                            if($item['level_name'] == "") $this->error('初级代理名称未配置');
                            if($item['own_commission_rate'] == "") $this->error('自购佣金比例不能为空');
                            if($item['direct_commission_rate'] == "") $this->error('直属初级佣金比例不能为空');
                            if($item['indirect_commission_rate'] == "") $this->error('间接初级佣金比例不能为空');
                            break;
                        case 2:
                            if($item['level_name'] == "") $this->error('中级代理名称未配置');
                            if($item['own_commission_rate'] == "") $this->error('自购佣金比例不能为空');
                            if($item['direct_commission_rate'] == "") $this->error('直属初级佣金比例不能为空');
                            if($item['indirect_commission_rate'] == "") $this->error('间接初级佣金比例不能为空');
                            if($item['direct_primary_count'] == "") $this->error('有效直属初级人数不能为空');
                            if($item['indirect_primary_count'] == "") $this->error('有效间接初级人数不能为空');
                            if($item['own_monthly_commission'] == "") $this->error('上月结算佣金不能为空');
                            break;
                        case 3                              :
                        if($item['level_name']              == "") $this->error('高级代理名称未配置');
                        if($item['own_commission_rate']     == "") $this->error('自购佣金比例不能为空');
                        if($item['direct_commission_rate']  == "") $this->error('直属初级佣金比例不能为空');
                        if($item['direct_intermediate_rate']== "") $this->error('直属中级佣金比例不能为空');
                        if($item['indirect_commission_rate']== "") $this->error('间接初级佣金比例不能为空');
                        // if($item['direct_primary_count'] == "") $this->error('有效直属初级人数不能为空');
                       // if($item['indirect_primary_count']== "") $this->error('有效间接初级人数不能为空');
                        if($item['own_monthly_commission']  == "") $this->error('上月结算佣金不能为空');
                        if($item['accumulated_commission']  == "") $this->error('累计结算佣金不能为空');
                            break;             
                    }
                }
                Db::startTrans();
                $agent_mode = config('site.agent_mode');
                $flag = false;
                if($agent_mode != $params['agent_mode']) $flag = true;
                if($flag){
                    $res_config = Db::name('config')->where(['name'=>'agent_mode'])->update(['value'=>$params['agent_mode']]);
                    if(!$res_config){
                        Db::rollback();
                        $this->error('代理模式设置失败');
                    }
                }
                foreach($data as $level => $save_data){
                    $save_data['update_time'] = time();
                    $res = Db::name('user_level')->where(['level'=>$level])->update($save_data);
                    if(!$res){
                        Db::rollback();
                        $this->error('比例设置失败');
                    }
                }
                Db::commit();
                ConfigModel::refreshFile();
                $this->success();
            }else{
                $this->error(__('Parameter %s can not be empty', ''));
            }
        }
        $data_list = Db::name('user_level')->where(['status'=>1])->select();
        $this->assign('agent_mode',config('site.agent_mode'));
        $this->assign('row',$data_list);
        return $this->view->fetch();
    }
 
    /**
     * 添加
     */
    public function add()
    {
        if (!config('app_debug')) {
            $this->error(__('Only work at development environment'));
        }
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a", [], 'trim');
            if ($params) {
                foreach ($params as $k => &$v) {
                    $v = is_array($v) && $k !== 'setting' ? implode(',', $v) : $v;
                }
                if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio', 'array'])) {
                    $params['content'] = json_encode(ConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE);
                } else {
                    $params['content'] = '';
                }
                try {
                    $result = $this->model->create($params);
                } catch (Exception $e) {
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    try {
                        ConfigModel::refreshFile();
                    } catch (Exception $e) {
                        $this->error($e->getMessage());
                    }
                    $this->success();
                } else {
                    $this->error($this->model->getError());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }
 
    /**
     * 编辑
     * @param null $ids
     */
    public function edit($ids = null)
    {
        if ($this->request->isPost()) {
            $this->token();
            $row = $this->request->post("row/a", [], 'trim');
            if ($row) {
                $configList = [];
                foreach ($this->model->all() as $v) {
                    if (isset($row[$v['name']])) {
                        $value = $row[$v['name']];
                        if (is_array($value) && isset($value['field'])) {
                            $value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);
                        } else {
                            $value = is_array($value) ? implode(',', $value) : $value;
                        }
                        $v['value'] = $value;
                        $configList[] = $v->toArray();
                    }
                }
                try {
                    $this->model->allowField(true)->saveAll($configList);
                } catch (Exception $e) {
                    $this->error($e->getMessage());
                }
                try {
                    ConfigModel::refreshFile();
                } catch (Exception $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
    }
 
   
}