Login.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018-2020 rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: TABLE ME
  8. // +----------------------------------------------------------------------
  9. // | Date: 2020-08-25 17:23
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\controller;
  13. use app\BaseController;
  14. use app\model\api\User as UserModel;
  15. use app\model\api\UserLogin;
  16. use app\model\api\UserShowTemplate;
  17. use app\Request;
  18. use library\lib\weixina;
  19. use library\services\JsonNull;
  20. use library\services\UtilService;
  21. use think\Cache;
  22. use think\Image;
  23. class Login extends BaseController
  24. {
  25. /**
  26. * 微信登录
  27. * @param Request $request
  28. */
  29. public function weixinLogin(Request $request){
  30. [$code,$nickname,$avatar,$device_id,$model,$platform,$system,$gender,$parent_uid] = UtilService::getMore([
  31. ['code', '', 'empty', 'code参数错误'],
  32. ['nickname','微信用户',],
  33. ['avatar','',],
  34. ['device_id',''],
  35. ['model',''],
  36. ['platform',''],
  37. ['system',''],
  38. ['gender','0'],
  39. ['parent_uid','0'],
  40. ], $request, true);
  41. //用户登录
  42. if(empty($avatar)){
  43. //默认通向
  44. $avatar = "";
  45. }
  46. if(!in_array((string)$gender, array("0","1","2"))){
  47. $gender = 0;
  48. }
  49. $regip = get_client_ip();
  50. if(empty($regip)){
  51. $regip = $request->ip();
  52. }
  53. $weixinA = new weixina();
  54. $data=$weixinA->auth_code2Session($code);//新版
  55. if(empty($data) || empty($data['openid'])) {
  56. return app('json')->fail("微信授权登录失败,关闭页面重新授权".$weixinA->error);
  57. }
  58. $member = (new UserModel)->where("openid",$data["openid"])->find();
  59. if(empty($member)){
  60. $p_id = 0;
  61. if(!empty($parent_uid)){
  62. $pcount = (new UserModel)->where("uid",$parent_uid)->count();
  63. if($pcount>0){
  64. $p_id = (int)$parent_uid;
  65. }
  66. }
  67. //用户注册
  68. $uid = (new UserModel)->insertGetId(array(
  69. "openid" =>$data["openid"],
  70. "unionid" =>empty($data["unionid"])?"":$data["unionid"],
  71. "nickname" =>$nickname,
  72. "avatar" =>$avatar,
  73. "sex" =>(int)$gender,
  74. "regtime" =>time(),
  75. "regip" =>$regip,
  76. "parent_uid" =>$p_id,
  77. "parent_time"=>$p_id <= 0 ? 0 : time(),
  78. ));
  79. if(empty($uid)){
  80. return app('json')->fail("网络繁忙,请稍后尝试");
  81. }
  82. //绑定初始化模板
  83. (new UserShowTemplate)->userBindTempInit($uid);
  84. //生成MD5
  85. $token = base64_encode(md5($uid ."openid" .$data["openid"] . time()));
  86. (new UserModel)->where('uid', $uid)->save(['lasttime'=>time(),'token'=>$token]);
  87. //生成登录日志
  88. (new UserLogin)->insert([
  89. 'uid' => $uid,
  90. 'ip' => $regip,
  91. 'time' => time(),
  92. 'device_id' => $device_id,
  93. 'from_plat' => $platform,
  94. 'device_type' => $system,
  95. 'version' => $model,
  96. ]);
  97. $result = [];
  98. $result['nickname'] = $nickname;
  99. $result['avatar'] = $avatar;
  100. $result['token'] = $token;
  101. return app('json')->success($result);
  102. }
  103. if ($member['status'] == -1) {
  104. return app('json')->fail("账号已被禁用");
  105. }
  106. //生成MD5
  107. $token = base64_encode(md5($member['uid'] ."openid" . $member['openid'] . time()));
  108. (new UserModel)->where('uid', $member['uid'])->save([
  109. 'lasttime' => time(),
  110. 'token' => $token,
  111. ]);
  112. //生成登录日志
  113. (new UserLogin)->insert([
  114. 'uid' => $member['uid'],
  115. 'ip' => $regip,
  116. 'time' => time(),
  117. 'device_id' => $device_id,
  118. 'from_plat' => $platform,
  119. 'device_type' => $system,
  120. 'version' => $model,
  121. ]);
  122. $result = [];
  123. $result['nickname'] = $member['nickname'];
  124. $result['avatar'] = $member['avatar'];
  125. $result['token'] = $token;
  126. return app('json')->success($result);
  127. }
  128. }