问题描述
经常碰到有需要链接获取用户openid的直接给代码 照例jfinal 1.开发者中心-->网页授权获取用户基本信息-->授权回调页面域名2.调用链接转换3. 获取code 用code 换取openid1. 获取授权签名链接function seturl(url){ var newUrl = encodeURIComponent(url); var reNewUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=${(AppId)!}&redirect_uri=' + newUrl + '&response_type=code&scope=snsapi_base&state=123#wechat_redirect';}2. code换取openid 照例jfinalpublic String getopendid(String code) { String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=AppId&secret=AppSecret&code=CODE&grant_type=authorization_code"; url = url.replace("AppId", AppId).replace("AppSecret", AppSecret) .replace("CODE", code); String jsonResult = HttpKit.get(url); JSONObject jsonTexts = (JSONObject) JSON.parse(jsonResult); String openid = ""; if (StringKit.notNull(jsonTexts.get("openid"))) { openid = jsonTexts.get("openid").toString(); } return openid;}