实现方案我们可以通过如下的方法实现小程序太阳码生成。
生成有限制太阳码实现步骤获取小程序的access_token
设置path、with相关参数
调用getwxacodeunlimit接口,并将返回图片存储到本地
获取小程序的access_tokenpublic static string getaccesstoken(string appid, string appsecret)    {        string requesturl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appsecret+"";        string accesstoken = null;        try        {            string response = httpclientutil.getinstance().sendhttpsget(                    requesturl, null);            jsonobject json = jsonobject.parseobject(response);            accesstoken = string.valueof(json.get("access_token"));        }        catch (exception e)        {            logger.error("getaccesstoken error", e);        }        return accesstoken;    }
说明:调用微信api接口传入小程序的appid和appsecret参数即可。
调用微信api生成小程序太阳码 public static string generatlimitsuncode(wxscancodeparam param) throws exception     {       string token =param.getaccesstoken();       map<string, string> params = new hashmap<>();       params.put("path", param.getpath());       params.put("width", "430");       closeablehttpclient httpclient = httpclientbuilder.create().build();       httppost httppost = new httppost("https://api.weixin.qq.com/wxa/getwxacode?access_token="+token);       httppost.addheader(http.content_type, "application/json");       string body = json.tojsonstring(params);       stringentity entity = new stringentity(body);       entity.setcontenttype("image/jpg");       httppost.setentity(entity);       httpresponse response = httpclient.execute(httppost);       int statuscode = response.getstatusline().getstatuscode();       if (statuscode == httpstatus.sc_ok)        {           httpentity entity2 = response.getentity();           if(!entity2.getcontenttype().getvalue().equals("image/jpeg"))           {               string result = entityutils.tostring(entity2, "utf-8");               logger.error("generate sun code error,http execute result:" + result);               return null;           }       }       else       {           logger.error("generate sun code error,http execute result:" + statuscode);       }              inputstream inputstream = response.getentity().getcontent();        // 保存图片到本地            int flag = saveimg(inputstream, param.getfilepath(), name);       if (flag == 0)       {           throw new sysexception("保存图片[" + name + "]失败");       }       else       {           logger.info("太阳码[{}]生成成功", name);       }       return param.getfilepath() + file.separatorchar + name;   }
说明参数说明path:扫码进入的小程序页面路径,最大长度 128 字节,不能为空;例如:pages/index/index
access_token:小程序授权token
注意事项需要特殊注意,本方案生成的小程序太阳码与二维码的总数不能超过10万个,微信没有提供对应的api接口查询的使用的数量,一旦超过了数量,将会导致小程序失效,且微信目前无法重置查询次数,适合于生成数量少的场景。
生成无限制太阳码获取小程序的access_token如同第一种的方案
调用微信api生成小程序太阳码/**     * 生成无限制的小程序码     * @param param     * @return     * @throws exception     */    public static string generatunlimitsuncode(wxscancodeparam param) throws exception     {       string token =param.getaccesstoken();       map<string, string> params = new hashmap<>();       params.put("scene", param.getscene());       params.put("page", param.getpath());       params.put("width", "430");       closeablehttpclient httpclient = httpclientbuilder.create().build();       httppost httppost = new httppost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+token);       httppost.addheader(http.content_type, "application/json");       string body = json.tojsonstring(params);       stringentity entity = new stringentity(body);       entity.setcontenttype("image/jpg");       httppost.setentity(entity);       httpresponse response = httpclient.execute(httppost);       int statuscode = response.getstatusline().getstatuscode();       if (statuscode == httpstatus.sc_ok)        {           httpentity entity2 = response.getentity();           if(!entity2.getcontenttype().getvalue().equals("image/jpeg"))           {               string result = entityutils.tostring(entity2, "utf-8");               logger.error("generate sun code error,http execute result:" + result);               return null;           }       }       else       {           logger.error("generate sun code error,http execute result:" + statuscode);       }              inputstream inputstream = response.getentity().getcontent();              //太阳码写标题       string content=param.getwritecontent();       if(stringutil.isnotempty(content) && param.iswrite())       {          inputstream = imageutil.addimagetitle(param.getwritecontent(), inputstream, 450, 450);       }             string name = param.getfilename()+".jpg";//文件名加后缀,跟上面对应              int flag = saveimg(inputstream, param.getfilepath(), name);// 保存图片       if (flag == 0)       {           throw new sysexception("保存图片[" + name + "]失败");       }       else       {           logger.info("太阳码[{}]生成成功", name);       }       return param.getfilepath() + file.separatorchar + name;   }
说明参数说明scene:最大32个可见字符,参数格式可以自行定义a&b或者a=1&b=2都行
access_token:小程序授权token
参数过长问题由于scene参数的长度只支持32位字符,如果参数超过了32位,我们将如何合处理?
解决方案改问题的解决方案为:设计一张小程序参数表,将生成的参数存储到表中,生成小程序是scene参数设置此表表的主键,小程序扫码后,先请求后台通过scene参数获取小程序的具体参数。
如下示例:
以上就是java中如何生成微信小程序太阳码的详细内容。
   
 
   