13283339616 6 vuotta sitten
vanhempi
commit
f7cd8ff547

+ 611 - 0
app/Api/V1/Controllers/UserController.php

@@ -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
+}

+ 26 - 0
app/Api/V1/Controllers/UserMessageController.php

@@ -0,0 +1,26 @@
1
+<?php
2
+namespace App\Api\V1\Controllers;
3
+
4
+use App\Api\V1\Controllers\BaseController;
5
+use App\Models\UserMessage;
6
+use Illuminate\Support\Facades\Hash;
7
+use Dingo\Api\Routing\Helpers;
8
+use Illuminate\Foundation\Auth\RegistersUsers;
9
+use Illuminate\Http\Request;
10
+use Illuminate\Support\Facades\Validator;
11
+
12
+class UserMessageController extends BaseController {
13
+
14
+ public function feedBack(Request $request){
15
+    if(!empty(trim($request->input('message')))){
16
+        $userMessage =new UserMessage();
17
+        if($userMessage->addFeedBack($request)){
18
+            return self::returnValue(['msg'=>'Feedback is successful']);
19
+        }else{
20
+            return self::returnValue(['msg'=>'Feedback failed'],9999);
21
+        }
22
+    }else{
23
+        return self::returnValue(['msg'=>'Comment can not be empty'], 4001);
24
+    }
25
+ }
26
+}

+ 15 - 0
app/Models/Channel.php

@@ -0,0 +1,15 @@
1
+<?php
2
+
3
+namespace App\Models;
4
+
5
+use Illuminate\Database\Eloquent\Model;
6
+use Illuminate\Support\Facades\DB;
7
+
8
+class Channel extends Model
9
+{
10
+    protected $table = 'channel';
11
+    public static function detail($id) {
12
+	$channel = DB::table('channel')->where('channel_id', $id)->first();
13
+	return $channel;
14
+    }
15
+}

+ 25 - 0
app/Models/Notice.php

@@ -0,0 +1,25 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: zhangda
5
+ * Date: 2018/1/11
6
+ * Time: 下午4:35
7
+ */
8
+
9
+namespace App\Models;
10
+
11
+
12
+use Illuminate\Database\Eloquent\Model;
13
+
14
+class Notice extends Model
15
+{
16
+    protected $table = 'notice';
17
+
18
+    public $timestamps = false;
19
+
20
+    public static function getNoticeByChannelId($channel_id, $type = 1)
21
+    {
22
+        $data = self::select('content', 'title', 'url')->where(['channel_id' => $channel_id, 'status' => 1, 'type' => $type])->get();
23
+        return $data;
24
+    }
25
+}

+ 70 - 0
app/Models/Securecode.php

@@ -0,0 +1,70 @@
1
+<?php
2
+
3
+namespace App\Models;
4
+
5
+use Illuminate\Database\Eloquent\Model;
6
+use Illuminate\Support\Facades\DB;
7
+use YPSMS;
8
+class Securecode extends Model
9
+{
10
+    protected $table = 'securecode';
11
+    public $timestamps = false;
12
+
13
+    public static function sendPhoneVerify($user, $type = 1, $operation='verify', $timeout = 900) {
14
+	$data = self::getLastSms($user->id, $operation);
15
+	$cur_time = time();
16
+	if(!$data || $data->timeout_time < $cur_time) {
17
+	    //创建数据
18
+	    $code = mt_rand(1000, 9999); 
19
+	    self::insertSms($user->id, $operation, $code, $timeout);
20
+	} else {
21
+	    $code = $data->code;
22
+	}
23
+
24
+	//验证码是否在60s
25
+	if( $data && ($data->create_time + 60 - $cur_time)>0 ){
26
+            return ['success' => false, 'wait_time' => (60 + $data->create_time - $cur_time)];
27
+        }
28
+	if($type == 1) {
29
+            $msg = self::genVerifyContent($code);
30
+            if($operation!='verify') {
31
+                $msg = self::genWjContent($code);
32
+            }
33
+            YPSMS::sendSMS($user->mobile, $msg);
34
+        } else{
35
+            YPSMS::sendVoiceCheck($user->mobile, $code);
36
+        }
37
+        return ['success' => true, 'wait_time' => 60];
38
+	
39
+    }
40
+
41
+
42
+    public static function receivePhoneVerify($userid, $code, $operation='verify') {
43
+	$securecode = DB::table('securecode')->where('user_id', $userid)->where('operation', $operation)->orderBy('id', 'desc')->first();
44
+        if (!$securecode || $securecode->code != $code || $securecode->timeout_time < time()) {
45
+            return false;
46
+        }
47
+        return true;
48
+    }
49
+
50
+    public static function genVerifyContent($code) {
51
+        return '【菜谱】验证码'.$code.',此验证码3分钟内有效,千万不要告诉别人!';
52
+    }
53
+
54
+    public static function genWjContent($code) {
55
+        return '【菜谱】验证码'.$code.'(3分钟有效),您正在修改菜谱用户密码,感谢您的支持!';
56
+    }
57
+
58
+    public static function getLastSms($userid, $operation) {
59
+	$securecode = DB::table('securecode')->where('user_id', $userid)->where('operation', $operation)->orderBy('id', 'desc')->first();
60
+	return $securecode;
61
+    }
62
+
63
+    public static function insertSms($userid, $operation, $code, $timeout) {
64
+	$cur_time = time();
65
+	$id = DB::table('securecode')->insertGetId(
66
+	    ['user_id' => $userid, 'operation' => $operation, 'code'=>$code, 'create_time'=>$cur_time, 'update_time'=>$cur_time, 'timeout_time'=>$cur_time + $timeout]
67
+	);
68
+	return $id;
69
+    }
70
+}

