package dev.xt.example.client.simplenews;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import com.kingdee.eas.mobile.xt.pa.MessageModelEnum;
import com.kingdee.eas.mobile.xt.pa.MessageTypeEnum;
import com.kingdee.eas.mobile.xt.util.FileUtil;
import com.kingdee.eas.mobile.xt.util.StringUtil;
import com.kingdee.eas.mobile.xt.pa.api.IMessageAdapter;
/**
*
* 讯通公共号SDK开发范例
* 简单新闻适配器
* 通过读取news.txt来获取新闻数据
*
*/
public class SimpleNewsAdapter implements IMessageAdapter {
//读取新闻数据的配置文件
private Properties newsfile = new Properties();
public SimpleNewsAdapter() throws IOException{
//读取news.txt文件
FileInputStream fis = new FileInputStream("news.txt");
newsfile.load(fis);
}
/**
* 获取讯通消息类型
* 对应于讯通公共号开放API接口中的type
*/
public MessageTypeEnum getType() {
//这个例子中使用图文类型
//在实际开发中,根据新闻的实际需要确定类型
return MessageTypeEnum.GRAPHIC;
}
/**
* 获取讯通排版展现模板
* 对应于讯通公共号开放API接口中的msg.model
*/
public MessageModelEnum getModel() {
//这个例子中使用单条图文混排模板
//在实际开发中,根据新闻的实际需要确定模板
return MessageModelEnum.SINGLEGRAPHIC;
}
/**
* 获取新闻更新时间
* 对应于讯通公共号开放API接口中的msg.list.date
*/
public Date getUpdateTime() {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = null;
try {
date = format.parse(newsfile.getProperty("updatetime"));
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 获取接收新闻的用户ID
* 对应于讯通公共号开放API接口中的to.user
*/
public List getUsers() {
String userid = StringUtil.stringChangeCode(newsfile.getProperty("users"));
String[] users = userid.split(",");
List list = new ArrayList();
for (int i = 0 ; i
list.add(users[i]);
}
return list;
}
/**
* 获取内容
*/
public Map getContent() {
Map message = new HashMap();
message.put("date", new SimpleDateFormat("yyyy-MM-dd").format(getUpdateTime()));
message.put("title", getTitle());
message.put("text", getSummary());
message.put("zip", StringUtil.getBase64FromByte(getFullPackage()));
message.put("url", "http://xt.kingdee.com");
message.put("name", getPictureName(true));
message.put("pic", StringUtil.getBase64FromByte(getPictureData()));
return message;
}
/**
* 是否需要推送讯通
*/
public boolean isNeedSendXT() {
//这个例子中需推送讯通
//在实际开发中,根据新闻的实际需要确定是否需要推送讯通
return true;
}
/**
* 清除临时文件
*/
public void clearTemporary() {
//这个例子中无须清除临时文件
}
/**
* 获取新闻ID
* 可以用作getFullPackage的唯一识别符参数
*/
private String getID() {
return StringUtil.stringChangeCode(newsfile.getProperty("id"));
}
/**
* 获取新闻标题
* 对应于讯通公共号开放API接口中的msg.list.title
*/
private String getTitle() {
return StringUtil.stringChangeCode(newsfile.getProperty("title"));
}
/**
* 获取新闻摘要
* 对应于讯通公共号开放API接口中的msg.list.text
*/
private String getSummary() {
return StringUtil.stringChangeCode(newsfile.getProperty("summary"));
}
/**
* 获取图片的文件名
* 对应于讯通公共号开放API接口中的msg.list.name
*/
private String getPictureName(boolean includeExtension) {
String path = "";
path = StringUtil.stringChangeCode(newsfile.getProperty("pic-path"));
File pic = new File(path);
String name = pic.getName();
if (!includeExtension)
name = name.substring(0,name.lastIndexOf("."));
return name;
}
/**
* 获取图片的二进制字节流
* 对应于讯通公共号开放API接口中的msg.list.pic
*/
private byte[] getPictureData() {
try {
String path = StringUtil.stringChangeCode(newsfile.getProperty("pic-path"));
FileInputStream fis = new FileInputStream(path);
if (fis != null) {
int len = fis.available();
byte[] in = new byte[len];
fis.read(in);
fis.close();
return in;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取内容压缩包
* 对应于讯通公共号开放API接口中的msg.list.zip
*/
private byte[] getFullPackage() {
byte[] packageFile = null;
try {
String newsPath = System.getProperty("user.dir");
newsPath = newsPath + "\\news";
String uidPath = StringUtil.encoder(getID());
if (uidPath.indexOf("%") > -1) {
uidPath = uidPath.replaceAll("%", "9");
}
uidPath = "\\" + uidPath;
String packagePath = newsPath + uidPath;
String contentPath = packagePath + "\\content";
//压缩打包
packageFile = FileUtil.readFile(FileUtil.compress(contentPath, packagePath + "\\package.zip"));
} catch (IOException e) {
e.printStackTrace();
}
return packageFile;
}
}
package dev.xt.example.client.simplenews;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import com.kingdee.eas.mobile.xt.pa.PubAcctConfig;
import com.kingdee.eas.mobile.xt.pa.PubAcctController;
import com.kingdee.eas.mobile.xt.pa.api.IMessageAdapter;
/**
*
* 讯通公共号SDK开发范例
* 讯通新闻客户端
*
*/
public class XTNewsClient {
public XTNewsClient(){
}
public static void main(String[] args) {
//读取新闻数据的配置文件
Properties newsfile = new Properties();
//读取news.txt文件
try {
FileInputStream fis = new FileInputStream("news.txt");
newsfile.load(fis);
} catch (IOException e) {
e.printStackTrace();
}
//将配置文件目录指向客户端所在路径
System.setProperty("xt.properties.dir",System.getProperty("user.dir")+"/mobile");
try{
//推送新闻
IMessageAdapter news=new SimpleNewsAdapter();
boolean isPushSuccessed = PubAcctController.push(news,
newsfile.getProperty("pubacct"),newsfile.getProperty("pubacckey"),newsfile.getProperty("code"));
//返回结果
if (isPushSuccessed){
System.out.print("已发送成功");
}else{
System.out.print("发送失败,原因:\n" + PubAcctController.getRefrenceInfo().substring(0,200));
}
}catch(Exception e){
System.out.print("发送失败,原因:\n" + e.getMessage());
}
}
}