Auth.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace App\Http\Controllers\Miniprogram;
  3. use App\Exceptions\AlertException;
  4. use App\Services\Auth\AuthKeyService;
  5. use JWTAuth;
  6. use Tymon\JWTAuth\Exceptions\JWTException;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Redis;
  9. use App\Http\Controllers\Controller;
  10. use App\Models\User\AuthKey;
  11. use App\Models\User\UserModel;
  12. class Auth extends Controller
  13. {
  14. /**
  15. * 用户认证
  16. * @return int uid 用户id
  17. * @return JWTException
  18. * @throws JWTException
  19. */
  20. public static function auth()
  21. {
  22. try {
  23. if (!$user = JWTAuth::parseToken()->authenticate()) {
  24. throw new JWTException("请先授权登陆", 401);
  25. }
  26. } catch (JWTException $e) {
  27. throw new JWTException("登陆已过期,请重新授权登陆", 401);
  28. }
  29. // 代理
  30. $debug_uid = Redis::hget("fpdx_admin_debug", $user->uid);
  31. if (!empty($debug_uid)) {
  32. return $debug_uid;
  33. }
  34. return $user->uid;
  35. }
  36. /**
  37. * 获取用户信息
  38. * @return array
  39. * @throws JWTException
  40. */
  41. public function user()
  42. {
  43. $uid = self::auth();
  44. $user = UserModel::find($uid);
  45. return [
  46. 'code' => 200,
  47. 'message' => 'success',
  48. 'data' => $user
  49. ];
  50. }
  51. /**
  52. * 绑定小程序登录方式
  53. * @param Request $request
  54. * @return array
  55. * @throws JWTException
  56. */
  57. public function bindAuthType(Request $request)
  58. {
  59. $this->validate($request, [
  60. 'unionid' => 'required',
  61. 'openid' => 'required'
  62. ]);
  63. try {
  64. if (!$user = JWTAuth::parseToken()->authenticate()) {
  65. throw new JWTException("请先授权登陆", 401);
  66. }
  67. } catch (JWTException $e) {
  68. throw new JWTException("登陆已过期,请重新授权登陆", 401);
  69. }
  70. $public_id = config('miniprogram.public_id');
  71. $auth = AuthKey::where('auth_key', $request->unionid)->first();
  72. if (collect($auth)->isEmpty()) {
  73. throw new JWTException("用户未授权", 401);
  74. } elseif ($user->uid != $auth->uid) {
  75. throw new JWTException("无绑定权限", 403);
  76. }
  77. $typeAuth = AuthKey::where(array(['uid', $auth->uid], ['auth_type', $public_id]))->first();
  78. if (!collect($typeAuth)->isEmpty()) {
  79. throw new JWTException("你已绑定过该微信号,请联系管理员解绑", 403);
  80. }
  81. $authkey = new AuthKey();
  82. $openAuth = $authkey->where('auth_key', $request->openid)->first();
  83. if (collect($openAuth)->isEmpty()) {
  84. $openAuth = $authkey->fill([
  85. 'uid' => $auth->uid,
  86. 'auth_key' => $request->openid,
  87. 'auth_type' => $public_id
  88. ]);
  89. if ($openAuth->save()) {
  90. return [
  91. 'code' => 200,
  92. 'message' => 'success'
  93. ];
  94. } else {
  95. throw new JWTException("绑定异常", 401);
  96. }
  97. } elseif ($openAuth->auth_type != $public_id) {
  98. throw new JWTException("该登陆key已被绑定,请联系管理员", 401);
  99. } else {
  100. return [
  101. 'code' => 200,
  102. 'message' => 'success'
  103. ];
  104. }
  105. }
  106. /**
  107. * 绑定微信id
  108. * @param Request $request
  109. * @return array
  110. * @throws AlertException
  111. */
  112. public function bindWxid(Request $request)
  113. {
  114. $this->validate($request, [
  115. 'wxid' => 'required'
  116. ]);
  117. $wxid = $request->post('wxid');
  118. $uid = \App\Http\Controllers\Core\Auth::auth();
  119. $aks = new AuthKeyService();
  120. $aks->bindKey($uid, $wxid, 'wxid');
  121. return array(
  122. 'code' => 200,
  123. 'message' => 'success'
  124. );
  125. }
  126. /**
  127. * jscode
  128. * @param string $jscode
  129. * @return array
  130. * @throws \Exception
  131. */
  132. public function jscode(string $jscode)
  133. {
  134. $core = new Core();
  135. $data = $core->jscode2session($jscode);
  136. return [
  137. 'code' => 200,
  138. 'message' => 'success',
  139. 'data' => $data
  140. ];
  141. }
  142. /**
  143. * 解密小程序数据包
  144. * @param Request $request
  145. * @return array
  146. * @throws AlertException
  147. */
  148. public function decryptData(Request $request)
  149. {
  150. $this->validate($request, [
  151. 'iv' => 'required',
  152. 'session_key' => 'required',
  153. 'encrypted_data' => 'required'
  154. ]);
  155. $appid = config('miniprogram.app_id');
  156. $session_key = $request->input('session_key');
  157. $decrypt = new Decrypt($appid, $session_key);
  158. $result = $decrypt->decryptData($request->input('encrypted_data'), $request->input('iv'), $data);
  159. if ($result == 0) {
  160. return array(
  161. 'code' => 200,
  162. 'message' => 'success',
  163. 'data' => $data
  164. );
  165. } else {
  166. throw new AlertException("解密失败", $result);
  167. }
  168. }
  169. }