+ 91 - 0
app/libs/sms/YPSMS.php

@@ -0,0 +1,91 @@
1
+<?php
2
+use App\libs\sms;
3
+define("YP_SMS_KEY", "fbdb5f2ddae13c2f4a592348bfe52137");
4
+define("YP_VOICE_URL", "http://voice.yunpian.com/v2/voice/send.json");
5
+define("YP_TPL_URL", "https://sms.yunpian.com/v2/sms/tpl_single_send.json");
6
+define("YP_TPL_ID", "2122814");
7
+
8
+class YPSMS{
9
+    private static function init(){
10
+        $ch = curl_init();
11
+        /* 设置验证方式 */
12
+        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
13
+            'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
14
+        /* 设置返回结果为流 */
15
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
16
+
17
+        /* 设置超时时间*/
18
+        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
19
+
20
+        /* 设置通信方式 */
21
+        curl_setopt($ch, CURLOPT_POST, 1);
22
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
23
+        return $ch;
24
+
25
+    }
26
+    public static function sendSMS($phone,$text){
27
+        $ch=self::init();
28
+
29
+        // $data = array('tpl_id' => YP_TPL_ID, 'tpl_value' => ('#code#').'='.urlencode($code), 'apikey' => YP_SMS_KEY, 'mobile' => $phone);
30
+        // $json_data = self::tpl_send($ch,$data);
31
+        // $array = json_decode($json_data,true);
32
+        // echo '<pre>';print_r($array);
33
+        $data=array('tpl_id' => YP_TPL_ID,'text'=>$text,'apikey'=>YP_SMS_KEY,'mobile'=>$phone);
34
+        $json_data = self::send($ch,$data);
35
+        //print_r($json_data);    ******************************maybe影响验证码发出
36
+        // $array = json_decode($json_data,true);
37
+        // echo '<pre>';print_r($array);
38
+        curl_close($ch);
39
+        return $json_data;
40
+    }
41
+    public static function sendVoiceCheck($phone,$code){
42
+        $ch=self::init();
43
+        $data=array('code'=>$code,'apikey'=>YP_SMS_KEY,'mobile'=>$phone);
44
+        $json_data =self::voice_send($ch,$data);
45
+        // $array = json_decode($json_data,true);
46
+        // echo '<pre>';print_r($array);
47
+        curl_close($ch);
48
+        return $json_data;
49
+
50
+    }
51
+    private static function checkErr($result,$error) {
52
+        if($result === false)
53
+        {
54
+            echo 'Curl error: ' . $error;
55
+        }
56
+//        else
57
+//        {
58
+//            echo '操作完成没有任何错误';
59
+//        }
60
+    }
61
+    private static function send($ch,$data){
62
+        curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
63
+        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
64
+        $result = curl_exec($ch);
65
+        $error = curl_error($ch);
66
+        self::checkErr($result,$error);
67
+        return $result;
68
+    }
69
+    private static function voice_send($ch,$data){
70
+        curl_setopt ($ch, CURLOPT_URL, YP_VOICE_URL);
71
+        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
72
+        $result = curl_exec($ch);
73
+        $error = curl_error($ch);
74
+        self::checkErr($result,$error);
75
+        return $result;
76
+    }
77
+    private static function tpl_send($ch,$data){
78
+        curl_setopt ($ch, CURLOPT_URL, YP_TPL_URL);
79
+        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
80
+        $result = curl_exec($ch);
81
+        $error = curl_error($ch);
82
+        self::checkErr($result,$error);
83
+        return $result;
84
+    }
85
+}
86
+// YPSMS::init();
87
+//$code=100;
88
+//$minutes=3;
89
+// YPSMS::sendSMS('13613665865','【钱多多随手记】您的验证码是' . $code . ',有效期为' . $minutes . '分钟,请尽快验证。');
90
+// YPSMS::sendVoiceCheck('13613665865','123456');
91
+

