每次使用微信给的API都忍不住想吐槽下,巨坑,天坑,真的坑。每次都需要花费好多时间来自己整理。
这次因为项目需求,使用上了微信卡卷的功能,调试过程就不用了说了,都是泪。还是直接讲一下流程吧。
在正式开始之前,我推荐一下有需求的各位看官先去了解一下微信平台给的开发文档
顺着里边的debug接口流程进行调试一下,因为做完这个过我相信你能对卡卷的开发流程会有一定的了解。
一、获取微信服务号的accesstToken
对于微信开发来说,accessToken很重要,很多的API接口都需要到这个,在这里我写一下获取的方法。
首先要获取token就要拿公众号的appId和appSecret去换取,直接上代码吧
private static String ACCESS_TOKEN_URL =
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
public String getAccessToken() throws IOException {
try{
JSONObject resp = HttpUtil.doGetJson(
StringUtils.replaceEachRepeatedly(ACCESS_TOKEN_URL,
new String[]{"APPID", "APPSECRET"},
new String[]{"这里写入appid", "这里写appsecret"})
);
accessToken = resp.getString("access_token");
if(resp.getString("access_token")!=null) {
accessToken = resp.getString("access_token");
WXConstant.ACCESS_TOKEN = resp.getString("access_token");
// 定时使access_token和jsapi_ticket失效
new Timer().schedule(new TimerTask() {
@Override
public void run() {
accessToken = "";
WXConstant.ACCESS_TOKEN = "";
WXConstant.JSAPI_TICKET = "";
}
}, resp.getInt("expires_in") * 1000);
}else {
logger.error("Failed to get access_token! {}", JSON.toJSONString(resp));
}
}catch(Exception e){
}
return accessToken;
}
使用的工具类方法是这样的:
public static JSONObject doGetJson(String urlstr)
throws ServletException, IOException {
JSONObject resultObject=null;
URL url = new URL(urlstr); //创建url连接
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); //打开连接
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("GET");
urlConnection.setUseCaches(false);
urlConnection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8"));
StringBuffer buffer = new StringBuffer();//存储服务器返回的信息;<span style="font-family: Arial, Helvetica, sans-serif;"></span>
String line = "";
String openid = ""; //用来接收用户的appid
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String result = buffer.toString();
System.out.println(result);
try {
resultObject= new JSONObject(result); //将服务器返回的字符串转换成json格式
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resultObject;
}
这样你就能如愿的拿到token了。获取accessToken在百度上应该也有一大堆文章写过了。
二、上传卡卷的Logo
上传Logo就是将Logo作为永久素材上传到微信服务器,请求的url为”http://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=”
+ accessToken + “&type=image”
详细如下:
public String uploadLogo(String filePath){
try {
ApiRest api = new ApiRest();
String accessToken="";//使用上面的方式获取到token
// 上传文件请求路径
String action = "http://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token="
+ accessToken + "&type=image";
URL url = new URL(action);
String result = null;
File file = new File(filePath);
if (!file.exists() || !file.isFile()) {
throw new IOException("上传的文件不存在");
}
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST"); // 以Post方式提交表单,默认get方式
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false); // post方式不能使用缓存
// 设置请求头信息
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
// 设置边界
String BOUNDARY = "----------" + System.currentTimeMillis();
con.setRequestProperty("Content-Type", "multipart/form-data; boundary="
+ BOUNDARY);
// 请求正文信息
// 第一部分:
StringBuilder sb = new StringBuilder();
sb.append("--"); // 必须多两道线
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"file\";filename=\""
+ file.getName() + "\"\r\n");
sb.append("Content-Type:application/octet-stream\r\n\r\n");
byte[] head = sb.toString().getBytes("utf-8");
// 获得输出流
OutputStream out = new DataOutputStream(con.getOutputStream());
// 输出表头
out.write(head);
// 文件正文部分
// 把文件已流文件的方式 推入到url中
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
// 结尾部分
byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// 定义最后数据分隔线
out.write(foot);
out.flush();
out.close();
StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
try {
// 定义BufferedReader输入流来读取URL的响应
reader = new BufferedReader(new InputStreamReader(con
.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
if (result == null) {
result = buffer.toString();
}
} catch (IOException e) {
System.out.println("发送POST请求出现异常!" + e);
e.printStackTrace();
throw new IOException("数据读取异常");
} finally {
if (reader != null) {
reader.close();
}
}
return result;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
上传成功后你会得到微信服务器给你的返回值,也就是logo的url,像这样:
拿到url后,咱们就能进行卡卷的创建了。
三、创建卡卷
在创建卡卷前,咱们得组装好所需要的参数,这时就可以使用到微信平台提供的SDK了,sdk中内容也不多,也很好理解
但是!但是!但是!注意了,我发现sdk的MAIN文件中组装的baseInfo对象并非全面,缺少了两三个必填字段,不知之前使用sdk的前辈们是否有发现这个问题,但我是遇到了(无奈脸–)。参考下微信卡卷总结这篇文章后和卡卷文档后,在这里还是拿其中团购卷来介绍一下吧。
{
“card”: {
//(必填)卡卷类型
“card_type”: “GROUPON”,
//注意了,这里的字段必须与前面的card_type一致,否则致错。
“groupon”: {
//这个节点不管那种卷都需要
“base_info”: {
//(必填)这是前面上传logo获取到的url
“logo_url”:
“http://mmbiz.qpic.cn/mmbiz/iaL1LJM1mF9aRKPZJkmG8xXhiaHqkKSVMMWeN3hLut7X7hicFNjakmx ibMLGWpXrEXB33367o7zHN0CwngnQY7zb7g/0”,
//(必填)商户名字
“brand_name”:”微信餐厅”,
//(必填)Code展示类型,”CODE_TYPE_TEXT”,文本;”CODE_TYPE_BARCODE”,一维码 ;”CODE_TYPE_QRCODE”,二维码;”CODE_TYPE_ONLY_QRCODE”,二维码无code显示;”CODE_TYPE_ONLY_BARCODE”,一维码无code显示;CODE_TYPE_NONE,不显示code和条形码类型,须开发者传入”立即使用”自定义cell完成线上券核销。
“code_type”:”CODE_TYPE_TEXT”,
//(必填)卡券名
“title”: “132元双人火锅套餐”,
//(非必填)卷名
“sub_title”: “周末狂欢必备”,
//(必填)卡卷颜色
“color”: “Color010”,
//(必填)卡卷使用提醒
“notice”: “使用时向服务员出示此券”,
//(非必填)客服电话
“service_phone”: “020-88888888”,
//(必填)卡卷使用说明
“description”: “不可与其他优惠同享\n如需团购券发票,请在消费时向商户提出\n店内均可使用,仅限堂食”,
//(必填)使用日期,有效期信息。
“date_info”: {
//(必填)使用时间的类型,自领取后按天算,旧文档采用的1和2依然生效,DATE_TYPE_FIX_TIME_RANGE 表示固定日期区间
“type”: “DATE_TYPE_FIX_TERM”,
//(必填)type为DATE_TYPE_FIX_TERM时专用,表示自领取后多少天内有效,不支持填写0
“fixed_term”: 15 ,
//(必填)type为DATE_TYPE_FIX_TERM时专用,表示自领取后多少天开始生效,领取后当天生效填写0。(单位为天)
“fixed_begin_term”: 0
},
//(必填)商品信息
“sku”: {
//(必填)卡卷库存的数量
“quantity”: 500000
},
//(非必填)每人可领券的数量限制,不填写默认为50
“get_limit”: 3,
//是否指定卷码(自定义卷码),true:是,仅支持API创建,false:否,支持API创建、公众平台创建(默认为非自定义Code码)
“use_custom_code”: false,
//是否指定用户领取,填写true或false。默认为false。通常指定特殊用户群体投放卡券或防止刷券时选择指定用户领取
“bind_openid”: false,
//卡券领取页面是否可分享 true or false
“can_share”: true,
//卡券是否可转赠true or false
“can_give_friend”: true,
//(非必填)自定义跳转外链的入口名字
“custom_url_name”: “立即使用”,
//(非必填)自定义跳转的URL。
“custom_url”: “http://www.qq.com“,
//(非必填)显示在入口右侧的提示语
“custom_url_sub_title”: “6个汉字tips”,
//(非必填)营销场景的自定义入口名称
“promotion_url_name”: “更多优惠”,
//(非必填)入口跳转外链的地址链接
“promotion_url”: “http://www.qq.com”
},
//(必填)团购券专用,团购详情,什么类型也写相对应的detail
“deal_detail”: “以下锅底2选1(有菌王锅、麻辣锅、大骨锅、番茄锅、清补凉锅、酸 菜鱼锅可选):\n大锅1份 12元\n小锅2份 16元 “}
}
}
拿SDK给的MAIN方法来看把`
WxCardGroupon card = new WxCardGroupon();
WxCardBaseInfo baseInfo = card.getBaseInfo();
baseInfo.setLogoUrl("http://mmbiz.qpic.cn/mmbiz/iaL1LJM1mF9aRKPZJkmG8xXhiaHqkKSVMMWeN3hLut7X7hicFNjakmx");
Calendar calendar = Calendar.getInstance(); calendar.setTime(Calendar.getInstance().getTime());
calendar.add(Calendar.DAY_OF_MONTH, -1);
Date date = calendar.getTime();
baseInfo.setDateInfoTimeRange(Calendar.getInstance().getTime(), date);
baseInfo.setBrandName("brandname");
baseInfo.setBindOpenid(false);
baseInfo.setCanGiveFriend(false);
baseInfo.setCanShare(true);
baseInfo.setDateInfoFixTerm(2);
baseInfo.setCodeType(WxCardBaseInfo.CODE_TYPE_QRCODE);
baseInfo.setColor("Color010");
baseInfo.setTitle("ruikd");
baseInfo.setDescription("desc");
baseInfo.setGetLimit(3);
baseInfo.setUseCustomCode(false);
baseInfo.setNotice("notice");
baseInfo.setServicePhone("13539735015");
baseInfo.addLocationIdList(123123);
baseInfo.addLocationIdList(5345);
baseInfo.setUseLimit(5);
baseInfo.setQuantity(10000000);
System.out.println(baseInfo.toJsonString());
ArrayList<Integer> locationIdList = new ArrayList<Integer>();
locationIdList.add(809809);
locationIdList.add(423532);
card.setDealDetail("团购详情啊啊啊啊啊!!!");
System.out.println(locationIdList.getClass().isArray());
baseInfo.setLocationIdList(locationIdList);`
HttpRequestUtil.response(url, card.toJsonString());
选择自己需要的数据进行封装(必填的必选),然后将数据进行post请求,请求接口为:”https://api.weixin.qq.com/card/create?access_token=“+accessToken;
response方法如下:
public static boolean response(String url,String content) {
String line = "";
String message = "";
String returnData = "";
boolean postState = false;
BufferedReader bufferedReader = null;
try {
URL urlObject = new URL(url);
HttpURLConnection urlConn = (HttpURLConnection) urlObject.openConnection();
urlConn.setDoOutput(true);
/*设定禁用缓存*/
urlConn.setRequestProperty("Cache-Control", "no-cache");
/*维持长连接*/
urlConn.setRequestProperty("Connection", "Keep-Alive");
/*设置字符集*/
urlConn.setRequestProperty("Charset", "UTF-8");
/*设定输出格式为json*/
urlConn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
/*设置使用POST的方式发送*/
urlConn.setRequestMethod("POST");
/*设置不使用缓存*/
urlConn.setUseCaches(false);
/*设置容许输出*/
urlConn.setDoOutput(true);
/*设置容许输入*/
urlConn.setDoInput(true);
urlConn.connect();
OutputStreamWriter outStreamWriter = new OutputStreamWriter(urlConn.getOutputStream(),"UTF-8");
outStreamWriter.write(content);
outStreamWriter.flush();
outStreamWriter.close();
/*若post失败*/
if((urlConn.getResponseCode() != 200)){
returnData = "{\"jsonStrStatus\":0,\"processResults\":[]}";
message = "发送POST失败!"+ "code="+urlConn.getResponseCode() + "," + "失败消息:"+ urlConn.getResponseMessage();
// 定义BufferedReader输入流来读取URL的响应
InputStream errorStream = urlConn.getErrorStream();
if(errorStream != null)
{
InputStreamReader inputStreamReader = new InputStreamReader(errorStream,"utf-8");
bufferedReader = new BufferedReader(inputStreamReader);
while ((line = bufferedReader.readLine()) != null) {
message += line;
}
inputStreamReader.close();
}
errorStream.close();
System.out.println("发送失败!错误信息为:"+message);
}else{
/*发送成功返回发送成功状态*/
postState = true;
// 定义BufferedReader输入流来读取URL的响应
InputStream inputStream = urlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"utf-8");
bufferedReader = new BufferedReader(inputStreamReader);
while ((line = bufferedReader.readLine()) != null) {
message += line;
}
returnData = message;
inputStream.close();
inputStreamReader.close();
System.out.println("发送POST成功!返回内容为:" + returnData);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return postState;
}
}
这样,卡卷就创建好了,可以在微信公众平台上以“功能——卡卷功能——优惠卷”的最下部看到你新创建的优惠卷。
本文是在作者刚做完创建卡卷时的总结,如有不对的地方,望指出。