OrderController.php
3.0 KB
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
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/11/14
* Time: 10:37
*/
namespace api\index\controller;
use api\index\model\OrderModel;
use cmf\controller\RestBaseController;
use think\Db;
use think\Validate;
/**
* @title 订单
* @description
*/
class OrderController extends RestBaseController
{
/**
* @title 创建订单
* @description
* @author GuoSheng
* @url /index/Order/createOrder
* @method GET
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:pay_id type:int require:1 other: desc:充值ID
* @param name:total type:int require:1 other: desc:售价
*
* @return order_id:订单id
*/
public function createOrder(){
$user_id = $this->getUserId();
$data = Db::name('integral')
->where('user_id',$user_id)
->find();
if(empty($data)){
$this->error('请先注册!');
}
$total = $this->request->param('total');
$pay_id = $this->request->param('pay_id');
if(empty($total)){
$this->error(['code'=>40005,'msg'=>'缺少必要参数']);
}
if(empty($pay_id)){
$this->error(['code'=>40005,'msg'=>'缺少必要参数']);
}
if($total<=0){
$this->error('非法操作');
}
$arr['num'] = cmf_get_order_sn();
$arr['user_id'] = $user_id;
$arr['pay_id'] = $pay_id;
$arr['total'] = $total;
$arr['create_time'] = time();
$arr['status'] = 1;
$orderModel = new OrderModel();
$result = $orderModel->order_add($arr);
if(empty($result)){
$this->error(['code'=>40006,'msg'=>'sql执行失败']);
}
$this->success('SUCCESS',['order_id'=>$result]);
}
/**
* @title 支付
* @author Guosheng
* @url /index/order/pay
* @method GET
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:order_id type:int require:1 other: desc:订单id
*/
public function pay(){
$user_id = $this->getUserId();
$order_id = $this->request->param('order_id',0,'intval');
if(empty($order_id)){
$this->error(['code'=>40005,'msg'=>'缺少必要参数']);
}
$where['id'] = ['eq',$order_id];
$where['user_id'] = ['eq',$user_id];
$data = Db::name('order')->where($where)->find();
if(empty($data)){
$this->error(['code'=>41001,'msg'=>'缺少必要参数']);
}
//微信支付
$openid = $this->getOpenid();
$pay = new \WeixinPay();
$this->success('SUCCESS',$pay->pay($openid['openid'],$data['num'],"废品回收",$data['total'],cmf_api_url('index/pay/notify','','',true)));
}
public function getOpenid()
{
$user_id = $this->getUserId();
$openid = Db::name('third_party_user')
->where('user_id',$user_id)
->field('openid')
->find();
return $openid;
}
}