Person.php
6.1 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
<?php
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Validate;
/**
* 个人信息接口**
*/
class Person extends Api
{
protected $noNeedLogin = [];
protected $noNeedRight = '*';
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": ""//公司营业执照
}
})
*/
public function getUserInfo(){
if($this->request->isGet()){
$data = Db::table('gc_user')
->where(['id'=>$this->user_id])
->field('id,username,mobile,type,address,id_num,id_img,company_name,license_num,license_img')
->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="image", 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());
$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 .= '/uploads/'.$date.'/'.$info->getFilename().',';
}else{
// 上传失败获取错误信息
$this->error($file->getError());
}
}
$this->success('上传成功',rtrim($images,','));
}
}