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
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: yaobin
 * Date: 2016/4/29
 * Time: 22:03
 */
class Product extends MY_Controller
{
 
    public function __construct()
    {
        parent::__construct();
        $this->load->library('CI_Wechat');
        $this->load->model('product_model');
        $this->buildWxData();
        $userID = $this->product_model->get_userID($this->session->userdata('openid'));
        $this->cismarty->assign('userID',$userID);
    }
 
    function index($id){
        $data = $this->product_model->get_product_info($id);
        $this->cismarty->assign('data',$data);
        $this->cismarty->display('index.html');
    }
    
    function product_main($flag=1){
        $data=$this->product_model->get_product_main($flag);
        $this->cismarty->assign('data',$data);
        $this->cismarty->display('product.html');
    }
    
    function product_list($id,$flag=1){
        $data=$this->product_model->get_product_list($id,$flag);
        $this->cismarty->assign('data',$data);
        $this->cismarty->display('product_list.html');
    }
 
    function product_info($id,$html_flag=-1){
        $data=$this->product_model->get_product_info($id);
        $this->cismarty->assign('html_flag',$html_flag);
        $this->cismarty->assign('data',$data);
        $this->cismarty->display('product_info.html');
    }
 
 
    function save_address(){
        $rs = $this->product_model->save_address();
        echo $rs;
    }
 
    function edit_address($id){
        $data = $this->product_model->get_address($id);
        $this->cismarty->assign('data',$data);
        $this->cismarty->display('edit_address.html');
    }
 
    function delete_address($id){
         $this->product_model->delete_address($id);
         redirect('product/my_address');
    }
 
    /** 这里保存订单信息 */
    function save_order(){
        $order_id = $this->product_model->save_order();
        if($order_id){
            redirect(site_url('product/pay').'/'.$order_id);
        }
    }
 
    /** 这里显示订单信息 */
    function show_order(){
        $data=$this->product_model->show_order();
        $tel = $this->product_model->get_tel();
        $this->cismarty->assign('data',$data);
        $this->cismarty->assign('tel',$tel);
        die($tel);
        $this->cismarty->display('my_order.html');
    }
 
    /** 这里显示订单详情 */
    function order_info($id){
        $data = $this->product_model->order_info($id);
        $data['express'] = '';
        if($data['main']['express']){
            $result = file_get_contents("http://www.kuaidi100.com/query?type={$data['main']['express']}&postid={$data['main']['express_num']}");
            $result = json_decode($result);
            $data['express'] = $this->json_array($result);
        }
        $this->cismarty->assign('data',$data);
        $this->cismarty->display('order_info.html');
    }
 
    /*
     * 解析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;
        }
    }
 
 
    /** 这里显示各种状态的订单 */
    function my_order(){
        $data=$this->product_model->show_order();
 
        $phone = $this->product_model->get_telphone();
        $this->cismarty->assign('telPhone',$phone);
        $this->cismarty->assign('data',$data);
        $this->cismarty->display('my_order.html');
    }
 
    function my_address(){
        $data=$this->product_model->my_address();
        $this->cismarty->assign('data',$data);
        $this->cismarty->display('my_address.html');
    }
 
    function feedback($id,$status=null){
        $data = $this->product_model->product_ids($id);
        $this->cismarty->assign('data',$data);
        $this->cismarty->assign('id',$id);
        $this->cismarty->assign('status',$status);
        $this->cismarty->display('feedback.html');
    }
 
    function save_feedback($status=null){
        $this->product_model->save_feedback();
        redirect('product/status_order/'.$status);
    }
 
    function delete_order($id,$status=null){
        $this->product_model->delete_order($id);
        if($status==1){
            redirect('product/status_order/1');
        }else{
            redirect('product/status_order/4');
        }
    }
 
