说实话呢,这次微信会员卡的开发文档是真的乱,四分五裂,我也是东凑西拿的才完成会员卡的开发,把开发经验和踩坑给大家说一下,希望对大家有所帮助
开发出来是这个效果如下图:
JAVA微信会员卡接口踩坑详细
直接上我写的接口吧;
/createCard 会员卡开卡接口,创建模板也是在这个接口;/getopenid 获取微信用户传过来的code,通过code获取用户的唯一标识符openid;/wxgetcode 根据这个接口传来的值去匹配数据库数据返回给微信小程序数据;/getparam 这个接口是获取encrypt_card_id, outer_str, biz 三个参数(没有用到换方法了,用的是 wx.add方法,没有用navigateToMiniProgram这个,快废弃掉了);/signature 获取签名(根据时间戳timestamp,随机串nonce_str,模板card_id,生成签名要和微信文档签名一致);
签名校验地址:/tools/weixinmpcardsign//delcard1 删除会员卡(根据创建的模板card_id删除);/selectcardid 查询会员卡是否已领取过进行判断防止用户重复领卡;/sendcardid 用户领取完会员卡保存模板card_id,为查询会员卡做提供;updatedh 修改用户手机号(根据自己的业务可有可无);
这些接口差不多就可以完成会员卡开发流程,从开卡——到发卡——到卡包中
下面就上业务代码吧
controller
package com.example.demo;import com.alibaba.fastjson.JSONObject;import com.example.demo.entities.CheckUtil;import com.example.demo.entities.NDShenPi;import com.example.demo.entities.wxuser;import com.example.demo.service.sfrz;import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.ParseException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicHeader;import org.apache.http.protocol.HTTP;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.ResponseBody;import java.io.*;import .HttpURLConnection;import .MalformedURLException;import .URL;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.time.LocalDateTime;import java.util.*;import static com.example.demo.shenfenrz.decode;/*** @author : 宋银义* @date : -06-11 17:27**/@Controllerpublic class wxcard {@Autowiredprivate sfrz sfrz;/**公众号APPID*/public static final String APPID = "xxx";/**小程序APPID*/public static final String APPID1 = "wx9472bxxx";/**公众号AppSecret*/public static final String AppSecret = "d9977bee1dxxx";/**小程序AppSecret*/public static final String AppSecret1 = "4d62c2ce155a929adc70xxx";@GetMapping("/updatedh")@ResponseBodypublic HashMap<String, String> updatedh(String sfzh ,String xm,String shouji){sfzh = new String(decode(sfzh));shouji = new String(decode(shouji));System.err.println("shouji:=="+shouji);HashMap<String, String> map = new HashMap<>();int i = sfrz.updatedh(sfzh,xm,shouji);System.err.println("I:=="+i);if (i>0){map.put("success","true");map.put("msg","修改成功");}else {map.put("success","false");map.put("msg","修改失败");}return map;}@GetMapping("/sendcardid")@ResponseBodypublic HashMap<String, String> cardout(String openid,String sfzh ,String xm,String cardid){sfzh = new String(decode(sfzh));openid = new String(decode(openid));cardid = new String(decode(cardid));int i=sfrz.savecardid(openid,sfzh,xm,cardid);HashMap<String, String> map = new HashMap<>();if (i>0){map.put("success","true");map.put("msg","插入成功");}else {map.put("msg","插入失败");}return map;}@GetMapping("/selectcardid")@ResponseBodypublic HashMap<String, String> selectcardid(String openid,String sfzh ,String xm){sfzh = new String(decode(sfzh));openid = new String(decode(openid));List<wxuser> list =sfrz.selectcardid(openid,sfzh,xm);System.err.println("cardid"+list.get(0).getOpencardid());HashMap<String, String> map = new HashMap<>();if (list.size()!=0){String opencardid = list.get(0).getOpencardid();if (opencardid!=null &&opencardid!=""){map.put("success","true");map.put("msg","卡已领取");}else {map.put("success","false");map.put("msg","可以领卡");}}else {map.put("success","false");map.put("msg","可以领卡");}return map;}@GetMapping("/delcard1")@ResponseBodypublic String delcard(String card_id){String accessToken = getAccessToken();String body="{\n" +" \"card_id\": \""+card_id+"\"\n" +"}";JSONObject jsonObject = JSONObject.parseObject(body);String url ="https://api./card/delete?access_token="+accessToken;String post = post(jsonObject, url);return post;}@GetMapping("/getopenid")@ResponseBodypublic HashMap<String, String> getopenid(String code){code = new String(decode(code));String openid = getOpenid(code);JSONObject jsonObject = JSONObject.parseObject(openid);String openid1 = jsonObject.getString("openid");System.err.println("openid1:=="+openid1);int i = sfrz.getapp_id1(openid1);System.err.println("i:=="+i);HashMap<String, String> map = new HashMap<>();if (i==0){map.put("success","2");map.put("openid",openid1);}else if(i>0){map.put("success","1");List<wxuser> list = sfrz.selectopenid(openid1);if (list.size()!=0){System.err.println("getName"+list.get(0).getName());NDShenPi ndShenPi = sfrz.getselect(list.get(0).getSfzh(),list.get(0).getName());if (ndShenPi != null) {map.put("id", String.valueOf(ndShenPi.getId()));map.put("BillNo", ndShenPi.getBillNo());map.put("OID", String.valueOf(ndShenPi.getOID()));map.put("ShiXiangMingCheng", ndShenPi.getShiXiangMingCheng());map.put("ShiXiangLeiXing", ndShenPi.getShiXiangLeiXing());map.put("ShouLiShiXian", ndShenPi.getShouLiShiXian());map.put("ShiXiangBianMa", ndShenPi.getShiXiangBianMa());map.put("ShouLiBuMen", ndShenPi.getShouLiBuMen());map.put("ShouLiChuangKou", ndShenPi.getShouLiChuangKou());map.put("ShouLiShiJian", ndShenPi.getShouLiShiJian());map.put("ShenQingRen", ndShenPi.getShenQingRen());map.put("ShenQingRenEN", ndShenPi.getShenQingRenEN());map.put("ZhengJianType", ndShenPi.getZhengJianType());map.put("ZhengJianHao", ndShenPi.getZhengJianHao());map.put("Img", ndShenPi.getImg());map.put("ShengRi", ndShenPi.getShengRi());map.put("XingBie", ndShenPi.getXingBie());map.put("MinZu", ndShenPi.getMinZu());map.put("GuoJia", ndShenPi.getGuoJia());map.put("HuKouSuoZaiDi", ndShenPi.getHuKouSuoZaiDi());map.put("XueLi", ndShenPi.getXueLi());map.put("XueLiQuanRiZhi", ndShenPi.getXueLiQuanRiZhi());map.put("QuanRiZhiZhuanYe", ndShenPi.getQuanRiZhiZhuanYe());map.put("HaiWai", ndShenPi.getHaiWai());map.put("XueLiZaiZhi", ndShenPi.getXueLiZaiZhi());map.put("ZaiZhiZhuanYe", ndShenPi.getZaiZhiZhuanYe());map.put("RenCaiLeiBie", ndShenPi.getRenCaiLeiBie());map.put("RenCaiChengHao", ndShenPi.getRenCaiChengHao());map.put("BiYeZhengShuBH", ndShenPi.getBiYeZhengShuBH());map.put("YJTime", ndShenPi.getYJTime());map.put("YouXiang", ndShenPi.getYouXiang());map.put("DianHua", ndShenPi.getDianHua());map.put("ShouJi", ndShenPi.getShouJi());map.put("YuanDanWei", ndShenPi.getYuanDanWei());map.put("YuanZhiCheng", ndShenPi.getYuanZhiCheng());map.put("YinJinLeiXing", ndShenPi.getYinJinLeiXing());map.put("QianYueQiXian", ndShenPi.getQianYueQiXian());map.put("ZhiCheng", ndShenPi.getZhiCheng());map.put("DanWei", ndShenPi.getDanWei());map.put("DiZhiQu", ndShenPi.getDiZhiQu());map.put("DiZhi", ndShenPi.getDiZhi());map.put("HangYe", ndShenPi.getHangYe());map.put("XingZhi", ndShenPi.getXingZhi());map.put("ShengZhi", ndShenPi.getShengZhi());map.put("FaRen", ndShenPi.getFaRen());map.put("LianXiRen", ndShenPi.getLianXiRen());map.put("LianXiDianHua", ndShenPi.getLianXiDianHua());map.put("GZShiJian", ndShenPi.getGZShiJian());map.put("GZGongZuo", ndShenPi.getGZGongZuo());map.put("GZZhiWu", ndShenPi.getGZZhiWu());map.put("JLMingCheng", ndShenPi.getJLMingCheng());map.put("JLShiJian", ndShenPi.getJLShiJian());map.put("JLBuMen", ndShenPi.getJLBuMen());map.put("QXingMing", ndShenPi.getQXingMing());map.put("QZhengHao", ndShenPi.getQZhengHao());map.put("QXingBie", ndShenPi.getQXingBie());map.put("QGuanXi", ndShenPi.getQGuanXi());map.put("QMingZu", ndShenPi.getQMingZu());map.put("QShengRi", ndShenPi.getQShengRi());map.put("QZhiWu", ndShenPi.getQZhiWu());map.put("QHuKouDiZhi", ndShenPi.getQHuKouDiZhi());map.put("QHuKou", ndShenPi.getQHuKou());map.put("YuanHuKou", ndShenPi.getYuanHuKou());map.put("YuanHuKouDiZhi", ndShenPi.getYuanHuKouDiZhi());map.put("HuKou", ndShenPi.getHuKou());map.put("HuKouDiZhi", ndShenPi.getHuKouDiZhi());map.put("GuestNo", ndShenPi.getGuestNo());map.put("GuestName", ndShenPi.getGuestName());map.put("BeiZhu", ndShenPi.getBeiZhu());map.put("ShouLiTiaoJian", ndShenPi.getShouLiTiaoJian());map.put("ShenQingShiJian", ndShenPi.getShenQingShiJian());map.put("Type", ndShenPi.getType());map.put("FromTo", ndShenPi.getFromTo());map.put("YuShenTxt", ndShenPi.getYuShenTxt());map.put("YuShenTime", ndShenPi.getYuShenTime());map.put("YuShenRen", ndShenPi.getYuShenRen());map.put("YuShenAccount", ndShenPi.getYuShenAccount());map.put("PJQStar", ndShenPi.getPJQStar());map.put("SmsNote", ndShenPi.getSmsNote());map.put("PJQMsg", ndShenPi.getPJQMsg());map.put("CardNo", ndShenPi.getCardNo());SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");if (ndShenPi.getCardDate() == null) {map.put("CardDate", null);} else {map.put("CardDate", sdf.format(ndShenPi.getCardDate()));}if (ndShenPi.getCardDateDQ() == null) {map.put("CardDateDQ", null);} else {map.put("CardDateDQ", sdf.format(ndShenPi.getCardDateDQ()));}if (ndShenPi.getCardFiveDQTime() == null) {map.put("CardFiveDQTime", null);} else {map.put("CardFiveDQTime", sdf.format(ndShenPi.getCardFiveDQTime()));}map.put("CardState", String.valueOf(ndShenPi.getCardState()));map.put("CardType", String.valueOf(ndShenPi.getCardType()));if (ndShenPi.getCardZKDate() == null) {map.put("CardZKDate", null);} else {map.put("CardZKDate", sdf.format(ndShenPi.getCardZKDate()));}map.put("CardSerial", ndShenPi.getCardSerial());map.put("RenCaiFlg", String.valueOf(ndShenPi.getRenCaiFlg()));map.put("RenCaiFlgContent", ndShenPi.getRenCaiFlgContent());map.put("CardTypeS", ndShenPi.getCardTypeS());if (ndShenPi.getRenCaiZXTime() == null) {map.put("RenCaiZXTime", null);} else {map.put("RenCaiZXTime", sdf.format(ndShenPi.getRenCaiZXTime()));}if (ndShenPi.getFuShenTime() == null) {map.put("FuShenTime", null);} else {map.put("FuShenTime", sdf.format(ndShenPi.getFuShenTime()));}if (ndShenPi.getAddTime() == null) {map.put("AddTime", null);} else {map.put("AddTime", sdf.format(ndShenPi.getAddTime()));}map.put("cardcode", ndShenPi.getCardcode());map.put("zzmm", ndShenPi.getZzmm());map.put("FuShen", ndShenPi.getFuShen());map.put("openid",openid1);}else {map.put("success","3");map.put("openid",openid1);}}else {map.put("msg","该用户不存在");}}// System.err.println("map:=="+map);return map;};/*** encrypt_card_id, outer_str, biz 三个参数*/@GetMapping("/getparam")@ResponseBodypublic JSONObject quzhi() throws IOException {HashMap<String, String> map = new HashMap<>();JSONObject jsonObject = new JSONObject();String accessToken = getAccessToken();System.err.println("getparam:"+accessToken);long time = new Date().getTime();String url="https://api./card/membercard/activate/geturl?access_token="+accessToken;String body="{ \"card_id\" : \"pzmGcjmMhHzSxHjGqNgWO0pr_kh0" +"\", \"outer_str\" : \""+time+"\" }";JSONObject jsonObject2 = JSONObject.parseObject(body);String post = post(jsonObject2, url);System.err.println("send"+post);JSONObject jsonObject1 = JSONObject.parseObject(post);String errcode = jsonObject1.getString("errcode");if (errcode.equals("0")){String url1 = jsonObject1.getString("url");System.err.println("url:=="+url1);String returnstr = url1.substring(url1.indexOf("encrypt_card_id=") + "encrypt_card_id".length(), url1.indexOf("#"));System.err.println("returnstr:=="+returnstr);String[] strparam = returnstr.split("&");String encryptCardId = "";String outerStr = "";String biz = "";if (strparam.length > 2){//带有场景值encryptCardId=strparam[0];outerStr=strparam[1].split("=")[1];biz=strparam[2].split("=")[1];}else {//没有场景值得情况encryptCardId=strparam[0];biz=strparam[1].split("=")[1];}jsonObject.put("encryptCardId", encryptCardId);jsonObject.put("outerStr",outerStr);jsonObject.put("biz",biz);jsonObject.put("success","success");// map.put("send",post);}return jsonObject;}/*** 获取openid* @param code* @param sfzh* @param xmF*/@GetMapping("/wxgetcode")@ResponseBodypublic HashMap<String, String> openid(String openid,String sfzh, String xm){System.err.println("openid:=="+openid);sfzh = new String(decode(sfzh));openid = new String(decode(openid));String flag = "3";HashMap<String, String> map = new HashMap<>();int wxsave=0;List<wxuser> list1 = sfrz.selectopenid(openid);List<wxuser> list = sfrz.selectopenid1(sfzh);if (list1.size()!=0){if (list.get(0).getOpenid().equals(openid)&&list.get(0).getSfzh().equals(sfzh)){// map.put("success","2");NDShenPi ndShenPi = sfrz.getselect(sfzh, xm);if (ndShenPi != null) {map.put("id", String.valueOf(ndShenPi.getId()));map.put("BillNo", ndShenPi.getBillNo());map.put("OID", String.valueOf(ndShenPi.getOID()));map.put("ShiXiangMingCheng", ndShenPi.getShiXiangMingCheng());map.put("ShiXiangLeiXing", ndShenPi.getShiXiangLeiXing());map.put("ShouLiShiXian", ndShenPi.getShouLiShiXian());map.put("ShiXiangBianMa", ndShenPi.getShiXiangBianMa());map.put("ShouLiBuMen", ndShenPi.getShouLiBuMen());map.put("ShouLiChuangKou", ndShenPi.getShouLiChuangKou());map.put("ShouLiShiJian", ndShenPi.getShouLiShiJian());map.put("ShenQingRen", ndShenPi.getShenQingRen());map.put("ShenQingRenEN", ndShenPi.getShenQingRenEN());map.put("ZhengJianType", ndShenPi.getZhengJianType());map.put("ZhengJianHao", ndShenPi.getZhengJianHao());map.put("Img", ndShenPi.getImg());map.put("ShengRi", ndShenPi.getShengRi());map.put("XingBie", ndShenPi.getXingBie());map.put("MinZu", ndShenPi.getMinZu());map.put("GuoJia", ndShenPi.getGuoJia());map.put("HuKouSuoZaiDi", ndShenPi.getHuKouSuoZaiDi());map.put("XueLi", ndShenPi.getXueLi());map.put("XueLiQuanRiZhi", ndShenPi.getXueLiQuanRiZhi());map.put("QuanRiZhiZhuanYe", ndShenPi.getQuanRiZhiZhuanYe());map.put("HaiWai", ndShenPi.getHaiWai());map.put("XueLiZaiZhi", ndShenPi.getXueLiZaiZhi());map.put("ZaiZhiZhuanYe", ndShenPi.getZaiZhiZhuanYe());map.put("RenCaiLeiBie", ndShenPi.getRenCaiLeiBie());map.put("RenCaiChengHao", ndShenPi.getRenCaiChengHao());map.put("BiYeZhengShuBH", ndShenPi.getBiYeZhengShuBH());map.put("YJTime", ndShenPi.getYJTime());map.put("YouXiang", ndShenPi.getYouXiang());map.put("DianHua", ndShenPi.getDianHua());map.put("ShouJi", ndShenPi.getShouJi());map.put("YuanDanWei", ndShenPi.getYuanDanWei());map.put("YuanZhiCheng", ndShenPi.getYuanZhiCheng());map.put("YinJinLeiXing", ndShenPi.getYinJinLeiXing());map.put("QianYueQiXian", ndShenPi.getQianYueQiXian());map.put("ZhiCheng", ndShenPi.getZhiCheng());map.put("DanWei", ndShenPi.getDanWei());map.put("DiZhiQu", ndShenPi.getDiZhiQu());map.put("DiZhi", ndShenPi.getDiZhi());map.put("HangYe", ndShenPi.getHangYe());map.put("XingZhi", ndShenPi.getXingZhi());map.put("ShengZhi", ndShenPi.getShengZhi());map.put("FaRen", ndShenPi.getFaRen());map.put("LianXiRen", ndShenPi.getLianXiRen());map.put("LianXiDianHua", ndShenPi.getLianXiDianHua());map.put("GZShiJian", ndShenPi.getGZShiJian());map.put("GZGongZuo", ndShenPi.getGZGongZuo());map.put("GZZhiWu", ndShenPi.getGZZhiWu());map.put("JLMingCheng", ndShenPi.getJLMingCheng());map.put("JLShiJian", ndShenPi.getJLShiJian());map.put("JLBuMen", ndShenPi.getJLBuMen());map.put("QXingMing", ndShenPi.getQXingMing());map.put("QZhengHao", ndShenPi.getQZhengHao());map.put("QXingBie", ndShenPi.getQXingBie());map.put("QGuanXi", ndShenPi.getQGuanXi());map.put("QMingZu", ndShenPi.getQMingZu());map.put("QShengRi", ndShenPi.getQShengRi());map.put("QZhiWu", ndShenPi.getQZhiWu());map.put("QHuKouDiZhi", ndShenPi.getQHuKouDiZhi());map.put("QHuKou", ndShenPi.getQHuKou());map.put("YuanHuKou", ndShenPi.getYuanHuKou());map.put("YuanHuKouDiZhi", ndShenPi.getYuanHuKouDiZhi());map.put("HuKou", ndShenPi.getHuKou());map.put("HuKouDiZhi", ndShenPi.getHuKouDiZhi());map.put("GuestNo", ndShenPi.getGuestNo());map.put("GuestName", ndShenPi.getGuestName());map.put("BeiZhu", ndShenPi.getBeiZhu());map.put("ShouLiTiaoJian", ndShenPi.getShouLiTiaoJian());map.put("ShenQingShiJian", ndShenPi.getShenQingShiJian());map.put("Type", ndShenPi.getType());map.put("FromTo", ndShenPi.getFromTo());map.put("YuShenTxt", ndShenPi.getYuShenTxt());map.put("YuShenTime", ndShenPi.getYuShenTime());map.put("YuShenRen", ndShenPi.getYuShenRen());map.put("YuShenAccount", ndShenPi.getYuShenAccount());map.put("PJQStar", ndShenPi.getPJQStar());map.put("SmsNote", ndShenPi.getSmsNote());map.put("PJQMsg", ndShenPi.getPJQMsg());map.put("CardNo", ndShenPi.getCardNo());SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");if (ndShenPi.getCardDate() == null) {map.put("CardDate", null);} else {map.put("CardDate", sdf.format(ndShenPi.getCardDate()));}if (ndShenPi.getCardDateDQ() == null) {map.put("CardDateDQ", null);} else {map.put("CardDateDQ", sdf.format(ndShenPi.getCardDateDQ()));}if (ndShenPi.getCardFiveDQTime() == null) {map.put("CardFiveDQTime", null);} else {map.put("CardFiveDQTime", sdf.format(ndShenPi.getCardFiveDQTime()));}map.put("CardState", String.valueOf(ndShenPi.getCardState()));map.put("CardType", String.valueOf(ndShenPi.getCardType()));if (ndShenPi.getCardZKDate() == null) {map.put("CardZKDate", null);} else {map.put("CardZKDate", sdf.format(ndShenPi.getCardZKDate()));}map.put("CardSerial", ndShenPi.getCardSerial());map.put("RenCaiFlg", String.valueOf(ndShenPi.getRenCaiFlg()));map.put("RenCaiFlgContent", ndShenPi.getRenCaiFlgContent());map.put("CardTypeS", ndShenPi.getCardTypeS());if (ndShenPi.getRenCaiZXTime() == null) {map.put("RenCaiZXTime", null);} else {map.put("RenCaiZXTime", sdf.format(ndShenPi.getRenCaiZXTime()));}if (ndShenPi.getFuShenTime() == null) {map.put("FuShenTime", null);} else {map.put("FuShenTime", sdf.format(ndShenPi.getFuShenTime()));}if (ndShenPi.getAddTime() == null) {map.put("AddTime", null);} else {map.put("AddTime", sdf.format(ndShenPi.getAddTime()));}map.put("cardcode", ndShenPi.getCardcode());map.put("zzmm", ndShenPi.getZzmm());map.put("FuShen", ndShenPi.getFuShen());flag = "1";}else {flag = "2";}}else {flag="4";}}else {if (list.size()>=1){flag="4";}else {if (openid!=null&&sfzh!=null){wxsave = sfrz.wxsave(openid, sfzh, xm);if(wxsave!=0){NDShenPi ndShenPi = sfrz.getselect(sfzh, xm);if (ndShenPi != null) {map.put("id", String.valueOf(ndShenPi.getId()));map.put("BillNo", ndShenPi.getBillNo());map.put("OID", String.valueOf(ndShenPi.getOID()));map.put("ShiXiangMingCheng", ndShenPi.getShiXiangMingCheng());map.put("ShiXiangLeiXing", ndShenPi.getShiXiangLeiXing());map.put("ShouLiShiXian", ndShenPi.getShouLiShiXian());map.put("ShiXiangBianMa", ndShenPi.getShiXiangBianMa());map.put("ShouLiBuMen", ndShenPi.getShouLiBuMen());map.put("ShouLiChuangKou", ndShenPi.getShouLiChuangKou());map.put("ShouLiShiJian", ndShenPi.getShouLiShiJian());map.put("ShenQingRen", ndShenPi.getShenQingRen());map.put("ShenQingRenEN", ndShenPi.getShenQingRenEN());map.put("ZhengJianType", ndShenPi.getZhengJianType());map.put("ZhengJianHao", ndShenPi.getZhengJianHao());map.put("Img", ndShenPi.getImg());map.put("ShengRi", ndShenPi.getShengRi());map.put("XingBie", ndShenPi.getXingBie());map.put("MinZu", ndShenPi.getMinZu());map.put("GuoJia", ndShenPi.getGuoJia());map.put("HuKouSuoZaiDi", ndShenPi.getHuKouSuoZaiDi());map.put("XueLi", ndShenPi.getXueLi());map.put("XueLiQuanRiZhi", ndShenPi.getXueLiQuanRiZhi());map.put("QuanRiZhiZhuanYe", ndShenPi.getQuanRiZhiZhuanYe());map.put("HaiWai", ndShenPi.getHaiWai());map.put("XueLiZaiZhi", ndShenPi.getXueLiZaiZhi());map.put("ZaiZhiZhuanYe", ndShenPi.getZaiZhiZhuanYe());map.put("RenCaiLeiBie", ndShenPi.getRenCaiLeiBie());map.put("RenCaiChengHao", ndShenPi.getRenCaiChengHao());map.put("BiYeZhengShuBH", ndShenPi.getBiYeZhengShuBH());map.put("YJTime", ndShenPi.getYJTime());map.put("YouXiang", ndShenPi.getYouXiang());map.put("DianHua", ndShenPi.getDianHua());map.put("ShouJi", ndShenPi.getShouJi());map.put("YuanDanWei", ndShenPi.getYuanDanWei());map.put("YuanZhiCheng", ndShenPi.getYuanZhiCheng());map.put("YinJinLeiXing", ndShenPi.getYinJinLeiXing());map.put("QianYueQiXian", ndShenPi.getQianYueQiXian());map.put("ZhiCheng", ndShenPi.getZhiCheng());map.put("DanWei", ndShenPi.getDanWei());map.put("DiZhiQu", ndShenPi.getDiZhiQu());map.put("DiZhi", ndShenPi.getDiZhi());map.put("HangYe", ndShenPi.getHangYe());map.put("XingZhi", ndShenPi.getXingZhi());map.put("ShengZhi", ndShenPi.getShengZhi());map.put("FaRen", ndShenPi.getFaRen());map.put("LianXiRen", ndShenPi.getLianXiRen());map.put("LianXiDianHua", ndShenPi.getLianXiDianHua());map.put("GZShiJian", ndShenPi.getGZShiJian());map.put("GZGongZuo", ndShenPi.getGZGongZuo());map.put("GZZhiWu", ndShenPi.getGZZhiWu());map.put("JLMingCheng", ndShenPi.getJLMingCheng());map.put("JLShiJian", ndShenPi.getJLShiJian());map.put("JLBuMen", ndShenPi.getJLBuMen());map.put("QXingMing", ndShenPi.getQXingMing());map.put("QZhengHao", ndShenPi.getQZhengHao());map.put("QXingBie", ndShenPi.getQXingBie());map.put("QGuanXi", ndShenPi.getQGuanXi());map.put("QMingZu", ndShenPi.getQMingZu());map.put("QShengRi", ndShenPi.getQShengRi());map.put("QZhiWu", ndShenPi.getQZhiWu());map.put("QHuKouDiZhi", ndShenPi.getQHuKouDiZhi());map.put("QHuKou", ndShenPi.getQHuKou());map.put("YuanHuKou", ndShenPi.getYuanHuKou());map.put("YuanHuKouDiZhi", ndShenPi.getYuanHuKouDiZhi());map.put("HuKou", ndShenPi.getHuKou());map.put("HuKouDiZhi", ndShenPi.getHuKouDiZhi());map.put("GuestNo", ndShenPi.getGuestNo());map.put("GuestName", ndShenPi.getGuestName());map.put("BeiZhu", ndShenPi.getBeiZhu());map.put("ShouLiTiaoJian", ndShenPi.getShouLiTiaoJian());map.put("ShenQingShiJian", ndShenPi.getShenQingShiJian());map.put("Type", ndShenPi.getType());map.put("FromTo", ndShenPi.getFromTo());map.put("YuShenTxt", ndShenPi.getYuShenTxt());map.put("YuShenTime", ndShenPi.getYuShenTime());map.put("YuShenRen", ndShenPi.getYuShenRen());map.put("YuShenAccount", ndShenPi.getYuShenAccount());map.put("PJQStar", ndShenPi.getPJQStar());map.put("SmsNote", ndShenPi.getSmsNote());map.put("PJQMsg", ndShenPi.getPJQMsg());map.put("CardNo", ndShenPi.getCardNo());SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");if (ndShenPi.getCardDate() == null) {map.put("CardDate", null);} else {map.put("CardDate", sdf.format(ndShenPi.getCardDate()));}if (ndShenPi.getCardDateDQ() == null) {map.put("CardDateDQ", null);} else {map.put("CardDateDQ", sdf.format(ndShenPi.getCardDateDQ()));}if (ndShenPi.getCardFiveDQTime() == null) {map.put("CardFiveDQTime", null);} else {map.put("CardFiveDQTime", sdf.format(ndShenPi.getCardFiveDQTime()));}map.put("CardState", String.valueOf(ndShenPi.getCardState()));map.put("CardType", String.valueOf(ndShenPi.getCardType()));if (ndShenPi.getCardZKDate() == null) {map.put("CardZKDate", null);} else {map.put("CardZKDate", sdf.format(ndShenPi.getCardZKDate()));}map.put("CardSerial", ndShenPi.getCardSerial());map.put("RenCaiFlg", String.valueOf(ndShenPi.getRenCaiFlg()));map.put("RenCaiFlgContent", ndShenPi.getRenCaiFlgContent());map.put("CardTypeS", ndShenPi.getCardTypeS());if (ndShenPi.getRenCaiZXTime() == null) {map.put("RenCaiZXTime", null);} else {map.put("RenCaiZXTime", sdf.format(ndShenPi.getRenCaiZXTime()));}if (ndShenPi.getFuShenTime() == null) {map.put("FuShenTime", null);} else {map.put("FuShenTime", sdf.format(ndShenPi.getFuShenTime()));}if (ndShenPi.getAddTime() == null) {map.put("AddTime", null);} else {map.put("AddTime", sdf.format(ndShenPi.getAddTime()));}map.put("cardcode", ndShenPi.getCardcode());map.put("zzmm", ndShenPi.getZzmm());map.put("FuShen", ndShenPi.getFuShen());flag = "1";}else {flag = "2";}}}}}map.put("success", flag);return map;}@GetMapping("/signature")@ResponseBodypublic JSONObject code(String card_id,String accessToken) throws Exception {card_id = new String(decode(card_id));accessToken = new String(decode(accessToken));// String accessToken = getAccessToken();System.err.println(accessToken);String url="https://api./cgi-bin/ticket/getticket?access_token="+accessToken+"&type=wx_card";// String url1="https://api./cgi-bin/ticket/getticket?access_token="+accessToken+"&type=jsapi";String s = doGet(url);JSONObject jsonObject1 = JSONObject.parseObject(s);String ticket="";if (jsonObject1!=null){ticket = jsonObject1.getString("ticket");System.err.println(ticket);}System.err.println(s);WeixinSignature signature = new WeixinSignature(ticket,timestamp(), card_id, new Accessnonce_str().nonce_str()); //获取signature// PrintWriter out = response.getWriter(); //返回网页请求的数据JSONObject jsonObject = new JSONObject(); //返回JSON格式数据jsonObject.put("apitick",ticket);// System.out.println("时间:"+Open_Id.getTime());// System.out.println("随机串:"+nonce);// System.out.println("加密字符:"+signature.sign());// System.out.println("cardid:"+Card_Id.getCardid());jsonObject.put("timestamp", timestamp());// jsonObject.put("openid", openid);jsonObject.put("nonce_str", signature.getNonce_str());jsonObject.put("signature",signature.sign());jsonObject.put("card_id",card_id);// jsonObject.put("openid",getopenid(code));return jsonObject;}/*** 获取时间戳* @return*/public String timestamp(){String time = String.valueOf(System.currentTimeMillis()/1000); //获取一个时间戳 转换为String类型return time;}/*** Created by hubo on /11/7* 随机字符串nonce_str*/public static class Accessnonce_str {private final static String string = "0123456789";final private static char[] chars = string.toCharArray();public String nonce_str(){String nonce = new String();for(int i=0; i<10; i++){int rannum = (int)(Math.random()*1000) % (chars.length);nonce += chars[rannum];}return nonce;}}/*** Created by hubo on /11/7* 加密signature*/public class WeixinSignature {private String api_ticket;private String timestamp;private String card_id;private String nonce_str;public String getNonce_str() {return nonce_str;}public void setNonce_str(String nonce_str) {this.nonce_str = nonce_str;}public WeixinSignature(String api_ticket, String timestamp, String card_id, String nonce_str){System.err.println("nonce_str"+nonce_str);this.api_ticket = api_ticket;this.timestamp = timestamp;this.card_id = card_id;this.nonce_str = nonce_str;}/*** 加密参数* @return*/public String sign() throws Exception {String[] str = {api_ticket,timestamp,card_id,nonce_str};Arrays.sort(str);String string = new String();for(int i=0; i<4; i++){string += str[i];System.err.println("string"+string);}//加密结果String signature = CheckUtil.shaEncode(string); //Sha1加密return signature;}}/*判读时间差距,两个时间相差多少天,时,分,秒*/public static Long getDay(String date) {DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Long days = null;try {Date currentTime = dateFormat.parse(dateFormat.format(new Date()));//现在系统当前时间Date pastTime = dateFormat.parse(date);//过去时间long diff = pastTime.getTime() - currentTime.getTime();days = diff / (1000 * 60 * 60 * 24);} catch (ParseException | java.text.ParseException e) {e.printStackTrace();}return days;}/*** 发送post请求* @return* @throws ParseException* @throws IOException*/@GetMapping("/createCard")@ResponseBodypublic HashMap<String, String> createCard(String sfzh,String xm) throws ParseException, IOException {sfzh = new String(decode(sfzh));JSONObject jsonObject = new JSONObject();String accessToken = getAccessToken();NDShenPi ndShenPi = sfrz.getselect(sfzh, xm);String card="";if(ndShenPi.getCardNo()!=null){card=ndShenPi.getCardNo();}else {card="没有卡号";}String leixing = "";if(ndShenPi.getCardTypeS()!=null){leixing = ndShenPi.getCardTypeS();}else {leixing = "还没有填写";}SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");LocalDateTime now = LocalDateTime.now();System.err.println("now"+now);System.err.println("date"+new Date());String format1 ="";Long day=null;Date date = new Date();String format = sdf.format(date);System.err.println("ndShenPi.getCardFiveDQTime():"+ndShenPi.getCardFiveDQTime());if (ndShenPi.getCardFiveDQTime()!=null){format1 = sdf.format(ndShenPi.getCardFiveDQTime());day = getDay(format1);System.err.println("day:"+day);}else {format1 = format;day = getDay(format1);}String body="{\n" +" \"card\": {\n" +" \"card_type\": \"MEMBER_CARD\",\n" +" \"member_card\": {\n" +" \"background_pic_url\": \"/mmbiz_jpg/9v1YWUY176giaaFkXdSb5mOE35GSARDibPSC1vHR1tpQjPNibIJNicSdib6Hm3x1xHwUdj0b1FbjYj9fnarJjFEwnSg/0\",\n" +" \"base_info\": {\n" +"\"logo_url\": \"/mmbiz_png/9v1YWUY176giaaFkXdSb5mOE35GSARDibPOSiadwAOjRIiaYW7azsG4VInvbVias5zt8tGGAOjyJ5UIIxaC4v1SeH0g/0\",\n" +"\"brand_name\": \"定州市电子英才服务卡\",\n" +"\"code_type\": \"CODE_TYPE_NONE\",\n" +"\"title\": \"英才服务卡\",\n" +"\"color\": \"Color010\",\n" +"\"notice\": \"使用时向服务员出示此券\",\n" +"\"description\": \"A卡或B卡类型\",\n" +"\"date_info\": {\n" +"\"type\": \"DATE_TYPE_FIX_TERM\",\n" +"\"fixed_term\": "+(day+2)+"\n" +//"\"end_timestamp\":"+(int) (System.currentTimeMillis() / 1000)+",\n" +//"\"begin_timestamp\":"+(int) (System.currentTimeMillis() / 1000)+"\n" +"},\n" +"\"sku\": {\n" +"\"quantity\": 1\n" +"},\n" +"\"get_limit\": 1,\n" +"\"use_custom_code\": false,\n" +"\"can_give_friend\": true,\n" +"\"location_id_list\": [\n" +"123,\n" +"12321\n" +"],\n" +"\"custom_url_name\": \"卡号\",\n" +//跳转小程序页面"\"custom_app_brand_user_name\": \"gh_a45555fc6ccf@app\",\n" +"\"custom_app_brand_pass\": \"pages/activate/index\",\n" +"\"custom_url\": \"\",\n" +"\"custom_url_sub_title\": \""+card+"\",\n" +"\"need_push_on_view\": true\n" +" },\n" +" \"advanced_info\": {\n" +"\"use_condition\": {\n" +" \"accept_category\": \"\",\n" +" \"reject_category\": \"\",\n" +" \"can_use_with_other_discount\": false\n" +"}\n" +" },\n" +" \"supply_bonus\": false,\n" +" \"supply_balance\": false,\n" +" \"prerogative\": \"持定州市电子英才服务卡者,可到相关服务部门享受服务,具体内容,详询定州市组织部人才科\",\n" +" \"auto_activate\": true,\n" +" \"wx_activate\": true,\n" +" \"activate_url\": \"\",\n" +" \"custom_cell1\": {\n" +"\"name\": \"姓名\",\n" +"\"tips\": \""+xm+"\",\n" +"\"url\": \"http://123.56.117.167:8000/\"\n" +" },\n" +" \"custom_cell2\": {\n" +"\"name\": \"类型\",\n" +"\"tips\": \""+leixing+"卡\",\n" +"\"url\": \"http://123.56.117.167:8000/\"\n" +" }\n" +" }\n" +" }\n" +"}";String url="https://api./card/create?access_token="+accessToken;HashMap<String, String> map= new HashMap<>();JSONObject jsonObject1 = JSONObject.parseObject(body);System.err.println(jsonObject1);String post = post(jsonObject1, url);JSONObject jsonObject2 = JSONObject.parseObject(post);String card_id = jsonObject2.getString("card_id");map.put("card_id",card_id);map.put("accessToken",accessToken);return map;}/*获取getAccessToken*//*@GetMapping("/getAccessToken")@ResponseBody*/public String getAccessToken(){String url="https://api./cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+AppSecret;String s = doGet(url);JSONObject jsonObject = JSONObject.parseObject(s);String access_token = jsonObject.getString("access_token");return access_token;}//发送get请求public String doGet(String URL){HttpURLConnection conn = null;InputStream is = null;BufferedReader br = null;StringBuilder result = new StringBuilder();try{//创建远程url连接对象.URL url = new URL(URL);//通过远程url连接对象打开一个连接,强转成HTTPURLConnection类conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");//设置连接超时时间和读取超时时间conn.setConnectTimeout(15000);conn.setReadTimeout(60000);conn.setRequestProperty("Accept", "application/json");//发送请求conn.connect();//通过conn取得输入流,并使用Reader读取if (200 == conn.getResponseCode()){is = conn.getInputStream();br = new BufferedReader(new InputStreamReader(is, "UTF-8"));String line;while ((line = br.readLine()) != null){result.append(line);System.out.println(line);}}else{System.out.println("ResponseCode is an error code:" + conn.getResponseCode());}}catch (MalformedURLException e){e.printStackTrace();}catch (IOException e){e.printStackTrace();}catch (Exception e){e.printStackTrace();}finally {try{if(br != null){br.close();}if(is != null){is.close();}}catch (IOException ioe){ioe.printStackTrace();}conn.disconnect();}return result.toString();}public static String post(JSONObject json,String URL) {HttpClient client = new DefaultHttpClient();HttpPost post = new HttpPost(URL);post.setHeader("Content-Type", "application/json");post.addHeader("Authorization", "Basic YWRtaW46");String result = "";try {StringEntity s = new StringEntity(json.toString(), "utf-8");s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));post.setEntity(s);// 发送请求HttpResponse httpResponse = client.execute(post);// 获取响应输入流InputStream inStream = httpResponse.getEntity().getContent();BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));StringBuilder strber = new StringBuilder();String line = null;while ((line = reader.readLine()) != null)strber.append(line + "\n");inStream.close();result = strber.toString();System.out.println(result);if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {System.out.println("请求服务器成功,做相应处理");} else {System.out.println("请求服务端失败");}} catch (Exception e) {System.out.println("请求异常");throw new RuntimeException(e);}return result;}/*获取openid*/private static String getOpenid(String code){//临时登录凭证String URL = "https://api./sns/jscode2session?appid="+APPID1+"&secret="+AppSecret1+"&js_code="+code+"&grant_type=authorization_code";String openId=interfaceUtil(URL, "");return openId;};private static String interfaceUtil(String path,String data) {String openId = "";try {URL url = new URL(path);//打开和url之间的连接HttpURLConnection conn = (HttpURLConnection) url.openConnection();PrintWriter out = null;//请求方式//conn.setRequestMethod("POST");// //设置通用的请求属性conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");//设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个//最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,//post与get的 不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。conn.setDoOutput(true);conn.setDoInput(true);//获取URLConnection对象对应的输出流out = new PrintWriter(conn.getOutputStream());//发送请求参数即数据out.print(data);//缓冲数据out.flush();//获取URLConnection对象对应的输入流InputStream is = conn.getInputStream();//构造一个字符流缓存BufferedReader br = new BufferedReader(new InputStreamReader(is));String str = "";while ((str = br.readLine()) != null) {openId = str;}//关闭流is.close();//断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。//固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些。conn.disconnect();//System.out.println("完整结束");} catch (Exception e) {e.printStackTrace();}return openId;}}
service
package com.example.demo.service;import com.example.demo.entities.wxuser;import java.util.List;/*** @author : 宋银义* @date : -06-11 17:29**/public interface sfrz {List<wxuser> selectopenid(String openid);List<wxuser> selectopenid1(String sfzh);int delappid(String sfzh);int save(String sfzh, String xm,String appid);int save1(String appid,String biz_card_no);int wxsave(String openid1,String sfzh,String xm);int savecardid(String openid,String sfzh,String xm,String cardid);List<wxuser> selectcardid(String openid,String sfzh, String xm);int updatedh(String sfzh,String xm,String shouji);}
application.yml
server:port: 8888spring:application:name: cloud-provider-servicedatasource:# type: com.alibaba.druid.pool.DruidDataSource#当前数据源操作类型# driver-class-name: org.gjt.mm.mysql.Driver #mysql驱动包# url: jdbc:mysql://localhost:3306/db326?useUnicode=true&characterEncoding-utr-8&useSSL=false# username: root# password: root# url: jdbc:sqlserver://localhost;DatabaseName=dzrclkcs# username: sa# password: Dingzhou!#%url: jdbc:sqlserver://192.168.1.65;DatabaseName=ccflow-dzrclkusername: sapassword: admin123W#使用druid数据源type: com.alibaba.druid.pool.DruidDataSourcedriverClassName: com.microsoft.sqlserver.jdbc.SQLServerDrivermybatis:mapper-locations: classpath:mapper/*.xmltype-aliases-package: com.example.demo.entities #所有Entity别名类所在包# configuration: map-underscore-to-camel-case=true
差不多就是这些业务逻辑;
小程序代码见我的百度网盘:
链接:/s/1GhkHGLSFqewjx5Ywf0VCMg
提取码:galy
有什么不清楚的可有留言咨询,谢谢大家,希望大家少踩坑!!!!