|
@@ -1,306 +0,0 @@
|
|
|
-package beans.wechatIndex;
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSONArray;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.sun.org.apache.regexp.internal.RE;
|
|
|
-import common.BaseClass;
|
|
|
-import common.YosException;
|
|
|
-import common.data.*;
|
|
|
-import common.data.db.DBConnect;
|
|
|
-import utility.tools.WebRequest;
|
|
|
-import utility.wechat.miniprogram.WechatUserID;
|
|
|
-import utility.wechat.work.receive.core.CreateMsg;
|
|
|
-
|
|
|
-import java.util.Calendar;
|
|
|
-import java.util.HashMap;
|
|
|
-
|
|
|
-/**
|
|
|
- * 微信公众号
|
|
|
- */
|
|
|
-public class WechatService extends WechatServiceMsgRecevier {
|
|
|
-
|
|
|
- //有效期2小时,需定时刷新,重复获取将导致上次获取的失效
|
|
|
- private static HashMap<String, String> access_tokenMap = new HashMap<>();
|
|
|
- private static HashMap<String, Long> access_token_timelimitMap = new HashMap<>();
|
|
|
-
|
|
|
- String appid;
|
|
|
- String secret;
|
|
|
- String systemclient;
|
|
|
-
|
|
|
- private static CreateMsg createMsg = new CreateMsg();
|
|
|
-
|
|
|
- /**
|
|
|
- * 构造函数
|
|
|
- *
|
|
|
- * @param systemclient 系统注册的微信应用操作端
|
|
|
- */
|
|
|
- public WechatService(String systemclient) throws YosException {
|
|
|
- Rows rows = new DBConnect().runSqlQuery("select appid,secret from sys_wechatapp where systemclient='" + systemclient + "'");
|
|
|
- if (rows.isNotEmpty()) {
|
|
|
- this.appid = rows.get(0).getString("appid");
|
|
|
- this.secret = rows.get(0).getString("secret");
|
|
|
- }
|
|
|
- this.systemclient = systemclient;
|
|
|
- }
|
|
|
-
|
|
|
- private String getAccessToken() throws YosException {
|
|
|
- long nowtime = Calendar.getInstance().getTimeInMillis();
|
|
|
- if (!access_tokenMap.containsKey(systemclient) || access_tokenMap.get(systemclient).equals("") || !access_token_timelimitMap.containsKey(systemclient) || access_token_timelimitMap.get(systemclient) < nowtime) {
|
|
|
- String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + this.appid + "&secret=" + this.secret;
|
|
|
- String res = new WebRequest().doGet(url);
|
|
|
- JSONObject resobject = JSONObject.parseObject(res);
|
|
|
- if (resobject.containsKey("access_token")) {
|
|
|
- access_tokenMap.put(systemclient, resobject.getString("access_token"));
|
|
|
- access_token_timelimitMap.put(systemclient, nowtime + 1000 * resobject.getIntValue("expires_in"));
|
|
|
- } else {
|
|
|
- access_tokenMap.put(systemclient, "");
|
|
|
- }
|
|
|
- }
|
|
|
- return access_tokenMap.get(systemclient);
|
|
|
- }
|
|
|
-
|
|
|
- public JSONArray getTemplate_List() throws YosException {
|
|
|
- String url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=" + getAccessToken();
|
|
|
- String result = new WebRequest().doGet(url);
|
|
|
- JSONObject object = JSONObject.parseObject(result);
|
|
|
- if (object.getStringValue("errcode").equals("40001")) {
|
|
|
- access_token_timelimitMap.remove(systemclient);
|
|
|
- }
|
|
|
- if (object.containsKey("template_list")) {
|
|
|
- return object.getJSONArray("template_list");
|
|
|
- }
|
|
|
- return new JSONArray();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param userid
|
|
|
- * @param template_id
|
|
|
- * @param client_msg_id 本地消息ID
|
|
|
- * @param tourl
|
|
|
- * @param datamap
|
|
|
- * @param tominiprogram_appid
|
|
|
- * @param tominiprogram_pagepath
|
|
|
- * @throws YosException
|
|
|
- */
|
|
|
- public void send(long userid, String template_id, String client_msg_id, String tourl, HashMap<String, String> datamap, String tominiprogram_appid, String tominiprogram_pagepath) throws YosException {
|
|
|
- Rows rows = new DBConnect().runSqlQuery("select openid from sys_wechatapp_openids where systemclient='" + systemclient + "' and userid=" + userid);
|
|
|
- for (Row row : rows) {
|
|
|
- JSONObject object = new JSONObject();
|
|
|
- object.put("touser", row.getString("openid"));
|
|
|
- object.put("template_id", template_id);
|
|
|
- if (!tourl.equals("")) {
|
|
|
- object.put("tourl", tourl);
|
|
|
- }
|
|
|
- if (!tominiprogram_appid.equals("") && !tominiprogram_pagepath.equals("")) {
|
|
|
- JSONObject miniprogramObject = new JSONObject();
|
|
|
- miniprogramObject.put("appid", tominiprogram_appid);
|
|
|
- miniprogramObject.put("pagepath", tominiprogram_pagepath);
|
|
|
- object.put("miniprogram", miniprogramObject);
|
|
|
- }
|
|
|
- object.put("client_msg_id", client_msg_id);
|
|
|
- JSONObject dataobject = new JSONObject();
|
|
|
- for (String keyword : datamap.keySet()) {
|
|
|
- JSONObject keyObject = new JSONObject();
|
|
|
- keyObject.put("value", datamap.get(keyword));
|
|
|
- dataobject.put(keyword, keyObject);
|
|
|
- }
|
|
|
- object.put("data", dataobject);
|
|
|
-
|
|
|
- String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + getAccessToken();
|
|
|
- new WebRequest().doPost(object.toString(), url);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据code获取微信的openid和unionid
|
|
|
- *
|
|
|
- * @param wechat_code
|
|
|
- * @return
|
|
|
- */
|
|
|
- public WechatUserID getWechatUserID(String wechat_code) throws YosException {
|
|
|
- WechatUserID wechatUserID = new WechatUserID();
|
|
|
- WebRequest request = new WebRequest();
|
|
|
- String result = request.doGet("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + wechat_code + "&grant_type=authorization_code");
|
|
|
- JSONObject openidobject = JSONObject.parseObject(result);
|
|
|
- wechatUserID.setSucc(openidobject.containsKey("openid"));
|
|
|
- if (openidobject.containsKey("openid")) {
|
|
|
- wechatUserID.setOpenid(openidobject.getString("openid"));
|
|
|
-
|
|
|
- String result2 = request.doGet("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + getAccessToken() + "&openid=" + openidobject.getString("openid") + "&lang=zh_CN");
|
|
|
- JSONObject unionidobject = JSONObject.parseObject(result2);
|
|
|
-
|
|
|
- if (unionidobject.containsKey("unionid")) {
|
|
|
- wechatUserID.setUnionid(unionidobject.getString("unionid"));
|
|
|
- }
|
|
|
- }
|
|
|
- wechatUserID.setResult(result);
|
|
|
- return wechatUserID;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- @Override
|
|
|
- public String switchTextMsg(String Content, String FromUserName,
|
|
|
- String ToUserName) {
|
|
|
- return createMsg.createTextMsg(Content, FromUserName,
|
|
|
- ToUserName);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String switchClickEventMsg(String EventKey, String FromUserName,
|
|
|
- String ToUserName) {
|
|
|
- return createMsg.createTextMsg("您点击的按钮键值是:" + EventKey,
|
|
|
- FromUserName, ToUserName);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String switchImageMsg(String PicUrl, String MediaId,
|
|
|
- String FromUserName, String ToUserName) {
|
|
|
- return createMsg.createTextMsg("图片地址:" + PicUrl, FromUserName,
|
|
|
- ToUserName);
|
|
|
- /*
|
|
|
- * String savefile = Parameter.SaveFile.Image + FromUserName + "_" +
|
|
|
- * Calendar.getInstance().getTimeInMillis() + ".jpg";// 文件保存地址 DownLoad
|
|
|
- * downLoad = new DownLoad(MediaId, savefile); return
|
|
|
- * createMsg.createTextMsg("图片上传成功\n大小:" + downLoad.getfilesize() +
|
|
|
- * "\n网络地址:" + PicUrl, FromUserName, ToUserName);
|
|
|
- */
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String switchVoiceMsg(String MediaId, String Format,
|
|
|
- String FromUserName, String ToUserName) {
|
|
|
- return createMsg.createTextMsg("媒体ID:" + MediaId, FromUserName,
|
|
|
- ToUserName);
|
|
|
- /*
|
|
|
- * String savefile = Parameter.SaveFile.Voice + FromUserName + "_" +
|
|
|
- * Calendar.getInstance().getTimeInMillis() + "." + Format;// 文件保存地址
|
|
|
- * DownLoad downLoad = new DownLoad(MediaId, savefile); return
|
|
|
- * createMsg.createTextMsg("音频上传成功\n格式:" + Format + "\n大小:" +
|
|
|
- * downLoad.getfilesize(), FromUserName, ToUserName);
|
|
|
- */
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String switchVideoMsg(String MediaId, String ThumbMediaId,
|
|
|
- String FromUserName, String ToUserName) {
|
|
|
- return createMsg.createTextMsg("媒体ID:" + MediaId, FromUserName,
|
|
|
- ToUserName);
|
|
|
- /*
|
|
|
- * String savefile = Parameter.SaveFile.video + FromUserName + "_" +
|
|
|
- * Calendar.getInstance().getTimeInMillis() + ".rm";// 文件保存地址 DownLoad
|
|
|
- * downLoad = new DownLoad(MediaId, savefile); return
|
|
|
- * createMsg.createTextMsg("视频上传成功\n大小:" + downLoad.getfilesize(),
|
|
|
- * FromUserName, ToUserName);
|
|
|
- */
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String switchShortvideoMsg(String MediaId, String ThumbMediaId,
|
|
|
- String FromUserName, String ToUserName) {
|
|
|
- return createMsg.createTextMsg("媒体ID:" + MediaId, FromUserName,
|
|
|
- ToUserName);
|
|
|
- /*
|
|
|
- * String savefile = Parameter.SaveFile.shortvideo + FromUserName + "_"
|
|
|
- * + Calendar.getInstance().getTimeInMillis() + ".rm";// 文件保存地址 DownLoad
|
|
|
- * downLoad = new DownLoad(MediaId, savefile);
|
|
|
- *
|
|
|
- * return createMsg.createTextMsg("小视频上传成功\n大小:" +
|
|
|
- * downLoad.getfilesize(), FromUserName, ToUserName);
|
|
|
- */
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String switchLocationMsg(String Location_X, String Location_Y,
|
|
|
- String Scale, String Label, String FromUserName, String ToUserName) {
|
|
|
- return createMsg.createTextMsg("纬度:" + Location_X + "\n经度:"
|
|
|
- + Location_Y + "\n位置描述:" + Label, FromUserName, ToUserName);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 成员关注/取消关注事件
|
|
|
- */
|
|
|
- @Override
|
|
|
- public String switchSubscribeEventMsg(String FromUserName, String ToUserName, String eventtype) {
|
|
|
- CreateMsg createMsg = new CreateMsg();
|
|
|
- BaseClass controller = new BaseClass();
|
|
|
- try {
|
|
|
- if (eventtype.equalsIgnoreCase("subscribe")) {
|
|
|
- Rows rows1 = controller.dbConnect.runSqlQuery("select * from sys_wechatapp_openids where openid='" + FromUserName + "'");
|
|
|
- if (rows1.isEmpty()) {
|
|
|
- return createMsg.createTextMsg("此微信未关联E订单系统,请先关联E订单系统", FromUserName, ToUserName);
|
|
|
- }
|
|
|
- long sys_wechatapp_openids = controller.createTableID("sys_wechatapp_openids");
|
|
|
- InsertSQL insert = SQLFactory.createInsertSQL(controller.dbConnect, "sys_wechatapp_openids");
|
|
|
- insert.setValue("wechatapp_openidsid", sys_wechatapp_openids);
|
|
|
- insert.setValue("systemclient", systemclient);
|
|
|
- insert.setValue("openid", FromUserName);
|
|
|
- insert.setValue("userid", rows1.get(0).getLong("userid"));
|
|
|
- insert.setValue("userinfo", rows1.get(0).getString("userinfo"));
|
|
|
- insert.insert();
|
|
|
- return createMsg.createTextMsg("绑定成功", FromUserName, ToUserName);
|
|
|
- } else {
|
|
|
- DeleteSQL deleteSQL = SQLFactory.createDeleteSQL(controller.dbConnect, "sys_wechatapp_openids");
|
|
|
- deleteSQL.setWhere("systemclient", systemclient);
|
|
|
- deleteSQL.setWhere("openid", FromUserName);
|
|
|
- deleteSQL.delete();
|
|
|
- return "";
|
|
|
- }
|
|
|
- } catch (YosException e) {
|
|
|
- System.out.println(e.getMessage());
|
|
|
- return "";
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String switchViewEventMsg(String EventKey, String FromUserName,
|
|
|
- String ToUserName) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String switchScancode_pushEventMsg(String EventKey, String ScanType,
|
|
|
- String ScanResult, String FromUserName, String ToUserName) {
|
|
|
-
|
|
|
- String strs[] = ScanResult.split(",");
|
|
|
- return createMsg.createTextMsg("类型:" + ScanType + "\n条码格式:" + strs[0]
|
|
|
- + "\n条码内容:" + strs[1], FromUserName, ToUserName);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String switchScancode_waitmsgEventMsg(String EventKey,
|
|
|
- String ScanType, String ScanResult, String FromUserName,
|
|
|
- String ToUserName) {
|
|
|
-
|
|
|
- if (ScanType.equals("barcode")) {
|
|
|
- String strs[] = ScanResult.split(",");
|
|
|
- return createMsg.createTextMsg("类型:" + ScanType + "\n条码格式:"
|
|
|
- + strs[0] + "\n条码内容:" + strs[1], FromUserName, ToUserName);
|
|
|
- } else {
|
|
|
- return createMsg.createTextMsg("类型:" + ScanType + "\n条码内容:"
|
|
|
- + ScanResult, FromUserName, ToUserName);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String switchEnter_agentEventMsg(String EventKey,
|
|
|
- String FromUserName, String ToUserName) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 位置事件
|
|
|
- */
|
|
|
- @Override
|
|
|
- public String switchLocationEventMsg(String Latitude,
|
|
|
- String Longitude, String Precision, String FromUserName,
|
|
|
- String ToUserName) {
|
|
|
- return createMsg.createTextMsg("您当前所处的位置是\n纬度:" + Latitude + "\n经度:"
|
|
|
- + Longitude, FromUserName, ToUserName);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-}
|