UserController.php
5.3 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
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/11/13
* Time: 17:04
*/
namespace api\index\controller;
use cmf\controller\RestBaseController;
use think\Cache;
use think\cache\driver\Redis;
use think\Validate;
use think\Db;
/**
* @title 个人中心
* @description
*/
class UserController extends RestBaseController
{
/**
* @title 登录注册
* @description 接口说明
* @author Guosheng
* @url /index/User/login
* @method POST
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:phone type:int require:1 default: other: desc:手机号
* @param name:code type:string require:1 default: other: desc:验证码
*
*/
public function login()
{
$user_id = $this->getUserId();
$param = $this->request->param();
$param['create_time'] = time();
$param['user_id'] = $user_id;
$validate = new Validate([
'phone' => 'require|max:11',
'code'=>'require'
]);
if (!$validate->check($param)) {
$this->error(['code'=>40005,'msg'=>$validate->getError()]);
}
$code = Cache::get($param['phone']);
if($code == $param['code']){
$data = Db::name('integral')
->insert($param);
if($data){
$this->success('SUCCESS');
}
}else{
$this->error(['code'=>40002,'msg'=>'验证码错误或者失效,请重新发送']);
}
}
/**
* @title 获取验证码
* @description
* @author Guosheng
* @url /index/User/getcode
* @method POST
*
* @param name:phone type:string require:1 other: desc:手机号码
*
* @return code:验证码
*/
public function getcode()
{
$phone = $this->request->param('phone');
if (empty($phone)) {
$this->error(['code' => 40005, 'msg' => '缺少必要参数']);
}
if (!preg_match('/^1[0-9]{10}$/', $phone)) {
$this->error(['code' => 40005, 'msg' => "请输入正确的手机格式!"]);
}
//生成验证码
$number = generateCode();
//发送验证码
$data = array(
'content' => "您的验证码是:" . $number."十分钟之内有效",//短信内容
'mobile' => $phone,//手机号码
'productid' => '676767',//产品id
'xh' => ''//小号
);
$result = send_sms($data);
if (substr($result, 0, strpos($result, ',')) != "1") {
$this->error(['code' => 42001, 'msg' => $result]);
}
Cache::set($phone,$number,600);
// $code = Db::name('code')
// ->where('phone',$phone)
// ->find();
// if (empty($code)) {
// $arr['phone'] = $phone;
// $arr['create_time'] = time();
// $arr['expire_time'] = time() + 60;
// $arr['code'] = $number;
// $result = Db::name('code')
// ->insertGetId($arr);
// } else {
// $arr['code'] = $number;
// $arr['update_time'] = time();
// $arr['expire_time'] = time() + 60;
// $result = Db::name('code')
// ->where('phone',$phone)
// ->update($arr);
// }
// if (empty($result)) {
// $this->error(['code' => 40006, 'msg' => 'sql执行失败']);
// }
$this->success('SUCCESS', ['code' => $number]);
}
/**
* @title 我的首页
* @description 接口说明
* @author Guosheng
* @url /index/User/userInfo
* @method POST
*
* @header name:XX-Token require:1 default: desc:token
*
* @return user_nickname:昵称
* @return avatar:头像
* @return now:当前积分
* @return num:参与次数
* @return add_total:累计积分
*
*/
public function userInfo()
{
$user_id = $this->getUserId();
$info = Db::name('integral')
->alias('a')
->join('user b','a.user_id = b.id')
->field('a.*,b.user_nickname,b.avatar')
->where('a.user_id',$user_id)
->find();
if(empty($info)){
$this->error(['code'=>40006,'msg'=>'您还没有注册,请先注册!']);
}
$this->success('SUCCESS',$info);
}
/**
* @title 充值积分列表
* @description 接口说明
* @author Guosheng
* @url /index/User/pay
* @method POST
*
* @header name:XX-Token require:1 default: desc:token
*
* @return integral:积分
* @return money:金额
*
*/
public function pay(){
$user_id = $this->getUserId();
$data = Db::name('pay')
->where('delete_time',0)
->select();
$this->success('SUCCESS',$data);
}
/**
* @title 提现金额列表
* @description 接口说明
* @author Guosheng
* @url /index/User/money
* @method POST
*
* @header name:XX-Token require:1 default: desc:token
*
* @return integral:积分
* @return money:金额
*
*/
public function money(){
$user_id = $this->getUserId();
$data = Db::name('money')
->where('delete_time',0)
->select();
$this->success('SUCCESS',$data);
}
}