AuthController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjdjYW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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. $user = Auth::user();
  129. if ($user->token()->delete()) {
  130. return $this->api(['result' => true]);
  131. }
  132. return $this->error(ErrorCode::LOGOUT_FAILED);
  133. }
  134. /**
  135. * @api {post} /api/auth/code 获取验证码(get code)
  136. * @apiDescription 获取验证码(get code),验证码有效期暂定为15分钟
  137. * @apiGroup Auth
  138. * @apiPermission none
  139. * @apiVersion 0.1.0
  140. * @apiParam {string} phone 手机
  141. * @apiSuccessExample {json} Success-Response:
  142. * HTTP/1.1 200 OK
  143. * {
  144. * "state": true,
  145. * "code": 0,
  146. * "message": "",
  147. * "data": {
  148. * "verify_code": "1234"//该值调试时使用,sms调通后取消
  149. * }
  150. * }
  151. * @apiErrorExample {json} Error-Response:
  152. * HTTP/1.1 400 Bad Request
  153. * {
  154. * "state": false,
  155. * "code": 1000,
  156. * "message": "传入参数不正确",
  157. * "data": null or []
  158. * }
  159. * 可能出现的错误代码:
  160. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  161. */
  162. public function getCode(Request $request)
  163. {
  164. $validator = Validator::make($request->all(),
  165. [
  166. 'phone' => 'required|regex:/^1[34578]\d{9}$/',
  167. ],
  168. [
  169. 'phone.required' => '手机号码必填',
  170. 'phone.regex' => '手机号码格式不正确',
  171. ]
  172. );
  173. if ($validator->fails())
  174. return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  175. $phone = $request->phone;
  176. $keyexist = $this->keySmsCodeExist . $phone;
  177. $times = Cache::store('file')->get($keyexist);
  178. if($times>5) {
  179. return $this->error(ErrorCode::VERIFY_CODE_TOO_MUCH);
  180. }else{
  181. $times++;
  182. Cache::store('file')->put($keyexist, $times, $this->expireTimeExist);
  183. }
  184. $verify_code = (string) mt_rand(1000, 9999);
  185. \Log::info('verify_code:'.$verify_code);
  186. $key = $this->keySmsCode . $phone;
  187. Cache::store('file')->put($key, $verify_code, $this->expireTime);
  188. $msg = '【喵喵】您的验证码是:' . $verify_code;
  189. $result = $this->sendSms($msg, $phone);
  190. /* if (!$result)
  191. $this->logger->Error("Send sms failed.");*/
  192. return $this->api(['verify_code' => $verify_code]);
  193. }
  194. public function refreshToken() {
  195. $token = '';//TODO
  196. return $this->api([
  197. 'token' => $token,
  198. ]);
  199. }
  200. /**
  201. * @api {post} /api/auth/password 设置密码(password)
  202. * @apiDescription 上传头像(password)
  203. * @apiGroup Auth
  204. * @apiPermission Passport
  205. * @apiVersion 0.1.0
  206. * @apiParam {String} password 密码
  207. * @apiSuccessExample {json} Success-Response:
  208. * HTTP/1.1 200 OK
  209. * {
  210. * "state": true,
  211. * "code": 0,
  212. * "message": "",
  213. * "data": {
  214. * "result": true,
  215. * }
  216. * }
  217. * @apiErrorExample {json} Error-Response:
  218. * HTTP/1.1 400 Bad Request
  219. * {
  220. * "state": false,
  221. * "code": 1000,
  222. * "message": "传入参数不正确",
  223. * "data": null or []
  224. * }
  225. * 可能出现的错误代码:
  226. * 200 SAVE_USER_FAILED 保存用户数据失败
  227. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  228. */
  229. public function setPassword(Request $request) {
  230. $validator = Validator::make($request->input(),
  231. [
  232. 'password' => 'required|between:6,16',
  233. ],
  234. [
  235. 'password.required' => '请输入密码',
  236. 'password.between' => '密码长度6~16位',
  237. ]
  238. );
  239. if ($validator->fails()) {
  240. return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  241. }
  242. $pass = $request->get('password');
  243. $user = Auth::user();
  244. $user->password = bcrypt($pass);
  245. if (!$user->save()) {
  246. return $this->error(ErrorCode::SAVE_USER_FAILED);
  247. }
  248. return $this->api([
  249. 'result' => true,
  250. ]);
  251. }
  252. public function isLogin()
  253. {
  254. $user = Auth::user();
  255. $res = true;
  256. if(!$user) $res = false;
  257. return $this->api([
  258. 'result' => $res,
  259. ]);
  260. }
  261. public function check_password(Request $request)
  262. {
  263. $password = Auth::user()->password;
  264. if(!Hash::check($request->oldpassword,$password)) return $this->error(ErrorCode::CHECK_OLDPASSWORD_FAILED);
  265. return $this->api(null,0,'验证通过');
  266. }
  267. /**
  268. * @api {post} /api/auth/reset 找回密码(reset)
  269. * @apiDescription 找回密码(reset)
  270. * @apiGroup Auth
  271. * @apiPermission none
  272. * @apiVersion 0.1.0
  273. * @apiParam {Phone} phone 手机
  274. * @apiParam {int} type 帐户类型:1.个人,2.商户
  275. * @apiParam {String} verify_code 手机验证码
  276. * @apiParam {String} password password
  277. * @apiSuccessExample {json} Success-Response:
  278. * HTTP/1.1 200 OK
  279. * {
  280. * "state": true,
  281. * "code": 0,
  282. * "message": "",
  283. * "data": {
  284. * "result": true,
  285. * }
  286. * }
  287. * @apiErrorExample {json} Error-Response:
  288. * HTTP/1.1 400 Bad Request
  289. * {
  290. * "state": false,
  291. * "code": 1000,
  292. * "message": "传入参数不正确",
  293. * "data": null or []
  294. * }
  295. * 可能出现的错误代码:
  296. * 200 SAVE_USER_FAILED 保存用户数据失败
  297. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  298. * 1101 INCORRECT_VERIFY_CODE 输入验证码错误
  299. * 1105 USER_DOES_NOT_EXIST 用户不存在
  300. */
  301. public function reset(Request $request) {
  302. $validator = Validator::make($request->all(),
  303. [
  304. 'phone' => 'required|regex:/^1[34578]\d{9}$/',
  305. 'verify_code' => 'required',
  306. 'password' => 'required|between:6,16',
  307. ],
  308. [
  309. 'phone.required' => '手机号码必填',
  310. 'phone.regex' => '手机号码格式不正确',
  311. 'verify_code.required' => '请输入校验码',
  312. 'password.required' => '请输入密码',
  313. 'password.between' => '密码长度6~16位',
  314. ]
  315. );
  316. if ($validator->fails()) {
  317. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '', $validator->messages());
  318. }
  319. $phone = $request->get('phone');
  320. $verify_code = $request->get('verify_code');
  321. $pass = $request->get('password');
  322. $key = $this->keySmsCode . $phone;
  323. // if (Redis::exists($key)) {
  324. if (Cache::store('file')->has($key)) {
  325. // $code = Redis::get($key);
  326. $code = Cache::store('file')->get($key);
  327. if ($code == $verify_code) {
  328. $user = User::where([
  329. 'phone' => $phone,
  330. ])->first();
  331. if (!$user) {
  332. return $this->error(ErrorCode::USER_DOES_NOT_EXIST);
  333. }
  334. //$password = app('hash')->make($request->get('password'));
  335. $user->password = bcrypt($pass);
  336. if (!$user->save()) {
  337. return $this->error(ErrorCode::SAVE_USER_FAILED);
  338. }
  339. Cache::store('file')->forget($key);
  340. return $this->api(['result' => true]);
  341. }
  342. }
  343. return $this->error(ErrorCode::INCORRECT_VERIFY_CODE);
  344. }
  345. }