bin.shen
2016-12-05 a4c9331bbfe3e8765ccdc1c54cc6931bac49cc82
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
/**
 * 后台画面控制器
 *
 * @package        app
 * @subpackage    core
 * @category    controller
 * @author        yaobin<645894453@qq.com>
 *
 */
class Manage extends CI_Controller {
 
    public function __construct()
    {
        parent::__construct();
        ini_set('date.timezone','Asia/Shanghai');
        $this->load->model('manage_model');
        $this->load->library('image_lib');
        $this->load->helper('directory');
    }
 
    function _remap($method,$params = array())
    {
        if (!$this->session->userdata('username')) {
            if ($this->input->is_ajax_request()) {
                header('Content-type: text/json');
                echo '{
                        "statusCode":"301",
                        "message":"\u4f1a\u8bdd\u8d85\u65f6\uff0c\u8bf7\u91cd\u65b0\u767b\u5f55\u3002"
                    }';
            } else {
                redirect(site_url('manage_login/login'));
            }
 
        } else {
            return call_user_func_array(array($this, $method), $params);
        }
    }
 
    public function index()
    {
        $this->load->view('manage/index.php');
    }
 
    //===============================================public start==========================================//
    public function upload_pic(){
        $path = './././uploadfiles/others/';
        $path_out = '/uploadfiles/others/';
        $msg = '';
 
        //设置原图限制
        $config['upload_path'] = $path;
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '1000';
        $config['encrypt_name'] = true;
        $this->load->library('upload', $config);
 
        if($this->upload->do_upload('filedata')){
            $data = $this->upload->data();
            $targetPath = $path_out.$data['file_name'];
            $msg="{'url':'".$targetPath."','localname':'','id':'1'}";
            $err = '';
        }else{
            $err = $this->upload->display_errors();
        }
        echo "{'err':'".$err."','msg':".$msg."}";
    }
 
 
    //===============================================public end==========================================//
 
    public function list_product(){
        $data = $this->manage_model->list_product();
        $this->load->view('manage/list_product.php',$data);
    }
 
    public function add_product(){
        $this->load->view('manage/add_product.php');
    }
 
    public function edit_product($id){
        $data = $this->manage_model->get_product($id);
        $this->load->view('manage/add_product.php',$data);
    }
 
    public function save_product(){
        if($_FILES["userfile"]['name'] and $this->input->post('old_img')){//修改上传的图片,需要先删除原来的图片
            @unlink('./././uploadfiles/products/'.$this->input->post('old_img'));//del old img
        }else if(!$_FILES["userfile"]['name'] and !$this->input->post('old_img')){//未上传图片
            form_submit_json("300", "请添加图片");exit;
        }
 
        if(!$_FILES["userfile"]['name'] and $this->input->post('old_img')){//不修改图片信息
//            $data = $this->input->post();
//            unset($data['ajax']);
//            unset($data['old_img']);
            $data = array(
                'name'=>$this->input->post('name'),
                'cdate'=>date('Y-m-d H:i:s'),
                'desc'=>$this->input->post('desc'),
                'status'=>$this->input->post('status'),
                'recommend'=>$this->input->post('recommend'),
            );
 
            $rs = $this->manage_model->save_product($data);
        }else{
            $config['upload_path'] = './././uploadfiles/products';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $config['max_size'] = '1000';
            $config['encrypt_name'] = true;
            $this->load->library('upload', $config);
            if($this->upload->do_upload()){
                $img_info = $this->upload->data();
                $data = array(
                    'name'=>$this->input->post('name'),
                    'cdate'=>date('Y-m-d H:i:s'),
                    'desc'=>$this->input->post('desc'),
                    'status'=>$this->input->post('status'),
                    'recommend'=>$this->input->post('recommend'),
                    'bg_pic'=> $img_info['file_name']
                );
                $rs = $this->manage_model->save_product($data);
            }else{
                form_submit_json("300", $this->upload->display_errors('<b>','</b>'));
                exit;
            }
        }
 
 
//        $rs = $this->manage_model->save_product();
        if ($rs === 1) {
            form_submit_json("200", "操作成功", "list_product");
        } else {
            form_submit_json("300", $rs);
        }
    }
 
