AuthController.php 7.8 KB

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