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); } }