随着新零售时代的到来,许多店铺使用微信公众号实现扫码开门,下面简单介绍。
public void creatQrcode(String storeCode){
String url = WechatConstant.getCreatQrcodeUrl(wechatAccessTokenService.takeAccessToken());
String payResult = OkHttpUtil.getInstance().doPost(url,WechatConstant.getCreatQrcodePARAM("PAY_"+storeCode));
String inDoorResult = OkHttpUtil.getInstance().doPost(url,WechatConstant.getCreatQrcodePARAM("INDOOR_"+storeCode));
String outDoorResult = OkHttpUtil.getInstance().doPost(url,WechatConstant.getCreatQrcodePARAM("OUTDOOR_"+storeCode));
String helpResult = OkHttpUtil.getInstance().doPost(url,WechatConstant.getCreatQrcodePARAM("HELP_"+storeCode));
JSONObject payObject = JSON.parseObject(payResult);
JSONObject inDoorObject = JSON.parseObject(inDoorResult);
JSONObject outDoorObject = JSON.parseObject(outDoorResult);
JSONObject helpObject = JSON.parseObject(helpResult);
String payUrl = payObject.getString("url");
String indoorUrl = inDoorObject.getString("url");
String outdoorUrl = outDoorObject.getString("url");
String helpUrl = helpObject.getString("url");
if(!TextUtils.isEmpty(payUrl)&&!TextUtils.isEmpty(indoorUrl)&&!TextUtils.isEmpty(outdoorUrl)&&!TextUtils.isEmpty(helpUrl)){
logger.info("update store urls===storeNum="+storeCode+"==payUrl=="+payUrl
+"==inDoorUrl=="+indoorUrl+"==outDoorUrl=="+outdoorUrl+"==helpUrl=="+helpUrl);
augeStoreMapper.updateUrlsByCode(storeCode,payUrl,indoorUrl,outdoorUrl,helpUrl);
}
}
boolean isAddComplete = storeService.addStore(augeStore);
if (isAddComplete) {
//新增完成后,调用微信的相关services来更新支付,进出门以及帮助地址
try {
wechatService.creatQrcode(augeStore.getCode());
} catch (Exception e) {
e.printStackTrace();
}
List storeList = storeService.getByOrgan(admin.getOrganId());
session.setAttribute("storeList", storeList);
clientResponseJSONBean = ClientResponseJSONBean.getSuccessClientResponseJSONBean();
clientResponseJSONBean.setMessage("成功");
return clientResponseJSONBean.toJSONString();
}
注:生成的店铺url使用草料二维码生成二维码图片,以供使用。
if (MsgType.SCAN.getValue().equals(event)) {
//已经订阅后扫码 开门
logger.info("MsgType.SCAN====" + inputMsg.getEventKey());
//查询用户信息
AugeWechatUser wechatUser = augeWechatUserMapper.selectByPrimaryKey(custermname);
if (param.length == 2) {
OkHttpUtil.getInstance().doGet(BookApiUtils.GET_REDIS_SET + custermname + "/" + param[1]);
}
if (param.length == 3) {
OkHttpUtil.getInstance().doGet(BookApiUtils.GET_REDIS_SET + custermname + "/" + param[2]);
}
String backMsg = MSG_SUCCESS;
//数据库未保存该用户
if (wechatUser == null) {
//保存该用户,返回注册图文消息
augeWechatUserMapper.insertOrUpdate(new AugeWechatUser(custermname));
ArticlesMessage outputMsg = getLoginMessage(custermname, servername, returnTime);
backMsg = xs.toXML(outputMsg);
logger.info("返回图文消息===" + backMsg);
} else if (TextUtils.isEmpty(wechatUser.getPhone())) {
//没有注册,返回注册图文消息
ArticlesMessage outputMsg = getLoginMessage(custermname, servername, returnTime);
backMsg = xs.toXML(outputMsg);
logger.info("返回图文消息===" + backMsg);
}
String[] storeInfo = inputMsg.getEventKey().split("_");
if (storeInfo != null && storeInfo.length == 2) {
AugeStore store = augeStoreMapper.selectStoreByCode(storeInfo[1]);
if (store != null) {
switch (storeInfo[0]) {
//进门
case WechatConstant.INDOOR:
logger.info("用户==" + wechatUser.getPhone() + "扫码请求进门");
inDoor(wechatUser, store);
break;
//出门
case WechatConstant.OUTDOOR:
logger.info("用户==" + wechatUser.getPhone() + "扫码请求出门");
outDoor(wechatUser, store);
break;
default:
break;
}
}
}
注:用户扫描二维码开门,首先判断用户是否注册,未注册返回注册的图文消息,已注册调用开门方法。
public void inDoor(final AugeWechatUser wechatUser, final AugeStore store) {
//内网穿透直接开门
taskExecutor.execute(new Runnable() {
@Override
public void run() {
OkHttpUtil.getInstance().doGet(WechatConstant.getUrl(store.getIp(), WechatConstant.INDOOR_URL + "?phonenum=" + wechatUser.getPhone()));
}
});
}
public class OkHttpUtil {
private static OkHttpUtil okHttpUtil;
private static OkHttpClient okHttpClient;
private OkHttpUtil() {
/**
* OkHttp是一个第三方类库,
* 用于android中请求网络
* 创建OkHttpClient对象
*/
okHttpClient = new OkHttpClient();
}
public static OkHttpUtil getInstance() {
if (okHttpUtil == null) {
okHttpUtil = new OkHttpUtil();
}
return okHttpUtil;
}
public String doGet(String url) {
/**
* 请求接口
* 创建Request对象
*/
Request request = new Request.Builder()
.url(url)
.build();
//得到request对象
Call call = okHttpClient.newCall(request);
try {
Response response = call.execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String doPost(String url, Map param) {
FormBody.Builder builder = new FormBody.Builder();
for (Map.Entry entry : param.entrySet()) {
builder.add(entry.getKey(), entry.getValue());
}
RequestBody body = builder.build();
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Call call = okHttpClient.newCall(request);
try {
Response response = call.execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String doPostParam(String url, Map param) {
FormBody.Builder builder = new FormBody.Builder();
for (Map.Entry entry : param.entrySet()) {
builder.add(entry.getKey(), (String) entry.getValue());
}
RequestBody body = builder.build();
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Call call = okHttpClient.newCall(request);
try {
Response response = call.execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String doPost(String url, String param) {
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, param);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Call call = okHttpClient.newCall(request);
try {
Response response = call.execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}