AuthController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\api\controller\wechat;
  3. use app\models\user\WechatUser;
  4. use app\Request;
  5. use crmeb\services\CacheService;
  6. use crmeb\services\MiniProgramService;
  7. use crmeb\services\UtilService;
  8. use app\models\user\UserToken;
  9. use crmeb\services\SystemConfigService;
  10. use app\models\user\User;
  11. use app\models\routine\RoutineFormId;
  12. use think\facade\Cache;
  13. use crmeb\services\SubscribeTemplateService;
  14. use think\facade\Db;
  15. /**
  16. * 小程序相关
  17. * Class AuthController
  18. * @package app\api\controller\wechat
  19. */
  20. class AuthController
  21. {
  22. /**
  23. * 小程序授权登录
  24. * @param Request $request
  25. * @return mixed
  26. * @throws \Psr\SimpleCache\InvalidArgumentException
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. * @throws \think\exception\DbException
  30. */
  31. public function mp_auth(Request $request)
  32. {
  33. if (WechatUser::count() > User::count()) {
  34. WechatUser::where('uid', 'not in', User::column('uid'))->delete();
  35. }
  36. $cache_key = '';
  37. list($code, $post_cache_key, $login_type) = UtilService::postMore([
  38. ['code', ''],
  39. ['cache_key', ''],
  40. ['login_type', '']
  41. ], $request, true);
  42. $session_key = Cache::get('eb_api_code_' . $post_cache_key);
  43. if (!$code && !$session_key)
  44. return app('json')->fail('授权失败,参数有误');
  45. if ($code && !$session_key) {
  46. try {
  47. $userInfoCong = MiniProgramService::getUserInfo($code);
  48. $session_key = $userInfoCong['session_key'];
  49. $cache_key = md5(time() . $code);
  50. Cache::set('eb_api_code_' . $cache_key, $session_key, 86400);
  51. } catch (\Exception $e) {
  52. return app('json')->fail('获取session_key失败,请检查您的配置!', ['line' => $e->getLine(), 'message' => $e->getMessage()]);
  53. }
  54. }
  55. $data = UtilService::postMore([
  56. ['spread_spid', 0],
  57. ['spread_code', ''],
  58. ['iv', ''],
  59. ['encryptedData', ''],
  60. ]);//获取前台传的code
  61. try {
  62. //解密获取用户信息
  63. $userInfo = MiniProgramService::encryptor($session_key, $data['iv'], $data['encryptedData']);
  64. } catch (\Exception $e) {
  65. if ($e->getCode() == '-41003') return app('json')->fail('获取会话密匙失败');
  66. }
  67. if (!isset($userInfo['openId'])) $userInfo['openId'] = $userInfoCong['openid'];
  68. if (!isset($userInfo['openId'])) return app('json')->fail('openid获取失败');
  69. if (!isset($userInfo['unionId'])) $userInfo['unionId'] = $userInfoCong['unionid'] ?? '';
  70. $userInfo['spid'] = $data['spread_spid'];
  71. $userInfo['code'] = $data['spread_code'];
  72. $userInfo['session_key'] = $session_key;
  73. $userInfo['login_type'] = $login_type;
  74. $uid = WechatUser::routineOauth($userInfo);
  75. $userInfo = User::where('uid', $uid)->find();
  76. if ($userInfo->login_type == 'h5' && ($h5UserInfo = User::where(['account' => $userInfo->phone, 'phone' => $userInfo->phone, 'user_type' => 'h5'])->find()))
  77. $token = UserToken::createToken($userInfo, 'routine');
  78. else
  79. $token = UserToken::createToken($userInfo, 'routine');
  80. if ($token) {
  81. event('UserLogin', [$userInfo, $token]);
  82. return app('json')->successful('登陆成功!', [
  83. 'token' => $token->token,
  84. 'userInfo' => $userInfo,
  85. 'expires_time' => strtotime($token->expires_time),
  86. 'cache_key' => $cache_key
  87. ]);
  88. } else
  89. return app('json')->fail('获取用户访问token失败!');
  90. }
  91. /**
  92. * 获取授权logo
  93. * @param Request $request
  94. * @return mixed
  95. */
  96. public function get_logo(Request $request)
  97. {
  98. $logoType = $request->get('type', 1);
  99. switch ((int)$logoType) {
  100. case 1:
  101. $logo = sys_config('routine_logo');
  102. break;
  103. case 2:
  104. $logo = sys_config('wechat_avatar');
  105. break;
  106. default:
  107. $logo = '';
  108. break;
  109. }
  110. if (strstr($logo, 'http') === false && $logo) $logo = sys_config('site_url') . $logo;
  111. return app('json')->successful(['logo_url' => str_replace('\\', '/', $logo)]);
  112. }
  113. /**
  114. * 保存form id
  115. * @param Request $request
  116. * @return mixed
  117. */
  118. public function set_form_id(Request $request)
  119. {
  120. $formId = $request->post('formId', '');
  121. if (!$formId) return app('json')->fail('缺少form id');
  122. return app('json')->successful('保存form id 成功!', ['uid' => $request->uid()]);
  123. }
  124. /**
  125. * 小程序支付回调
  126. */
  127. public function notify()
  128. {
  129. MiniProgramService::handleNotify();
  130. }
  131. /**
  132. * 获取小程序订阅消息id
  133. * @return mixed
  134. */
  135. public function teml_ids()
  136. {
  137. $temlIdsName = SubscribeTemplateService::getConstants();
  138. $temlIdsList = CacheService::get('TEML_IDS_LIST', function () use ($temlIdsName) {
  139. $temlId = [];
  140. foreach ($temlIdsName as $key => $item) {
  141. $temlId[strtolower($key)] = SubscribeTemplateService::setTemplateId($item);
  142. }
  143. return $temlId;
  144. });
  145. return app('json')->success($temlIdsList);
  146. }
  147. /**
  148. * 获取小程序直播列表
  149. * @param Request $request
  150. * @return mixed
  151. */
  152. public function live(Request $request)
  153. {
  154. [$page, $limit] = UtilService::getMore([
  155. ['page', 1],
  156. ['limit', 10],
  157. ], $request, true);
  158. $list = CacheService::get('WECHAT_LIVE_LIST_' . $page . '_' . $limit, function () use ($page, $limit) {
  159. $list = MiniProgramService::getLiveInfo($page, $limit);
  160. foreach ($list as &$item) {
  161. $item['_start_time'] = date('m-d H:i', $item['start_time']);
  162. }
  163. return $list;
  164. }, 600);
  165. return app('json')->success($list);
  166. }
  167. }