|
@ -0,0 +1,78 @@
|
|
|
package com.yihu.jw.hospital.endpoint.wechat;
|
|
|
|
|
|
import com.yihu.jw.entity.base.wx.WxAccessTokenDO;
|
|
|
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
|
|
|
import com.yihu.jw.util.wechat.wxhttp.HttpUtil;
|
|
|
import com.yihu.jw.wechat.service.WxAccessTokenService;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import org.json.JSONObject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
|
/**
|
|
|
* Created by chenweida on 2017/8/4.
|
|
|
*/
|
|
|
@RestController
|
|
|
@RequestMapping(value = "/wechat/menu", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
|
|
@Api(description = "微信菜单相关管理")
|
|
|
public class WechatMenuController extends EnvelopRestEndpoint {
|
|
|
|
|
|
private static String wechat_token ="xAeQrX7uOD0OusIZ5JUQzrLPYIQBlqbS" ;
|
|
|
@Autowired
|
|
|
private HttpUtil httpUtil;
|
|
|
@Autowired
|
|
|
private WxAccessTokenService wxAccessTokenService;
|
|
|
@Value("${wechat.id}")
|
|
|
private String wechatId;
|
|
|
|
|
|
/**
|
|
|
* 微信菜单创建
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
@ApiOperation(value = "微信菜单创建")
|
|
|
@RequestMapping(value = "/menu/create", method = RequestMethod.GET)
|
|
|
public String createMenuTest() {
|
|
|
try {
|
|
|
WxAccessTokenDO wxAccessTokenDO = wxAccessTokenService.getWxAccessTokenById(wechatId);
|
|
|
String filePath = WechatCoreController.class.getResource("/").getPath() +
|
|
|
File.separator + "wechat" + File.separator + "weixin_menu.txt";
|
|
|
String url = " https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + wxAccessTokenDO.getAccessToken();
|
|
|
// 读取微信菜单配置文件
|
|
|
InputStreamReader reader = new InputStreamReader(new FileInputStream(filePath), "utf-8");
|
|
|
BufferedReader bufferedReader = new BufferedReader(reader);
|
|
|
String params = "";
|
|
|
String readTxt = "";
|
|
|
// 读取微信菜单
|
|
|
while ((readTxt = bufferedReader.readLine()) != null) {
|
|
|
System.out.println(readTxt);
|
|
|
params += readTxt;
|
|
|
}
|
|
|
|
|
|
bufferedReader.close();
|
|
|
reader.close();
|
|
|
// 请求微信接口创建菜单
|
|
|
String jsonStr = httpUtil.sendPost(url, params);
|
|
|
JSONObject result = new JSONObject(jsonStr);
|
|
|
if (result != null && result.get("errcode").toString().equals("0") && result.getString("errmsg").equals("ok")) {
|
|
|
return "创建成功!";
|
|
|
} else {
|
|
|
return "创建失败!";
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
return "创建失败";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|