IndexController.php
18.7 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;
//use cmf\controller\HomeBaseController;
use api\portal\model\MoneyDetailModel;
use api\portal\model\OrderModel;
use api\portal\model\RefundModel;
use app\portal\model\MemberModel;
use app\portal\service\PostService;
use think\Db;
use think\Request;
use think\Loader;
use traits\controller\Jump;
use think\Config;
/**
* @title 用户接口
* @description 接口说明
* @group 接口分组
*/
class IndexController extends CommonController
{
public function index()
{
// $return = $this->wxpay(['order_sn'=>201807211101],'保证金支付');
// $qrcode_url = url('portal/Index/qrcode',['data'=>urlencode($return['code_url'])],true,true);
// echo '<img src="'.$qrcode_url.'" alt=""/>';
// $back = [
// 'return'=>$return,
// 'code_url'=>base64_encode($return['code_url']),
// 'qrcode'=> '<img src="'.$qrcode_url.'" alt=""/>',
// 'alipay_url'=>url('portal/Alipay/alipay',array('order_sn'=>201807211101,'name'=>'保证金支付','price'=>0.01))
// ];
// $this->redirect($back['alipay_url']);
// $data = [
// 'out_trade_no'=>'2018072798495757',
// 'trade_no'=>'2018072721001004410589897579',
// 'out_request_no'=>'2018072798495757',
// 'refund_amount'=>0.01,
// 'refund_reason'=>'中介报价少于3人退款'
// ];
// $alipay = controller('Alipay');
// $result = $alipay->refundquery($data);
// $object = $result->alipay_trade_fastpay_refund_query_response;
// var_dump($result->alipay_trade_fastpay_refund_query_response);
// echo "<pre/>";
// print_r('7777');
// die;
//// 合作企业
// $where_coo['status'] = 1;
// $final['coop'] = Db::name('Cooperation')->where($where_coo)->order("score desc , create_time desc")->field('pic')->select()->toArray();
return $this->fetch(':index');
}
public function api_test() {
$key = '123456';
$data = [
'pid' => 17515,
'name' => 'huawei-6p'
];
// $id=$this->request->param('pid');
// $name=$this->request->param('name');
$id=$data['pid'];
$name=$data['name'];
$time=date('Y-m-d H:i:s');
$sign=md5($id.$name.$time.$key);
$file=[
'pid'=>$id,
'name'=>$name,
'time'=>$time,
'sign'=>$sign
];
$url="http://118.25.20.60/account.do?pid=$id&name=$name&time=$time&sign=$sign";
echo $url."<br/>";
// $result = file_get_contents($url);
$result = $this->curl($url,$file);
var_dump($result);
}
protected function curl($url,$data = null) {
// 初始化
$curl = curl_init();
// 设置抓取的url
curl_setopt($curl, CURLOPT_URL, $url);
// 设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
// 设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
// curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
// 执行命令
$data = curl_exec($curl);
if (curl_errno($curl)) {
$data = curl_error($curl);
}
// 关闭URL请求
curl_close($curl);
// 显示获得的数据
return $data;
}
public function wap_index()
{
// $return = $this->wxpay(['order_sn'=>201807211032],'保证金支付');
// $qrcode_url = url('portal/Index/qrcode',['data'=>urlencode($return['code_url'])],true,true);
// echo '<img src="'.$qrcode_url.'" alt=""/>';
$back = [
// 'return'=>$return,
// 'code_url'=>base64_encode($return['code_url']),
// 'qrcode'=> '<img src="'.$qrcode_url.'" alt=""/>',
'alipay_url'=>url('portal/AlipayWap/alipay',array('order_sn'=>201807211100,'name'=>'保证金支付','price'=>0.01))
];
$this->redirect($back['alipay_url']);
// echo "<pre/>";
// print_r('7777');
// die;
//// 合作企业
// $where_coo['status'] = 1;
// $final['coop'] = Db::name('Cooperation')->where($where_coo)->order("score desc , create_time desc")->field('pic')->select()->toArray();
return $this->fetch(':index');
}
// 微信h5支付(测试)
public function wxpayh5() {
require_once VENDOR_PATH.'WxpayAPI/WxpayH5.php';
$h5 = new \WxpayH5('wxf69d1d87d173c899','1507884301','VaY3CqbFLbq2Dw3FRENrixu24E9Qq1ZI','201807211465',
'保证金支付','1','{"h5_info": {"type":"Wap","wap_url": "http://houseprice.w.bronet.cn","wap_name": "保证金支付"}}' );
$result = $h5->pay();
var_dump($result);
$mweb_url = '';
if(!empty($result['mweb_url'])) {
$mweb_url = $result['mweb_url'];
}
$this->assign('mwen_url',$mweb_url);
return $this->fetch(':h5');
}
/**
* 微信h5支付(接口)
* @param user_id 用户ID
* @param order_sn 订单号
* @param _type 支付类型 1,发布需求(保证金);2,中介报价;3,透视卡
*/
public function wxpay_web() {
if($this->request->isPost()) {
$data = $this->request->param();
$order_model = new OrderModel();
$orderInfo = $order_model->where(['order_sn'=>$data['order_sn'],'type'=>$data['_type']])->find();
if(empty($orderInfo['status']) || $orderInfo['status'] > 1) {
$this->apiResponse(0,'订单已失效');
}
$type = [
1 => '保证金支付',
2 => '中介报价',
3 => '透视卡'
];
$scene_info = "{'h5_info': {'type':'Wap','wap_url': 'http://houseprice.w.bronet.cn','wap_name': ".$type[$data["_type"]]."}}";
require_once VENDOR_PATH.'WxpayAPI/WxpayH5.php';
// $h5 = new \WxpayH5(config('APP_ID'),config('MCH_ID'),config('WXPAY_KEY'),$data['order_sn'],
// $type[$data['_type']],'1',$scene_info);
$h5 = new \WxpayH5('wxf69d1d87d173c899','1507884301','VaY3CqbFLbq2Dw3FRENrixu24E9Qq1ZI',$data['order_sn'],
$type[$data['_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,'成功',$return);
}
}
// 微信退款(测试)
public function wxrefund() {
require_once VENDOR_PATH.'WxpayAPI/WxRefund.php';
$refund = new \WxRefund(Config::get('wx_appid'),Config::get('wx_mch_id'),Config::get('wx_key'),'4200000140201807275326669498',
'2018072710154991','1','1','中介报价少于3人退款');
$result = $refund->pay();
var_dump($result);
if($result['return_code'] == 'SUCCESS' && $result['return_msg'] == 'OK') {
$refund_model = new RefundModel();
$refund = $refund_model->where(['order_sn'=>$result['out_refund_no']])->find();
// if($refund) {
// echo '订单已申请退款';exit;
// }
$insert = [
'order_id' => 1,
'order_sn' => $result['out_refund_no'],
'user_id' => 24,
'user_type' => 1,
'refund_id' => $result['refund_id'],
'pay_type' => 1,
'reason' => 1,
'create_time' => time(),
'update_time' => time()
];
$refund_model->insertGetId($insert);
} else {
return false;
}
var_dump($result);
}
// 微信退款查询(测试)
public function wxrefundquery() {
require_once VENDOR_PATH.'WxpayAPI/WxRefundQuery.php';
$refund = new \WxRefundQuery(Config::get('wx_appid'),Config::get('wx_mch_id'),Config::get('wx_key'),'4200000139201807280275718105',
'2018072851481021','2018072810256541','50000107382018072805769543127');
$result = $refund->pay();
// if($refund['status'] == 2) {
// echo '订单已完成退款';exit;
// }
var_dump($result);
// if($result['return_code'] == 'SUCCESS' && $result['return_msg'] == 'OK') {
// Db::startTrans();
// $refund_update = [
// 'status' => 2,
// 'refund_time' => strtotime($result['refund_success_time_0'])
// ];
// $refund_result = $refund_model->where(['id'=>$refund['id']])->update($refund_update);
// if(!$refund_result) {
// Db::rollback();
// echo '退款状态修改失败';exit;
// }
// $order_model = new OrderModel();
// $order_update = [
// 'status' => 4,
// 'refund_time' => strtotime($result['refund_success_time_0'])
// ];
// $order_result = $order_model->where(['id'=>$refund['order_id']])->update($order_update);
// if(!$order_result) {
// Db::rollback();
// echo '订单状态修改失败';exit;
// }
// Db::commit();
// } else {
// $refund_update = [
// 'status' => 5,
// 'remark' => $result['return_msg']
// ];
// $refund_result = $refund_model->where(['id'=>$refund['id']])->update($refund_update);
// if(!$refund_result) {
// echo '退款状态修改失败';exit;
// }
// return false;
// }
// var_dump($result);
}
public function callback() {
// echo url('portal/Notify/refund_notify', '', '', true);
$info = Db::name('Test')->where(['id'=>144])->find();
$data = json_decode($info['data'],true);
if($data['return_code'] == 'SUCCESS' && !empty($data['req_info'])) {
$key = md5(Config::get('wx_key'));
$array = $this->xmlToArray($this->refund_decrypt($data['req_info'],$key));
var_dump($array);
$refund_where = [
'order_sn' => $array['out_refund_no'],
'refund_id' => $array['refund_id'],
];
$order_where = [
'transaction_id' => $array['transaction_id'],
];
$handle_sql = true;
// Db::startTrans();
if($array['refund_status'] == 'SUCCESS') {
$refund_update = [
'status' => 2,
'refund_time' => strtotime($array['success_time']),
'remark' => $array['refund_account'],
'more' => json_encode($array),
];
$refund_result = Db::name('Refund')->where($refund_where)->update($refund_update);
$order_update = [
'status' => 4,
'refund_time' => $array['success_time']
];
$order_result = Db::name('Order')->where($order_where)->update($order_update);
if(!$refund_result || !$order_result) {
$handle_sql = false;
}
} elseif($array['refund_status'] == 'CHANGE') {
$refund_update = [
'status' => 4,
// 'refund_time' => strtotime($array['success_time']),
// 'remark' => $array['refund_account'],
'more' => json_encode($array),
];
$refund_result = Db::name('Refund')->where($refund_where)->update($refund_update);
if(!$refund_result) {
$handle_sql = false;
}
} elseif($array['refund_status'] == 'REFUNDCLOSE') {
$refund_update = [
'status' => 3,
// 'refund_time' => strtotime($array['success_time']),
// 'remark' => $array['refund_account'],
'more' => json_encode($array),
];
$refund_result = Db::name('Refund')->where($refund_where)->update($refund_update);
if(!$refund_result) {
$handle_sql = false;
}
}
if(!$handle_sql) {
// Db::rollback();
} else {
// Db::commit();
// $this->return_success();
}
}
}
public function refund_decrypt($str, $key) {
$str = base64_decode($str);
$decrypted = openssl_decrypt($str, 'AES-256-ECB', $key, OPENSSL_RAW_DATA);
return $decrypted;
}
public function refund_alipay() {
$orderInfo = Db::name('Order')->where(['id'=>217])->find();
$orderInfo['user_type'] = 1;
$re = Db::name('Refund')->where(['id'=>265])->find();
$result = json_decode($re['more']);
$refund_model = new RefundModel();
if($result->code == '10000' && $result->msg == 'Success') {
$insert = [
'order_id' => $orderInfo['id'],
'order_sn' => $result->out_trade_no,
'user_id' => $orderInfo['user_id'],
'user_type' => $orderInfo['user_type'],
'refund_id' => $result->trade_no,
'money' => $orderInfo['money'],
'pay_type' => 2,
'status' => 2,
'reason' => 1,
'create_time' => time(),
'update_time' => time(),
'refund_time' => strtotime($result->gmt_refund_pay),
'more' => json_encode($result)
];
$refund_result = $refund_model->insertGetId($insert);
$update = [
'status' => 4,
'refund_time' => strtotime($result->gmt_refund_pay)
];
$order_result = Db::name('Order')->where(['id'=>$orderInfo['id']])->update($update);
if(!$refund_result || !$order_result) {
// return false;
}
// return true;
}
}
//xml转换成数组
private function xmlToArray($xml) {
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($xmlstring), true);
return $val;
}
/**
* 对密文进行解密
*
* @param string $encrypted 需要解密的密文
* @return string 解密得到的明文
*/
public function decrypt($encrypted,$key) {
$iv = substr($key, 0, 16);
$decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', $key, OPENSSL_ZERO_PADDING, $iv);
//去除补位字符
$result = $this->pkcs7Unpad($decrypted);
//去除16位随机字符串 加密时添加16为随机字符串
if (strlen($result) < 16) {
return "";
}
$content = substr($result, 16, strlen($result));
$lenList = unpack("N", substr($content, 0, 4));
$contentLen = $lenList[1];
return substr($content, 4, $contentLen);
}
/**
* 对解密后的明文进行补位删除
*
* @param string $text 解密后的明文
* @return string
*
*/
private function pkcs7Unpad($text) {
$pad = ord(substr($text, -1));
if ($pad < 1 || $pad > 32) {
$pad = 0;
}
return substr($text, 0, (strlen($text) - $pad));
}
// 登录,注册
//tel 手机号
//password 密码
//sure_password 确认密码
//name 真实姓名
public function login(Request $request)
{
if($request->Post()){
// 验证
$validate = Loader::validate('User');
if(!$validate->scene('add')->check($_POST)){
return json(array('code'=>0,'msg'=>$validate->getError()));
}
// 密码
$_POST['password'] = $this->md5($_POST['password']);
// 确认密码
if($_POST['password'] != $_POST['sure_password']){
$this->apiResponse('0','两次密码不一致');
}
// 短信验证码(未完)
// 判断手机号是否已注册
$user = new MemberModel($_POST);
$where_user['tel'] = $_POST['tel'];
$is_isset = $user->where($where_user)->find();
if($is_isset){
$this->apiResponse('0','您已注册过,请直接登录');
}
$add = $user->allowField(true)->save();
if($add){
echo "<pre/>";
print_r('1');
die;
}else{
echo "<pre/>";
print_r('2');
die;
}
}else{
// 服务协议
$service = new PostService();
$list = $service->publishedArticle(1,1)->toArray();
// 推荐人(未完)
if($list){
$this->apiResponse('1','成功',$list);
}else{
$this->apiResponse('0','暂无内容');
}
}
}
// 微信支付二维码
public function qrcode() {
$data = $this->request->param('data');
require_once VENDOR_PATH.'WxpayAPI/example/phpqrcode/phpqrcode.php';
$url = urldecode($data);
\QRcode::png($url);
}
// 检测微信支付是否完成
public function checkStatus() {
if($this->request->isAjax()) {
$order_sn = $this->request->param('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,'支付成功');
}
}
}
}