chengkun
2025-09-04 0ea78440cb53b7ecf0ef715e3f51d88918c5821d
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
 
namespace app\admin\controller;
 
use app\admin\validate\SellerAccountAdd;
use app\admin\validate\SellerAccountEdit;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
 
class Seller extends Common
{
 
    public function index()
    {
        ////////
        View::assign('menuitem', strtolower('seller-index'));
 
        $other['title'] = '分销商';
        $guide['one']['text'] = '分销商管理';
        $guide['two']['text'] = '分销商列表';
        View::assign('guide', $guide);
 
 
        View::assign('other', $other);
        return View::fetch('index');
    }
 
    /**
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function seller_list()
    {
        if (Request::isPost()) {
            $kw = input('kw');
            if (!empty($kw)) {
                $condition[] = array('a.phone|b.company_name|b.certificate_name', 'like', "%$kw%");
            }
            $p = input('p', 1);
            $_GET['p'] = $p;
            $other['page_size'] = input('page_size', 20);
            $order = 'a.id desc';
 
            $count = Db::name('seller_account')
                ->alias('a')->join('seller_base_info b', 'a.id = b.seller_id')
                ->where($condition)->count();
 
            $list = Db::name('seller_account')
                ->alias('a')
                ->leftJoin('seller_base_info b', 'a.id = b.seller_id')
                ->field("a.id,a.phone,b.id as seller_id,b.account_type,b.certificate_name,b.first_name,b.last_name,b.company_name,login_time,login_ip,a.end_time,a.start_time,a.sub_ban_access")
                ->where($condition)
                ->order($order)
                ->page($p, $other['page_size'])
                ->select()
                ->toArray();
 
            $other['count'] = $count;
            if (!$list) {
                $list = [];
            } else {
                foreach ($list as &$item) {
                    $item['endtime'] = date('Y-m-d', $item['end_time']);
                }
            }
            $other['admin_info'] = $this->cinfo;
            $d['code'] = 200;
            $d['list'] = $list;
            $d['other'] = $other;
            return $d;
        }
    }
 
    public function update_ban_access()
    {
        header('Content-Type:text/html;charset=utf-8');
        if (Request::isPost()) {
            $id = input('id');
            $sub_ban_access = input('sub_ban_access');
            $info = Db::name('seller_account')->where('id', $id)->find();
            if (!$info) {
                $d['code'] = 400;
                $d['message'] = '信息不存在';
                return $d;
            }
            if ($sub_ban_access == '1') {
                $dd['sub_ban_access'] = 1;
                $d['sub_ban_access'] = 1;
                $d['message'] = '允许登录';
            } else {
                $dd['sub_ban_access'] = 0;
                $d['sub_ban_access'] = 0;
                $d['message'] = '禁止登录';
            }
            $backval = Db::name('seller_account')->where('id', $id)->save($dd);
            if ($backval === FALSE) {
                $d['code'] = 400;
            } else {
                $d['code'] = 200;
            }
        } else {
            $d['code'] = 400;
            $d['message'] = '非法请求';
        }
        return $d;
    }
 
 
    /**
     * 分销商编辑页
     * @param int $id
     * @return string
     */
    public function add(int $id = 0): string
    {
        View::assign('menuitem', strtolower('seller-index'));
        if ($id && is_numeric($id)) {
            $other['title'] = '编辑分销商信息';
        } else {
            $other['title'] = '添加分销商信息';
        }
        $guide['one']['text'] = '分销商管理';
        $guide['two']['text'] = $other['title'];
 
        $other['id'] = $id;
        View::assign('guide', $guide);
        View::assign('other', $other);
        return View::fetch('add');
    }
 
