AuthController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. $userInfo['site_id'] = $request->site_id();
  73. $uid = WechatUser::routineOauth($userInfo);
  74. $userInfo = User::where('uid', $uid)->find();
  75. if ($userInfo->login_type == 'h5' && ($h5UserInfo = User::where(['account' => $userInfo->phone, 'phone' => $userInfo->phone, 'user_type' => 'h5'])->find()))
  76. $token = UserToken::createToken($userInfo, 'routine');
  77. else
  78. $token = UserToken::createToken($userInfo, 'routine');
  79. if ($token) {
  80. event('UserLogin', [$userInfo, $token]);
  81. return app('json')->successful('登陆成功!', [
  82. 'token' => $token->token,
  83. 'userInfo' => $userInfo,
  84. 'expires_time' => strtotime($token->expires_time),
  85. 'cache_key' => $cache_key
  86. ]);
  87. } else
  88. return app('json')->fail('获取用户访问token失败!');
  89. }
  90. /**
  91. * 小程序授权登录
  92. * @param Request $request
  93. * @return mixed
  94. * @throws \Psr\SimpleCache\InvalidArgumentException
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. * @throws \think\exception\DbException
  98. */
  99. public function app_auth(Request $request)
  100. {
  101. Db::query("DELETE from eb_wechat_user where uid not in (select uid from eb_user)");
  102. $cache_key = '';
  103. $userInfo = UtilService::postMore([
  104. ['openId', ''],
  105. ['nickName', ''],
  106. ['city', ''],
  107. ['province', ''],
  108. ['country', ''],
  109. ['avatarUrl', ''],
  110. ['gender', ''],
  111. ['unionId', ''],
  112. ], $request);
  113. $data = UtilService::postMore([
  114. ['spread_spid', 0],
  115. ]);//获取前台传的code
  116. if (!isset($userInfo['openId'])) return app('json')->fail('openid获取失败');
  117. if (!isset($userInfo['unionId'])) $userInfo['unionId'] = '';
  118. $userInfo['spid'] = $data['spread_spid'];
  119. $uid = WechatUser::appOauth($userInfo);
  120. $userInfo = User::where('uid', $uid)->find();
  121. if ($userInfo->login_type == 'h5' && ($h5UserInfo = User::where(['account' => $userInfo->phone, 'phone' => $userInfo->phone, 'user_type' => 'h5'])->find()))
  122. $token = UserToken::createToken($userInfo, 'h5');
  123. else
  124. $token = UserToken::createToken($userInfo, 'h5');
  125. if ($token) {
  126. event('UserLogin', [$userInfo, $token]);
  127. return app('json')->successful('登陆成功!', [
  128. 'token' => $token->token,
  129. 'userInfo' => $userInfo,
  130. 'expires_time' => strtotime($token->expires_time),
  131. 'cache_key' => $cache_key
  132. ]);
  133. } else
  134. return app('json')->fail('获取用户访问token失败!');
  135. }
  136. /**
  137. * 获取授权logo
  138. * @param Request $request
  139. * @return mixed
  140. */
  141. public function get_logo(Request $request)
  142. {
  143. $logoType = $request->get('type', 1);
  144. switch ((int)$logoType) {
  145. case 1:
  146. $logo = sys_config('routine_logo');
  147. break;
  148. case 2:
  149. $logo = sys_config('wechat_avatar');
  150. break;
  151. default:
  152. $logo = '';
  153. break;
  154. }
  155. if (strstr($logo, 'http') === false && $logo) $logo = sys_config('site_url') . $logo;
  156. return app('json')->successful(['logo_url' => str_replace('\\', '/', $logo)]);
  157. }
  158. /**
  159. * 保存form id
  160. * @param Request $request
  161. * @return mixed
  162. */
  163. public function set_form_id(Request $request)
  164. {
  165. $formId = $request->post('formId', '');
  166. if (!$formId) return app('json')->fail('缺少form id');
  167. return app('json')->successful('保存form id 成功!', ['uid' => $request->uid()]);
  168. }
  169. /**
  170. * 小程序支付回调
  171. */
  172. public function notify()
  173. {
  174. MiniProgramService::handleNotify();
  175. }
  176. /**
  177. * 获取小程序订阅消息id
  178. * @return mixed
  179. */
  180. public function teml_ids()
  181. {
  182. $temlIdsName = SubscribeTemplateService::getConstants();
  183. $temlIdsList = CacheService::get('TEML_IDS_LIST', function () use ($temlIdsName) {
  184. $temlId = [];
  185. foreach ($temlIdsName as $key => $item) {
  186. $temlId[strtolower($key)] = SubscribeTemplateService::setTemplateId($item);
  187. }
  188. return $temlId;
  189. });
  190. return app('json')->success($temlIdsList);
  191. }
  192. /**
  193. * 获取小程序直播列表
  194. * @param Request $request
  195. * @return mixed
  196. */
  197. public function live(Request $request)
  198. {
  199. [$page, $limit] = UtilService::getMore([
  200. ['page', 1],
  201. ['limit', 10],
  202. ], $request, true);
  203. $list = CacheService::get('WECHAT_LIVE_LIST_' . $page . '_' . $limit, function () use ($page, $limit) {
  204. $list = MiniProgramService::getLiveInfo($page, $limit);
  205. foreach ($list as &$item) {
  206. $item['_start_time'] = date('m-d H:i', $item['start_time']);
  207. }
  208. return $list;
  209. }, 600);
  210. return app('json')->success($list);
  211. }
  212. }