Person.php
14.5 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
<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\admin\model\Bank;
use app\admin\model\off\Line;
use think\Db;
use think\Validate;
/**
* 个人信息接口**
*/
class Person extends Api
{
protected $noNeedLogin = ['getScore'];
protected $noNeedRight = ['getScore'];
protected $person = 0;//个人
protected $company = 1;//企业
protected $user_id = '';//token存贮user_id
public function _initialize()
{
parent::_initialize();
$this->user_id = $this->auth->getUserId();
}
/**
* @ApiTitle (获取会员信息)
* @ApiSummary (获取会员信息)
* @ApiMethod (GET)
* @ApiRoute (/api/person/getUserInfo)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1553838039",
"data": {
"id": 26,
"username": "风起时呀",//用户名
"mobile": "",//联系方式
"type": 0,//类型 0:个人 1:企业
"address": "", //上门地址
"id_num": "",//身份证号
"id_img": "",//身份证件
"company_name": "",//公司名字
"license_num": "",//公司编码
"license_img": ""//公司营业执照
"money":100//账户余额
"avatar"://头像
}
})
*/
public function getUserInfo(){
if($this->request->isGet()){
$data = Db::table('gc_user')
->where(['id'=>$this->user_id])
->field('id,nickname,username,mobile,type,address,id_num,id_img,company_name,license_num,license_img,money,avatar')
->find();
$this->success('成功', $data);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (修改会员信息)
* @ApiSummary (修改会员信息)
* @ApiMethod (POST)
* @ApiRoute (/api/person/updateInfo)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="username", type="string", required=true, description="用户名(姓名)")
* @ApiParams (name="mobile", type="string", required=true, description="联系电话")
* @ApiParams (name="address", type="string", required=true, description="上门地址")
* @ApiParams (name="type", type="integer", required=true, description="用户类型(0:个人,1企业)")
* @ApiParams (name="id_num", type="string", required=true, description="身份证号(个人必须)")
* @ApiParams (name="id_img", type="string", required=true, description="身份证件(个人必须)")
* @ApiParams (name="company_name", type="string", required=true, description="公司名称(企业必须)")
* @ApiParams (name="license_num", type="string", required=true, description="公司编号(企业必须)")
* @ApiParams (name="license_img", type="string", required=true, description="公司营业执照(企业必须)")
* @ApiReturn({
"code": 1,
"msg": "保存成功",
"time": "1553838137",
"data": null
})
*/
public function updateInfo(){
if ($this->request->isPost()) {
$raw_data = $this->request->post();
if(empty($raw_data['type'])){
$this->error('请选择用户类型');
}
$type = $raw_data['type'];
if($type == $this->person){
//验证类型为个人的数据
$rule = config('site.person');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check($raw_data)) {
$this->error($validate->getError());
}
}else if($type == $this->company){
//验证类型为企业的数据
$company_rule = config('site.company');
$company_validate = new Validate($company_rule['rule'],$company_rule['msg']);
if (!$company_validate->check($raw_data)) {
$this->error($company_validate->getError());
}
}else{
$this->error('用户类型不合法');
}
$user = new \app\admin\model\User();
$data = $user->where(['id'=>$this->user_id])->update($raw_data);
if($data){
$this->success('保存成功');
}else{
$this->error('保存失败');
}
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (上传文件)
* @ApiSummary (上传文件)
* @ApiMethod (POST)
* @ApiRoute (/api/person/upload)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="images[]", type="file", required=true, description="上传文件(图片)")
* @ApiReturn ({
"code": 1,
"msg": "上传成功",
"time": "1553777266",
"data":[
{
"/uploads/20190328/0179b6b111307ef8be3c832ef808e1c1.jpg,/uploads/20190328/42bbc2f2692023d43957c3ee0b580443.jpg"
}
]
})
*/
public function upload(){
// 获取表单上传文件
$files = request()->file('images');
if (empty($files)) {
$this->error('未检出文件上传');
}
$countFile = count($files);
if($countFile > 9){
$this->error('最多上传9张图片');
}
$date = date('Ymd',time());
$host = \app\admin\model\User::DOMAIN;
$images = '';
foreach($files as $file){
//移动到框架应用根目录/public/uploads/ 目录下
//允许文件大小200k
$info = $file->validate(['size'=>204800,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
if($info){
$images .= $host.'/uploads/'.$date.'/'.$info->getFilename().',';
}else{
// 上传失败获取错误信息
$this->error($file->getError());
}
}
$this->success('上传成功',rtrim($images,','));
}
/**
* @ApiTitle (账单明细)
* @ApiSummary (账单明细)
* @ApiMethod (GET)
* @ApiRoute (/api/person/accountDetail)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="page", type="integer", required=true, description="分页页码")
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1554270371",
"data": [
{
"id": 1,
"type": 0,//类型 0:充值 1:提现
"money": 0.01,//充值或提现金额
"createtime": "2019-04-03 13:03:57"//时间
}
]
})
*/
public function accountDetail(){
if($this->request->isGet()){
$page = $this->request->get('page');//分页页码
$limit = config('site.page_limit');//分页限制数量
$rule = config('site.gift_pages');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['page'=>$page])) {
$this->error($validate->getError());
}
$data = Db::table('gc_account')
->where(['uid'=>$this->user_id])
->page($page,$limit)
->field('id,type,money,createtime')
->select();
foreach($data as &$value){
$value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
}
$this->success('成功', $data);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (绑定银行卡)
* @ApiSummary (绑定银行卡)
* @ApiMethod (POST)
* @ApiRoute (/api/person/bindBank)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="bank_num", type="integer", required=true, description="银行卡号")
* @ApiParams (name="open_bank", type="string", required=true, description="开户行")
* @ApiReturn ({
"code": 1,
"msg": "保存成功",
"time": "1554270371",
"data": null
})
*/
public function bindBank(){
if($this->request->isPost()) {
$bank_num = $this->request->post('bank_num');//银行卡号
$open_bank = $this->request->post('open_bank');//开户行
$rule = config('site.bank');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['bank_num'=>$bank_num,'open_bank'=>$open_bank])) {
$this->error($validate->getError());
}
$bank = new Bank();
$data = $bank->where(['uid'=>$this->user_id,'bank_num'=>$bank_num])->find();
if($data){
$this->error('不能重复绑定');
}
$data1 = $bank::create(['uid'=>$this->user_id,'bank_num'=>$bank_num,'open_bank'=>$open_bank]);
if($data1){
$this->success('保存成功');
}else{
$this->error('保存失败');
}
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (银行卡列表)
* @ApiSummary (银行卡列表)
* @ApiMethod (GET)
* @ApiRoute (/api/person/bankList)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturn ([
{
"id": 3,//银行卡id
"bank_num": "6228480028807779179",//银行卡号
"open_bank": "农业银行"//开户行
},
{
"id": 7,
"bank_num": "6228480028306448000",
"open_bank": "open_bank"
}
])
*/
public function bankList(){
if($this->request->isGet()){
$data = Db::table('gc_bank')
->where(['uid'=>$this->user_id])
->field('id,bank_num,open_bank')
->order('id desc')
->select();
$this->success('成功', $data);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (修改银行卡)
* @ApiSummary (修改银行卡)
* @ApiMethod (POST)
* @ApiRoute (/api/person/updateBank)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="integer", required=true, description="银行卡id")
* @ApiParams (name="bank_num", type="integer", required=true, description="银行卡号")
* @ApiParams (name="open_bank", type="string", required=true, description="开户行")
* @ApiReturn ({
"code": 1,
"msg": "修改成功",
"time": "1555652055",
"data": null
})
*/
public function updateBank(){
if($this->request->isPost()) {
$id = $this->request->post('id');//银行卡id
$bank_num = $this->request->post('bank_num');//银行卡号
$open_bank = $this->request->post('open_bank');//开户行
$rule = config('site.update_bank');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['id'=>$id,'bank_num'=>$bank_num,'open_bank'=>$open_bank])) {
$this->error($validate->getError());
}
$data = Db::table('gc_bank')->where('id','neq',$id)->where(['uid'=>$this->user_id,'bank_num'=>$bank_num])->find();
if($data){
$this->error('此银行卡已绑定');
}
$bank = new Bank();
$data1 = $bank->where(['id'=>$id,'uid'=>$this->user_id])->update(['open_bank'=>$open_bank,'bank_num'=>$bank_num]);
if($data1){
$this->success('修改成功');
}else{
$this->error('修改失败');
}
}else{
$this->error('请求方式错误');
}
}
/**
* 根据规则增加积分
* @param $share_uid
* @param $type
* @return bool
*/
public function getScore($share_uid,$type){
$class = '';
if($type == 'share_purchase'){
$scoreRule = Db::table('gc_sscore')
->where('set_rule','like','%购买%')
->field('set_score')
->find();
$class = 1;
}else if($type == 'share_sell'){
$scoreRule = Db::table('gc_sscore')
->where('set_rule','like','%卖废品%')
->field('set_score')
->find();
$class = 0;
}
// $score = config('site.'.$type);
$score = 0;
if($scoreRule){
$score = $scoreRule['set_score'];
}
$offlineModel = new Line();
//此处判断分享用户否已有上线,如果有,则不能成为其他的下线
$each_other = $offlineModel->where(['off_uid'=>$share_uid])->select();
//判断本登录用户是否有上线
$other = $offlineModel->where(['off_uid'=>$this->user_id])->select();
//分享人是自己无效
if($share_uid != $this->user_id){
//分享用户有上线
if($each_other){
$on_uid = array_unique(array_column($each_other,'uid'))[0];
//给上线增加积分
$userModel = new \app\admin\model\User();
$userModel->where(['id'=>$on_uid,'status'=>'normal'])->setInc('score', $score);
//创建上线用户的积分记录
$offlineModel->create(['uid'=>$on_uid,'s_score'=>$score,'off_uid'=>$share_uid,'type'=>$class]);
}else{
//本登录用户没有上线
if(!$other){
//给分享用户增加积分
$userModel = new \app\admin\model\User();
$userModel->where(['id'=>$share_uid,'status'=>'normal'])->setInc('score', $score);
//成为分享用户的下线并创建积分记录
$offlineModel->create(['uid'=>$share_uid,'s_score'=>$score,'off_uid'=>$this->user_id,'type'=>$class]);
}
}
}
}
}