AuthController.php 6.3 KB

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