AuthController.php 6.5 KB

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