success(json_decode(WechatService::jsSdk($request->get('url')), true)); } /** * 公众号授权登陆 * @param Request $request * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function auth(Request $request) { $spreadId = intval($request->param('spread')); $login_type = $request->param('login_type', 1); @file_put_contents("auth.txt",json_encode(input())); try { $wechatInfo = WechatService::oauthService($this->cid)->user()->getOriginal(); } catch (\Exception $e) { $this->error(['message' => $e->getMessage(), 'line' => $e->getLine()]); } @file_put_contents("auth.txt","\r\n".json_encode($wechatInfo),8); try { if (!isset($wechatInfo['nickname'])) { try { $wechatInfo = WechatService::getUserInfo($this->cid, $wechatInfo['openid']); } catch (\Exception $e) { $this->error(['message' => $e->getMessage(), 'line' => $e->getLine()]); } if (!$wechatInfo['subscribe'] && !isset($wechatInfo['nickname'])) exit(WechatService::oauthService($this->cid)->scopes(['snsapi_userinfo']) ->redirect($this->request->url(true))->send()); if (isset($wechatInfo['tagid_list'])) $wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']); } else { if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']); if (!UserRelation::where(['openid' => $wechatInfo['openid']])->find()) $wechatInfo['subscribe'] = 0; } $openid = $wechatInfo['openid']; $wechatInfo['cid'] = $this->cid; $params = [$openid, $wechatInfo, $spreadId, $login_type]; @file_put_contents("auth.txt", "\r\n" . json_encode($params), 8); Hook::exec("\\app\admin\\behavior\\User", "WechatOauth", $params); $user = User::where('id', UserRelation::openidToUid($openid, 'openid'))->find(); if (!$user) $this->error('获取用户失败'); $this->auth->direct($user['id']); // 设置推广关系 User::setSpread(intval($spreadId), $user->id); return $this->success('登录成功', $this->auth->getUserinfo()); }catch (Exception $e) { @file_put_contents("error.txt",$e->getFile().'-',$e->getLine(),'-'.$e->getMessage()); } } }