AuthController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace App\Http\Controllers\Auth\Front;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Controllers\Traits\ApiResponseTrait;
  5. use App\Models\ApiDetection;
  6. use App\Models\ApiToken;
  7. use App\Models\Config;
  8. use App\Models\Member;
  9. use App\Models\Tip;
  10. use App\Servers\MemberServer;
  11. use App\Servers\Sign\SignServer;
  12. use App\Servers\Yp;
  13. use Carbon\Carbon;
  14. use Tymon\JWTAuth\Facades\JWTAuth;
  15. class AuthController extends Controller
  16. {
  17. use ApiResponseTrait;
  18. /**
  19. * Create a new AuthController instance.
  20. * 要求附带email和password(数据来源users表)
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. // 这里额外注意了:官方文档样例中只除外了『login』
  27. // 这样的结果是,token 只能在有效期以内进行刷新,过期无法刷新
  28. // 如果把 refresh 也放进去,token 即使过期但仍在刷新期以内也可刷新
  29. // 不过刷新一次作废
  30. $this->middleware('auth:api', ['except' => ['login', 'refresh','getSendToken']]);
  31. // 另外关于上面的中间件,官方文档写的是『auth:api』
  32. // 但是我推荐用 『jwt.auth』,效果是一样的,但是有更加丰富的报错信息返回
  33. }
  34. /**
  35. * 获取签名token
  36. * @return mixed
  37. */
  38. public function getSendToken()
  39. {
  40. $time=time();
  41. $send_token_num=session('send_token_num','0');
  42. $send_token_time=session('send_token_time','');
  43. session(['send_token_num' => ++$send_token_num]);
  44. if($send_token_time<$time-60 && $send_token_num>2){
  45. return response()->json([
  46. 'msg' => "",
  47. 'data' => [
  48. 'send_token_str' =>'验证失败'
  49. ],
  50. 'code' => 0,
  51. ]);
  52. }
  53. session(['send_token_time' =>$time]);
  54. $str = '';
  55. $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  56. $max = strlen($strPol) - 1;
  57. for ($i = 0; $i < 10; $i++) {
  58. $str .= $strPol[rand(0, $max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
  59. }
  60. session(['send_token_str' => $str]);
  61. return response()->json([
  62. 'msg' => "",
  63. 'data' => [
  64. 'send_token_str' =>$str
  65. ],
  66. 'code' => 1,
  67. ]);
  68. }
  69. /**
  70. * @return \Illuminate\Http\JsonResponse
  71. * @throws \App\Exceptions\Sign\SignException
  72. */
  73. public function login()
  74. {
  75. if( strtolower(env('APP_ENV')) == 'down' ){
  76. return $this->apiResponseError(null, '系统维护中...');
  77. }
  78. $is_behavior=Yp::validateYP();
  79. if(empty($is_behavior) ){
  80. return $this->apiResponseError(null, '请完成滑块验证...');
  81. // return $this->apiResponseError(null, '系统升级中...');
  82. }
  83. // $send_token_sign=request()->input('send_token_sign', '');
  84. // if($send_token_sign){
  85. // //滑块验证
  86. // $send_token_str=session('send_token_str');
  87. // if(empty($send_token_sign) || empty($send_token_str)){
  88. // return response()->json([
  89. // 'msg' => '请按住滑块验证',
  90. // 'data' => [],
  91. // 'code' => 0,
  92. // ]);
  93. // }
  94. // $sys_token_sign=md5( substr($send_token_str,5).'ziUDRO96uFhSOxdCMc58bPPznJDataaM');
  95. // if($send_token_sign!=$sys_token_sign){
  96. // return response()->json([
  97. // 'msg' => '滑块验证失败,请重新验证',
  98. // 'data' => [],
  99. // 'code' => 0,
  100. // ]);
  101. // }
  102. // }
  103. $credentials = request(['phone', 'password']);
  104. $rCount = Member::where('phone', request()->input('phone', ''))->count();
  105. if( $rCount <= 0 ){ return $this->apiResponseError(null, '账户不存在'); }
  106. if (! $token = auth('api')->attempt($credentials)) { return $this->apiResponseError(null, '密码不正确'); }
  107. // 登录成功缓存token
  108. $user = auth('api')->user();
  109. if ( !empty($user->deleted_at) ) { return $this->apiResponseError(null, '账号已被注销'); }
  110. $new_token = SignServer::loginSign($user);
  111. // 判断当前会员是否被冻结
  112. if( $user->is_show >= 1 ){
  113. JWTAuth::setToken($user->last_jwt_token)->invalidate(); // 加入黑名单
  114. auth('api')->logout();
  115. $apiDetection = ApiDetection::where('member_id', $user->id)->first();
  116. if( !empty($apiDetection) && $apiDetection->{'count'} >= 5 ){
  117. return $this->apiResponseError(null, '系统检查你是机器人,自动封号,请联系客服');
  118. }else{
  119. return $this->apiResponseError(null, '账号已被冻结');
  120. }
  121. }
  122. // if($user->id!= 1 && $user->id!=12277 && $user->phone!='18200172438'){
  123. // return $this->apiResponseError(null, '系统升级中...');
  124. // }
  125. if( $user->id > 1 && $user->id != 12277 ){
  126. $loginMac = request()->input('mac', '');
  127. // 记录会员数据
  128. // file_put_contents(base_path('member_login.txt'),"Phone:{$user->phone}; Mac:{$loginMac}; Member_user:{$user->mac}\r\n", FILE_APPEND);
  129. if( empty($loginMac)) {
  130. return $this->apiResponseError(null, '无法获取手机设备信息');
  131. }else{
  132. $loginMac=explode(',',$loginMac);
  133. if(!empty($loginMac[0]))$loginMac=$loginMac[0];
  134. }
  135. // 判断当前是否绑定MAC
  136. if ( !empty($user->mac) ) {
  137. $xMember = Member::where('mac', $loginMac)->where('id','<>',$user->id)->first();
  138. if( !empty($xMember) ){ return $this->apiResponseError(null, '已绑定'.":".(empty($xMember->{'phone'}) ? '' : $xMember->{'phone'}) ); }
  139. }else{
  140. // 判断mac是否已被绑定
  141. $countMac = Member::where('mac', $loginMac)->count();
  142. if( $countMac > 0 ) {
  143. $xMember = Member::where('mac', $loginMac)->first();
  144. return $this->apiResponseError(null, '已绑定:'.$xMember->{'phone'});
  145. }else{
  146. Member::where('id', $user->id)->update([
  147. 'mac' => $loginMac
  148. ]);
  149. }
  150. }
  151. }
  152. // 之前token加入黑名单
  153. $oldToken = $user->last_jwt_token;
  154. if( !empty($oldToken) && JWTAuth::setToken($oldToken)->check() ){
  155. JWTAuth::setToken($oldToken)->invalidate(); // 加入黑名单
  156. }
  157. // 清除登录信息
  158. ApiToken::where('member_id', $user->id)->update(['count' => 0]);
  159. // 保存最后一次token
  160. Member::where('id', $user->id)->update(['last_jwt_token' => $token]);
  161. return $this->respondWithToken($token,$user->id, $new_token);
  162. }
  163. /**
  164. * Get the authenticated User.
  165. *
  166. * @return \Illuminate\Http\JsonResponse
  167. */
  168. public function me()
  169. {
  170. $user = auth('api')->user();
  171. // 判断会员矿机状态
  172. $status = 1;
  173. if ( $user->machine_status == 0 ) {
  174. $status = 1; // 可以挖矿
  175. }else if( $user->machine_status == 1 ){
  176. // 判断当前是否已经释放完成
  177. if(
  178. Carbon::now(config('app.timezone'))->lt(Carbon::parse($user->machine_end_time, config('app.timezone')))
  179. ){
  180. $status = 2; // 挖矿进行中
  181. }else{
  182. // 挖矿已结束
  183. if( $user->machine_receive == 0 ){
  184. $status = 3; // 待领取
  185. }else{
  186. $status = 4; // 已领取
  187. }
  188. }
  189. }
  190. $config = Config::where('key', '_me_de')->first();
  191. $span = explode("\r\n", $config->{'value'});
  192. $st = Tip::where('member_id', $user->id)->where('status', 1)->count();
  193. return response()->json([
  194. 'msg' => '获取用户信息',
  195. 'data' => [
  196. 'm_id' => $user->{'id'}, // 会员ID
  197. 'username' => empty($user->username) ? "小富农" : $user->username, // 会员昵称
  198. 'phone' => $user->phone, // 会员手机账号
  199. 'referee_phone' => $user->referee_phone, // 推荐人账号
  200. 'level_name' => $user->level->name, // 会员级别
  201. 'star_name' => $user->star->name, // 会员星级
  202. 'fruit' => MemberServer::frontUnit($user->fruit / 1000000),// 会员果实(两位小数)
  203. 'contribution' => MemberServer::frontUnit($user->contribution), // 贡献值(两位小数)
  204. 'activity' => MemberServer::frontUnit($user->activity), // 活跃度(两位小数)
  205. 'honor' => MemberServer::frontUnit($user->honor), // 荣誉值(两位小数)
  206. 'is_prove' => $user->is_active, // 是否实名认证;0:未认证;1:已认证;
  207. 'is_prove_name' => $user->is_active > 1 ? '已认证' : '未认证', // 实名认证
  208. 'freeze' => $user->is_show, // 限制登录;0:正常用户;1:冻结账户
  209. 'avatar' => $user->avatar ?: asset('/storage/avatar/default/default_av.png'), // 用户头像
  210. 'machine_status' => $user->machine_status, // 矿机挖矿状态;0:今日未手动开启;1:当日手动开启;
  211. 'machine_start_time'=> Carbon::now(config('app.timezone'))->timestamp, // 矿机开始时间
  212. 'machine_end_time' => empty($user->machine_end_time) ? 0 : Carbon::parse($user->machine_end_time, config('app.timezone'))->timestamp, // 矿机结束时间
  213. 'machine_unit' => $user->machine_unit, // 矿机每秒单价
  214. 'machine_total' => empty($user->machine_start_time) ? 0 : (
  215. ((Carbon::now(config('app.timezone'))->timestamp >= Carbon::parse($user->machine_end_time, config('app.timezone'))->timestamp) ? Carbon::parse($user->machine_end_time, config('app.timezone'))->timestamp : Carbon::now(config('app.timezone'))->timestamp)
  216. -
  217. Carbon::parse($user->machine_start_time, config('app.timezone'))->timestamp
  218. ) * $user->machine_unit, // 矿机产矿总数
  219. 'machine_receive' => $user->machine_receive, // 挖矿领取状态;0:未领取;1:当日已领取;
  220. 'area_id' => $user->area_id > 0 ? $user->area->name : '', // 城主
  221. 'machine_receive_status' => $status, // 1:可以挖矿;2:挖矿进行中;3:待领取;4:已领取
  222. 'me_de' => "<span>{$span[0]}</span><span>{$span[1]}</span>", //
  223. 'is_unread' => $st > 0 ? true : false,
  224. 'mac' => $user->mac, // 设备信息
  225. ],
  226. 'code' => 1,
  227. ],200);
  228. }
  229. /**
  230. * Log the user out (Invalidate the token).
  231. *
  232. * @return \Illuminate\Http\JsonResponse
  233. */
  234. public function logout()
  235. {
  236. auth('api')->logout();
  237. return $this->apiResponseSuccess(null, '成功');
  238. }
  239. /**
  240. * Refresh a token.
  241. * 刷新token,如果开启黑名单,以前的token便会失效。
  242. * 值得注意的是用上面的getToken(登录)再获取一次Token并不算做刷新,两次获得的Token是并行的,即两个都可用。
  243. * @return \Illuminate\Http\JsonResponse
  244. */
  245. public function refresh()
  246. {
  247. $user = auth('api')->user();
  248. return $this->respondWithToken(auth('api')->refresh(), $user->id);
  249. }
  250. /**
  251. * Get the token array structure.
  252. *
  253. * @param $token
  254. * @param null $mId
  255. * @return \Illuminate\Http\JsonResponse
  256. */
  257. protected function respondWithToken($token, $mId = null, $new_token = null)
  258. {
  259. return $this->apiResponseSuccess(
  260. [
  261. 'access_token' => $token,
  262. 'token_type' => 'bearer',
  263. 'expires_in' => auth('api')->factory()->getTTL() * 60,
  264. 'm_id' => empty($mId) ? auth('api')->user()->{'id'} : $mId,
  265. ],
  266. 'token获取成功',1,200,[],0,$new_token, time()
  267. );
  268. }
  269. }