+ 43 - 0
routes/api.php

@@ -14,6 +14,49 @@ $api = app('Dingo\Api\Routing\Router');
14 14
 
15 15
 $api->version('v1', ['namespace' => 'App\Api\V1\Controllers'], function ($api) {
16 16
     // $api->group(['middleware' => 'sign.token'], function($api) {
17
+
18
+    //发送验证码
19
+//    $api->post('user/sendode', 'UserController@sendcode');
20
+    $api->post('user/logincode', 'UserController@loginCode');
21
+
22
+    //账户密码登陆
23
+    $api->post('user/login', 'UserController@login');
24
+    //微信登陆
25
+    $api->post('user/weChatLogin', 'UserController@weChatLogin');
26
+    //验证码动态登陆
27
+    //Route::post('codeLogin', 'UserController@codeLogin');
28
+    //注册
29
+    $api->post('user/register', 'UserController@register');
30
+    //检查手机号是否被注册
31
+    $api->post('user/checkMobile', 'UserController@checkMobile');
32
+    //发送验证码
33
+    $api->post('user/sendCode', 'UserController@sendCode');
34
+    //校验验证码
35
+    $api->post('user/validateCode', 'UserController@validateCode');
36
+    //找回密码
37
+    $api->post('user/getNewPassword', 'UserController@getNewPassword');
38
+    //登陆后才能使用的
39
+    $api->group(['middleware' => 'token.auth'], function ($api) {
40
+        // Endpoints registered here will have the "foo" middleware applied.
41
+        //获取用户个人信息
42
+//        $api->get('personalCenter', 'UserController@personalCenter');
43
+        //修改用户信息
44
+        $api->post('user/updatePersonalCenter', 'UserController@updatePersonalCenter');
45
+        //修改密码
46
+        $api->post('user/updatePassword', 'UserController@updatePassword');
47
+        //绑定手机号
48
+        $api->post('user/bindMobile', 'UserController@bindMobile');
49
+        //绑定微信号
50
+        $api->post('user/bindWeChat', 'UserController@bindWeChat');
51
+
52
+        $api->get('user/personalcentor', 'UserController@personalCentor');
53
+        $api->get('user/logout', 'UserController@logout');
54
+        //用户反馈
55
+        $api->post('user_message/feedBack', 'UserMessageController@feedBack');
56
+    });
57
+
58
+
59
+
17 60
     //功能列表
18 61
     $api->post('features/getCategoryList', 'FeaturesController@getCategoryList');
19 62
     $api->post('features/getTagList', 'FeaturesController@getTagList');

+ 1 - 0
vendor/composer/autoload_classmap.php

@@ -3285,6 +3285,7 @@ return array(
3285 3285
     'Webmozart\\Assert\\Assert' => $vendorDir . '/webmozart/assert/src/Assert.php',
3286 3286
     'XdgBaseDir\\Xdg' => $vendorDir . '/dnoegel/php-xdg-base-dir/src/Xdg.php',
3287 3287
     'YMSMS' => $baseDir . '/app/libs/sms/YMSMS.php',
3288
+    'YPSMS' => $baseDir . '/app/libs/sms/YPSMS.php',
3288 3289
     'Solr' => $baseDir . '/app/libs/solr/Solr.php',
3289 3290
     'phpDocumentor\\Reflection\\DocBlock' => $vendorDir . '/phpdocumentor/reflection-docblock/src/DocBlock.php',
3290 3291
     'phpDocumentor\\Reflection\\DocBlockFactory' => $vendorDir . '/phpdocumentor/reflection-docblock/src/DocBlockFactory.php',

+ 1 - 0
vendor/composer/autoload_static.php

@@ -3636,6 +3636,7 @@ class ComposerStaticInit4ce4212df89b604de9838fefa45804de
3636 3636
         'Webmozart\\Assert\\Assert' => __DIR__ . '/..' . '/webmozart/assert/src/Assert.php',
3637 3637
         'XdgBaseDir\\Xdg' => __DIR__ . '/..' . '/dnoegel/php-xdg-base-dir/src/Xdg.php',
3638 3638
         'YMSMS' => __DIR__ . '/../..' . '/app/libs/sms/YMSMS.php',
3639
+        'YPSMS' => __DIR__ . '/../..' . '/app/libs/sms/YPSMS.php',
3639 3640
         'Solr' => __DIR__ . '/../..' . '/app/libs/solr/Solr.php',
3640 3641
         'phpDocumentor\\Reflection\\DocBlock' => __DIR__ . '/..' . '/phpdocumentor/reflection-docblock/src/DocBlock.php',
3641 3642
         'phpDocumentor\\Reflection\\DocBlockFactory' => __DIR__ . '/..' . '/phpdocumentor/reflection-docblock/src/DocBlockFactory.php',