123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php
- namespace app\api\controller\wechat;
- use app\models\user\WechatUser;
- use app\Request;
- use crmeb\services\CacheService;
- use crmeb\services\MiniProgramService;
- use crmeb\services\template\Template;
- use crmeb\services\UtilService;
- use app\models\user\UserToken;
- use crmeb\services\SystemConfigService;
- use app\models\user\User;
- use Psr\SimpleCache\InvalidArgumentException;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\facade\Cache;
- use crmeb\services\SubscribeTemplateService;
- use think\facade\Config;
- /**
- * 小程序相关
- * Class AuthController
- * @package app\api\controller\wechat
- */
- class AuthController
- {
- /**
- * 小程序授权登录
- * @param Request $request
- * @return mixed
- * @throws InvalidArgumentException
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- * @throws \Exception
- */
- public function mp_auth(Request $request)
- {
- $cache_key = '';
- list($code, $post_cache_key, $login_type) = UtilService::postMore([
- ['code', ''],
- ['cache_key', ''],
- ['login_type', '']
- ], $request, true);
- $session_key = Cache::get('eb_api_code_' . $post_cache_key);
- if (!$code && !$session_key)
- return app('json')->fail('授权失败,参数有误');
- if ($code && !$session_key) {
- try {
- $userInfoCong = MiniProgramService::getUserInfo($code, $request->mer_id());//这里改成需要传商户id的获取方式
- $session_key = $userInfoCong['session_key'];
- $cache_key = md5(time() . $code);
- Cache::set('eb_api_code_' . $cache_key, $session_key, 86400);
- } catch (\Exception $e) {
- return app('json')->fail('获取session_key失败,请检查您的配置!', ['line' => $e->getLine(), 'message' => $e->getMessage()]);
- }
- }
- $data = UtilService::postMore([
- ['spread_spid', 0],
- ['spread_code', ''],
- ['iv', ''],
- ['encryptedData', ''],
- ]);//获取前台传的code
- try {
- //解密获取用户信息
- $userInfo = MiniProgramService::encryptor($session_key, $data['iv'], $data['encryptedData']);
- } catch (\Exception $e) {
- if ($e->getCode() == '-41003') return app('json')->fail('获取会话密匙失败');
- }
- if (!isset($userInfo['openId'])) return app('json')->fail('openid获取失败');
- if (!isset($userInfo['unionId'])) $userInfo['unionId'] = '';
- $userInfo['spid'] = $data['spread_spid'];
- $userInfo['code'] = $data['spread_code'];
- $userInfo['session_key'] = $session_key;
- $userInfo['login_type'] = $login_type;
- $userInfo['mer_id'] = $request->mer_id();
- $uid = WechatUser::routineOauth($userInfo);
- $userInfo = User::where('uid', $uid)->find();
- if ($userInfo->login_type == 'h5' && ($h5UserInfo = User::where(['account' => $userInfo->phone, 'phone' => $userInfo->phone, 'user_type' => 'h5'])->find()))
- $token = UserToken::createToken($userInfo, 'routine');
- else
- $token = UserToken::createToken($userInfo, 'routine');
- if ($token) {
- event('UserLogin', [$userInfo, $token]);
- return app('json')->successful('登陆成功!', [
- 'token' => $token['token'],
- 'userInfo' => $userInfo,
- 'expires_time' => $token['params']['exp'],
- 'cache_key' => $cache_key
- ]);
- } else
- return app('json')->fail('获取用户访问token失败!');
- }
- /**
- * 获取授权logo
- * @param Request $request
- * @return mixed
- */
- public function get_logo(Request $request)
- {
- $logoType = $request->get('type', 1);
- switch ((int)$logoType) {
- case 1:
- $logo = sys_config('routine_logo');
- break;
- case 2:
- $logo = sys_config('wechat_avatar');
- break;
- default:
- $logo = '';
- break;
- }
- if (strstr($logo, 'http') === false && $logo) $logo = sys_config('site_url', '', $request->mer_id()) . $logo;
- return app('json')->successful(['logo_url' => str_replace('\\', '/', $logo)]);
- }
- /**
- * 保存form id
- * @param Request $request
- * @return mixed
- */
- public function set_form_id(Request $request)
- {
- $formId = $request->post('formId', '');
- if (!$formId) return app('json')->fail('缺少form id');
- return app('json')->successful('保存form id 成功!', ['uid' => $request->uid()]);
- }
- /**
- * 小程序支付回调
- * @param $mer_id
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws \EasyWeChat\Core\Exceptions\FaultException
- * @throws \think\Exception
- */
- public function notify($mer_id)
- {
- @file_put_contents('callback.txt', $mer_id . PHP_EOL, FILE_APPEND);
- MiniProgramService::handleNotify($mer_id);
- }
- /**
- * 获取小程序订阅消息id
- * @return mixed
- * @throws \throwable
- */
- public function teml_ids()
- {
- $temlIdsName = Config::get('template.stores.subscribe.template_id', []);
- $temlIdsList = CacheService::get('TEML_IDS_LIST', function () use ($temlIdsName) {
- $temlId = [];
- $templdata = new Template('subscribe');
- foreach ($temlIdsName as $key => $item) {
- $temlId[strtolower($key)] = $templdata->getTempId($item);
- }
- return $temlId;
- });
- return app('json')->success($temlIdsList);
- }
- /**
- * 获取小程序直播列表
- * @param Request $request
- * @return mixed
- * @throws \throwable
- */
- public function live(Request $request)
- {
- [$page, $limit] = UtilService::getMore([
- ['page', 1],
- ['limit', 10],
- ], $request, true);
- // $list = CacheService::get('WECHAT_LIVE_LIST_' . $page . '_' . $limit, function () use ($page, $limit, $request) {
- $list = MiniProgramService::getLiveInfo($page, $limit, $request->mer_id());
- foreach ($list as $key => $value) {
- $list[$key]['_start_time'] = date('m-d H:i', $value['start_time']);
- }
- // return $list;
- // }, 600);
- return app('json')->success($list);
- }
- }
|