Skip to content

Commit

Permalink
feat:新增自定义菜单之小程序菜单
Browse files Browse the repository at this point in the history
* 1.优化自定义菜单功能
* 2.新增小程序菜单

Close #1
  • Loading branch information
yangqisheng committed Aug 9, 2018
1 parent 96a218b commit e449b34
Show file tree
Hide file tree
Showing 17 changed files with 139 additions and 105 deletions.
4 changes: 2 additions & 2 deletions weixin4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.weixin4j</groupId>
<artifactId>weixin4j</artifactId>
<version>0.1.1</version>
<version>0.1.2</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -39,5 +39,5 @@
<version>2.6</version>
</dependency>
</dependencies>
<name>weixin4j-0.1.1</name>
<name>weixin4j-0.1.2</name>
</project>
24 changes: 12 additions & 12 deletions weixin4j/src/main/java/org/weixin4j/Weixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,10 @@
*/
public class Weixin extends WeixinSupport implements java.io.Serializable {

/**
* 解决多线程并发重复获取token问题
*/
private volatile Token _token;
/**
* 同步锁
*/
private final byte[] lock = new byte[0];
private final static byte[] LOCK = new byte[0];
/**
* 公众号开发者ID
*/
Expand Down Expand Up @@ -106,13 +102,12 @@ public String getSecret() {
* @since 0.1.0
*/
public Token getToken() throws WeixinException {
_token = tokenLoader.get();
Token token = this._token;
Token token = tokenLoader.get();
if (token == null) {
synchronized (lock) {
token = this._token;
synchronized (LOCK) {
token = tokenLoader.get();
if (token == null) {
this._token = token = base().token();
token = base().token();
tokenLoader.refresh(token);
}
}
Expand All @@ -129,8 +124,13 @@ public Token getToken() throws WeixinException {
public Ticket getJsApiTicket() throws WeixinException {
Ticket ticket = ticketLoader.get(TicketType.JSAPI);
if (ticket == null) {
ticket = js().getJsApiTicket();
ticketLoader.refresh(ticket);
synchronized (LOCK) {
ticket = ticketLoader.get(TicketType.JSAPI);
if (ticket == null) {
ticket = js().getJsApiTicket();
ticketLoader.refresh(ticket);
}
}
}
return ticket;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

/**
* 自定义菜单组件
*
*
* @author 杨启盛<[email protected]>
* @since 0.1.0
*/
Expand Down
20 changes: 10 additions & 10 deletions weixin4j/src/main/java/org/weixin4j/model/menu/BaseButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@
* @author 杨启盛<[email protected]>
* @since 0.0.1
*/
public abstract class BaseButton {
public class BaseButton {

/**
* 菜单标题,不超过16个字节,子菜单不超过40个字节
*/
private String name;
private final String name;

/**
* 获取 菜单标题
* 基础按钮
*
* @return 菜单标题
* @param name 菜单标题
*/
public String getName() {
return name;
public BaseButton(String name) {
this.name = name;
}

/**
* 设置 菜单标题
* 获取 菜单标题
*
* @param name 菜单标题
* @return 菜单标题
*/
public void setName(String name) {
this.name = name;
public String getName() {
return name;
}
}
17 changes: 7 additions & 10 deletions weixin4j/src/main/java/org/weixin4j/model/menu/ClickButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,8 @@ public class ClickButton extends SingleButton {
*/
private String key;

public ClickButton() {
}

public ClickButton(String key) {
this.key = key;
}

public ClickButton(String name, String key) {
this.setName(name);
super(name);
this.key = key;
}

Expand All @@ -51,7 +44,9 @@ public String getType() {
/**
* 获取 菜单KEY值
*
* <p>click类型必须.菜单KEY值,用于消息接口推送,不超过128字节</p>
* <p>
* click类型必须.菜单KEY值, 用于消息接口推送,不超过128字节
* </p>
*
* @return 菜单KEY值
*/
Expand All @@ -62,7 +57,9 @@ public String getKey() {
/**
* 设置 菜单KEY值
*
* <p>click类型必须.菜单KEY值,用于消息接口推送,不超过128字节</p>
* <p>
* click类型必须.菜单KEY值,用于消息接口推送,不超过128字节
* </p>
*
* @param key 菜单KEY值
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,8 @@ public class LocationSelectButton extends SingleButton {
*/
private String key;

public LocationSelectButton() {
}

public LocationSelectButton(String key) {
this.key = key;
}

public LocationSelectButton(String name, String key) {
this.setName(name);
super(name);
this.key = key;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ public class MediaIdButton extends SingleButton {
*/
private String mediaId;

public MediaIdButton() {
}

public MediaIdButton(String name, String mediaId) {
this.setName(name);
super(name);
this.mediaId = mediaId;
}

Expand Down
3 changes: 1 addition & 2 deletions weixin4j/src/main/java/org/weixin4j/model/menu/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ private void recursion(JSONObject jsonButton, SingleButton menuButton) {
//判断对象type
if (type == null) {
//有子的自定义菜单
singleButton = new SingleButton();
singleButton.setName(jsonButton.getString("name"));
singleButton = new SingleButton(jsonButton.getString("name"));
} else if (type.equals(ButtonType.Click.toString())) {
//转成点击按钮
singleButton = new ClickButton(jsonButton.getString("name"), jsonButton.getString("key"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 微信公众平台(JAVA) SDK
*
* Copyright (c) 2014, Ansitech Network Technology Co.,Ltd All rights reserved.
*
* http://www.weixin4j.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.weixin4j.model.menu;

/**
* 打开小程序
*
* @author 杨启盛<[email protected]>
* @since 0.1.2
*/
public class MiniprogramButton extends SingleButton {

/**
* 小程序的appid(仅认证公众号可配置)
*/
private String appid;
/**
* 小程序的页面路径
*/
private String pagepath;
/**
* 网页 链接,用户点击菜单可打开链接,不超过1024字节。
*
* 不支持小程序的老版本客户端将打开本url。
*/
private String url;

public MiniprogramButton(String name) {
super(name);
}

public MiniprogramButton(String name, String appid, String pagepath, String url) {
super(name);
this.appid = appid;
this.pagepath = pagepath;
this.url = url;
}

public String getAppid() {
return appid;
}

public void setAppid(String appid) {
this.appid = appid;
}

public String getPagepath() {
return pagepath;
}

public void setPagepath(String pagepath) {
this.pagepath = pagepath;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,8 @@ public class PicPhotoOrAlbumButton extends SingleButton {
*/
private String key;

public PicPhotoOrAlbumButton() {
}

public PicPhotoOrAlbumButton(String key) {
this.key = key;
}

public PicPhotoOrAlbumButton(String name, String key) {
this.setName(name);
super(name);
this.key = key;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,8 @@ public class PicSysPhotoButton extends SingleButton {
*/
private String key;

public PicSysPhotoButton() {
}

public PicSysPhotoButton(String key) {
this.key = key;
}

public PicSysPhotoButton(String name, String key) {
this.setName(name);
super(name);
this.key = key;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,8 @@ public class PicWeixinButton extends SingleButton {
*/
private String key;

public PicWeixinButton() {
}

public PicWeixinButton(String key) {
this.key = key;
}

public PicWeixinButton(String name, String key) {
this.setName(name);
super(name);
this.key = key;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,8 @@ public class ScancodePushButton extends SingleButton {
*/
private String key;

public ScancodePushButton() {
}

public ScancodePushButton(String key) {
this.key = key;
}

public ScancodePushButton(String name, String key) {
this.setName(name);
super(name);
this.key = key;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,8 @@ public class ScancodeWaitMsgButton extends SingleButton {
*/
private String key;

public ScancodeWaitMsgButton() {
}

public ScancodeWaitMsgButton(String key) {
this.key = key;
}

public ScancodeWaitMsgButton(String name, String key) {
this.setName(name);
super(name);
this.key = key;
}

Expand Down
11 changes: 4 additions & 7 deletions weixin4j/src/main/java/org/weixin4j/model/menu/SingleButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,15 @@
*/
public class SingleButton extends BaseButton {

public SingleButton() {
}

public SingleButton(String name) {
this.setName(name);
}

/**
* 子菜单(此菜单需要手动添加,所以get和set方法能喝微信返回的json一致)
*/
private List<SingleButton> subButton;

public SingleButton(String name) {
super(name);
}

/**
* 设置 子菜单
*
Expand Down
Loading

0 comments on commit e449b34

Please sign in to comment.