Skip to content
774 changes: 387 additions & 387 deletions pom.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package me.chanjar.weixin.common.util.crypto;
import java.util.ArrayList;
public class ByteGroup {
ArrayList<Byte> byteContainer = new ArrayList<>();
public byte[] toBytes() {
byte[] bytes = new byte[this.byteContainer.size()];
for (int i = 0; i < this.byteContainer.size(); i++) {
bytes[i] = this.byteContainer.get(i);
}
return bytes;
}
public ByteGroup addBytes(byte[] bytes) {
for (byte b : bytes) {
this.byteContainer.add(b);
}
return this;
}
public int size() {
return this.byteContainer.size();
}
}
package me.chanjar.weixin.common.util.crypto;

import java.util.ArrayList;

public class ByteGroup {
ArrayList<Byte> byteContainer = new ArrayList<>();

public byte[] toBytes() {
byte[] bytes = new byte[this.byteContainer.size()];
for (int i = 0; i < this.byteContainer.size(); i++) {
bytes[i] = this.byteContainer.get(i);
}
return bytes;
}

public ByteGroup addBytes(byte[] bytes) {
for (byte b : bytes) {
this.byteContainer.add(b);
}
return this;
}

public int size() {
return this.byteContainer.size();
}
}
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
/**
* 对公众平台发送给公众账号的消息加解密示例代码.
*
* @copyright Copyright (c) 1998-2014 Tencent Inc.
*/
// ------------------------------------------------------------------------
package me.chanjar.weixin.common.util.crypto;
import java.nio.charset.Charset;
import java.util.Arrays;
/**
* 提供基于PKCS7算法的加解
*/
public class PKCS7Encoder {
private static final Charset CHARSET = Charset.forName("utf-8");
private static final int BLOCK_SIZE = 32;
/**
* 获得对明文进行补位填充的字节.
*
* @param count 需要进行填充补位操作的明文字节个数
* @return 补齐用的字节数组
*/
public static byte[] encode(int count) {
// 计算需要填充的位数
int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE);
if (amountToPad == 0) {
amountToPad = BLOCK_SIZE;
}
// 获得补位所用的字符
char padChr = chr(amountToPad);
String tmp = new String();
for (int index = 0; index < amountToPad; index++) {
tmp += padChr;
}
return tmp.getBytes(CHARSET);
}
/**
* 删除解密后明文的补位字符
*
* @param decrypted 解密后的明文
* @return 删除补位字符后的明文
*/
public static byte[] decode(byte[] decrypted) {
int pad = decrypted[decrypted.length - 1];
if (pad < 1 || pad > 32) {
pad = 0;
}
return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad);
}
/**
* 将数字转化成ASCII码对应的字符,用于对明文进行补码
*
* @param a 需要转化的数字
* @return 转化得到的字符
*/
public static char chr(int a) {
byte target = (byte) (a & 0xFF);
return (char) target;
}
}
/**
* 对公众平台发送给公众账号的消息加解密示例代码.
*
* @copyright Copyright (c) 1998-2014 Tencent Inc.
*/

// ------------------------------------------------------------------------

package me.chanjar.weixin.common.util.crypto;

import java.nio.charset.Charset;
import java.util.Arrays;

/**
* 提供基于PKCS7算法的加解
*/
public class PKCS7Encoder {

private static final Charset CHARSET = Charset.forName("utf-8");
private static final int BLOCK_SIZE = 32;

/**
* 获得对明文进行补位填充的字节.
*
* @param count 需要进行填充补位操作的明文字节个数
* @return 补齐用的字节数组
*/
public static byte[] encode(int count) {
// 计算需要填充的位数
int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE);
if (amountToPad == 0) {
amountToPad = BLOCK_SIZE;
}
// 获得补位所用的字符
char padChr = chr(amountToPad);
String tmp = new String();
for (int index = 0; index < amountToPad; index++) {
tmp += padChr;
}
return tmp.getBytes(CHARSET);
}

/**
* 删除解密后明文的补位字符
*
* @param decrypted 解密后的明文
* @return 删除补位字符后的明文
*/
public static byte[] decode(byte[] decrypted) {
int pad = decrypted[decrypted.length - 1];
if (pad < 1 || pad > 32) {
pad = 0;
}
return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad);
}

