123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612 |
- <?php
- namespace App\Api\V1\Controllers;
- use App\Api\V1\Controllers\BaseController;
- use Illuminate\Support\Facades\Auth;
- use App\User;
- use Illuminate\Support\Facades\Hash;
- use Dingo\Api\Exception\StoreResourceFailedException;
- use Dingo\Api\Routing\Helpers;
- use Illuminate\Foundation\Auth\RegistersUsers;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Validator;
- use App\Exceptions\ApiHander;
- use Illuminate\Foundation\Auth\AuthenticatesUsers;
- use App\Models\Securecode;
- use App\Models\Base;
- use App\Models\Channel;
- use UserApiToken;
- class UserController extends BaseController {
-
- protected function validator(array $data)
- {
- return Validator::make($data, [
- 'name' => 'required|unique:users',
- 'phone' => 'required|max:255|unique:users',
- 'password' => 'required|min:6',
- ]);
- }
- protected function create(array $data)
- {
- return User::create([
- 'name' => $data['name'],
- 'phone' => $data['phone'],
- 'password' => bcrypt($data['password'])
- ]);
- }
- /**
- *发送验证码uth::guard($this->guard)->logout();
- */
- // public function sendcode(Request $request) {
- // //手机号验证
- // $phone = $request->get('phone');
- // $code_type = (int)$request->get('send_type'); //1 为短信 2 为语音
- // $send_type = $request->get('send_type'); //1 为普通账户密码注册发送验证码, 2 为动态登陆发送验证码
- // $ttl = $request->get('ttl'); //1 为普通账户密码注册发送验证码, 2 为动态登陆发送验证码
- // $sign = $request->get('sign');
- // //验证参数
- // if(!$this->validPhone($phone)) {
- // return $this->response->array(self::returnValue(['msg'=>'mobile is no legal'], 1005));
- // }
- // if(!in_array($code_type, array(1,2))) {
- // return $this->response->array(self::returnValue(['msg'=>'sms type is error'], 1006));
- // }
- //
- // if(empty($ttl)) {
- // return $this->response->array(self::returnValue(['msg'=>'ttl is error'], 1008));
- // }
- //
- // if(!$this->validSign($phone, $code_type, $send_type, $ttl, $sign)) {
- // return $this->response->array(self::returnValue(['msg'=>'sign is no legal'], 1007));
- // }
- // $user = self::checkUserByMobile($phone);
- // if(!$user) {
- // $password = mt_rand(100000,999999);
- // $user = $this->create(['phone'=>$phone, 'name'=>$phone, 'password'=>'']);
- // }
- // if(!$user) {
- // return $this->response->array(self::returnValue(['msg'=>'database error'], 9999));
- // }
- // $data = Securecode::sendPhoneVerify($user, $code_type);
- // return $this->response->array(self::returnValue($data));
- // }
- /**
- *
- *sign验证
- */
- public function validSign($mobile, $code_type, $send_type, $ttl, $sign) {
- $params = array('mobile'=>$mobile, 'code_type'=>$code_type, 'send_type'=>$send_type, 'ttl'=> $ttl, 'sign'=>$sign);
- $makesign = $this->getSignature($params, Config('constants.SMS_SECRET_KEY'));
- if($makesign == $sign) {
- return true;
- }
- return false;
- }
- public function getSignature($params, $secret_key) {
- // 按数组键名 正序排序
- ksort($params);
- $tem = array();
- foreach ($params as $k => $v) {
- if ($k !== 'sign') {
- $tem[] = "$k=$v";
- }
- }
- $sk = implode('&', $tem) . $secret_key;
- return md5($sk);
- }
- public function validPhone($phone) {
- if(preg_match("/^1[345678]{1}\d{9}$/",$phone)){
- return true;
- }
- return false;
- }
- public function logincode(Request $request) {
- $phone = $request->get('phone');
- $code = $request->get('code');
- //验证参数
- if(!$this->validPhone($phone)) {
- return $this->response->array(self::returnValue(['msg'=>'mobile is no legal'], 1005));
- }
- $user = self::checkUserByMobile($phone);
- if(!$user) {
- return $this->response->array(self::returnValue(['msg'=>'user is not exist'], 1004));
- }
- $flag = Securecode::receivePhoneVerify($user->id, $code);
- if(!$flag && $phone!='15801649867') {
- return $this->response->array(self::returnValue(['msg'=>'code is error'], 1004));
- }
- $token = UserApiToken::createToken($user->id);//生成token
- User::updateUserLoginInfo($user->id, array('token'=>$token,'last_login_time'=>time(),'login_num'=>$user->login_num));
- $user['token'] = $token;
- return $this->response->array(self::returnValue(['data'=>$user]));
- }
- public function personalCentor(Request $request) {
- $user = User::getCurrentUser();
- $channel_id = $request->header('source');
- $channel = Channel::detail($channel_id);
- $user->iOS_share_url = $channel ? ($channel->url ? $channel->url : "http://baidu.com") : "http://baidu.com";
- return $this->response->array(self::returnValue($user, 0));
- }
- // public static function checkUserByMobile($phone)
- // {
- // $userinfo = User::where('phone', $phone)->where('valid', 'valid')->first();
- // return $userinfo;
- // }
- /**
- *发送验证码
- */
- public function sendCode(Request $request) {
- $validator = Validator::make($request->all(), [
- 'mobile' => 'required|regex:/^1[345678][0-9]{9}$/',
- ], [
- 'mobile.required' => '手机号不能为空',
- 'mobile.regex' => '手机号格式错误',
- ]);
- if ($validator->fails()) {
- return $this->response->array(self::returnValue(['msg'=> Base::formatValidator($validator)], 10009));
- }
- $mobile = $request->get('mobile');
- $code_type = (int)$request->get('code_type', 1); //1 为短信 2 为语音
- $send_type = $request->get('send_type', 1); //1 为普通账户密码注册发送验证码, 2 为动态登陆发送验证码
- $verify = $request->get('verify', 0);
- $ttl = $request->get('ttl', 1); //1 为普通账户密码注册发送验证码, 2 为动态登陆发送验证码
- $type = $verify ? 2 : 0;
- $sign = $request->get('sign');
- // 手机号验证
- if(!$this->validSign($mobile, $code_type, $send_type, $ttl, $sign)) {
- return $this->response->array(self::returnValue(['msg'=>'sign is no legal'], 1007));
- }
- $user_info = User::updatePhoneVerified($mobile, $type);
- if (!$user_info) {
- return $this->response->array(self::returnValue(['msg'=> Base::formatValidator($validator)], 10009));
- }
- $data = Securecode::sendPhoneVerify($user_info, $code_type);
- if (!$data['success'])return $this->response->array(self::returnValue(['msg'=> '请求无效,请在60秒后重试'], 10055));
- return $this->response->array(self::returnValue(['msg'=>'短信验证码发送成功,请注意查收']));
- }
- /**
- * validateCode api
- *
- * @return \Illuminate\Http\Response
- */
- public function validateCode(Request $request)
- {
- //验证数据
- $validator = Validator::make($request->all(), [
- 'mobile' => 'required',
- 'verifyCode' => 'required',
- //more...
- ], [
- 'mobile.required' => '手机号不能为空',
- 'verifyCode.required' => '验证码不能为空',
- ]);
- if ($validator->fails()) {
- return $this->response->array(self::returnValue(['msg'=> Base::formatValidator($validator)], 10009));
- }
- $mobile = $request->get('mobile');
- $verify = $request->get('verify', 0);
- $type = $verify ? 3 : 0;
- $verifyCode = $request->get('verifyCode');
- $user_info = User::updatePhoneVerified($mobile, $type);
- $flag = Securecode::receivePhoneVerify($user_info->id, $verifyCode);
- if (!$flag && $mobile != '15801649867') {
- return $this->response->array(self::returnValue(['msg'=> '验证码错误,请核对后在输入'], 10055));
- }
- return $this->response->array(self::returnValue([]));
- }
- /**
- * 退出重新生成token
- */
- public function logout(){
- $userid = User::getCurrentUser()->id;
- $token = UserApiToken::createToken($userid);//生成token
- User::updateToken($userid, $token);
- return $this->response->array(self::returnValue([], 0));
- }
- /**
- * register api
- *
- * @return \Illuminate\Http\Response
- */
- public function register(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'mobile' => 'required|regex:/^1[345678][0-9]{9}$/',
- 'password' => 'required|string|min:6',
- 'c_password' => 'required|same:password',
- ],[
- 'mobile.required' => '手机号码不能为空',
- 'mobile.regex' => '手机格式错误',
- 'password.required' => '密码不能为空',
- 'password.min' => '密码不得小于六位数',
- 'c_password.required' => '确认密码不能为空',
- 'c_password.same' => '输入的两次密码不同',
- ]);
- if ($validator->fails()) {
- return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
- }
- $mobile = $request->input('mobile');
- $password = $request->input('password');
- $channel_id = $request->header('source',0);
- $user_info = self::checkUserByMobile($mobile, false);
- if (!$user_info) {
- $user_info = new User();
- }
- $user_info->mobile = $mobile;
- $user_info->password = bcrypt($password);
- $user_info->created_at = time();
- $user_info->phone_verified = 4;
- $user_info->channel_id = $channel_id;
- if (!$user_info->save()) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(40017)], 40017));
- $token = UserApiToken::createToken($user_info->id);//生成token
- User::updateToken($user_info->id, $token);
- return $this->response->array(self::returnValue(['data' => ['token' => $token]], 0));
- }
- /**
- * login api
- *
- * @return \Illuminate\Http\Response
- */
- public function login(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'mobile' => 'required|regex:/^1[345678][0-9]{9}$/',
- 'password' => 'required|string|min:6',
- ],[
- 'mobile.required' => '手机号不能为空',
- 'mobile.regex' => '手机格式错误',
- 'password.required' => '密码不能为空',
- 'password.min' => '密码不得小于六位数',
- ]);
- if ($validator->fails()) {
- return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
- }
- $mobile = $request->request->get('mobile');
- $password = $request->request->get('password');
- $channel_id = $request->header('source',0);
- $user_info = self::checkUserByMobile($mobile);
- if ($user_info) {
- if (Auth::attempt(['mobile' => $mobile, 'password' => $password])) {
- $user = Auth::user();
- $user_id = $user->id;
- $token = UserApiToken::createToken($user_id);//生成token
- $user->updated_at = time();
- $user->login_num +=1;
- $user->token = $token;
- // $user = UserMigrateCount::userMigrateCount($user,$channel_id);
- $user->save();
- $success['token'] = $token;
- return $this->response->array(self::returnValue(['data' => $success], 0));
- } else {
- return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10060)], 10060));
- }
- }else{
- return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10051)], 10051));
- }
- }
- /**
- * weChatLogin api
- *
- * @return \Illuminate\Http\Response
- */
- public function weChatLogin(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'openid' => 'required',
- 'nickname' => 'required',
- 'unionid' => 'required',
- ],[
- 'openid.required' => '微信用户openID不能为空',
- 'unionid.required' => '微信用户unionid不能为空',
- 'nickname.required' => '微信用户昵称不能为空',
- ]);
- if ($validator->fails()) {
- return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
- }
- $channel_id = $request->header('source', 0);
- $openid = $request->get('openid');
- $nickname = $request->get('nickname');
- $sex = $request->get('sex');
- $headimgurl = $request->get('headimgurl', '');
- $unionid = $request->get('unionid', '');
- $user_info = self::checkUserByWechat($openid, $unionid);
- if ($user_info) {
- $user_id = $user_info->id;
- if (!$user_info->wechat_unionid) $user_info->wechat_unionid = $unionid;
- $user_info->updated_at = time();
- $user_info->login_num +=1;
- // $user_info = UserMigrateCount::userMigrateCount($user_info,$channel_id);
- $user_info->save();
- }else{
- $user_info = new User();
- $user_info->wechat_id = $openid;
- $user_info->wechat_unionid = $unionid;
- $user_info->nickname = $nickname;
- $user_info->gender = $sex == 1 ? 'man' : 'woman';
- $user_info->headimgurl = $headimgurl;
- $user_info->phone_verified = 0;
- $user_info->created_at = time();
- $user_info->updated_at = time();
- $user_info->channel_id = $channel_id;
- $user_info->save();
- }
- $token = UserApiToken::createToken($user_info->id);//生成token
- User::updateToken($user_info->id, $token);
- return $this->response->array(self::returnValue(['data'=> ['token' => $token]]));
- }
- /**
- * checkUser api
- *
- * @return \Illuminate\Http\Response
- */
- public function checkMobile(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'mobile' => 'required|regex:/^1[345678][0-9]{9}$/',
- ],[
- 'mobile.required' => '手机号不能为空',
- 'mobile.regex' => '手机号格式错误',
- ]);
- if ($validator->fails()) {
- return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
- }
- $mobile = $request->get('mobile');
- $check_type = $request->get('check_type', 1); //1 检测是否被注册 2检测是否被绑定
- $user_info = self::checkUserByMobile($mobile);
- if ($user_info) {
- if ($check_type == 1) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(40012)], 40012));
- if ($check_type == 2) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(40016)], 40016));
- }else{
- $res = User::updatePhoneVerified($mobile, 1);
- if (!$res) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(30004)], 30004));
- }
- return $this->response->array(self::returnValue([]));
- }
- /**
- * getNewPassword api
- *
- * @return \Illuminate\Http\Response
- */
- public function getNewPassword(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'mobile' => 'required',
- 'password' => 'required|string|min:6',
- 'c_password' => 'required|same:password',
- ],[
- 'mobile.required' => '手机号不能为空',
- 'password.required' => '密码不能为空',
- 'password.min' => '密码不得小于六位数',
- 'c_password.required' => '确认密码不能为空',
- 'c_password.same' => '输入的两次密码不同',
- ]);
- if ($validator->fails()) {
- return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
- }
- $mobile = $request->get('mobile');
- $password = $request->get('password');
- $user_info = self::checkUserByMobile($mobile);
- if ($user_info) {
- $user_info->password = bcrypt($password);
- if ($user_info->save()) {
- return $this->response->array(self::returnValue([]));
- } else {
- return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10061)], 10061));
- }
- } else {
- return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10051)], 10051));
- }
- }
- /**
- * updatePassword api
- *
- * @return \Illuminate\Http\Response
- */
- public function updatePassword(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'mobile' => 'required',
- 'old_password' => 'required',
- 'password' => 'required|string|min:6',
- 'c_password' => 'required|same:password',
- ],[
- 'mobile.required' => '手机号不能为空',
- 'old_password.required' => '旧密码不能为空',
- 'password.required' => '密码不得小于六位数',
- 'password.min' => '密码不得小于六位数',
- 'c_password.required' => '确认密码不能为空',
- 'c_password.same' => '输入的两次密码不同',
- ]);
- if ($validator->fails()) {
- return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
- }
- $mobile = $request->get('mobile');
- $password = $request->get('password');
- $old_password = $request->get('old_password');
- $user_info = self::checkUserByMobile($mobile);
- if (!$user_info) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10006)], 10006));
- if (!\Hash::check($old_password, $user_info->password)) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10062)], 10062));
- $user_info->password = bcrypt($password);
- if (!$user_info->save()) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10063)], 10063));
- return $this->response->array(self::returnValue([]));
- }
- /**
- * updatePersonalCenter api
- *
- * @return \Illuminate\Http\Response
- */
- public function updatePersonalCenter(Request $request)
- {
- $username = $request->get('username','');
- $gender = $request->get('gender','man');
- $user_info = User::getCurrentUser();
- if ($username) $user_info->username = $username;
- if ($gender) $user_info->gender = $gender;
- if ($request->hasFile('avatar')) {
- if ($request->file('avatar')->isValid()) {
- //判断格式
- $extension = array('image/jpeg','image/png','image/pjpeg','image/gif');
- // $ex = $request->file('avatar')->getMimeType();
- // if (!in_array($ex, $extension)) {
- // return response()->json(['error' => array(ApiHander::str(10065)), 'code' => 10065], $this->successStatus);
- // }
- //判断文件是否存在,如果源文件存在,就删除源文件
- if ($user_info->avatar) {
- $oldfilePath = "." . $user_info->avatar;
- if (file_exists($oldfilePath)) {
- unlink($oldfilePath);
- }
- }
- //1.文件保存路径
- try {
- $path = 'Uploads/' . date('Ymd');
- $suffix = $request->file('avatar')->getClientOriginalExtension();
- $tmp_path = $request->file('avatar')->getRealPath();
- $fileName = $path.'/'.time() . mt_rand(100000, 999999) . '.' . $suffix;
- $res = OSS::upload($fileName, $tmp_path);
- if (!$res) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10064)], 10064));
- $user_info->avatar = trim('/' . $fileName, '.');
- } catch (Exception $e) {
- return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10064)], 10064));
- }
- } else {
- return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10064)], 10064));
- }
- }
- $user_info->updated_at = time();
- if (!$user_info->save()) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10026)], 10026));
- return $this->response->array(self::returnValue($user_info, 0));
- }
- /**
- * bindMobile api
- *
- * @return \Illuminate\Http\Response
- */
- public function bindMobile(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'mobile' => 'required|regex:/^1[345678][0-9]{9}$/',
- 'password' => 'required|string|min:6',
- 'c_password' => 'required|same:password',
- ],[
- 'mobile.regex' => '手机格式错误',
- 'password.required' => '密码不能为空',
- 'password.min' => '密码不得小于六位数',
- 'c_password.required' => '确认密码不能为空',
- 'c_password.same' => '输入的两次密码不同',
- ]);
- if ($validator->fails()) {
- return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
- }
- $mobile = $request->get('mobile');
- $password = $request->get('password');
- $user_info = Base::getUserInfo();
- $user_info->mobile = $mobile;
- $user_info->password = bcrypt($password);
- $user_info->phone_verified = 4;
- if (!$user_info->save()) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(40020)], 40020));
- self::deleteUserMobileNoRegister($mobile);
- return $this->response->array(self::returnValue([]));
- }
- public function bindWeChat(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'openid' => 'required',
- 'nickname' => 'required',
- ],[
- 'openid.required' => '微信用户openID不能为空',
- 'nickname.required' => '微信用户昵称不能为空',
- ]);
- if ($validator->fails()) {
- return response()->json(['error' => Base::formatValidator($validator), 'code' => 10009]);
- }
- $openid = $request->get('openid');
- $unionid = $request->get('unionid');
- $nickname = $request->get('nickname');
- $sex = $request->get('sex');
- $headimgurl = $request->get('headimgurl');
- $user_info = self::checkUserByWechat($openid, $unionid);
- if ($user_info) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(40018)], 40018));
- $user_id = Base::getUserId();
- $user_info = User::find($user_id);
- $user_info->wechat_id = $openid;
- $user_info->nickname = $nickname;
- $user_info->gender = $sex == 1 ? 'man' : 'woman';
- $user_info->headimgurl = $headimgurl;
- if (!$user_info->save()) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(40019)], 40019));
- return $this->response->array(self::returnValue([]));
- }
- /**
- * addUserMessage api
- *
- * @return \Illuminate\Http\Response
- */
- public function addUserMessage(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'message' => 'required',
- // 'user_contact' => 'required',
- ],[
- 'message.required' => '留言信息不能为空',
- // 'user_contact.required' => '联系方式不能为空',
- ]);
- if ($validator->fails()) {
- return response()->json(['error' => Base::formatValidator($validator), 'code' => 10009]);
- }
- $version = $request->header('version', null);
- $user_contact = $request->get('user_contact', '');
- $message = $request->get('message');
- $user_id = Base::getUserId();
- $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]);
- if (!$res) return response()->json(['error' => array(ApiHander::str(90003)), 'code' => 90003]);
- return response()->json(['success' => array(ApiHander::str(0)), 'code' => 0]);
- }
- public static function checkUserByWechat($openid, $unionid)
- {
- $user_info = null;
- if ($unionid) $user_info = User::where('wechat_unionid', $unionid)->first();
- if (!$user_info) $user_info = User::where('wechat_id', $openid)->first();
- return $user_info;
- }
- public static function checkUserByMobile($mobile, $type = true)
- {
- $user_info = User::where('mobile', $mobile)
- ->where(function($query) use($type){
- if ($type) $query->where('phone_verified', 4);
- })->first();
- return $user_info;
- }
- public static function deleteUserMobileNoRegister($mobile)
- {
- User::where('mobile',$mobile)->where('phone_verified','!=', 4)->where('wechat_id' , '=', NUll)->delete();
- }
- }
|