AuthController.php 7.9 KB

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