/**
* 将数字转化成ASCII码对应的字符,用于对明文进行补码
*
* @param a 需要转化的数字
* @return 转化得到的字符
*/
public static char chr(int a) {
byte target = (byte) (a & 0xFF);
return (char) target;
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package me.chanjar.weixin.mp.api;

import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.WxMpShakeInfoResult;
import me.chanjar.weixin.mp.bean.WxMpShakeQuery;
import me.chanjar.weixin.mp.bean.shake.*;

/**
* 摇一摇周边的相关接口
Expand All @@ -24,4 +26,36 @@ public interface WxMpShakeService {
*/
WxMpShakeInfoResult getShakeInfo(WxMpShakeQuery wxMpShakeQuery) throws WxErrorException;

/**
* <pre>
* 页面管理<br/>
* 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1459246752
* </pre>
* @param shakeAroundPageAddQuery
* @return
* @throws WxErrorException
*/
WxMpShakeAroundPageAddResult pageAdd(WxMpShakeAroundPageAddQuery shakeAroundPageAddQuery) throws WxErrorException;

/**
* <pre>
* 配置设备与页面的关联关系<br/>
* 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1459301931
* </pre>
* @param shakeAroundDeviceBindPageQuery
* @return
* @throws WxErrorException
*/
WxError deviceBindPageQuery(WxMpShakeAroundDeviceBindPageQuery shakeAroundDeviceBindPageQuery) throws WxErrorException;

/**
* <pre>
* 查询设备与页面的关联关系<br/>
* 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1443447914
* </pre>
* @param shakeAroundRelationSearchQuery
* @return
* @throws WxErrorException
*/
WxMpShakeAroundRelationSearchResult relationSearch(WxMpShakeAroundRelationSearchQuery shakeAroundRelationSearchQuery) throws WxErrorException;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package me.chanjar.weixin.mp.api.impl;

import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpShakeService;
import me.chanjar.weixin.mp.bean.WxMpShakeInfoResult;
import me.chanjar.weixin.mp.bean.WxMpShakeQuery;
import me.chanjar.weixin.mp.bean.shake.*;

/**
* Created by rememberber on 2017/6/5.
*
* @author rememberber
*/
public class WxMpShakeServiceImpl implements WxMpShakeService {
Expand All @@ -27,7 +30,7 @@ public WxMpShakeServiceImpl(WxMpService wxMpService) {
* 接口地址:https://api.weixin.qq.com/shakearound/user/getshakeinfo?access_token=ACCESS_TOKE
* </pre>
*
* @param wxMpShakeQuery 查询参数
* @param wxMpShakeQuery 查询参数
*/
@Override
public WxMpShakeInfoResult getShakeInfo(WxMpShakeQuery wxMpShakeQuery) throws WxErrorException {
Expand All @@ -36,4 +39,28 @@ public WxMpShakeInfoResult getShakeInfo(WxMpShakeQuery wxMpShakeQuery) throws Wx
String responseContent = this.wxMpService.post(url, postData);
return WxMpShakeInfoResult.fromJson(responseContent);
}

@Override
public WxMpShakeAroundPageAddResult pageAdd(WxMpShakeAroundPageAddQuery shakeAroundPageAddQuery) throws WxErrorException {
String url = "https://api.weixin.qq.com/shakearound/page/add";
String postData = shakeAroundPageAddQuery.toJsonString();
String responseContent = this.wxMpService.post(url, postData);
return WxMpShakeAroundPageAddResult.fromJson(responseContent);
}

@Override
public WxError deviceBindPageQuery(WxMpShakeAroundDeviceBindPageQuery shakeAroundDeviceBindPageQuery) throws WxErrorException {
String url = "https://api.weixin.qq.com/shakearound/device/bindpage";
String postData = shakeAroundDeviceBindPageQuery.toJsonString();
String responseContent = this.wxMpService.post(url, postData);
return WxError.fromJson(responseContent);
}

@Override
public WxMpShakeAroundRelationSearchResult relationSearch(WxMpShakeAroundRelationSearchQuery shakeAroundRelationSearchQuery) throws WxErrorException {
String url = "https://api.weixin.qq.com/shakearound/relation/search";
String postData = shakeAroundRelationSearchQuery.toJsonString();
String responseContent = this.wxMpService.post(url, postData);
return WxMpShakeAroundRelationSearchResult.fromJson(responseContent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package me.chanjar.weixin.mp.bean.shake;

import com.google.gson.JsonObject;
import lombok.Data;

import java.io.Serializable;

@Data
public class WxMpDeviceIdentifier implements Serializable {
private Integer device_id;
private String uuid;
private Integer page_id;
private Integer major;
private Integer minor;
public JsonObject toJsonObject(){
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("device_id", device_id);
jsonObject.addProperty("uuid", uuid);
jsonObject.addProperty("major", major);
jsonObject.addProperty("minor", minor);
return jsonObject;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package me.chanjar.weixin.mp.bean.shake;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import lombok.Data;

import java.util.Collection;

@Data
public class WxMpShakeAroundDeviceBindPageQuery {
private WxMpDeviceIdentifier deviceIdentifier;
private Collection<Integer> pageIds;
public String toJsonString(){
JsonObject jsonObject = new JsonObject();
jsonObject.add("device_identifier", deviceIdentifier.toJsonObject());
JsonArray jsonArray = new JsonArray();
for(Integer pageid: pageIds){
jsonArray.add(pageid);
}
jsonObject.add("page_ids", jsonArray);
return jsonObject.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package me.chanjar.weixin.mp.bean.shake;

import com.google.gson.JsonObject;
import lombok.Data;

import java.io.Serializable;
@Data
public class WxMpShakeAroundPageAddQuery implements Serializable {
private String title;
private String description;
private String pageUrl;
private String comment;
private String iconUrl;
public String toJsonString(){
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("title", title);
jsonObject.addProperty("description", description);
jsonObject.addProperty("page_url", pageUrl);
jsonObject.addProperty("comment", comment);
jsonObject.addProperty("icon_url", iconUrl);
return jsonObject.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package me.chanjar.weixin.mp.bean.shake;

import com.google.gson.JsonObject;
import lombok.Data;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;

import java.io.Serializable;

@Data
public class WxMpShakeAroundPageAddResult implements Serializable {
private Integer errorCode;
private String errorMsg;
private Integer pageId;
public static WxMpShakeAroundPageAddResult fromJson(String json) {
JsonObject jsonObject = WxMpGsonBuilder.INSTANCE.create().fromJson(json, JsonObject.class);
WxMpShakeAroundPageAddResult result = new WxMpShakeAroundPageAddResult();
result.setErrorCode(GsonHelper.getInteger(jsonObject, "errcode"));
result.setErrorMsg(GsonHelper.getString(jsonObject, "errmsg"));
jsonObject = jsonObject.getAsJsonObject("data");
if(jsonObject != null){
result.setPageId(GsonHelper.getInteger(jsonObject, "page_id"));
}
return result;
}
}
Loading