AuthController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Helper\SmsHelper;
  4. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  5. use App\Models\UserInfoModel;
  6. use Illuminate\Http\Request;
  7. use App\Services\Base\ErrorCode;
  8. use Validator, Auth, Cache;
  9. class AuthController extends Controller
  10. {
  11. use SmsHelper,AuthenticatesUsers;
  12. private $expireTime = 1;
  13. private $keySmsCode = 'auth:sms:';
  14. private $keySmsCodeExist = 'auth:sms:exist';
  15. private $expireTimeExist = 24*60;
  16. public function test(){
  17. // return $this->error(ErrorCode::SAVE_USER_FAILED);
  18. return $this->api(['test' => 'test']);
  19. }
  20. /**
  21. * @api {post} /api/auth/login 登陆(login)
  22. * @apiDescription 登陆(login)
  23. * @apiGroup Auth
  24. * @apiPermission none
  25. * @apiVersion 0.1.0
  26. * @apiParam {string} phone 手机号码
  27. * @apiParam {String} verify_code 手机验证码
  28. * @apiSuccessExample {json} Success-Response:
  29. * HTTP/1.1 200 OK
  30. * {
  31. * "state": true,
  32. * "code": 0,
  33. * "message": "",
  34. * "data": {
  35. * "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjdjYWUyYzFmYTUwMTIyZDI0ZTRiYTZhZGZhNmQxYmZlOWNiMzIxMTBmYWJlZjNjYzIyNmViZjRmNGExNWM3NjllNmU2ZTNiYWE5OGNhOWUzIn0.eyJhdWQiOiIxIiwianRpIjoiN2NhZTJjMWZhNTAxMjJkMjRlNGJhNmFkZmE2ZDFiZmU5Y2IzMjExMGZhYmVmM2NjMjI2ZWJmNGY0YTE1Yzc2OWU2ZTZlM2JhYTk4Y2E5ZTMiLCJpYXQiOjE0NzU0MTE1NTgsIm5iZiI6MTQ3NTQxMTU1OCwiZXhwIjo0NjMxMDg1MTU4LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.E9YGEzuRUOk02aV1EiWLJ_pD0hKoCyW0k_sGy63hM3u5X8K_HI1kVhaU6JNLqLZeszIAroTEDB8XMgZKAqTLlwtL8PLCJcuDoxfk1BRHbfjhDheTsahBysKGalvNEpzRCrGlao0mS0Cg9qDpEsndtypPFS8sfaflToOzbJjiSK2DvQiHSH8xZI3zHJTezgZMz-pB_hPTxp8ajdv0ve1gWtWjs3vERr0Y91X4hngO8X7LuXtAYtfxGZRIye12YE7TuLBMYzj8CCfiRt7Smhyf4palNW5mzKlZpa2l87n6NQ14Iy4oMzQ2PON1j_swrosuE2yZohGOn6fDdSCBRdJ6dLD_emjBdQCQOoB63R7BbhFZgvFX25TjzFJ7r9AdVMiGmebuRKEVSZV_JCGu1C71OIbQk-UK35s00gSr2fmJGBbN2cZTXBRTJpfuMZ_ihFYEZrvVq_Ih2X0xkd36JUuxaUld1BXRgPZvH-9jBuhe0YW2OOlgwpdm6ZB8BMcuS4ftLoi6FipgzFqfIuy-0ZqPMDnJaG7Gycrdpxza00mgOFxYxJtqwZNsUWFRZEVU881l6VC_cy294YXSPQxUwEoyKg-G5Pm8AEB9bqv5z4EU4B8-XTd3zKNqtNba_snHbc711i4EytCiZfYSjNB1hwenq45YYOAhPTwOpFI0kxyRazc",
  36. * "user": {
  37. * "id": 1,
  38. * "name": "15888888888",
  39. * "email": "abcdefg@gmail.com",
  40. * "type": 2,
  41. * "phone": "15888888888",
  42. * "avatar": null,
  43. * "last_ip": null,
  44. * "created_at": "2016-09-30 00:45:13",
  45. * "updated_at": "2016-09-29 16:43:36"
  46. * }
  47. * }
  48. * }
  49. * @apiErrorExample {json} Error-Response:
  50. * HTTP/1.1 400 Bad Request
  51. * {
  52. * "state": false,
  53. * "code": 1000,
  54. * "message": "传入参数不正确",
  55. * "data": null or []
  56. * }
  57. * 可能出现的错误代码:
  58. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  59. * 1103 VERIFY_CODE_TOO_MUCH 验证码大于5次
  60. * 1610 SERVICE_CODE_FAILED 验证码错误
  61. *
  62. */
  63. public function login(Request $request) {
  64. $validator = Validator::make($request->all(),
  65. [
  66. 'phone' => 'required|regex:/^1[34578]\d{9}$/',
  67. // 'verify_code' => 'required',
  68. ],
  69. [
  70. 'phone.required' => '手机号码必填',
  71. 'phone.regex' => '手机号码格式不正确',
  72. // 'verify_code.required' => '短信验证码必填',
  73. ]
  74. );
  75. if ($validator->fails())
  76. return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  77. $phone = $request->phone;
  78. $key = $this->keySmsCode . $phone;
  79. $code = Cache::store('file')->get($key);
  80. $password = 123456;
  81. // if ($request->verify_code != $code)
  82. // return $this->error(ErrorCode::SERVICE_CODE_FAILED);
  83. $user = UserInfoModel::where('phone',$phone)->first();
  84. if (empty($user)) {
  85. UserInfoModel::create(['phone'=>$phone,'password'=>bcrypt(123456)]);
  86. }
  87. $status =empty($user) ? 0 : $user->status;
  88. if ($status == 0) return $this->error(ErrorCode::LOCK_USER);
  89. if (Auth::attempt(['phone'=>$phone,'password'=>$password])) {
  90. $user = Auth::user();
  91. $token = $user->createToken($user->phone)->accessToken;
  92. return $this->api(compact( 'user', 'code','token'));
  93. }else{
  94. return $this->error(ErrorCode::INCORRECT_USER_OR_PASS);
  95. }
  96. }
  97. /**
  98. * @api {get} /api/auth/logout 退出(logout)
  99. * @apiDescription 退出(logout)
  100. * @apiGroup Auth
  101. * @apiPermission Passport
  102. * @apiVersion 0.1.0
  103. * @apiSuccessExample {json} Success-Response:
  104. * HTTP/1.1 200 OK
  105. * {
  106. * "state": true,
  107. * "code": 0,
  108. * "message": "",
  109. * "data": {
  110. * "result": true/false
  111. * }
  112. * }
  113. * @apiErrorExample {json} Error-Response:
  114. * HTTP/1.1 400 Bad Request
  115. * {
  116. * "state": false,
  117. * "code": 1104,
  118. * "message": "退出失败",
  119. * "data": null
  120. * }
  121. * 可能出现的错误代码:
  122. * 1104 LOGOUT_FAILED 退出失败
  123. */
  124. public function logout() {
  125. $user = Auth::user();
  126. if ($user->token()->delete()) {
  127. return $this->api(['result' => true]);
  128. }
  129. return $this->error(ErrorCode::LOGOUT_FAILED);
  130. }
  131. /**
  132. * @api {post} /api/auth/code 获取验证码(get code)
  133. * @apiDescription 获取验证码(get code),验证码有效期暂定为15分钟
  134. * @apiGroup Auth
  135. * @apiPermission none
  136. * @apiVersion 0.1.0
  137. * @apiParam {string} phone 手机
  138. * @apiSuccessExample {json} Success-Response:
  139. * HTTP/1.1 200 OK
  140. * {
  141. * "state": true,
  142. * "code": 0,
  143. * "message": "",
  144. * "data": {
  145. * "verify_code": "1234"//该值调试时使用,sms调通后取消
  146. * }
  147. * }
  148. * @apiErrorExample {json} Error-Response:
  149. * HTTP/1.1 400 Bad Request
  150. * {
  151. * "state": false,
  152. * "code": 1000,
  153. * "message": "传入参数不正确",
  154. * "data": null or []
  155. * }
  156. * 可能出现的错误代码:
  157. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  158. */
  159. public function getCode(Request $request)
  160. {
  161. $validator = Validator::make($request->all(),
  162. [
  163. 'phone' => 'required|regex:/^1[34578]\d{9}$/',
  164. ],
  165. [
  166. 'phone.required' => '手机号码必填',
  167. 'phone.regex' => '手机号码格式不正确',
  168. ]
  169. );
  170. if ($validator->fails())
  171. return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  172. $phone = $request->phone;
  173. $keyexist = $this->keySmsCodeExist . $phone;
  174. $times = Cache::store('file')->get($keyexist);
  175. if($times>5) {
  176. return $this->error(ErrorCode::VERIFY_CODE_TOO_MUCH);
  177. }else{
  178. $times++;
  179. Cache::store('file')->put($keyexist, $times, $this->expireTimeExist);
  180. }
  181. $verify_code = (string) mt_rand(1000, 9999);
  182. \Log::info('verify_code:'.$verify_code);
  183. $key = $this->keySmsCode . $phone;
  184. Cache::store('file')->put($key, $verify_code, $this->expireTime);
  185. $msg = '【喵喵】您的验证码是:' . $verify_code;
  186. /*
  187. $result = $this->sendSms($msg, $phone);
  188. if (!$result)
  189. $this->logger->Error("Send sms failed.");
  190. */
  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. public function isLogin()
  200. {
  201. $user = Auth::user();
  202. $res = true;
  203. if(!$user) $res = false;
  204. return $this->api([
  205. 'result' => $res,
  206. ]);
  207. }
  208. /**
  209. * @api {post} /api/auth/avatar 上传头像(avatar)
  210. * @apiDescription 上传头像(reset)
  211. * @apiGroup Auth
  212. * @apiPermission Passport
  213. * @apiVersion 0.1.0
  214. * @apiParam {File} avatar 头像图片
  215. * @apiSuccessExample {json} Success-Response:
  216. * HTTP/1.1 200 OK
  217. * {
  218. * "state": true,
  219. * "code": 0,
  220. * "message": "",
  221. * "data": {
  222. * "md5": "fdf8dd78eb383b8acf6d94d4752c1424",
  223. * }
  224. * }
  225. * @apiErrorExample {json} Error-Response:
  226. * HTTP/1.1 400 Bad Request
  227. * {
  228. * "state": false,
  229. * "code": 1000,
  230. * "message": "传入参数不正确",
  231. * "data": null or []
  232. * }
  233. * 可能出现的错误代码:
  234. * 200 SAVE_USER_FAILED 保存用户数据失败
  235. * 201 ATTACHMENT_MKDIR_FAILED 创建附件目录失败
  236. * 202 ATTACHMENT_UPLOAD_INVALID 上传附件文件无效
  237. * 203 ATTACHMENT_SAVE_FAILED 保存附件失败
  238. * 204 ATTACHMENT_MOVE_FAILED 移动附件失败
  239. * 205 ATTACHMENT_DELETE_FAILED 删除附件文件失败
  240. * 206 ATTACHMENT_RECORD_DELETE_FAILED 删除附件记录失败
  241. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  242. * 1101 INCORRECT_VERIFY_CODE 输入验证码错误
  243. * 1105 USER_DOES_NOT_EXIST 用户不存在
  244. * 1200 ATTACHMENT_UPLOAD_FAILED 附件上传失败
  245. * 1201 ATTACHMENT_SIZE_EXCEEDED 附件大小超过限制
  246. * 1202 ATTACHMENT_MIME_NOT_ALLOWED 附件类型不允许
  247. * 1203 ATTACHMENT_NOT_EXIST 附件不存在
  248. */
  249. public function avatar(Request $request) {
  250. // $user = Auth::user();
  251. $user = $this->getUser();
  252. $old_avatar = $user->avatar;
  253. $result = $this->uploadAttachment($request, 'avatar', 'avatar', 4 * 1024 * 1024, [
  254. 'image/jpeg',
  255. 'image/png',
  256. 'image/gif',
  257. ]);
  258. if (is_array($result)) {
  259. $result = array_shift($result);
  260. }
  261. if (is_string($result)) {
  262. $user->avatar = $result;
  263. if (!$user->save()) {
  264. return $this->error(ErrorCode::SAVE_USER_FAILED);
  265. }
  266. $this->deleteAttachment($old_avatar);
  267. return $this->api(['md5' => $result]);
  268. }
  269. return $this->error($result);
  270. }
  271. }