JAVA-微信小程序-登录,小程序码,小程序二维码

1.前端传微信code获取openid

		//接口
 	    public static final String CODE2SESSION = "https://api.weixin.qq.com/sns/jscode2session";

  		//获取openid
        if (StringUtils.isNotEmpty(apUserBean.getWxCode())) {

            StringBuffer param = new StringBuffer();
            param.append("appid=").append(WechatParam.APPID).append("&secret=")
                    .append(WechatParam.APPSECRET).append("&js_code=")
                    .append(apUserBean.getWxCode()).append("&grant_type=authorization_code");

            String res = HttpUtil.sendGet(WechatParam.CODE2SESSION, param.toString());
            JSONObject jsonObject = JSONObject.fromObject(res);

            //获取openid
            apUserBean.setOpenid(jsonObject.getString("openid"));
        }

2.获取手机号

		//接口
		public static final String GETPHONE = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

	    //获取手机号
        if (StringUtils.isNotEmpty(apUserBean.getPhoneCode())) {
            StringBuffer param = new StringBuffer();
            param.append("appid=").append(WechatParam.APPID).append("&secret=")
                    .append(WechatParam.APPSECRET).append("&grant_type=client_credential");
                    
			//获取AccessToken
            String res = HttpUtil.sendGet(WechatParam.GETACCESSTOKEN, param.toString());
            JSONObject jsonObject = JSONObject.fromObject(res);
            String token = jsonObject.getString("access_token");

            String jsonCode = "{\"code\":\"" + apUserBean.getPhoneCode() + "\"}";

			//获取手机号
            String resPhone = com.kuyuntech.appraisal.appraisalplatform.util.core.HttpUtil.sendPostRequestStr(WechatParam.GETPHONE + token, jsonCode);
            JSONObject jsonObject1 = JSONObject.fromObject(resPhone);
            int errcode = jsonObject1.getInt("errcode");
            if (errcode != 0) {
                return ResponseBean.serverError("获取手机号失败,请重试!");
            }
            JSONObject phoneInfo = jsonObject1.getJSONObject("phone_info");
            String phone = phoneInfo.getString("phoneNumber");

            apUserBean.setPhone(phone);
        }

3.获取无限制的小程序码

	//接口
    public static final String GETUNLIMITEDQRCODE = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=";

    @RequestMapping
    public Object getUnlimitedQrcode(HttpServletRequest request, HttpServletResponse response) {
        ApUserBean loginUserBean = (ApUserBean) request.getSession().getAttribute(LoginSession.USER_LOGIN_INFO);

        if (null == loginUserBean) {
            response.setStatus(401);
            return ResponseBean.unAuthorize("请先登录");
        }

        StringBuffer param = new StringBuffer();
        param.append("appid=").append(WechatParam.APPID).append("&secret=")
                .append(WechatParam.APPSECRET).append("&grant_type=client_credential");

        String res = HttpUtil.sendGet(WechatParam.GETACCESSTOKEN, param.toString());
        JSONObject jsonObject = JSONObject.fromObject(res);
        String token = jsonObject.getString("access_token");

        com.alibaba.fastjson.JSONObject paramJson = new com.alibaba.fastjson.JSONObject();
        paramJson.put("page", "pages/index/index");
        paramJson.put("scene", "inviteCode=" + loginUserBean.getInvitationCode()); //携带的参数
        paramJson.put("check_path", "true");
        paramJson.put("env_version", "release"); //正式版,其他版本测试时获取不到参数

        logger.info(paramJson.toString());
        byte[] bytes = com.kuyuntech.appraisal.appraisalplatform.util.core.HttpUtil.sendPostRequest(WechatParam.GETUNLIMITEDQRCODE + token, paramJson.toString());
        return bytes;
    }

4.生成小程序二维码携带参数

	   //接口
 	   public static final String CREATEQRCODE = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=";

        //生成二维码
        StringBuffer param = new StringBuffer();
        param.append("appid=").append(WechatParam.APPID).append("&secret=")
                .append(WechatParam.APPSECRET).append("&grant_type=client_credential");

        String res = HttpUtil.sendGet(WechatParam.GETACCESSTOKEN, param.toString());
        JSONObject jsonObject = JSONObject.fromObject(res);
        String token = jsonObject.getString("access_token");

        String jsonCode = "{\"path\":\"pages/antique-detial/antique-detial?code=" + appraisal.getCode() + "\"}";

        byte[] bytes = HttpUtil.sendPostByte(WechatParam.CREATEQRCODE + token, jsonCode);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值