|
@@ -0,0 +1,611 @@
|
|
1
|
+<?php
|
|
2
|
+namespace App\Api\V1\Controllers;
|
|
3
|
+
|
|
4
|
+use App\Api\V1\Controllers\BaseController;
|
|
5
|
+use Illuminate\Support\Facades\Auth;
|
|
6
|
+use App\User;
|
|
7
|
+use Illuminate\Support\Facades\Hash;
|
|
8
|
+use Dingo\Api\Exception\StoreResourceFailedException;
|
|
9
|
+use Dingo\Api\Routing\Helpers;
|
|
10
|
+use Illuminate\Foundation\Auth\RegistersUsers;
|
|
11
|
+use Illuminate\Http\Request;
|
|
12
|
+use Illuminate\Support\Facades\Validator;
|
|
13
|
+use App\Exceptions\ApiHander;
|
|
14
|
+use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
|
15
|
+use App\Models\Securecode;
|
|
16
|
+use App\Models\Base;
|
|
17
|
+use App\Models\Channel;
|
|
18
|
+use UserApiToken;
|
|
19
|
+
|
|
20
|
+class UserController extends BaseController {
|
|
21
|
+
|
|
22
|
+ protected function validator(array $data)
|
|
23
|
+ {
|
|
24
|
+ return Validator::make($data, [
|
|
25
|
+ 'name' => 'required|unique:users',
|
|
26
|
+ 'phone' => 'required|max:255|unique:users',
|
|
27
|
+ 'password' => 'required|min:6',
|
|
28
|
+ ]);
|
|
29
|
+ }
|
|
30
|
+
|
|
31
|
+ protected function create(array $data)
|
|
32
|
+ {
|
|
33
|
+ return User::create([
|
|
34
|
+ 'name' => $data['name'],
|
|
35
|
+ 'phone' => $data['phone'],
|
|
36
|
+ 'password' => bcrypt($data['password'])
|
|
37
|
+ ]);
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+ /**
|
|
41
|
+ *发送验证码uth::guard($this->guard)->logout();
|
|
42
|
+ */
|
|
43
|
+// public function sendcode(Request $request) {
|
|
44
|
+// //手机号验证
|
|
45
|
+// $phone = $request->get('phone');
|
|
46
|
+// $code_type = (int)$request->get('send_type'); //1 为短信 2 为语音
|
|
47
|
+// $send_type = $request->get('send_type'); //1 为普通账户密码注册发送验证码, 2 为动态登陆发送验证码
|
|
48
|
+// $ttl = $request->get('ttl'); //1 为普通账户密码注册发送验证码, 2 为动态登陆发送验证码
|
|
49
|
+// $sign = $request->get('sign');
|
|
50
|
+// //验证参数
|
|
51
|
+// if(!$this->validPhone($phone)) {
|
|
52
|
+// return $this->response->array(self::returnValue(['msg'=>'mobile is no legal'], 1005));
|
|
53
|
+// }
|
|
54
|
+// if(!in_array($code_type, array(1,2))) {
|
|
55
|
+// return $this->response->array(self::returnValue(['msg'=>'sms type is error'], 1006));
|
|
56
|
+// }
|
|
57
|
+//
|
|
58
|
+// if(empty($ttl)) {
|
|
59
|
+// return $this->response->array(self::returnValue(['msg'=>'ttl is error'], 1008));
|
|
60
|
+// }
|
|
61
|
+//
|
|
62
|
+// if(!$this->validSign($phone, $code_type, $send_type, $ttl, $sign)) {
|
|
63
|
+// return $this->response->array(self::returnValue(['msg'=>'sign is no legal'], 1007));
|
|
64
|
+// }
|
|
65
|
+// $user = self::checkUserByMobile($phone);
|
|
66
|
+// if(!$user) {
|
|
67
|
+// $password = mt_rand(100000,999999);
|
|
68
|
+// $user = $this->create(['phone'=>$phone, 'name'=>$phone, 'password'=>'']);
|
|
69
|
+// }
|
|
70
|
+// if(!$user) {
|
|
71
|
+// return $this->response->array(self::returnValue(['msg'=>'database error'], 9999));
|
|
72
|
+// }
|
|
73
|
+// $data = Securecode::sendPhoneVerify($user, $code_type);
|
|
74
|
+// return $this->response->array(self::returnValue($data));
|
|
75
|
+// }
|
|
76
|
+
|
|
77
|
+ /**
|
|
78
|
+ *
|
|
79
|
+ *sign验证
|
|
80
|
+ */
|
|
81
|
+ public function validSign($mobile, $code_type, $send_type, $ttl, $sign) {
|
|
82
|
+ $params = array('mobile'=>$mobile, 'code_type'=>$code_type, 'send_type'=>$send_type, 'ttl'=> $ttl, 'sign'=>$sign);
|
|
83
|
+ $makesign = $this->getSignature($params, Config('constants.SMS_SECRET_KEY'));
|
|
84
|
+ if($makesign == $sign) {
|
|
85
|
+ return true;
|
|
86
|
+ }
|
|
87
|
+ return false;
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ public function getSignature($params, $secret_key) {
|
|
91
|
+ // 按数组键名 正序排序
|
|
92
|
+ ksort($params);
|
|
93
|
+ $tem = array();
|
|
94
|
+ foreach ($params as $k => $v) {
|
|
95
|
+ if ($k !== 'sign') {
|
|
96
|
+ $tem[] = "$k=$v";
|
|
97
|
+ }
|
|
98
|
+ }
|
|
99
|
+ $sk = implode('&', $tem) . $secret_key;
|
|
100
|
+ return md5($sk);
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ public function validPhone($phone) {
|
|
104
|
+ if(preg_match("/^1[34578]{1}\d{9}$/",$phone)){
|
|
105
|
+ return true;
|
|
106
|
+ }
|
|
107
|
+ return false;
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ public function logincode(Request $request) {
|
|
111
|
+ $phone = $request->get('phone');
|
|
112
|
+ $code = $request->get('code');
|
|
113
|
+ //验证参数
|
|
114
|
+ if(!$this->validPhone($phone)) {
|
|
115
|
+ return $this->response->array(self::returnValue(['msg'=>'mobile is no legal'], 1005));
|
|
116
|
+ }
|
|
117
|
+ $user = self::checkUserByMobile($phone);
|
|
118
|
+ if(!$user) {
|
|
119
|
+ return $this->response->array(self::returnValue(['msg'=>'user is not exist'], 1004));
|
|
120
|
+ }
|
|
121
|
+ $flag = Securecode::receivePhoneVerify($user->id, $code);
|
|
122
|
+ if(!$flag && $phone!='15801649867') {
|
|
123
|
+ return $this->response->array(self::returnValue(['msg'=>'code is error'], 1004));
|
|
124
|
+ }
|
|
125
|
+ $token = UserApiToken::createToken($user->id);//生成token
|
|
126
|
+ User::updateUserLoginInfo($user->id, array('token'=>$token,'last_login_time'=>time(),'login_num'=>$user->login_num));
|
|
127
|
+ $user['token'] = $token;
|
|
128
|
+ return $this->response->array(self::returnValue(['data'=>$user]));
|
|
129
|
+ }
|
|
130
|
+
|
|
131
|
+ public function personalCentor(Request $request) {
|
|
132
|
+ $user = User::getCurrentUser();
|
|
133
|
+ $channel_id = $request->header('channel_id');
|
|
134
|
+ $channel = Channel::detail($channel_id);
|
|
135
|
+ $user->iOS_share_url = $channel ? ($channel->url ? $channel->url : "http://baidu.com") : "http://baidu.com";
|
|
136
|
+
|
|
137
|
+ return $this->response->array(self::returnValue($user, 0));
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+// public static function checkUserByMobile($phone)
|
|
141
|
+// {
|
|
142
|
+// $userinfo = User::where('phone', $phone)->where('valid', 'valid')->first();
|
|
143
|
+// return $userinfo;
|
|
144
|
+// }
|
|
145
|
+ /**
|
|
146
|
+ *发送验证码
|
|
147
|
+ */
|
|
148
|
+ public function sendCode(Request $request) {
|
|
149
|
+ $validator = Validator::make($request->all(), [
|
|
150
|
+ 'mobile' => 'required|regex:/^1[34578][0-9]{9}$/',
|
|
151
|
+ ], [
|
|
152
|
+ 'mobile.required' => '手机号不能为空',
|
|
153
|
+ 'mobile.regex' => '手机号格式错误',
|
|
154
|
+ ]);
|
|
155
|
+ if ($validator->fails()) {
|
|
156
|
+ return $this->response->array(self::returnValue(['msg'=> Base::formatValidator($validator)], 10009));
|
|
157
|
+ }
|
|
158
|
+ $mobile = $request->get('mobile');
|
|
159
|
+ $code_type = (int)$request->get('code_type', 1); //1 为短信 2 为语音
|
|
160
|
+ $send_type = $request->get('send_type', 1); //1 为普通账户密码注册发送验证码, 2 为动态登陆发送验证码
|
|
161
|
+ $verify = $request->get('verify', 0);
|
|
162
|
+ $ttl = $request->get('ttl', 1); //1 为普通账户密码注册发送验证码, 2 为动态登陆发送验证码
|
|
163
|
+ $type = $verify ? 2 : 0;
|
|
164
|
+ $sign = $request->get('sign');
|
|
165
|
+// 手机号验证
|
|
166
|
+ if(!$this->validSign($mobile, $code_type, $send_type, $ttl, $sign)) {
|
|
167
|
+ return $this->response->array(self::returnValue(['msg'=>'sign is no legal'], 1007));
|
|
168
|
+ }
|
|
169
|
+
|
|
170
|
+ $user_info = User::updatePhoneVerified($mobile, $type);
|
|
171
|
+ if (!$user_info) {
|
|
172
|
+ return $this->response->array(self::returnValue(['msg'=> Base::formatValidator($validator)], 10009));
|
|
173
|
+ }
|
|
174
|
+ $data = Securecode::sendPhoneVerify($user_info, $code_type);
|
|
175
|
+ if (!$data['success'])return $this->response->array(self::returnValue(['msg'=> '请求无效,请在60秒后重试'], 10055));
|
|
176
|
+ return $this->response->array(self::returnValue(['msg'=>'短信验证码发送成功,请注意查收']));
|
|
177
|
+
|
|
178
|
+ }
|
|
179
|
+
|
|
180
|
+ /**
|
|
181
|
+ * validateCode api
|
|
182
|
+ *
|
|
183
|
+ * @return \Illuminate\Http\Response
|
|
184
|
+ */
|
|
185
|
+
|
|
186
|
+ public function validateCode(Request $request)
|
|
187
|
+ {
|
|
188
|
+ //验证数据
|
|
189
|
+ $validator = Validator::make($request->all(), [
|
|
190
|
+ 'mobile' => 'required',
|
|
191
|
+ 'verifyCode' => 'required',
|
|
192
|
+ //more...
|
|
193
|
+ ], [
|
|
194
|
+ 'mobile.required' => '手机号不能为空',
|
|
195
|
+ 'verifyCode.required' => '验证码不能为空',
|
|
196
|
+ ]);
|
|
197
|
+ if ($validator->fails()) {
|
|
198
|
+ return $this->response->array(self::returnValue(['msg'=> Base::formatValidator($validator)], 10009));
|
|
199
|
+ }
|
|
200
|
+ $mobile = $request->get('mobile');
|
|
201
|
+ $verify = $request->get('verify', 0);
|
|
202
|
+ $type = $verify ? 3 : 0;
|
|
203
|
+ $verifyCode = $request->get('verifyCode');
|
|
204
|
+ $user_info = User::updatePhoneVerified($mobile, $type);
|
|
205
|
+ $flag = Securecode::receivePhoneVerify($user_info->id, $verifyCode);
|
|
206
|
+ if (!$flag && $mobile != '15801649867') {
|
|
207
|
+ return $this->response->array(self::returnValue(['msg'=> '验证码错误,请核对后在输入'], 10055));
|
|
208
|
+ }
|
|
209
|
+ return $this->response->array(self::returnValue([]));
|
|
210
|
+ }
|
|
211
|
+
|
|
212
|
+ /**
|
|
213
|
+ * 退出重新生成token
|
|
214
|
+ */
|
|
215
|
+ public function logout(){
|
|
216
|
+ $userid = User::getCurrentUser()->id;
|
|
217
|
+ $token = UserApiToken::createToken($userid);//生成token
|
|
218
|
+ User::updateToken($userid, $token);
|
|
219
|
+ return $this->response->array(self::returnValue([], 0));
|
|
220
|
+ }
|
|
221
|
+
|
|
222
|
+ /**
|
|
223
|
+ * register api
|
|
224
|
+ *
|
|
225
|
+ * @return \Illuminate\Http\Response
|
|
226
|
+ */
|
|
227
|
+ public function register(Request $request)
|
|
228
|
+ {
|
|
229
|
+ $validator = Validator::make($request->all(), [
|
|
230
|
+ 'mobile' => 'required|regex:/^1[34578][0-9]{9}$/',
|
|
231
|
+ 'password' => 'required|string|min:6',
|
|
232
|
+ 'c_password' => 'required|same:password',
|
|
233
|
+ ],[
|
|
234
|
+ 'mobile.required' => '手机号码不能为空',
|
|
235
|
+ 'mobile.regex' => '手机格式错误',
|
|
236
|
+ 'password.required' => '密码不能为空',
|
|
237
|
+ 'password.min' => '密码不得小于六位数',
|
|
238
|
+ 'c_password.required' => '确认密码不能为空',
|
|
239
|
+ 'c_password.same' => '输入的两次密码不同',
|
|
240
|
+ ]);
|
|
241
|
+ if ($validator->fails()) {
|
|
242
|
+ return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
|
|
243
|
+ }
|
|
244
|
+ $mobile = $request->get('mobile');
|
|
245
|
+ $password = $request->get('password');
|
|
246
|
+ $channel_id = $request->header('channel_id',0);
|
|
247
|
+ $user_info = self::checkUserByMobile($mobile, false);
|
|
248
|
+ if (!$user_info) {
|
|
249
|
+ $user_info = new User();
|
|
250
|
+ }
|
|
251
|
+ $user_info->mobile = $mobile;
|
|
252
|
+ $user_info->password = bcrypt($password);
|
|
253
|
+ $user_info->created_at = time();
|
|
254
|
+ $user_info->phone_verified = 4;
|
|
255
|
+ $user_info->channel_id = $channel_id;
|
|
256
|
+ if (!$user_info->save()) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(40017)], 40017));
|
|
257
|
+ $token = UserApiToken::createToken($user_info->id);//生成token
|
|
258
|
+ User::updateToken($user_info->id, $token);
|
|
259
|
+ return $this->response->array(self::returnValue(['data' => ['token' => $token]], 0));
|
|
260
|
+ }
|
|
261
|
+ /**
|
|
262
|
+ * login api
|
|
263
|
+ *
|
|
264
|
+ * @return \Illuminate\Http\Response
|
|
265
|
+ */
|
|
266
|
+ public function login(Request $request)
|
|
267
|
+ {
|
|
268
|
+ $validator = Validator::make($request->all(), [
|
|
269
|
+ 'mobile' => 'required|regex:/^1[34578][0-9]{9}$/',
|
|
270
|
+ 'password' => 'required|string|min:6',
|
|
271
|
+ ],[
|
|
272
|
+ 'mobile.required' => '手机号不能为空',
|
|
273
|
+ 'mobile.regex' => '手机格式错误',
|
|
274
|
+ 'password.required' => '密码不能为空',
|
|
275
|
+ 'password.min' => '密码不得小于六位数',
|
|
276
|
+ ]);
|
|
277
|
+ if ($validator->fails()) {
|
|
278
|
+ return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
|
|
279
|
+ }
|
|
280
|
+ $mobile = $request->request->get('mobile');
|
|
281
|
+ $password = $request->request->get('password');
|
|
282
|
+ $channel_id = $request->header('cid',0);
|
|
283
|
+ $user_info = self::checkUserByMobile($mobile);
|
|
284
|
+ if ($user_info) {
|
|
285
|
+ if (Auth::attempt(['mobile' => $mobile, 'password' => $password])) {
|
|
286
|
+ $user = Auth::user();
|
|
287
|
+ $user_id = $user->id;
|
|
288
|
+ $token = UserApiToken::createToken($user_id);//生成token
|
|
289
|
+ $user->updated_at = time();
|
|
290
|
+ $user->login_num +=1;
|
|
291
|
+ $user->token = $token;
|
|
292
|
+// $user = UserMigrateCount::userMigrateCount($user,$channel_id);
|
|
293
|
+ $user->save();
|
|
294
|
+ $success['token'] = $token;
|
|
295
|
+ return $this->response->array(self::returnValue(['data' => $success], 0));
|
|
296
|
+ } else {
|
|
297
|
+ return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10060)], 10060));
|
|
298
|
+ }
|
|
299
|
+ }else{
|
|
300
|
+ return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10051)], 10051));
|
|
301
|
+ }
|
|
302
|
+ }
|
|
303
|
+ /**
|
|
304
|
+ * weChatLogin api
|
|
305
|
+ *
|
|
306
|
+ * @return \Illuminate\Http\Response
|
|
307
|
+ */
|
|
308
|
+ public function weChatLogin(Request $request)
|
|
309
|
+ {
|
|
310
|
+ $validator = Validator::make($request->all(), [
|
|
311
|
+ 'openid' => 'required',
|
|
312
|
+ 'nickname' => 'required',
|
|
313
|
+ 'unionid' => 'required',
|
|
314
|
+ ],[
|
|
315
|
+ 'openid.required' => '微信用户openID不能为空',
|
|
316
|
+ 'unionid.required' => '微信用户unionid不能为空',
|
|
317
|
+ 'nickname.required' => '微信用户昵称不能为空',
|
|
318
|
+ ]);
|
|
319
|
+ if ($validator->fails()) {
|
|
320
|
+ return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
|
|
321
|
+ }
|
|
322
|
+ $channel_id = $request->header('channel_id', 0);
|
|
323
|
+ $openid = $request->get('openid');
|
|
324
|
+ $nickname = $request->get('nickname');
|
|
325
|
+ $sex = $request->get('sex');
|
|
326
|
+ $headimgurl = $request->get('headimgurl', '');
|
|
327
|
+ $unionid = $request->get('unionid', '');
|
|
328
|
+ $user_info = self::checkUserByWechat($openid, $unionid);
|
|
329
|
+ if ($user_info) {
|
|
330
|
+ $user_id = $user_info->id;
|
|
331
|
+ if (!$user_info->wechat_unionid) $user_info->wechat_unionid = $unionid;
|
|
332
|
+ $user_info->updated_at = time();
|
|
333
|
+ $user_info->login_num +=1;
|
|
334
|
+// $user_info = UserMigrateCount::userMigrateCount($user_info,$channel_id);
|
|
335
|
+ $user_info->save();
|
|
336
|
+ }else{
|
|
337
|
+ $user_info = new User();
|
|
338
|
+ $user_info->wechat_id = $openid;
|
|
339
|
+ $user_info->wechat_unionid = $unionid;
|
|
340
|
+ $user_info->nickname = $nickname;
|
|
341
|
+ $user_info->gender = $sex == 1 ? 'man' : 'woman';
|
|
342
|
+ $user_info->headimgurl = $headimgurl;
|
|
343
|
+ $user_info->phone_verified = 0;
|
|
344
|
+ $user_info->created_at = time();
|
|
345
|
+ $user_info->updated_at = time();
|
|
346
|
+ $user_info->channel_id = $channel_id;
|
|
347
|
+ $user_info->save();
|
|
348
|
+ }
|
|
349
|
+ $token = UserApiToken::createToken($user_info->id);//生成token
|
|
350
|
+ User::updateToken($user_info->id, $token);
|
|
351
|
+ return $this->response->array(self::returnValue(['data'=> ['token' => $token]]));
|
|
352
|
+ }
|
|
353
|
+
|
|
354
|
+ /**
|
|
355
|
+ * checkUser api
|
|
356
|
+ *
|
|
357
|
+ * @return \Illuminate\Http\Response
|
|
358
|
+ */
|
|
359
|
+ public function checkMobile(Request $request)
|
|
360
|
+ {
|
|
361
|
+ $validator = Validator::make($request->all(), [
|
|
362
|
+ 'mobile' => 'required|regex:/^1[34578][0-9]{9}$/',
|
|
363
|
+ ],[
|
|
364
|
+ 'mobile.required' => '手机号不能为空',
|
|
365
|
+ 'mobile.regex' => '手机号格式错误',
|
|
366
|
+ ]);
|
|
367
|
+ if ($validator->fails()) {
|
|
368
|
+ return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
|
|
369
|
+ }
|
|
370
|
+ $mobile = $request->get('mobile');
|
|
371
|
+ $check_type = $request->get('check_type', 1); //1 检测是否被注册 2检测是否被绑定
|
|
372
|
+ $user_info = self::checkUserByMobile($mobile);
|
|
373
|
+ if ($user_info) {
|
|
374
|
+ if ($check_type == 1) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(40012)], 40012));
|
|
375
|
+ if ($check_type == 2) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(40016)], 40016));
|
|
376
|
+ }else{
|
|
377
|
+ $res = User::updatePhoneVerified($mobile, 1);
|
|
378
|
+ if (!$res) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(30004)], 30004));
|
|
379
|
+ }
|
|
380
|
+ return $this->response->array(self::returnValue([]));
|
|
381
|
+ }
|
|
382
|
+
|
|
383
|
+ /**
|
|
384
|
+ * getNewPassword api
|
|
385
|
+ *
|
|
386
|
+ * @return \Illuminate\Http\Response
|
|
387
|
+ */
|
|
388
|
+ public function getNewPassword(Request $request)
|
|
389
|
+ {
|
|
390
|
+ $validator = Validator::make($request->all(), [
|
|
391
|
+ 'mobile' => 'required',
|
|
392
|
+ 'password' => 'required|string|min:6',
|
|
393
|
+ 'c_password' => 'required|same:password',
|
|
394
|
+ ],[
|
|
395
|
+ 'mobile.required' => '手机号不能为空',
|
|
396
|
+ 'password.required' => '密码不能为空',
|
|
397
|
+ 'password.min' => '密码不得小于六位数',
|
|
398
|
+ 'c_password.required' => '确认密码不能为空',
|
|
399
|
+ 'c_password.same' => '输入的两次密码不同',
|
|
400
|
+ ]);
|
|
401
|
+ if ($validator->fails()) {
|
|
402
|
+ return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
|
|
403
|
+ }
|
|
404
|
+ $mobile = $request->get('mobile');
|
|
405
|
+ $password = $request->get('password');
|
|
406
|
+ $user_info = self::checkUserByMobile($mobile);
|
|
407
|
+ if ($user_info) {
|
|
408
|
+ $user_info->password = bcrypt($password);
|
|
409
|
+ if ($user_info->save()) {
|
|
410
|
+ return $this->response->array(self::returnValue([]));
|
|
411
|
+ } else {
|
|
412
|
+ return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10061)], 10061));
|
|
413
|
+ }
|
|
414
|
+ } else {
|
|
415
|
+ return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10051)], 10051));
|
|
416
|
+ }
|
|
417
|
+
|
|
418
|
+ }
|
|
419
|
+
|
|
420
|
+ /**
|
|
421
|
+ * updatePassword api
|
|
422
|
+ *
|
|
423
|
+ * @return \Illuminate\Http\Response
|
|
424
|
+ */
|
|
425
|
+ public function updatePassword(Request $request)
|
|
426
|
+ {
|
|
427
|
+ $validator = Validator::make($request->all(), [
|
|
428
|
+ 'mobile' => 'required',
|
|
429
|
+ 'old_password' => 'required',
|
|
430
|
+ 'password' => 'required|string|min:6',
|
|
431
|
+ 'c_password' => 'required|same:password',
|
|
432
|
+ ],[
|
|
433
|
+ 'mobile.required' => '手机号不能为空',
|
|
434
|
+ 'old_password.required' => '旧密码不能为空',
|
|
435
|
+ 'password.required' => '密码不得小于六位数',
|
|
436
|
+ 'password.min' => '密码不得小于六位数',
|
|
437
|
+ 'c_password.required' => '确认密码不能为空',
|
|
438
|
+ 'c_password.same' => '输入的两次密码不同',
|
|
439
|
+ ]);
|
|
440
|
+ if ($validator->fails()) {
|
|
441
|
+ return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
|
|
442
|
+ }
|
|
443
|
+ $mobile = $request->get('mobile');
|
|
444
|
+ $password = $request->get('password');
|
|
445
|
+ $old_password = $request->get('old_password');
|
|
446
|
+ $user_info = self::checkUserByMobile($mobile);
|
|
447
|
+ if (!$user_info) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10006)], 10006));
|
|
448
|
+ if (!\Hash::check($old_password, $user_info->password)) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10062)], 10062));
|
|
449
|
+ $user_info->password = bcrypt($password);
|
|
450
|
+ if (!$user_info->save()) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10063)], 10063));
|
|
451
|
+ return $this->response->array(self::returnValue([]));
|
|
452
|
+ }
|
|
453
|
+
|
|
454
|
+ /**
|
|
455
|
+ * updatePersonalCenter api
|
|
456
|
+ *
|
|
457
|
+ * @return \Illuminate\Http\Response
|
|
458
|
+ */
|
|
459
|
+ public function updatePersonalCenter(Request $request)
|
|
460
|
+ {
|
|
461
|
+ $username = $request->get('username','');
|
|
462
|
+ $gender = $request->get('gender','man');
|
|
463
|
+ $user_info = User::getCurrentUser();
|
|
464
|
+ if ($username) $user_info->username = $username;
|
|
465
|
+ if ($gender) $user_info->gender = $gender;
|
|
466
|
+ if ($request->hasFile('avatar')) {
|
|
467
|
+ if ($request->file('avatar')->isValid()) {
|
|
468
|
+ //判断格式
|
|
469
|
+ $extension = array('image/jpeg','image/png','image/pjpeg','image/gif');
|
|
470
|
+// $ex = $request->file('avatar')->getMimeType();
|
|
471
|
+// if (!in_array($ex, $extension)) {
|
|
472
|
+// return response()->json(['error' => array(ApiHander::str(10065)), 'code' => 10065], $this->successStatus);
|
|
473
|
+// }
|
|
474
|
+ //判断文件是否存在,如果源文件存在,就删除源文件
|
|
475
|
+ if ($user_info->avatar) {
|
|
476
|
+ $oldfilePath = "." . $user_info->avatar;
|
|
477
|
+ if (file_exists($oldfilePath)) {
|
|
478
|
+ unlink($oldfilePath);
|
|
479
|
+ }
|
|
480
|
+ }
|
|
481
|
+ //1.文件保存路径
|
|
482
|
+ try {
|
|
483
|
+ $path = 'Uploads/' . date('Ymd');
|
|
484
|
+ $suffix = $request->file('avatar')->getClientOriginalExtension();
|
|
485
|
+ $tmp_path = $request->file('avatar')->getRealPath();
|
|
486
|
+ $fileName = $path.'/'.time() . mt_rand(100000, 999999) . '.' . $suffix;
|
|
487
|
+ $res = OSS::upload($fileName, $tmp_path);
|
|
488
|
+ if (!$res) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10064)], 10064));
|
|
489
|
+ $user_info->avatar = trim('/' . $fileName, '.');
|
|
490
|
+ } catch (Exception $e) {
|
|
491
|
+ return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10064)], 10064));
|
|
492
|
+ }
|
|
493
|
+ } else {
|
|
494
|
+ return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10064)], 10064));
|
|
495
|
+ }
|
|
496
|
+ }
|
|
497
|
+ $user_info->updated_at = time();
|
|
498
|
+ if (!$user_info->save()) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10026)], 10026));
|
|
499
|
+ return $this->response->array(self::returnValue($user_info, 0));
|
|
500
|
+ }
|
|
501
|
+
|
|
502
|
+ /**
|
|
503
|
+ * bindMobile api
|
|
504
|
+ *
|
|
505
|
+ * @return \Illuminate\Http\Response
|
|
506
|
+ */
|
|
507
|
+ public function bindMobile(Request $request)
|
|
508
|
+ {
|
|
509
|
+ $validator = Validator::make($request->all(), [
|
|
510
|
+ 'mobile' => 'required|regex:/^1[34578][0-9]{9}$/',
|
|
511
|
+ 'password' => 'required|string|min:6',
|
|
512
|
+ 'c_password' => 'required|same:password',
|
|
513
|
+ ],[
|
|
514
|
+ 'mobile.regex' => '手机格式错误',
|
|
515
|
+ 'password.required' => '密码不能为空',
|
|
516
|
+ 'password.min' => '密码不得小于六位数',
|
|
517
|
+ 'c_password.required' => '确认密码不能为空',
|
|
518
|
+ 'c_password.same' => '输入的两次密码不同',
|
|
519
|
+ ]);
|
|
520
|
+ if ($validator->fails()) {
|
|
521
|
+ return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
|
|
522
|
+ }
|
|
523
|
+ $mobile = $request->get('mobile');
|
|
524
|
+ $password = $request->get('password');
|
|
525
|
+ $user_info = Base::getUserInfo();
|
|
526
|
+ $user_info->mobile = $mobile;
|
|
527
|
+ $user_info->password = bcrypt($password);
|
|
528
|
+ $user_info->phone_verified = 4;
|
|
529
|
+ if (!$user_info->save()) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(40020)], 40020));
|
|
530
|
+ self::deleteUserMobileNoRegister($mobile);
|
|
531
|
+ return $this->response->array(self::returnValue([]));
|
|
532
|
+ }
|
|
533
|
+
|
|
534
|
+ public function bindWeChat(Request $request)
|
|
535
|
+ {
|
|
536
|
+ $validator = Validator::make($request->all(), [
|
|
537
|
+ 'openid' => 'required',
|
|
538
|
+ 'nickname' => 'required',
|
|
539
|
+ ],[
|
|
540
|
+ 'openid.required' => '微信用户openID不能为空',
|
|
541
|
+ 'nickname.required' => '微信用户昵称不能为空',
|
|
542
|
+ ]);
|
|
543
|
+ if ($validator->fails()) {
|
|
544
|
+ return response()->json(['error' => Base::formatValidator($validator), 'code' => 10009]);
|
|
545
|
+ }
|
|
546
|
+ $openid = $request->get('openid');
|
|
547
|
+ $unionid = $request->get('unionid');
|
|
548
|
+ $nickname = $request->get('nickname');
|
|
549
|
+ $sex = $request->get('sex');
|
|
550
|
+ $headimgurl = $request->get('headimgurl');
|
|
551
|
+ $user_info = self::checkUserByWechat($openid, $unionid);
|
|
552
|
+ if ($user_info) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(40018)], 40018));
|
|
553
|
+ $user_id = Base::getUserId();
|
|
554
|
+ $user_info = User::find($user_id);
|
|
555
|
+ $user_info->wechat_id = $openid;
|
|
556
|
+ $user_info->nickname = $nickname;
|
|
557
|
+ $user_info->gender = $sex == 1 ? 'man' : 'woman';
|
|
558
|
+ $user_info->headimgurl = $headimgurl;
|
|
559
|
+ if (!$user_info->save()) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(40019)], 40019));
|
|
560
|
+ return $this->response->array(self::returnValue([]));
|
|
561
|
+ }
|
|
562
|
+
|
|
563
|
+ /**
|
|
564
|
+ * addUserMessage api
|
|
565
|
+ *
|
|
566
|
+ * @return \Illuminate\Http\Response
|
|
567
|
+ */
|
|
568
|
+ public function addUserMessage(Request $request)
|
|
569
|
+ {
|
|
570
|
+ $validator = Validator::make($request->all(), [
|
|
571
|
+ 'message' => 'required',
|
|
572
|
+// 'user_contact' => 'required',
|
|
573
|
+ ],[
|
|
574
|
+ 'message.required' => '留言信息不能为空',
|
|
575
|
+// 'user_contact.required' => '联系方式不能为空',
|
|
576
|
+ ]);
|
|
577
|
+ if ($validator->fails()) {
|
|
578
|
+ return response()->json(['error' => Base::formatValidator($validator), 'code' => 10009]);
|
|
579
|
+ }
|
|
580
|
+ $version = $request->header('version', null);
|
|
581
|
+ $user_contact = $request->get('user_contact', '');
|
|
582
|
+ $message = $request->get('message');
|
|
583
|
+ $user_id = Base::getUserId();
|
|
584
|
+ $res = DB::insert("insert into user_message(user_id, message, created_at, updated_at, version, user_contact) VALUES (?, ?, ?, ?, ?, ?)",[$user_id, $message, time(), time(), $version, $user_contact]);
|
|
585
|
+ if (!$res) return response()->json(['error' => array(ApiHander::str(90003)), 'code' => 90003]);
|
|
586
|
+ return response()->json(['success' => array(ApiHander::str(0)), 'code' => 0]);
|
|
587
|
+ }
|
|
588
|
+
|
|
589
|
+ public static function checkUserByWechat($openid, $unionid)
|
|
590
|
+ {
|
|
591
|
+ $user_info = null;
|
|
592
|
+ if ($unionid) $user_info = User::where('wechat_unionid', $unionid)->first();
|
|
593
|
+ if (!$user_info) $user_info = User::where('wechat_id', $openid)->first();
|
|
594
|
+ return $user_info;
|
|
595
|
+ }
|
|
596
|
+
|
|
597
|
+ public static function checkUserByMobile($mobile, $type = true)
|
|
598
|
+ {
|
|
599
|
+ $user_info = User::where('mobile', $mobile)
|
|
600
|
+ ->where(function($query) use($type){
|
|
601
|
+ if ($type) $query->where('phone_verified', 4);
|
|
602
|
+ })->first();
|
|
603
|
+ return $user_info;
|
|
604
|
+ }
|
|
605
|
+
|
|
606
|
+ public static function deleteUserMobileNoRegister($mobile)
|
|
607
|
+ {
|
|
608
|
+ User::where('mobile',$mobile)->where('phone_verified','!=', 4)->where('wechat_id' , '=', NUll)->delete();
|
|
609
|
+ }
|
|
610
|
+
|
|
611
|
+}
|