    /**
     * 获取分销商详细信息
     * @return array
     */
    public function get_seller_info(): array
    {
        try {
            if (!Request::isPost()) {
                throw new Exception("请求方式有误");
            }
            $seller_id = input('id');
            if (!$seller_id || !is_numeric($seller_id)) {
                throw new Exception("参数有误");
            }
            $where = [
                'a.id' => $seller_id
            ];
            $seller_info = Db::name('seller_account')->where('id', $seller_id)->find();
            if (!$seller_info) {
                throw new Exception("分销商信息有误");
            }
            unset($seller_info['password']);
            $base_info = Db::name('seller_base_info')->where('seller_id', $seller_id)->find();
            if (!$base_info) {
                $seller_info['base_info'] = [];
            } else {
 
 
                $base_info['main_area'] = $base_info['main_area'] ? array_map('intval', explode(',', $base_info['main_area'])) : [];
                $base_info['main_dis_platform'] = $base_info['main_dis_platform'] ? array_map('intval', explode(',', $base_info['main_dis_platform'])) : [];
                $cate_code = [];
                if ($base_info['country_id']) {
                    $cate_code[] = $base_info['country_id'];
                }
                if ($base_info['province_id']) {
                    $cate_code[] = $base_info['province_id'];
                }
                if ($base_info['city_id']) {
                    $cate_code[] = $base_info['city_id'];
                }
                $base_info['cate_code'] = $cate_code;
 
                $seller_info['base_info'] = $base_info;
            }
            $seller_info['end_time'] = date("Y-m-d", $seller_info['end_time']);
 
            $result = [
                'code'    => 200,
                'message' => "成功",
                'data'    => $seller_info
            ];
        } catch (Exception $exc) {
            $result = [
                'code'    => $exc->getCode(),
                'message' => $exc->getMessage()
            ];
        }
        return $result;
    }
 
    /**
     * 保存分销商帐号
     * @return array
     */
    public function save_seller_info(): array
    {
        try {
            if (!Request::isPost()) {
                throw new Exception("请求方式有误");
            }
            $params = Request::param();
            $phone = trim($params['phone'] ?? '');
            if (!$phone) {
                throw new Exception("请填写分销商手机号");
            }
 
            $seller_id = $params['id'] ?? 0;
            $where = [];
            if ($seller_id) {
                $where[] = ['phone', '=', $phone];
                $where[] = ['id', '<>', $seller_id];
                /////更新/////
                $find = Db::name('seller_account')->where($where)->find();
                if ($find) {
                    throw new Exception("该手机号已被其它分销商注册");
                }
                $seller_info = Db::name('seller_account')->where('id', $seller_id)->find();
                if (!$seller_info) {
                    throw new Exception("分销商账号信息有误");
                }
                $add_data = [
                    'phone' => $phone,
                    'end_time' => strtotime($params['end_time']) + 86400 - 1,
                    'sub_ban_access' => $params['sub_ban_access'] ? 1 : 0,
                ];
                if ($params['password']) {
                    $add_data['password'] = joinmd5($params['password']);
                }
                Db::name('seller_account')->where('id', $seller_id)->update($add_data);
                $jump_url = "";
            } else {
                if (!$params['password']) {
                    throw new Exception("请填写分销商密码");
                }
                /////添加/////
                $where[] = ['phone', '=', trim($phone)];
                $seller_info = Db::name('seller_account')->where($where)->find();
                if ($seller_info) {
                    throw new Exception("该手机号已被其它分销商注册");
                }
                $add_data = [
                    'phone' => $params['phone'],
                    'password' => joinmd5($params['password']),
                    'end_time' => strtotime($params['end_time']) + 86400 - 1,
                    'sub_ban_access' => $params['sub_ban_access'] ? 1 : 0,
                    'start_time' => time(),
                    'add_time' => time()
                ];
                $seller_id = Db::name('seller_account')->insertGetId($add_data);
                if (!$seller_id) {
                    throw new Exception("添加分销商失败");
                }
                $jump_url = url('admin/seller/index');
            }
            $result = [
                'code'     => 200,
                'message'  => "保存成功",
                'jump_url' => $jump_url
            ];
        } catch (Exception $exc) {
            $result = [
                'code'    => $exc->getCode(),
                'message' => $exc->getMessage()
            ];
        }
        return $result;
    }
    /*
     * 获取国家-省份-城市列表
     */
    public function getCountryCodeList()
    {
        if (!Request::isPost()) {
            throw new Exception(lang('request_method_incorrect'));
        }
        try {
 
            $result['list'] = $this->getCountrychildrenids(0);
            return $this->successResponse($result);
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    /*
     * 获取分销平台列表
     */
    public function getDisPlatformList()
    {
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            //            $seller_id = $this->sellerId;
            $condition[] = ['seller_status', '=', 1];
            $order = 'order_id asc,id asc';
            $list = Db::name('dis_platform')->field("id,platform_name")->where($condition)->order($order)->select()->toArray();
            if ($list) {
                $result['list'] = $list;
                return $this->successResponse($result);
            } else {
                throw new Exception(lang('no_data_found'));
            }
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
}