OrderController.php
6.1 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
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
<?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 wxapp\pay\WeixinPay;
/**
* @title 订单
* @description
*/
class OrderController extends RestBaseController
{
public function _initialize()
{
$user_id = $this->getUserId();
$data = Db::name('integral')
->where('user_id',$user_id)
->find();
if(empty($data)){
$this->error(['code'=>40005,'msg'=>'请先注册']);
}
}
/**
* @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();
$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('非法操作');
}
$money = Db::name('pay')
->where('id',$pay_id)
->find();
if($money['money'] != $total){
$this->success('请正确输入充值金额');
}
$arr['num'] = cmf_get_order_sn();
$arr['user_id'] = $user_id;
$arr['pay_id'] = $pay_id;
$arr['total'] = $total;
$arr['create_time'] = time();
$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'=>'数据错误,没有此订单']);
}
if($data['status'] == 2){
$this->error('您已经支付过了,无需重复支付');
}
//微信支付
$openid = $this->getOpenid();
$pay = new WeixinPay();
$this->success('SUCCESS',$pay->pay($openid['openid'],$data['num'],"废品回收",$data['total'],cmf_api_url('index/pay/notify','','',true)));
}
/**
* @title 提现
* @author Guosheng
* @url /index/order/deposit
* @method GET
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:money_id type:int require:1 other: desc:提现id
* @param name:total type:int require:1 other: desc:提现金额
*
*/
public function deposit()
{
$user_id = $this->getUserId();
$total = $this->request->param('total');
$money_id = $this->request->param('money_id');
if(empty($total) || empty($money_id)){
$this->error(['code'=>40005,'msg'=>'缺少必要参数']);
}
$info = Db::name('money')
->where('id',$money_id)
->find();
$list = Db::name('integral')
->where('user_id',$user_id)
->find();
if($info['money'] != $total){
$this->error('提现金额有误');
}
if($list['now_integral'] < $info['integral']){
$this->error('您的积分不足');
}
//创建支付对象
$data['openid'] = $this->getOpenid();
$data['total'] = $total;
$pay = new PayController();
$withdraw = $pay->tixian($data);
if($withdraw['return_code'] == 'SUCCESS') {
if ($withdraw['result_code'] == 'SUCCESS') {
// 提现成功
$this->success('success123');
}else{
$this->error($withdraw['err_code_des']);
}
}else{
$this->error($withdraw['return_msg']);
}
}
/**
* @title 积分明细
* @description
* @author GuoSheng
* @url /index/Order/detail
* @method GET
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:page type:int require:0 other: desc:当前页(默认1)
* @param name:pageNum type:int require:0 other: desc:每页显示数据个数(默认10)
*
* @return type:类型(1充值,2兑换商品,3获得奖励,4提现,5回收商品所得,6家政预约扣除,7取消兑换商品,8取消家政预约)
* @return total: 变动的积分
* @return create_time: 创建时间
*/
public function detail()
{
$user_id = $this->getUserId();
$page = $this->request->param('page',1,'intval');
$pageNum = $this->request->param('pageNum',10,'intval');
$list = Db::name('detail')
->where('user_id',$user_id)
->field('id,user_id,type,total,create_time')
->page($page,$pageNum)
->select()
->toArray();
foreach ($list as &$v){
$v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
}
$this->success('SUCCESS',$list);
}
public function getOpenid()
{
$user_id = $this->getUserId();
$openid = Db::name('third_party_user')
->where('user_id',$user_id)
->field('openid')
->find();
return $openid;
}
}