    function show_express($id,$status=null){
        $data = $this->product_model->show_express($id);
        //die(var_dump($data));
        if($data['head']!=1){
            $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->cismarty->assign('status',$status);
        $this->cismarty->assign('data',$data);
        $this->cismarty->display('express_info.html');
    }
 
 
    function buy_pro($pid,$pd_id,$qty){
        $data = $this->product_model->get_pro_info($pid,$pd_id);
        $rs = $this->product_model->get_allow_fund_score();
        $score = $rs['allow_score'];
        $this->cismarty->assign('score',$score);
        $this->cismarty->assign('pid',$pid);
        $this->cismarty->assign('pd_id',$pd_id);
        $this->cismarty->assign('qty',$qty);
        $this->cismarty->assign('data',$data);
        $this->cismarty->display('order_confirm.html');
    }
 
    public function pay($order_id){
 
        //微信支付配置的参数配置读取
        $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);
        //商户交易单号
        $order_data = $this->product_model->order_info($order_id);
        $out_trade_no = $order_data['main']['num'];
        $total_fee = 0;
        $body_info = "";
        $body_detail = 1;
 
        foreach($order_data['detail'] as $key => $value){
            $total_fee += $value['price'] * $value['qty'];
            if($key == 0){
                $body_info = $value['pro_name'].$value['de_size'];
            }else{
                $body_detail += 1;
            }
        }
        $total_fee = round($total_fee - $order_data['main']['score'],2);
        if($body_detail==1){
            $param['body'] = $body_info;
        }else{
            $param['body'] = $body_info.'等'.$body_detail.'个商品';
        }
 
        if($total_fee == 0){
            $this->product_model->confirm_order($out_trade_no,$this->session->userdata('openid'));
            redirect(site_url('product/my_order'));
        }
 
//        $param['attach'] = '0012';
        $param['detail'] = "摩瑞尔电商平台-".$out_trade_no;
        $param['out_trade_no'] = $out_trade_no;
        $param['total_fee'] = $total_fee * 100;
        $param["spbill_create_ip"] = $_SERVER['REMOTE_ADDR'];
        $param["time_start"] = date("YmdHis");
        $param["time_expire"] = date("YmdHis", time() + 600);
        $param["goods_tag"] = "摩瑞尔电商平台";
        $param["notify_url"] = base_url()."/ajax/notify";
        $param["trade_type"] = "JSAPI";
        $param["openid"] = $this->session->userdata('openid');
 
        //统一下单,获取结果,结果是为了构造jsapi调用微信支付组件所需参数
        $result = $this->wechatpay->unifiedOrder($param);
//        var_dump($result);die;
 
        //如果结果是成功的我们才能构造所需参数,首要判断预支付id
 
        if (isset($result["prepay_id"]) && !empty($result["prepay_id"])) {
            //调用支付类里的get_package方法,得到构造的参数
            $data['parameters'] = json_encode($this->wechatpay->get_package($result['prepay_id']));
            $data['notifyurl'] = $param["notify_url"];
            $data['fee'] = $total_fee;
            $data['productList'] = $order_data['detail'];
            $data['pubid'] = $out_trade_no;
            $data['orderid'] = $order_id;
            $this->load->view('pay', $data);
        }
//        $this->load->view('pay', $data);
    }
 
//    function notify($order_num)
//    {
//        if ($this->session->userdata('openid')) {
//            $rs = $this->product_model->confirm_order($order_num);
//            echo $rs;
//        }
//    }
 
 
 
    public function user_center(){
        $data = $this->product_model->get_user_info();
        $this->cismarty->assign('data',$data);
        $this->cismarty->assign('headimgurl',$this->session->userdata('headimgurl'));
        $this->cismarty->assign('nickname',$this->session->userdata('nickname'));
        $this->cismarty->display('user_center.html');
    }
 
    public function explain(){
        $this->cismarty->display('explain.html');
    }
 
    public function fund_detail($page = 1){
        $data = $this->product_model->fund_detail($page);
        $this->cismarty->assign('data',$data);
        $this->cismarty->display('fund_detail.html');
    }
 
    public function fund_detail_ajax($page){
        $data = $this->product_model->fund_detail($page);
        if($data['res_list']){
            echo json_encode($data);
        }else{
            echo 1;
        }
    }
 
    public function score_detail($page = 1){
        $data = $this->product_model->score_detail($page);
        $this->cismarty->assign('data',$data);
        $this->cismarty->display('score_detail.html');
    }
 
    public function score_detail_ajax($page){
        $data = $this->product_model->score_detail($page);
        if($data['res_list']){
            echo json_encode($data);
        }else{
            echo 1;
        }
    }
 
    public function withdraw(){
        $surplus = $this->product_model->get_allow_fund_score();
        $this->cismarty->assign('surplus',$surplus['allow_fund']);
        $this->cismarty->display('withdraw.html');
    }
 
    public function save_withdraw(){
        $rs = $this->product_model->save_withdraw();
        if($rs == 1){
            $this->cismarty->assign('cdate',date("y-m-d H:i:s",time()));
            $this->cismarty->assign('fund',$this->input->post('money'));
            $this->cismarty->display('withdraw_success.html');
        }else{
            redirect(site_url('product/user_center'));
        }
    }
 
    public function del_order($id){
        $this->product_model->del_order($id);
        redirect(site_url('product/my_order'));
    }
 
    public function confirm_receive($id){
        $this->product_model->confirm_receive($id);
        redirect(site_url('product/my_order'));
    }
}