medicalrecord.html:1 XMLHttpRequest cannot load https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6a7b19b0fbd21040&redirect_uri=https%3A%2F%2F24736g26e1.zicp.vip%2Falfx%2Fwx%2Fweixin%2Faccess_token&response_type=code&scope=snsapi_base&state=2019#wechat_redirect. Response for preflight is invalid (redirect)
下面为代码,大神帮看看
package cn.wx;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.View;
import org.nutz.mvc.adaptor.VoidAdaptor;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.weixin.impl.WxApi2Impl;
import org.nutz.weixin.impl.WxLoginImpl;
import org.nutz.weixin.spi.WxApi2;
import org.nutz.weixin.spi.WxLogin;
import org.nutz.weixin.spi.WxResp;
import org.nutz.weixin.util.Wxs;
import cn.base.bean.Patient;
@At("/wx")
@IocBean(create = "init")
public class WeixinModule { // 并不要求你继承任何东西
/*
* wxHandler是被动请求的主要处理类, 里面写的1234567890就是"接口配置信息"里面提到的"token",
*/
@Inject
protected DefaultWxHandler wxHandler;
@Inject
protected Dao dao;
@Inject
protected WxApi2 wxApi2;
@Inject
protected PropertiesProxy conf;
public void init() {
WxApi2Impl wxApi2 = new WxApi2Impl();
wxApi2.configure(conf, "weixin."); // 从配置信息里面读取各种需要的参数
this.wxApi2 = wxApi2;
}
// protected WxHandler wxHandler = new BasicWxHandler("1234567890");
@At // 拼起来的全路径就是 /weixin/msgin
@Filters
@AdaptBy(type = VoidAdaptor.class)
public View msgin(HttpServletRequest req) throws IOException {
return Wxs.handle(wxHandler, req, "default");
}
/*
*
* @At
*
* @Filters public String getAccessToken() { return wxApi2.getAccessToken(); }
*
* @At
*
* @Filters public String getQrcodeUrl(@Param("doctorid") String doctorid) {
* WxResp wr = wxApi2.qrcode_create(doctorid, 0); return
* wxApi2.qrcode_show((String) wr.get("ticket")); }
*
* @At
*
* @Filters public NutMap getJsSDKConfig() { List<WxMenu> l = new ArrayList();
* WxMenu m = new WxMenu(); m.setName("首页"); m.setType("view");
* m.setUrl("https://24736g26e1.zicp.vip/alfx/wx/index.html"); l.add(m);
* wxApi2.menu_create(l); return
* wxApi2.genJsSDKConfig("https://24736g26e1.zicp.vip/alfx/wx/index.html",
* "scanQRCode", "getLocation"); }
*/
@At("/authorize")
@Ok(">>:${obj}")
@Filters
public String authorize(HttpServletRequest req) {
String redirect_uri = req.getRequestURL().toString().replace("authorize", "weixin/access_token");
redirect_uri = redirect_uri.replace("http", "https");
return wxLogin("weixin").authorize(redirect_uri, "snsapi_base", "2019"); // snsapi_base静默登录,snsapi_userinfo需要确认,后者才能获取用户详细信息
}
@At("/?/access_token")
// @Ok(">>:/wx/html/medicalrecord.html?msg=${obj}")
@Filters
public String access_token(String prefix, @Param("code") String code, @Param("state") String state,
HttpSession session) {
WxResp resp = wxLogin(prefix).access_token(code);
if (resp == null) {
return "登录失败";
}
String openid = resp.getString("openid");
Patient user = dao.fetch(Patient.class, Cnd.where("openid", "=", openid));
if (user == null) {
// 新用户 xxooo
}
// 执行登录操作 (普通方式)
session.setAttribute("me", user);
// 最后呢, 如果不是公众号的静默登录snsapi_base,还可以获取用户详细信息
String access_token = resp.getString("access_token");
resp = wxLogin(prefix).userinfo(openid, access_token);
// 然后做你想做的事吧...
return "ok";
}
protected WxLogin wxLogin(String prefix) {
WxLoginImpl w = new WxLoginImpl();
return w.configure(conf, prefix + ".");
}
}