chengkun
2025-08-06 b54e02d98e42ae73071e3c01e59f12671d13d06a
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
<?php
 
namespace app\admin\controller;
 
use think\Exception;
use think\facade\Config;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use \app\admin\validate\SalesPlatform as SalesPlatformValidate;
 
class SalesPlatform extends Common {
    
    private $tableName = 'sales_platform';
    private $viewPath = 'salesPlatform';
    /**
     * 销售平台列表
     * @return string
     */
    public function index() {
        ////////
        if (!Request::isPost()) {
            View::assign('viewPath', $this->viewPath);
            return View::fetch();
        } else {
            try {
                $kw = input('kw');
                if (!empty($kw)) {
                    $condition[] = array('platform_name', 'like', "%$kw%");
                }
                $p = input('p', 1);
                $other['page_size'] = input('page_size', 20);
                $order = 'order_id asc,id asc';
                $count = Db::name($this->tableName)->where($condition)->count();
                $list = Db::name($this->tableName)->field("*")->where($condition)->withAttr('platform_region_id', function ($value) {
//                    $info['goods_purpose'] = json_decode($info['goods_purpose'], TRUE);
                    return json_decode($value, TRUE);
                })->order($order)->page($p, $other['page_size'])->select()->toArray();
                $other['count'] = $count;
                if (!$list) {
                    $list = [];
                }
                $result['other'] = $other;
                $result['list'] = $list;
                return $this->successResponse($result);
            } catch (Exception $exc) {
                return $this->errorResponse($exc->getMessage());
            }
        }
    }
    
    /**
     *保存销售平台
     */
    public function save() {
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        //开始事务
        Db::startTrans();
        try {
            $data = Request::post();
            $validate = new SalesPlatformValidate();
            $check_result = $validate->check($data);
            if (!$check_result) {
                throw new Exception($validate->getError());
            }
            if ($data['platform_region_id']) {
                $data['platform_region_id'] = json_encode(array_map('intval', $data['platform_region_id']));////将数组中的值转为数字类型/////
            }
            $id = input('post.id');
            if (is_numeric($id)) {
                //更新数据
                $update_result = Db::name($this->tableName)->where('id', $id)->withoutField('id')->save($data);
                if ($update_result === FALSE) {
                    return $this->errorResponse(lang('editing_failed'));
                }
                //提交事务
                Db::commit();
                return $this->successResponse('修改成功!');
            } else {
                //添加数据
                unset($data['id']);
                $add_result = Db::name($this->tableName)->insertGetId($data);
                if (!$add_result) {
                    return $this->errorResponse('添加失败!');
                }
                Db::name($this->tableName)->where('id', $add_result)->save(['order_id' => $add_result]);
                //提交事务
                Db::commit();
                return $this->successResponse('添加成功!');
            }
        } catch (Exception $exc) {
            // 回滚事务
            Db::rollback();
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    function updateExt() {
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $postField = 'id,is_show';
            $data = Request::only(explode(',', $postField), 'post');
            if (!$data['id']) {
                throw new Exception(lang('parameter_error'));
            }
            Db::name($this->tableName)->update($data);
            $result['message'] = '操作成功';
            return $this->successResponse($result);
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    public function uploadLogo() {
        
        $logo = input('logo');
        $id = input('id');
        
        $ext_arr = array(
            'image' => array('jpg', 'jpeg', 'png'),
        );
        $maxSizeArr = array(
            'image' => 1024 * 1024 * 10,
        );
        $bannersize = Config::get('app.BANNER_SIZE');
        $width = $bannersize['suolue']['w'];
        $height = $bannersize['suolue']['h'];
        $ratio = $bannersize['suolue']['r'];
        
        $upload = new \common\UploadFile(); // 实例化上传类
        $upload->uploadReplace = TRUE; //同名文件自动覆盖
        $upload->autoSub = TRUE; // 启用子目录保存文件
        $upload->subType = 'date';  // 子目录创建方式 可以使用hash date custom
        $upload->dateFormat = 'Y/m/d'; //定义 subType=date 后,才需要定义dateFormat
        $upload->maxSize = 1024 * 1024 * 0; // 设置附件上传大小 10M
        $upload->maxSizeArr = $maxSizeArr; // 设置附件上传大小 10M
        $upload->allowExts = $ext_arr; // 设置附件上传类型
        $upload->savePath = './static/upload/'; // 设置附件上传目录
        $upload->ratio = $ratio; //1:按比例缩放,2:裁切固定值大小
        
        $upload->thumb = FALSE;   // 使用对上传图片进行缩略图处理
        //$upload->imageClassPath='LIB.Util.Image';    // 图库类包路径 用于生成缩略图
        $upload->thumbMaxWidth = $width; // 缩略图最大宽度
        $upload->thumbMaxHeight = $height; // 缩略图最大高度
        $upload->thumbRemoveOrigin = FALSE; // 是否移除原图  移除原图时,必须给 thumbPrefix指定一个前缀,否则会将原图误删除
        
        
        $backinfo = $upload->uploadmore();
        if (!$backinfo) {// 上传错误提示错误信息
            return $this->errorResponse($upload->getErrorMsg());
        } else {
            $update_info = $upload->getUploadFileInfo();
            $photo = $update_info['savename'];
            if (is_numeric($id) && $id > 0) {
                $info = Db::name($this->tableName)->field('logo')->where('id', $id)->find();
                if($info){
                    if($info['logo']){
                        delpic($info['logo']);
                    }
                    $update_data['logo'] = $photo;
                    Db::name($this->tableName)->where('id', $id)->update($update_data);//更新数据
                    $this->uploadFileToCos($photo);//上传到cos
                }
                else{
                    //数据不存在视为新增数据,删除旧图片
                    if ($logo) {
                        delpic($logo);
                        $this->deleteFileFromCos($logo);//从cos删除文件
                    }
                }
            } else {
                //新增数据,删除旧图片
                if ($logo) {
                    delpic($logo);
                    $this->deleteFileFromCos($logo);//从cos删除文件
                }
            }
            // 上传成功 获取上传文件信息
            //上传可同时上传多个,上传方式为一次上传多个
            $result['logo'] = $photo;
            $result['message'] = lang('successfully_uploaded');
            return $this->successResponse($result);
        }
    }
    
    /**
     * 删除销售平台
     * @return array|int[]
     */
    public function delete() {
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        //开始事务
        Db::startTrans();
        try {
            $id = input('id');
            if (!is_numeric($id)) {
                return $this->errorResponse(lang('parameter_error'));
            }
            $condition['id'] = $id;
            $info = Db::name($this->tableName)->field('id')->where($condition)->find();
            if (!$info) {
                return $this->errorResponse(lang('parameter_error'));
            }
            Db::name($this->tableName)->where('id', $id)->delete();
            //提交事务
            Db::commit();
            return $this->successResponse('删除成功!');
        } catch (Exception $exc) {
            // 回滚事务
            Db::rollback();
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    
    public function getPlatformRegion() {
        ////////
            try {
                if(!Request::isPost()){
                    throw new Exception(lang('request_method_incorrect'));
                }
                $list = Db::name('platform_region')->field("*")->order('order_id,id')->select()->toArray();
                if (!$list) {
                    $list = [];
                }
                $result['list'] = $list;
                return $this->successResponse($result);
            } catch (Exception $exc) {
                return $this->errorResponse($exc->getMessage());
            }
        
    }
    
}