OrderController.php
11.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
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
<?php
/**
* Created by PhpStorm.
* User: 29925
* Date: 2018/7/21
* Time: 14:38
*/
namespace api\portal\controller;
use api\portal\model\AgencyModel;
use api\portal\model\MemberModel;
use api\portal\model\OrderModel;
use api\portal\model\UserNeedModel;
use api\portal\controller\IntermediaryController;
/**
* @title 订单接口
* @description 接口说明
* @group 接口分组
*/
class OrderController extends CommonController
{
/**
* @title 生成订单(需求、中介报价、透视卡)
* @description 接口说明
* @author 开发者
* @url /api/portal/Order/createOrder
* @method POST
* @param name:token type:char require:1 default: other: desc:用户token
* @param name:need_id type:int require:1 default: other: desc:需求(中介报价)ID
* @param name:_type type:int require:1 default: other: desc:订单类型:1,需求;2,中介报价;3,透视卡
*
*/
public function createOrder()
{
if($this->request->isPost()) {
$data = $this->request->param();
$member_model = new MemberModel();
$user_id = $member_model->where(['token'=>$data['token']])->value('id');
$user_need_model = new UserNeedModel();
$agency_model = new AgencyModel();
$order_model = new OrderModel();
if($data['_type'] == 1 || $data['_type'] == 3) {
$info = $user_need_model->where(['id'=>$data['need_id'],'user_id'=>$user_id])->find();
if(empty($info['status'])) {
$this->apiResponse(0,'需求不存在');
}
$need_id = $info['id'];
$money = $info['pay_money'];
}
if($data['_type'] == 2) {
$info = $agency_model->where(['id'=>$data['need_id'],'agency_id'=>$user_id])->find();
$need = $user_need_model->where(['id'=>$info['need_id']])->find();
if($need['status'] != 3) {
$this->apiResponse(0,'该需求当前状态无法进行报价');
}
$need_id = $info['id'];
$money = $info['deposit'];
}
if(($data['_type'] == 1 && $info['status'] == 1) ||
($data['_type'] == 3 && $info['status'] == 3) ||
($data['_type'] == 2 && $info['status'] == 1)) {
$orderInfo = $order_model->where(['type'=>$data['_type'],'need_id'=>$need_id,'user_id'=>$user_id])->find();
if(empty($orderInfo)) {
$order = [
'order_sn' => cmf_get_order_sn(),
'user_id' => $user_id,
'type' => $data['_type'],
'need_id' => $need_id,
'is_get' => 2,
'money' => $money,
'status' => 1,
'create_time' => time()
];
$result = $this->validate($order, 'portal/Order');
if ($result !== true) {
$this->apiResponse(0,$result);
}
$order_model = new OrderModel();
$insert = $order_model->isUpdate(false)->allowField(true)->insertGetId($order);
if(!$insert) {
$this->apiResponse(0,'订单生成失败');
}
} else {
$order = $orderInfo;
}
$this->apiResponse(1,'订单生成成功',$order['order_sn']);
} else {
$this->apiResponse(0,'无法生成订单');
}
}
}
/**
* @title 选择订单支付方式
* @description 接口说明
* @author 开发者
* @url /api/portal/Order/changePayType
* @method POST
* @param name:token type:char require:1 default: other: desc:用户token
* @param name:order_sn type:char require:1 default: other: desc:订单编号
* @param name:_type type:int require:1 default: other: desc:支付类型:1,pc微信;2,手机微信;3,pc支付宝;4,手机支付宝
*
*/
public function changePayType()
{
if($this->request->isPost()) {
$data = $this->request->param();
$member_model = new MemberModel();
$user = $member_model->field(['id,money'])->where(['token'=>$data['token']])->find();
$order_model = new OrderModel();
$orderInfo = $order_model->where(['order_sn'=>$data['order_sn'],'user_id'=>$user['id']])->find();
if(empty($orderInfo['status'])) {
$this->apiResponse(0,'订单不存在');
}
if($orderInfo['status'] == 1) {
$user_need_model = new UserNeedModel();
$agency_model = new AgencyModel();
if($orderInfo['type'] == 1) {
$info = $user_need_model->where(['id'=>$orderInfo['need_id'],'user_id'=>$user['id']])->find();
$sort = '需求保证金支付';
}
if($orderInfo['type'] == 2) {
$info = $agency_model->where(['id'=>$orderInfo['need_id'],'user_id'=>$user['id']])->find();
$need = $user_need_model->where(['id'=>$info['need_id'],'user_id'=>$user['id']])->find();
if($need['status'] != 3) {
$this->apiResponse(0,'该需求当前状态无法进行报价');
}
$sort = '中介报价保证金支付';
}
if($orderInfo['type'] == 3) {
$info = $user_need_model->where(['id'=>$orderInfo['need_id'],'user_id'=>$user['id']])->find();
$sort = '透视卡支付';
}
if(($orderInfo['type'] == 1 && $info['status'] == 1) ||
($orderInfo['type'] == 3 && $info['status'] == 3) ||
($orderInfo['type'] == 2 && $info['status'] == 1)) {
// if($data['_type'] == 5) {
// if($user['money'] < $orderInfo['money']) {
// $this->apiResponse(0,'余额不足');
// } else {
// $order_model->startTrans();
// $where = [
// 'order_sn' => $data['order_sn']
// ];
// $update = [
// 'status' => 2,
// 'pay_type' => 5,
// 'pay_time' => time()
// ];
// if($orderInfo['type'] == 1) {
// $pay = $order_model->where($where)->update($update);
// $status = $user_need_model->where()->update(['status'=>2,'pay_time'=>time()]);
// }
// if($orderInfo['type'] == 2) {
// $pay = $order_model->where($where)->update($update);
// $status = $agency_model->where()->update(['status'=>2,'pay_time'=>time()]);
// }
// if($orderInfo['type'] == 3) {
// $Intermediary = controller('Intermediary','controller');
// $agencyOffer = $Intermediary->agencyOffer($user['id'],$info['id']);
// $update['lower_price'] = $agencyOffer['money'];
// $pay = $order_model->where($where)->update($update);
// $status = 1;
// }
// if(!$pay || !$status) {
// $order_model->rollback();
// $this->apiResponse(0,'支付失败');
// }
// $order_model->commit();
// $this->apiResponse(5,'支付成功');
// }
// }
if($data['_type'] == 1) {
$return = $this->wxpay(['order_sn'=>$data['order_sn']],$sort);
$qrcode_url = url('portal/Qrcode/qrcode',['data'=>urlencode($return['code_url'])],true,true);
$this->apiResponse(1,'微信支付',['type'=>1,'url'=>$qrcode_url]);
} elseif($data['_type'] == 3) {
$alipay_url = url('portal/Alipay/alipay',['order_sn'=>$data['order_sn'],'name'=>$sort,'price'=>0.01],true,true);
$this->apiResponse(1,'支付宝支付',['type'=>3,'url'=>$alipay_url]);
}
} else {
$this->apiResponse(0,'该订单已失效');
}
} else {
$this->apiResponse(0,'该订单已支付');
}
}
}
/**
* @title 支付页面订单金额查询
* @description 接口说明
* @author 开发者
* @url /api/portal/Order/getOrderPrice
* @method POST
* @param name:token type:char require:1 default: other: desc:用户token
* @param name:order_sn type:char require:1 default: other: desc:订单编号
*
*/
public function getOrderPrice()
{
if($this->request->isPost()) {
$data = $this->request->param();
$member_model = new MemberModel();
$user = $member_model->field(['id,money'])->where(['token'=>$data['token']])->find();
$order_model = new OrderModel();
$orderInfo = $order_model->where(['order_sn'=>$data['order_sn'],'user_id'=>$user['id']])->find();
if(empty($orderInfo['status'])) {
$this->apiResponse(0,'订单不存在');
}
if($orderInfo['status'] == 2) {
$this->apiResponse(0,'订单已付款');
}
if($orderInfo['status'] == 4) {
$this->apiResponse(0,'订单已退款');
}
$this->apiResponse(1,'成功',$orderInfo['money']);
}
}
/**
* @title 支付页面检测微信支付是否完成
* @description 接口说明
* @author 开发者
* @url /api/portal/Order/checkWechatWebPay
* @method POST
* @param name:token type:char require:1 default: other: desc:用户token
* @param name:order_sn type:char require:1 default: other: desc:订单编号
*
*/
public function checkWechatWebPay() {
if($this->request->isPost()) {
$data = $this->request->param();
$member_model = new MemberModel();
$user = $member_model->field(['id,money'])->where(['token'=>$data['token']])->find();
$order_sn = $data['order_sn'];
$order_model = new OrderModel();
$status = $order_model->where(['order_sn'=>$order_sn])->value('status');
if(empty($status)) {
$this->apiResponse(0,'订单不存在');
}
if($status == 1 || $status == 9) {
$this->apiResponse(0,'未支付成功');
}
if($status == 2) {
$this->apiResponse(1,'支付成功');
}
}
}
}