AuthController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Helper\SmsHelper;
  4. use App\Models\UserBanks;
  5. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  6. use App\Models\UserInfoModel;
  7. use Illuminate\Http\Request;
  8. use App\Services\Base\ErrorCode;
  9. use App\User;
  10. use Illuminate\Support\Facades\Hash;
  11. use Validator, Auth, Cache;
  12. class AuthController extends Controller
  13. {
  14. use SmsHelper,AuthenticatesUsers;
  15. private $expireTime = 1;
  16. private $keySmsCode = 'auth:sms:';
  17. private $keySmsCodeExist = 'auth:sms:exist';
  18. private $expireTimeExist = 24*60;
  19. public function test(){
  20. // return $this->error(ErrorCode::SAVE_USER_FAILED);
  21. return $this->api(['test' => 'test']);
  22. }
  23. /**
  24. * @api {post} /api/auth/login 登陆(login)
  25. * @apiDescription 登陆(login)
  26. * @apiGroup Auth
  27. * @apiPermission none
  28. * @apiVersion 0.1.0
  29. * @apiParam {string} phone 手机号码
  30. * @apiParam {String} verify_code 手机验证码
  31. * @apiSuccessExample {json} Success-Response:
  32. * HTTP/1.1 200 OK
  33. * {
  34. * "state": true,
  35. * "code": 0,
  36. * "message": "",
  37. * "data": {
  38. * "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjdjYWU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* "email": "abcdefg@gmail.com",
  43. * "type": 2,
  44. * "phone": "15888888888",
  45. * "avatar": null,
  46. * "last_ip": null,
  47. * "created_at": "2016-09-30 00:45:13",
  48. * "updated_at": "2016-09-29 16:43:36"
  49. * }
  50. * }
  51. * }
  52. * @apiErrorExample {json} Error-Response:
  53. * HTTP/1.1 400 Bad Request
  54. * {
  55. * "state": false,
  56. * "code": 1000,
  57. * "message": "传入参数不正确",
  58. * "data": null or []
  59. * }
  60. * 可能出现的错误代码:
  61. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  62. * 1103 VERIFY_CODE_TOO_MUCH 验证码大于5次
  63. * 1610 SERVICE_CODE_FAILED 验证码错误
  64. *
  65. */
  66. public function login(Request $request) {
  67. $validator = Validator::make($request->all(),
  68. [
  69. 'phone' => 'required|regex:/^1[34578]\d{9}$/',
  70. // 'verify_code' => 'required',
  71. ],
  72. [
  73. 'phone.required' => '手机号码必填',
  74. 'phone.regex' => '手机号码格式不正确',
  75. // 'verify_code.required' => '短信验证码必填',
  76. ]
  77. );
  78. if ($validator->fails())
  79. return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  80. $phone = $request->phone;
  81. $key = $this->keySmsCode . $phone;
  82. $code = Cache::store('file')->get($key);
  83. $password = 123456;
  84. // if ($request->verify_code != $code)
  85. // return $this->error(ErrorCode::SERVICE_CODE_FAILED);
  86. $a = UserInfoModel::where('tel',$phone)->first();
  87. if (count($a) == 0) {
  88. UserInfoModel::create(['tel'=>$phone,'password'=>bcrypt(123456)]);
  89. }
  90. $status = UserInfoModel::where('tel',$phone)->first()->status;
  91. if ($status == 0) return $this->error(ErrorCode::LOCK_USER);
  92. if (Auth::attempt(['tel'=>$phone,'password'=>$password])) {
  93. $user = Auth::user();
  94. $token = $user->createToken($user->tel)->accessToken;
  95. return $this->api(compact( 'user', 'code','token'));
  96. }else{
  97. return $this->error(ErrorCode::INCORRECT_USER_OR_PASS);
  98. }
  99. }
  100. /**
  101. * @api {get} /api/auth/logout 退出(logout)
  102. * @apiDescription 退出(logout)
  103. * @apiGroup Auth
  104. * @apiPermission Passport
  105. * @apiVersion 0.1.0
  106. * @apiSuccessExample {json} Success-Response:
  107. * HTTP/1.1 200 OK
  108. * {
  109. * "state": true,
  110. * "code": 0,
  111. * "message": "",
  112. * "data": {
  113. * "result": true/false
  114. * }
  115. * }
  116. * @apiErrorExample {json} Error-Response:
  117. * HTTP/1.1 400 Bad Request
  118. * {
  119. * "state": false,
  120. * "code": 1104,
  121. * "message": "退出失败",
  122. * "data": null
  123. * }
  124. * 可能出现的错误代码:
  125. * 1104 LOGOUT_FAILED 退出失败
  126. */
  127. public function logout() {
  128. if (Auth::user()->token()->delete()) {
  129. return $this->api(['result' => true]);
  130. }
  131. return $this->error(ErrorCode::LOGOUT_FAILED);
  132. }
  133. /**
  134. * @api {post} /api/auth/code 获取验证码(get code)
  135. * @apiDescription 获取验证码(get code),验证码有效期暂定为15分钟
  136. * @apiGroup Auth
  137. * @apiPermission none
  138. * @apiVersion 0.1.0
  139. * @apiParam {string} phone 手机
  140. * @apiSuccessExample {json} Success-Response:
  141. * HTTP/1.1 200 OK
  142. * {
  143. * "state": true,
  144. * "code": 0,
  145. * "message": "",
  146. * "data": {
  147. * "verify_code": "1234"//该值调试时使用,sms调通后取消
  148. * }
  149. * }
  150. * @apiErrorExample {json} Error-Response:
  151. * HTTP/1.1 400 Bad Request
  152. * {
  153. * "state": false,
  154. * "code": 1000,
  155. * "message": "传入参数不正确",
  156. * "data": null or []
  157. * }
  158. * 可能出现的错误代码:
  159. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  160. */
  161. public function getCode(Request $request)
  162. {
  163. $validator = Validator::make($request->all(),
  164. [
  165. 'phone' => 'required|regex:/^1[34578]\d{9}$/',
  166. ],
  167. [
  168. 'phone.required' => '手机号码必填',
  169. 'phone.regex' => '手机号码格式不正确',
  170. ]
  171. );
  172. if ($validator->fails())
  173. return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  174. $phone = $request->phone;
  175. $keyexist = $this->keySmsCodeExist . $phone;
  176. $times = Cache::store('file')->get($keyexist);
  177. if($times>5) {
  178. return $this->error(ErrorCode::VERIFY_CODE_TOO_MUCH);
  179. }else{
  180. $times++;
  181. Cache::store('file')->put($keyexist, $times, $this->expireTimeExist);
  182. }
  183. $verify_code = (string) mt_rand(1000, 9999);
  184. \Log::info('verify_code:'.$verify_code);
  185. $key = $this->keySmsCode . $phone;
  186. Cache::store('file')->put($key, $verify_code, $this->expireTime);
  187. $msg = '【喵喵】您的验证码是:' . $verify_code;
  188. $result = $this->sendSms($msg, $phone);
  189. /* if (!$result)
  190. $this->logger->Error("Send sms failed.");*/
  191. return $this->api(['verify_code' => $verify_code]);
  192. }
  193. public function refreshToken() {
  194. $token = '';//TODO
  195. return $this->api([
  196. 'token' => $token,
  197. ]);
  198. }
  199. /**
  200. * @api {post} /api/auth/password 设置密码(password)
  201. * @apiDescription 上传头像(password)
  202. * @apiGroup Auth
  203. * @apiPermission Passport
  204. * @apiVersion 0.1.0
  205. * @apiParam {String} password 密码
  206. * @apiSuccessExample {json} Success-Response:
  207. * HTTP/1.1 200 OK
  208. * {
  209. * "state": true,
  210. * "code": 0,
  211. * "message": "",
  212. * "data": {
  213. * "result": true,
  214. * }
  215. * }
  216. * @apiErrorExample {json} Error-Response:
  217. * HTTP/1.1 400 Bad Request
  218. * {
  219. * "state": false,
  220. * "code": 1000,
  221. * "message": "传入参数不正确",
  222. * "data": null or []
  223. * }
  224. * 可能出现的错误代码:
  225. * 200 SAVE_USER_FAILED 保存用户数据失败
  226. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  227. */
  228. public function setPassword(Request $request) {
  229. $validator = Validator::make($request->input(),
  230. [
  231. 'password' => 'required|between:6,16',
  232. ],
  233. [
  234. 'password.required' => '请输入密码',
  235. 'password.between' => '密码长度6~16位',
  236. ]
  237. );
  238. if ($validator->fails()) {
  239. return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  240. }
  241. $pass = $request->get('password');
  242. $user = Auth::user();
  243. $user->password = bcrypt($pass);
  244. if (!$user->save()) {
  245. return $this->error(ErrorCode::SAVE_USER_FAILED);
  246. }
  247. return $this->api([
  248. 'result' => true,
  249. ]);
  250. }
  251. public function isLogin()
  252. {
  253. $user = Auth::user();
  254. $res = true;
  255. if(!$user) $res = false;
  256. return $this->api([
  257. 'result' => $res,
  258. ]);
  259. }
  260. public function check_password(Request $request)
  261. {
  262. $password = Auth::user()->password;
  263. if(!Hash::check($request->oldpassword,$password)) return $this->error(ErrorCode::CHECK_OLDPASSWORD_FAILED);
  264. return $this->api(null,0,'验证通过');
  265. }
  266. /**
  267. * @api {post} /api/auth/reset 找回密码(reset)
  268. * @apiDescription 找回密码(reset)
  269. * @apiGroup Auth
  270. * @apiPermission none
  271. * @apiVersion 0.1.0
  272. * @apiParam {Phone} phone 手机
  273. * @apiParam {int} type 帐户类型:1.个人,2.商户
  274. * @apiParam {String} verify_code 手机验证码
  275. * @apiParam {String} password password
  276. * @apiSuccessExample {json} Success-Response:
  277. * HTTP/1.1 200 OK
  278. * {
  279. * "state": true,
  280. * "code": 0,
  281. * "message": "",
  282. * "data": {
  283. * "result": true,
  284. * }
  285. * }
  286. * @apiErrorExample {json} Error-Response:
  287. * HTTP/1.1 400 Bad Request
  288. * {
  289. * "state": false,
  290. * "code": 1000,
  291. * "message": "传入参数不正确",
  292. * "data": null or []
  293. * }
  294. * 可能出现的错误代码:
  295. * 200 SAVE_USER_FAILED 保存用户数据失败
  296. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  297. * 1101 INCORRECT_VERIFY_CODE 输入验证码错误
  298. * 1105 USER_DOES_NOT_EXIST 用户不存在
  299. */
  300. public function reset(Request $request) {
  301. $validator = Validator::make($request->all(),
  302. [
  303. 'phone' => 'required|regex:/^1[34578]\d{9}$/',
  304. 'verify_code' => 'required',
  305. 'password' => 'required|between:6,16',
  306. ],
  307. [
  308. 'phone.required' => '手机号码必填',
  309. 'phone.regex' => '手机号码格式不正确',
  310. 'verify_code.required' => '请输入校验码',
  311. 'password.required' => '请输入密码',
  312. 'password.between' => '密码长度6~16位',
  313. ]
  314. );
  315. if ($validator->fails()) {
  316. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '', $validator->messages());
  317. }
  318. $phone = $request->get('phone');
  319. $verify_code = $request->get('verify_code');
  320. $pass = $request->get('password');
  321. $key = $this->keySmsCode . $phone;
  322. // if (Redis::exists($key)) {
  323. if (Cache::store('file')->has($key)) {
  324. // $code = Redis::get($key);
  325. $code = Cache::store('file')->get($key);
  326. if ($code == $verify_code) {
  327. $user = User::where([
  328. 'phone' => $phone,
  329. ])->first();
  330. if (!$user) {
  331. return $this->error(ErrorCode::USER_DOES_NOT_EXIST);
  332. }
  333. //$password = app('hash')->make($request->get('password'));
  334. $user->password = bcrypt($pass);
  335. if (!$user->save()) {
  336. return $this->error(ErrorCode::SAVE_USER_FAILED);
  337. }
  338. Cache::store('file')->forget($key);
  339. return $this->api(['result' => true]);
  340. }
  341. }
  342. return $this->error(ErrorCode::INCORRECT_VERIFY_CODE);
  343. }
  344. }