AuthController.php 6.2 KB

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