AuthController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\api\controller\wechat;
  3. use app\models\user\WechatUser;
  4. use app\Request;
  5. use crmeb\services\MiniProgramService;
  6. use crmeb\services\UtilService;
  7. use app\models\user\UserToken;
  8. use crmeb\services\SystemConfigService;
  9. use app\models\user\User;
  10. use app\models\routine\RoutineFormId;
  11. use think\facade\Cache;
  12. /**
  13. * 小程序相关
  14. * Class AuthController
  15. * @package app\api\controller\wechat
  16. */
  17. class AuthController
  18. {
  19. /**
  20. * 小程序授权登录
  21. * @param Request $request
  22. * @return mixed
  23. * @throws \Psr\SimpleCache\InvalidArgumentException
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. * @throws \think\exception\DbException
  27. */
  28. public function mp_auth(Request $request)
  29. {
  30. $cache_key = '';
  31. list($code,$post_cache_key,$login_type) = UtilService::postMore([
  32. ['code',''],
  33. ['cache_key',''],
  34. ['login_type','']
  35. ],$request,true);
  36. $session_key = Cache::get('eb_api_code_'.$post_cache_key);
  37. if (!$code && !$session_key)
  38. return app('json')->fail('授权失败,参数有误');
  39. if($code && !$session_key){
  40. try {
  41. $userInfoCong = MiniProgramService::getUserInfo($code);
  42. $session_key = $userInfoCong['session_key'];
  43. $cache_key = md5(time().$code);
  44. Cache::set('eb_api_code_'.$cache_key,$session_key,86400);
  45. } catch (\Exception $e) {
  46. return app('json')->fail('获取session_key失败,请检查您的配置!', ['line' => $e->getLine(), 'message' => $e->getMessage()]);
  47. }
  48. }
  49. $data = UtilService::postMore([
  50. ['spread_spid', 0],
  51. ['spread_code', ''],
  52. ['iv', ''],
  53. ['encryptedData', ''],
  54. ]);//获取前台传的code
  55. try {
  56. //解密获取用户信息
  57. $userInfo = MiniProgramService::encryptor($session_key, $data['iv'], $data['encryptedData']);
  58. } catch (\Exception $e) {
  59. if ($e->getCode() == '-41003') return app('json')->fail('获取会话密匙失败');
  60. }
  61. if (!isset($userInfo['openId'])) return app('json')->fail('openid获取失败');
  62. if (!isset($userInfo['unionId'])) $userInfo['unionId'] = '';
  63. $userInfo['spid'] = $data['spread_spid'];
  64. $userInfo['code'] = $data['spread_code'];
  65. $userInfo['session_key'] = $session_key;
  66. $userInfo['login_type'] = $login_type;
  67. $uid = WechatUser::routineOauth($userInfo);
  68. $userInfo = User::where('uid',$uid)->find();
  69. if($userInfo->login_type == 'h5' && ($h5UserInfo = User::where(['account'=>$userInfo->phone,'phone'=>$userInfo->phone,'user_type'=>'h5'])->find()))
  70. $token = UserToken::createToken($userInfo, 'routine');
  71. else
  72. $token = UserToken::createToken($userInfo, 'routine');
  73. if($token) {
  74. event('UserLogin', [$userInfo, $token]);
  75. return app('json')->successful('登陆成功!', [
  76. 'token' => $token->token,
  77. 'userInfo' => $userInfo,
  78. 'expires_time' => strtotime($token->expires_time),
  79. 'cache_key' => $cache_key
  80. ]);
  81. }else
  82. return app('json')->fail('获取用户访问token失败!');
  83. }
  84. /**
  85. * 获取授权logo
  86. * @param Request $request
  87. * @return mixed
  88. */
  89. public function get_logo(Request $request)
  90. {
  91. $logoType = $request->get('type',1);
  92. switch ((int)$logoType) {
  93. case 1:
  94. $logo = sysConfig('routine_logo');
  95. break;
  96. case 2:
  97. $logo = sysConfig('wechat_avatar');
  98. break;
  99. default:
  100. $logo = '';
  101. break;
  102. }
  103. if (strstr($logo,'http') === false && $logo) $logo = sysConfig('site_url').$logo;
  104. return app('json')->successful(['logo_url'=>str_replace('\\','/',$logo)]);
  105. }
  106. /**
  107. * 保存form id
  108. * @param Request $request
  109. * @return mixed
  110. */
  111. public function set_form_id(Request $request)
  112. {
  113. $formId = $request->post('formId','');
  114. if(!$formId) return app('json')->fail('缺少form id');
  115. RoutineFormId::SetFormId($formId,$request->uid());
  116. return app('json')->successful('保存form id 成功!',['uid'=>$request->uid()]);
  117. }
  118. /**
  119. * 小程序支付回调
  120. */
  121. public function notify()
  122. {
  123. MiniProgramService::handleNotify();
  124. }
  125. }