$token->token, * 'userInfo' => $userInfo, * 'expires_time' => strtotime($token->expires_time), * 'cache_key' => $cache_key * 以上兼容旧协议 * 'status' => 0/1 0 表示正常登录成功, 1 表示需要进一步调用 wx.getUserProfile 进行授权 */ public function mp_auth_simple(Request $request) { list($code) = UtilService::postMore([ ['code', ''], ], $request, true); // Log::debug(__FUNCTION__ . " param code: $code"); try { $json2sess = MiniProgramService::getUserInfo($code); // $sess_key = $json2sess['session_key'] ?? ''; $openId = $json2sess['openid'] ?? ''; $unionid = $json2sess['unionid'] ?? ''; // Log::debug("openid=$openId, unionid=$unionid"); // find by unionid if ($unionid != '') { $uid = WechatUser::where(['unionid' => $unionid]) ->where('user_type', 'routine')->value('uid'); } if (!$uid && $openId != '') { $uid = WechatUser::where(['routine_openid' => $openId])->where('user_type', 'routine')->value('uid'); } if (!$uid) { return app('json')->successful([ 'token' => '', 'userInfo' => [], 'expires_time' => 0, 'cache_key' => '', 'status' => 1, // 需进一步 ]); } $user = User::get($uid); if (!$user) { return app('json')->successful([ 'token' => '', 'userInfo' => [], 'expires_time' => 0, 'cache_key' => '', 'status' => 1, // 需进一步 ]); } $token = UserToken::createToken($user, 'routine'); if (!$token) { return app('json')->fail('获取用户访问token失败!'); } // Log::debug("token=" . $token->token); // 缓存 session_key $cache_key = md5(time() . $code); Cache::set('eb_api_code_' . $cache_key, $json2sess, SECONDS_OF_ONEDAY); // 获取用户上次刷新时间,距今超过 2 周就刷新 $last = (new UserRds)->get($uid, UserRds::FIELD_LASTREFRESH); if (time() - intval($last) >= SECONDS_OF_ONEDAY * 20) { return app('json')->successful([ 'token' => '', 'userInfo' => [], 'expires_time' => 0, 'cache_key' => '', 'status' => 1, // 需进一步 ]); } // 返回登录成功 event('UserLogin', [$user, $token]); return app('json')->successful([ 'token' => $token->token, 'userInfo' => $user->toArray(), 'expires_time' => strtotime($token->expires_time), 'cache_key' => $cache_key, 'status' => 0, // 登录成功 ]); } catch (\Exception $e) { errlog(__FUNCTION__ . 'exception:' . $e->getMessage()); return app('json')->fail('获取session_key失败'); } } /** * @deprecated * 小程序在 mp_auth_login 返回特定值(表示用户不存在,或刷新用户信息)后, * 调用 wx.getUserProfile 获取用户信息,来注册或刷新信息。 */ public function mp_auth_with_userinfo(Request $request) { list($cache_key, $spread_spid, $spread_code, $iv, $encryptedData, $login_type) = UtilService::postMore([ ['cache_key', ''], ['spread_spid', 0], ['spread_code', ''], ['iv', ''], ['encryptedData', ''], ['login_type', ''], ], $request, true); // 获取缓存 openId, sessionKey $json2sess = Cache::get('eb_api_code_' . $cache_key); if (!$json2sess) { return app('json')->fail('访问超时'); } // 解密用户数据 try { $userInfo = MiniProgramService::encryptor($json2sess['session_key'], $iv, $encryptedData); } catch (\Exception $e) { if ($e->getCode() == '-41003') return app('json')->fail('获取会话密匙失败'); } if (!isset($userInfo['unionId'])) { $userInfo['unionId'] = ''; } // 新增或更新 $userInfo['openId'] = $json2sess['openid']; $userInfo['spid'] = $spread_spid; $userInfo['code'] = $spread_code; $userInfo['session_key'] = $json2sess['session_key']; $userInfo['login_type'] = $login_type; $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' => strtotime($token->expires_time), 'cache_key' => $cache_key, 'status' => 0, ]); } else { return app('json')->fail('获取用户访问token失败!'); } } /** * 小程序授权登录 * @param Request $request * @return mixed * @throws \Psr\SimpleCache\InvalidArgumentException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function mp_auth(Request $request) { $cache_key = ''; list($code, $post_cache_key, $login_type) = UtilService::postMore([ ['code', ''], ['cache_key', ''], ['login_type', ''] ], $request, true); // Log::debug("code=$code, post_cache_key=$post_cache_key, login_type=$login_type"); $session_key = Cache::get('eb_api_code_' . $post_cache_key); if (!$code && !$session_key) return app('json')->fail('授权失败,参数有误'); if ($code && !$session_key) { try { /** * 属性 类型 说明 openid string 用户唯一标识 session_key string 会话密钥 unionid string 用户在开放平台的唯一标识符,若当前小程序已绑定到微信开放平台帐号下会返回,详见 UnionID 机制说明。 errcode number 错误码 errmsg string 错误信息 */ $userInfoWx = MiniProgramService::getUserInfo($code); // Log::debug('userinfo=' . json_encode($userInfoWx)); $session_key = $userInfoWx['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']); // Log::debug('userinfo=' . json_encode($userInfo)); } catch (\Exception $e) { if ($e->getCode() == '-41003') return app('json')->fail('获取会话密匙失败'); } if (!isset($userInfoWx['openid'])) return app('json')->fail('openid获取失败'); if (!isset($userInfo['unionId'])) { $userInfo['unionId'] = ''; } $userInfo['openId'] = $userInfoWx['openid']; $userInfo['spid'] = $data['spread_spid']; $userInfo['code'] = $data['spread_code']; $userInfo['session_key'] = $session_key; $userInfo['login_type'] = $login_type; $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]); (new UserRds)->set($uid, UserRds::FIELD_LASTREFRESH, time()); return app('json')->successful('登陆成功!', [ 'token' => $token->token, 'userInfo' => $userInfo, 'expires_time' => strtotime($token->expires_time), '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') . $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()]); } /** * @api {get|post} /routine/notify 小程序支付回调 * @apiName RoutineNotify * @apiGroup Wechat * */ public function notify() { MiniProgramService::handleNotify(); } /** * 获取小程序订阅消息id * @return mixed */ public function teml_ids() { $temlIdsName = SubscribeTemplateService::getConstants(); $temlIdsList = CacheService::get('TEML_IDS_LIST', function () use ($temlIdsName) { $temlId = []; foreach ($temlIdsName as $key => $item) { $temlId[strtolower($key)] = SubscribeTemplateService::setTemplateId($item); } return $temlId; }); return app('json')->success($temlIdsList); } /** * 获取小程序直播列表 * @param Request $request * @return mixed */ 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) { $list = MiniProgramService::getLiveInfo($page, $limit); foreach ($list as &$item) { $item['_start_time'] = date('m-d H:i', $item['start_time']); } return $list; }, 600); return app('json')->success($list); } }