AuthController.php 6.3 KB

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