上節講了調用微信公衆平台擷取Access token,有了Access token就可以進行平台接口測試了。
當然在此之前還需要自己的測試号來檢視自己接口調用的效果。測試号申請如下:
進入測試号,裡面有一個測試号二維碼,手機掃碼即可浏覽測試号情況
這樣就有了一個空白的測試公衆号了
自定義菜單開發
參考微信接口文檔可以看到按鈕的類型有很多(click點選推事件、view跳轉 URL、scancode_push:掃碼推事件、scancode_waitmsg:掃碼推事件且彈出“消息接收中”、pic_sysphoto:彈出系統拍照發圖、pic_photo_or_album:彈出拍照或者相冊發圖、pic_weixin:彈出微信相冊發圖器、location_select:彈出地理位置選擇器、media_id:下發消息(除文本消息)。。。。)
根據請求執行個體可以看出 url需要攜帶access_token(後面所有必然都要攜帶) 并上送json格式請求資料
1.json請求格式上送資料測試
HttpClientUtils請求工具類
/**
* 封裝HTTP POST方法
*
* @param
* @param (如JSON串)
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String post(String url, String data) throws ClientProtocolException, IOException {
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
//設定請求和傳輸逾時時間
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-Type", "text/json; charset=utf-8");
httpPost.setEntity(new StringEntity(data, "UTF-8"));
HttpResponse response = httpClient.execute(httpPost);
String httpEntityContent = getHttpEntityContent(response);
httpPost.abort();
return httpEntityContent;
}
常量類WeCharConstant增加 建立菜單請求url常量
/**
* 建立菜單URL
*/
public static final String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
菜單:MenuService
import cn.org.spring.common.util.HttpClientUtils;
import com.ctsi.sddx.constants.WeCharConstant;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
/**
* @Author : lizzu
* @create 2022/9/25 15:19
*/
@Service
public class MenuService {
private static final String CREATE_URL = WeCharConstant.CREATE_MENU_URL;
@Resource
private AccessTokenService accessTokenService;
/**
* 建立菜單
*
* @param json
* @throws IOException
*/
public String createMenu(String json) throws IOException {
return sendPost(json);
}
/**
* 發送POST請求
*
* @param data 請求資料
* @return
* @throws IOException
*/
private String sendPost(String data) throws IOException {
return HttpClientUtils.post(CREATE_URL.replace("ACCESS_TOKEN", accessTokenService.getAccessToken()), data);
}
}
MenuController
import java.io.IOException;
/**
* @Author : lizzu
* @create 2022/9/25 15:24
*/
@RestController
@RequestMapping("/v1/weChart")
public class MenuController {
@Autowired
MenuService menuService;
@GetMapping("/createMenuToJson")
public String createMenuToJson(String json) throws IOException {
return menuService.createMenu(json);
}
@GetMapping("/deleteMenu")
public String deleteMenu() throws IOException {
return menuService.deleteMenu();
}
}
上送json封包:
{
"button": [
{
"key": "20",
"name": "拍照",
"sub_button": [
{
"name": "搜尋",
"type": "view",
"url": "http://www.soso/com/"
},
{
"name": "搜尋1",
"type": "view",
"url": "http://www.baidu.com/"
}
],
"type": "click"
},
{
"key": "20",
"name": "個人中心",
"sub_button": [
{
"name": "百度",
"type": "view",
"url": "http://www.baidu.com/"
}
],
"type": "click"
},
{
"key": "20",
"name": "我的會員",
"type": "click"
}
]
}
傳回
{
"errcode": 0,
"errmsg": "ok"
}
檢視效果