AuthController.php 11 KB

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