UserController.php
21.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
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
namespace api\wxapp\controller;
use cmf\controller\RestBaseController;
use think\Db;
use think\Log;
use think\Validate;
use wxapp\aes\WXBizDataCrypt;
/**
* @title 用户模块
* @description 用户管理模块
*/
class UserController extends RestBaseController
{
/**
* @title 获取用户信息(普通用户/业务员)
* @description 用户
* @url /wxapp/user/getUserInfo
* @method POST
*
* @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
* @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
*
* @return id:用户id/业务员id
* @return mobile:电话(微信绑定)
* @return age:年龄
* @return job:工作信息 1上班 2做生意
* @return house:房产信息 1全款房 2分期房 0无
* @return policy:保单信息 1有 0无
* @return car:车辆信息 1全款车 0无
*/
public function getUserInfo() {
$result = Db::name('user')
->where(['id' => $this->userId])
->field('id,mobile,age,job,house,policy,car')
->find();
if ($result === false) {
$this->error(['code'=>'40000','msg'=>'获取失败']);
}
$this->success('获取成功',$result);
}
/**
* @title 编辑用户信息(普通用户/业务员)
* @description 用户
* @url /wxapp/user/editUserInfo
* @method POST
*
* @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
* @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
*
* @param name:mobile type:string require:1 other: desc:电话(微信绑定)
* @param name:age type:string require:1 other: desc:年龄
* @param name:job type:string require:1 other: desc:工作信息1上班2做生意
* @param name:house type:string require:1 other: desc:房产信息1全款房2分期房0无
* @param name:policy type:string require:1 other: desc:保单信息1有0无
* @param name:car type:string require:1 other: desc:车辆信息1全款车0无
*/
public function editUserInfo() {
$validate = new Validate([
'mobile' => 'require',
'age' => 'require',
'job' => 'require|in:1,2',
'house' => 'require|in:0,1,2',
'policy' => 'require|in:0,1',
'car' => 'require|in:0,1',
]);
$validate->message([
'mobile.require' => '缺少参数mobile!',
'age.require' => '缺少参数age!',
'job.require' => '缺少参数job!',
'job.in' => '参数job范围1-2!',
'house.require' => '缺少参数house!',
'house.in' => '参数house范围0-2!',
'policy.require' => '缺少参数policy!',
'policy.in' => '参数policy范围0-1!',
'car.require' => '缺少参数car!',
'car.in' => '参数car范围0-1!',
]);
$data = $this->request->param();
if (!$validate->check($data)) {
$this->error(['code'=>'40003','msg'=>$validate->getError()]);
}
$arr = [
'mobile' => $data['mobile'],
'age' => $data['age'],
'job' => $data['job'],
'house' => $data['house'],
'policy' => $data['policy'],
'car' => $data['car']
];
$result = Db::name('user')->where(['id' => $this->userId])->update($arr);
if ($result === false) {
$this->error(['code'=>'40000','msg'=>'操作失败']);
}
$this->success('操作成功');
}
/**
* @title 获取查看我名片的用户列表
* @description 位置:业务员--我的用户
* @url /wxapp/user/getUserListCard
* @method POST
*
* @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
* @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
*
* @param name:page type:string require:0 other: desc:页码
* @param name:per_page type:string require:0 other: desc:每页数据量
*
* @return total:总数据量
* @return per_page:每页数据量
* @return current_page:当前页
* @return last_page:尾页
* @return data:对象信息@!
* @data id:用户id avatar:用户头像 user_nickname:用户昵称 mobile:用户手机号(微信绑定)
*/
public function getUserListCard() {
$data = $this->request->param();
$per_page = $data['per_page'] ? : config('per_page');
$subQuery = Db::name('card')
->alias('c')
->join('browse_log b','c.id = b.topic_id')
->field('b.user_id')
->where(['c.user_id' => $this->userId])
->group('b.user_id')
->buildSql();
$result = Db::table($subQuery)
->alias('a')
->join('user u','a.user_id = u.id')
->field('u.id,u.avatar,u.user_nickname,u.mobile')
->paginate($per_page);
if ($result === false) {
$this->error(['code'=>'40000','msg'=>'获取失败']);
}
$this->success('获取成功',$result);
}
/**
* @title 获取申请产品的用户列表
* @description 位置:业务员--c产品申请
* @url /wxapp/user/getUserListApply
* @method POST
*
* @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
* @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
*
* @param name:page type:string require:0 other: desc:页码
* @param name:per_page type:string require:0 other: desc:每页数据量
*
* @return total:总数据量
* @return per_page:每页数据量
* @return current_page:当前页
* @return last_page:尾页
* @return data:对象信息@!
* @data id:用户id avatar:用户头像 user_nickname:用户昵称 mobile:用户手机号(微信绑定)product_name:产品名称 logo:产品logo
*/
public function getUserListApply() {
$data = $this->request->param();
$per_page = $data['per_page'] ? : config('per_page');
$subQuery = Db::name('product')
->alias('p')
->join('loans_apply l','p.id = l.product_id')
->field('l.user_id,p.name,p.logo')
->where(['p.user_id' => $this->userId])
->buildSql();
$result = Db::table($subQuery)
->alias('a')
->join('user u','a.user_id = u.id')
->field('u.id,u.avatar,u.user_nickname,u.mobile,a.name product_name,a.logo')
->paginate($per_page);
if ($result === false) {
$this->error(['code'=>'40000','msg'=>'获取失败']);
}
$this->success('获取成功',$result);
}
/**
* @url /wxapp/User/getWxData
* @method POST
*
* @param name:code type:string require:1 other: desc:code
*
*/
public function getWxData(){
$id = config('appId');
$secret = config('appSecret');
$code = $this->request->param('code');
$url = "https://api.weixin.qq.com/sns/jscode2session?appid={$id}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
$result = json_decode(file_get_contents($url),true);
$this->success('获取成功',$result);
}
/**
* 微信小程序用户加密数据的解密
* @param Request $request
* @return mixed
*/
public function wxDecode(){
$appid = config('appId');
$sessionKey = $this->request->param('sessionKey');
$encryptedData = $this->request->param('encryptedData');
$iv = $this->request->param('iv');
$pc = new WXBizDataCrypt($appid, $sessionKey);
$errCode = $pc->decryptData($encryptedData, $iv, $data);
if ($errCode == 0){
$this->success('获取成功',json_decode($data,true));
} else {
$arr = array(
-41001=>'encodingAesKey非法',
-41003=>'aes解密失败',
-41004=>'解密后得到的buffer非法',
-41005=>'base64加密失败',
-41016=>'base64解密失败'
);
$errorMsg = isset($arr[$errCode]) ? $arr[$errCode] : $errCode;
Log::error('微信数据解析失败: '.$errorMsg);
$this->error('获取失败',$errorMsg);
}
}
/**
* @title 激励金管理
* @description 用户
* @url /wxapp/user/getMoneyInfo
* @method POST
*
* @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
* @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
*
* @return money:激励金
* @return num:邀请人次
* @return is_first:是否首次设置激励金 1是 0否
*/
public function getMoneyInfo() {
if ($this->userType != 3) {
$this->error(['code'=>'40007','msg'=>'非信贷员身份']);
}
$result = Db::name('user')->where(['id' => $this->userId])->field('money,num,is_first')->find();
if ($result === false) {
$this->error(['code'=>'40000','msg'=>'获取失败']);
}
$this->success('获取成功',$result);
}
/**
* @title 设置激励金
* @description 用户
* @url /wxapp/user/setMoneyInfo
* @method POST
*
* @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
* @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
*
* @param name:money type:int require:1 other: desc:激励金
* @param name:num type:int require:1 other: desc:邀请人次
*
* @return appId:小程序ID
* @return timeStamp:时间戳
* @return nonceStr:随机字符串
* @return package:数据包
* @return signType:MD5签名方式
* @return paySign:签名
*/
public function setMoneyInfo() {
if ($this->userType != 3) {
$this->error(['code'=>'40007','msg'=>'非信贷员身份']);
}
$validate = new Validate([
'money' => 'require',
'num' => 'require',
]);
$validate->message([
'money.require' => '缺少参数money!',
'num.require' => '缺少参数num!',
]);
$data = $this->request->param();
if (!$validate->check($data)) {
$this->error(['code'=>'40003','msg'=>$validate->getError()]);
}
$arr = [
'appid' => config('appId'),
'mch_id' => config('mchId'),
'nonce_str' => md5(rand(100000, 999999)),
'sign_type' => 'MD5',
'body' => '奖励金充值',
'out_trade_no' => 'R_'.date('YmdHis').rand(1000,9999),
'total_fee' => $data['money'] * 100, // 转换为分
//'total_fee' => 1, // 转换为分
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
'notify_url' => config('rechargeNotifyUrl'),
'trade_type' => 'JSAPI',
'openid' => $this->user['openid']
];
// 预创建订单
$array = [
'order_sn' => $arr['out_trade_no'],
'user_id' => $this->userId,
'money' => $data['money'],
'num' => $data['num']
];
$result = Db::name('recharge_log')->insert($array);
if (!$result){
$this->error(['code'=>'40008','msg'=>'订单创建失败']);
}
$sign = $this->getSign($arr);
$arr['sign'] = $sign;
$Data = $this->array2xml($arr);
$return = $this->curl_post_ssl('https://api.mch.weixin.qq.com/pay/unifiedorder', $Data);
if ($return['return_code'] == 'SUCCESS'){
$back = [
'appId' => config('appId'),
'timeStamp' => (string)time(),
'nonceStr' => md5(rand(100000, 999999)),
'package' => 'prepay_id=' . $return['prepay_id'],
'signType' => 'MD5',
];
$paySign = $this->getSign($back);
$back['paySign'] = $paySign;
$this->success('操作成功',$back);
} else {
$this->error(['code'=>'40000','msg'=>$return]);
}
}
/**
* @title 提现
* @description 用户
* @url /wxapp/user/withDraw
* @method POST
*
* @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
* @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
*
* @param name:money type:int require:1 other: desc:提现金额
* @param name:id type:int require:1 other: desc:名片id
*
* @return appId:小程序ID
* @return timeStamp:时间戳
* @return nonceStr:随机字符串
* @return package:数据包
* @return signType:MD5签名方式
* @return paySign:签名
*/
public function withDraw() {
$validate = new Validate([
'money' => 'require',
'id' => 'require',
]);
$validate->message([
'money.require' => '缺少参数money!',
'id.require' => '缺少参数id!',
]);
$data = $this->request->param();
if (!$validate->check($data)) {
$this->error(['code'=>'40003','msg'=>$validate->getError()]);
}
$condition = [
'card_id' => $data['id'],
'user_id' => $this->userId
];
if ($data['money'] < 2) {
$this->error(['code'=>'40013','msg'=>'余额不足2元']);
}
$arr = [
'mch_appid' => config('appId'),//申请商户号的appid或商户号绑定的appid
'mchid' => config('mchId'),//商户ID
'nonce_str' => md5(rand(100000, 999999)),
'desc' => '佣金提现',
'partner_trade_no' => 'W'.date('YmdHis').rand(1000,9999),//商户订单号,需要唯一
'amount' => $data['money'] * 100 * 0.8, // 转换为分
//'notify_url' => config('withDrawNotifyUrl'),
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
'check_name'=> 'NO_CHECK', //OPTION_CHECK不强制校验真实姓名, FORCE_CHECK:强制 NO_CHECK:
'openid' => $this->user['openid']
];
// 预创建订单
$array = [
'order_sn' => $arr['partner_trade_no'],
'card_id' => $data['id'],
'user_id' => $this->userId,
'money' => $data['money'] * 0.8,
];
$result = Db::name('withdraw_log')->insert($array);
if (!$result){
$this->error(['code'=>'40008','msg'=>'订单创建失败']);
}
$sign = $this->getSign($arr);
$arr['sign'] = $sign;
$Data = $this->array2xml($arr);
$return = $this->curl_post_ssl('https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers', $Data);
if ($return['return_code'] == 'SUCCESS'){
if ($return['result_code'] == 'FAIL') {
$this->error($return['err_code_des']);
}
$res = Db::name('withdraw_log')->where(['order_sn' => $return['partner_trade_no']])->field('id,money,user_id,card_id')->find();
if (!$res){
Log::error('订单更新失败(订单不存在)');
$this->error('提现失败');
}
// 更新订单状态
$status = Db::name('withdraw_log')->where(['order_sn' => $return['partner_trade_no']])->update(['state' => 1, 'pay_time' => date('Y-m-d H:i:s')]);
$array2 = [
'money' => ['exp','money -'.$data['money']]
];
Db::name('money_log')->where($condition)->update($array2);
if (!$status){
$this->error('提现失败');
}
$this->success('提现成功');
} else {
$this->error(['code'=>'40000','msg'=>$return['err_code_des']]);
}
}
/**
* 生成支付签名
* @param $arr
* @return string
*/
private function getSign($arr){
//去除数组中的空值
$arr = array_filter($arr);
//如果数组中有签名删除签名
if (isset($arr['sing'])){
unset($arr['sing']);
}
//按照键名字典排序
ksort($arr);
//生成URL格式的字符串
$str = http_build_query($arr) . "&key=" . config('payKey');
$str = $this->arrToUrl($str);
return strtoupper(md5($str));
}
/**
* URL解码为中文
* @param $str
* @return string
*/
private function arrToUrl($str){
return urldecode($str);
}
private function array2xml($arr){
if (!is_array($arr) || count($arr) <= 0){
return false;
}
$xml = "<xml>";
foreach ($arr as $key => $val){
if (is_numeric($val)){
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
private function xml2array($xmlobject) {
if ($xmlobject) {
foreach ((array)$xmlobject as $k=>$v) {
$data[$k] = !is_string($v) ? $this->xml2array($v) : $v;
}
return $data;
}
}
private function xmlToArray($xml){
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $values;
}
private function postXmlCurl($url,$xml, $second = 30)
{
$ch = curl_init();
//设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //严格校验
//设置header
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//post提交方式
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
set_time_limit(0);
//运行curl
$data = curl_exec($ch);
//返回结果
if ($data) {
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
curl_close($ch);
$this->error(['code'=>'40000','msg'=>$error]);
}
}
private function curl_post_ssl($url, $xmldata, $second = 30, $aHeader = array()){
$isdir = $_SERVER['DOCUMENT_ROOT']."/wechat/cert/";//证书位置;绝对路径
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_TIMEOUT, $second);//设置执行最长秒数
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 终止从服务端进行验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');//证书类型
curl_setopt($ch, CURLOPT_SSLCERT, $isdir . 'apiclient_cert.pem');//证书位置
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');//CURLOPT_SSLKEY中规定的私钥的加密类型
curl_setopt($ch, CURLOPT_SSLKEY, $isdir . 'apiclient_key.pem');//证书位置
/*curl_setopt($ch, CURLOPT_CAINFO, 'PEM');
curl_setopt($ch, CURLOPT_CAINFO, $isdir . 'rootca.pem');*/
if (count($aHeader) >= 1) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);//设置头部
}
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldata);//全部数据使用HTTP协议中的"POST"操作来发送
$data = curl_exec($ch);//执行回话
if ($data) {
curl_close($ch);
return $this->xmlToArray($data);
} else {
$error = curl_errno($ch);
echo "call faild, errorCode:$error\n";
curl_close($ch);
return false;
}
}
}