AuthController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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($userInfo['openId'])) return app('json')->fail('openid获取失败');
  64. if (!isset($userInfo['unionId'])) $userInfo['unionId'] = '';
  65. $userInfo['spid'] = $data['spread_spid'];
  66. $userInfo['code'] = $data['spread_code'];
  67. $userInfo['session_key'] = $session_key;
  68. $userInfo['login_type'] = $login_type;
  69. $uid = WechatUser::routineOauth($userInfo);
  70. $userInfo = User::where('uid', $uid)->find();
  71. if ($userInfo->login_type == 'h5' && ($h5UserInfo = User::where(['account' => $userInfo->phone, 'phone' => $userInfo->phone, 'user_type' => 'h5'])->find()))
  72. $token = UserToken::createToken($userInfo, 'routine');
  73. else
  74. $token = UserToken::createToken($userInfo, 'routine');
  75. if ($token) {
  76. event('UserLogin', [$userInfo, $token]);
  77. return app('json')->successful('登陆成功!', [
  78. 'token' => $token->token,
  79. 'userInfo' => $userInfo,
  80. 'expires_time' => strtotime($token->expires_time),
  81. 'cache_key' => $cache_key
  82. ]);
  83. } else
  84. return app('json')->fail('获取用户访问token失败!');
  85. }
  86. /**
  87. * 获取授权logo
  88. * @param Request $request
  89. * @return mixed
  90. */
  91. public function get_logo(Request $request)
  92. {
  93. $logoType = $request->get('type', 1);
  94. switch ((int)$logoType) {
  95. case 1:
  96. $logo = sys_config('routine_logo');
  97. break;
  98. case 2:
  99. $logo = sys_config('wechat_avatar');
  100. break;
  101. default:
  102. $logo = '';
  103. break;
  104. }
  105. if (strstr($logo, 'http') === false && $logo) $logo = sys_config('site_url') . $logo;
  106. return app('json')->successful(['logo_url' => str_replace('\\', '/', $logo)]);
  107. }
  108. /**
  109. * 保存form id
  110. * @param Request $request
  111. * @return mixed
  112. */
  113. public function set_form_id(Request $request)
  114. {
  115. $formId = $request->post('formId', '');
  116. if (!$formId) return app('json')->fail('缺少form id');
  117. return app('json')->successful('保存form id 成功!', ['uid' => $request->uid()]);
  118. }
  119. /**
  120. * 小程序支付回调
  121. */
  122. public function notify()
  123. {
  124. MiniProgramService::handleNotify();
  125. }
  126. /**
  127. * 获取小程序订阅消息id
  128. * @return mixed
  129. */
  130. public function teml_ids()
  131. {
  132. $temlIdsName = SubscribeTemplateService::getConstants();
  133. $temlIdsList = CacheService::get('TEML_IDS_LIST', function () use ($temlIdsName) {
  134. $temlId = [];
  135. foreach ($temlIdsName as $key => $item) {
  136. $temlId[strtolower($key)] = SubscribeTemplateService::setTemplateId($item);
  137. }
  138. return $temlId;
  139. });
  140. return app('json')->success($temlIdsList);
  141. }
  142. /**
  143. * 获取小程序直播列表
  144. * @param Request $request
  145. * @return mixed
  146. */
  147. public function live(Request $request)
  148. {
  149. [$page, $limit] = UtilService::getMore([
  150. ['page', 1],
  151. ['limit', 10],
  152. ], $request, true);
  153. $list = CacheService::get('WECHAT_LIVE_LIST_' . $page . '_' . $limit, function () use ($page, $limit) {
  154. $list = MiniProgramService::getLiveInfo($page, $limit);
  155. foreach ($list as &$item) {
  156. $item['_start_time'] = date('m-d H:i', $item['start_time']);
  157. }
  158. return $list;
  159. }, 600);
  160. return app('json')->success($list);
  161. }
  162. }