AuthController.php 5.9 KB

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