//
//    /**
//     * 树状结构菜单
//     **/
//    public function subtree($arr,$id=0,$lev=1)
//    {
//        static $subs = array();
//        foreach($arr as $v){
//            if((int)$v['parent_id']==$id){
//                $v['lev'] = $lev;
//                $subs[]=$v;
//                $this->subtree($arr,$v['id'],$lev+1);
//            }
//        }
//        return $subs;
//    }
 
    public function list_order(){
        $data = $this->manage_model->list_order();
        $this->load->view('manage/list_order.php',$data);
    }
 
    public function edit_order($id){
 
        $data = $this->manage_model->get_order($id);
        $data['express'] = '';
 
        if($data['head']->express_num){
            $express = $this->getorder($data['head']->express,$data['head']->express_num);
            if(isset($express['data'])){
                $data['express'] = $express['data'];
            }else{
                $data['express'] = array(array('context'=>'暂无快递信息','time'=>date('Y-m-d H:i:s',time())));
            }
        }
        $this->load->view('manage/edit_order.php',$data);
    }
 
    public function save_order(){
        $rs = $this->manage_model->save_order();
        if ($rs === 1) {
            form_submit_json("200", "操作成功", "list_order");
        } else {
            form_submit_json("300", $rs);
        }
    }
 
    public function fahuo($id){
        $data = $this->manage_model->get_order($id);
        $data['express'] = $this->manage_model->get_express();
        $data['id'] = $id;
        $this->load->view('manage/fahuo_dialog.php',$data);
    }
 
    public function save_fahuo() {
        $ret = $this->manage_model->save_fahuo();
        if($ret == 1){
            form_submit_json("200", "操作成功", 'edit_order');
        } else {
            form_submit_json("300", "保存失败");
        }
    }
 
 
    /*
     * 采集网页内容的方法
     */
    private function getcontent($url){
        if(function_exists("file_get_contents")){
            $file_contents = file_get_contents($url);
        }else{
            $ch = curl_init();
            $timeout = 5;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $file_contents = curl_exec($ch);
            curl_close($ch);
        }
        return $file_contents;
    }
 
    /*
     * 解析object成数组的方法
     * @param $json 输入的object数组
     * return $data 数组
     */
    private function json_array($json){
        if($json){
            foreach ((array)$json as $k=>$v){
                $data[$k] = !is_string($v)?$this->json_array($v):$v;
            }
            return $data;
        }
    }
 
    /*
     * 返回$data array      快递数组
     * @param $name         快递名称
     * 支持输入的快递名称如下
     * (申通-EMS-顺丰-圆通-中通-如风达-韵达-天天-汇通-全峰-德邦-宅急送-安信达-包裹平邮-邦送物流
     * DHL快递-大田物流-德邦物流-EMS国内-EMS国际-E邮宝-凡客配送-国通快递-挂号信-共速达-国际小包
     * 汇通快递-华宇物流-汇强快递-佳吉快运-佳怡物流-加拿大邮政-快捷速递-龙邦速递-联邦快递-联昊通
     * 能达速递-如风达-瑞典邮政-全一快递-全峰快递-全日通-申通快递-顺丰快递-速尔快递-TNT快递-天天快递
     * 天地华宇-UPS快递-新邦物流-新蛋物流-香港邮政-圆通快递-韵达快递-邮政包裹-优速快递-中通快递)
     * 中铁快运-宅急送-中邮物流
     * @param $order        快递的单号
     * $data['ischeck'] ==1   已经签收
     * $data['data']        快递实时查询的状态 array
     */
    public  function getorder($name,$order){
        $result = $this->getcontent("http://www.kuaidi100.com/query?type={$name}&postid={$order}");
        $result = json_decode($result);
        $data = $this->json_array($result);
        return $data;
    }
 
    //申请退款
    public function refund($id="")
    {
        if ($id == "") {
            //方便我手动调用退单
            $id = $this->uri->segment(3);
        }
        if (isset($id) && $id != "") {
            //1、取消订单可以退款。2、失败订单可以退款
            $pub = $this->manage_model->order_info($id);
            if ($pub['main']!=1 and $pub['main']['status'] == 5) {
                $listno = $pub['main']['num'];
                $fee = 0;
                if($pub['detail']==1){
 
                }
                foreach($pub['detail'] as $key => $value){
                    $fee += $value['price'] * $value['qty'];
                }
                $this->load->config('wxpay_config');
                $wxconfig['appid'] = $this->config->item('appid');
                $wxconfig['mch_id'] = $this->config->item('mch_id');
                $wxconfig['apikey'] = $this->config->item('apikey');
                $wxconfig['appsecret'] = $this->config->item('appsecret');
                $wxconfig['sslcertPath'] = $this->config->item('sslcertPath');
                $wxconfig['sslkeyPath'] = $this->config->item('sslkeyPath');
                $this->load->library('Wechatpay', $wxconfig);
 
                if (isset($listno) && $listno != "") {
                    $out_trade_no = $listno;
                    $total_fee = $fee * 100;
                    $refund_fee = $fee * 100;
                    //自定义商户退单号
                    $out_refund_no = $wxconfig['mch_id'] . date("YmdHis");
                    $result = $this->wechatpay->refund($out_trade_no, $out_refund_no, $total_fee, $refund_fee, $wxconfig['mch_id']);
 
                    //log::DEBUG(json_encode($result));
                    if (isset($result["return_code"]) && $result["return_code"] = "SUCCESS" && isset($result["result_code"]) && $result["result_code"] = "SUCCESS") {
                        $this->manage_model->update_order($id);
                        form_submit_json("200", "操作成功", 'edit_order');
                    }else{
                        form_submit_json("300", "退款失败");
                    }
                    //佣金状态更改为已退款
 
                    //form_submit_json("200", "操作成功", 'edit_order');
                }
            }
        }
 
    }
 
    public function list_user(){
        $data = $this->manage_model->list_user();
        $this->load->view('manage/list_user.php',$data);
    }
 
    public function edit_user($id){
 
        $data = $this->manage_model->get_user($id);
        $data['id']=$id;
        $this->load->view('manage/edit_user.php',$data);
    }
 
    /**
     *
     * ***************************************以下为联系电话*******************************************************************
     */
 
    public function list_tel()
    {
        $data = $this->manage_model->list_tel();
        $this->load->view('manage/list_tel.php',$data);
    }
 
    public function save_tel(){
        $rs = $this->manage_model->save_tel();
        if ($rs === 1) {
            form_submit_json("200", "操作成功", "list_tel");
        } else {
            form_submit_json("300", $rs);
        }
    }
 
    public function edit_tel($id){
        $data = $this->manage_model->get_tel($id);
        $this->load->view('manage/add_tel.php',$data);
    }
 
    public function list_withdraw(){
        $data = $this->manage_model->list_withdraw();
        $this->load->view('manage/list_withdraw.php',$data);
    }
 
 
    public function save_withdraw(){
        $rs = $this->manage_model->save_withdraw();
        if ($rs === 1) {
            form_submit_json("200", "操作成功", "list_withdraw");
        } else {
            form_submit_json("300", $rs);
        }
    }
 
    public function edit_withdraw($id){
        $data = $this->manage_model->get_withdraw($id);
        $this->load->view('manage/edit_withdraw.php',$data);
    }
 
    public function remark($id){
        $data = $this->manage_model->get_order($id);
        $this->load->view('manage/remark_dialog.php',$data['head']);
    }
 
    public function save_remark() {
        $ret = $this->manage_model->save_remark();
        if($ret == 1){
            form_submit_json("200", "操作成功", 'edit_order');
        } else {
            form_submit_json("300", "保存失败");
        }
    }
 
    public function list_fund(){
        $data = $this->manage_model->list_fund();
        $this->load->view('manage/list_fund.php',$data);
    }
 
    public function list_score(){
        $data = $this->manage_model->list_score();
        $this->load->view('manage/list_score.php',$data);
    }
 
    public function close_about($id){
        $rs = $this->manage_model->close_about($id);
        if ($rs === 1) {
            form_submit_json("200", "操作成功", "list_order", "", "");
        } else if($rs === -2){
            form_submit_json("300", '只有待发货状态才能关闭');
        }
 
        else {
            form_submit_json("300", $rs);
        }
    }
 
    public function export_dialog(){
        $this->load->view('manage/export_dialog.php');
    }
 
    public function export_order(){
 
        $rs = $this->manage_model->get_export_data();
 
        // 载入类库
        $this->load->library('PHPExcel');
        $this->load->library('PHPExcel/IOFactory');
 
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("export")->setDescription("none");
 
        $objPHPExcel->setActiveSheetIndex(0);
 
        // 第一行为字段名
        $fields_name = array('下单日期','付款日期','发货日期','订单号','物流单号','物流公司','商品名称','数量','单价','总金额','收货人','手机号码','收货地址');
        $col = 0;
        foreach ($fields_name as $field)
        {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
            $col++;
        }
 
        $fields = array('cdate','pdate','sdate','num','express_num','express_name','pro_name','qty','price','total','name','phone','address');
        // 数据内容
        $row = 2;
        foreach($rs as $data)
        {
            $col = 0;
            foreach ($fields as $field)
            {
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);
                $col++;
            }
 
            $row++;
        }
 
        $objPHPExcel->setActiveSheetIndex(0);
 
        $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
 
        // 发送下载
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="report_to_excel_'.date('Ymd').'.xls"');
        header('Cache-Control: max-age=0');
 
        $objWriter->save('php://output');
 
//        form_submit_json("200", "导出成功", 'list_order');
    }
 
}