OrderController.php
14.8 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
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
<?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;
use think\Db;
/**
* @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']])->find();
if(empty($info['status'])) {
$this->apiResponse(0,'需求不存在');
}
$need_id = $info['id'];
$money = $info['pay_money'];
if($data['_type'] == 3) {
$money = Db::name('else')->where(['id'=>1])->value('money');
}
}
if($data['_type'] == 2) {
$info = $agency_model->where(['need_id'=>$data['need_id'],'agency_id'=>$user_id])->order(['create_time'=>'DESC'])->find();
if(!$info) {
$this->apiResponse(0,'未提交过该需求的报价');
}
$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 {
if($orderInfo['status'] == 2 || $orderInfo['status'] == 4) {
$this->apiResponse(0,'订单已完成');
}
$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']])->find();
$sort = '需求保证金支付';
}
if($orderInfo['type'] == 2) {
$info = $agency_model->where(['id'=>$orderInfo['need_id']])->find();
$need = $user_need_model->where(['id'=>$info['need_id']])->find();
if($need['status'] != 3) {
$this->apiResponse(0,'该需求当前状态无法进行报价');
}
$sort = '中介报价保证金支付';
}
if($orderInfo['type'] == 3) {
$info = $user_need_model->where(['id'=>$orderInfo['need_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'] == 1) {
$return = $this->wxpay(['order_sn'=>$data['order_sn']],$sort);
if(empty($return['code_url'])) {
$this->apiResponse(0,$return['return_msg']);
} else {
$code_url = $return['code_url'];
}
$qrcode_url = url('portal/Qrcode/qrcode',['data'=>urlencode($code_url)],true,true);
$this->apiResponse(1,'微信支付',['type'=>1,'url'=>$qrcode_url]);
} elseif($data['_type'] == 2) {
$type = [
1 => '保证金支付',
2 => '中介报价',
3 => '透视卡'
];
require_once VENDOR_PATH.'WxpayAPI/WxpayH5.php';
$scene_info = "{'h5_info': {'type':'Wap','wap_url': 'http://houseprice.w.bronet.cn','wap_name': ".$sort."}}";
$h5 = new \WxpayH5(config('wx_appid'),config('wx_mch_id'),config('wx_key'),$data['order_sn'],
$type[$orderInfo['type']],'1',$scene_info);
$result = $h5->pay();
if(empty($result['mweb_url'])) {
$this->apiResponse(0,'生成支付失败');
}
// $return = [
// 'mwen_url' => $result['mweb_url'],
// 'create_time' => time(),
// 'over_time'=>time()+5*60
// ];
$this->apiResponse(1,'微信H5支付',['type'=>2,'url'=>$result['mweb_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]);
} elseif($data['_type'] == 4) {
$alipay_url = url('portal/Alipaywap/alipay',['order_sn'=>$data['order_sn'],'name'=>$sort,'price'=>0.01],true,true);
$this->apiResponse(1,'支付宝手机支付',['type'=>4,'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,'支付成功');
}
}
}
/**
* @title 根据订单号查询订单状态,支付结果页面显示不同内容
* @description 接口说明
* @author 开发者
* @url /api/portal/Order/getOrderType
* @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 getOrderType() {
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();
$orderInfo = $order_model->where(['order_sn'=>$order_sn,'user_id'=>$user['id'],'status'=>2])->find();
if(empty($orderInfo)) {
$this->apiResponse(0,'订单未支付');
}
$name = '';
if($orderInfo['type'] == 1) {
$name = '需求押金缴纳成功';
}
if($orderInfo['type'] == 2) {
$name = '中介押金缴纳成功';
}
if($orderInfo['type'] == 3) {
$name = '透视卡支付成功';
}
$this->apiResponse(1,'成功', ['type'=>$name,'param'=>$orderInfo['need_id'],'sort'=>$orderInfo['type']]);
}
}
/**
* @title 根据订单号查询订单信息(购买透视卡查询到的最低价)
* @description 接口说明
* @author 开发者
* @url /api/portal/Order/getOrderInfo
* @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
*
*/
public function getOrderInfo($token,$need_id) {
// if($this->request->isPost()) {
$member_model = new MemberModel();
$user = $member_model->field(['id,money'])->where(['token'=>$token])->find();
$order_model = new OrderModel();
$array = $order_model->field('pay_time,lower_price')->where(['need_id'=>$need_id,'user_id'=>$user['id'],'type'=>3,'status'=>2])->find();
if(empty($array)) {
return false;
}
return $array;
// }
}
/**
* @title 取消需求
* @description 接口说明
* @author 开发者
* @url /api/portal/Order/cancelNeed
* @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
*
*/
public function cancelNeed() {
if($this->request->isPost()) {
$data = $this->request->param();
$member_model = new MemberModel();
$user = $member_model->where(['token'=>$data['token']])->value('id');
$needInfo = Db::name('UserNeed')->where(['id'=>$data['need_id'],'user_id'=>$user])->find();
if(!$needInfo) {
$this->apiResponse(0,'需求不存在');
}
$del_need = Db::name('UserNeed')->where(['id'=>$data['need_id'],'user_id'=>$user])->delete();
$orderInfo = Db::name('Order')->where(['need_id'=>$data['need_id'],'user_id'=>$user,'type'=>1])->find();
if(!$orderInfo) {
$del_order = true;
} else {
$del_order = Db::name('Order')->where(['need_id'=>$data['need_id'],'user_id'=>$user,'type'=>1])->delete();
}
if(!$del_need || !$del_order) {
$this->apiResponse(0,'需求取消失败');
}
$this->apiResponse(1,'需求取消成功');
}
}
}