hz-zhhq-app-service/greenH5modul/.svn/pristine/05/05d0de271166df51c8e288f59d6...

251 lines
12 KiB
Plaintext
Raw Normal View History

2025-01-21 13:12:35 +08:00
package com.jysoft.weChat.service;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jysoft.AccessTokenServlet;
import com.jysoft.weChat.util.HttpUtil;
import com.jysoft.weChat.util.MessageUtil;
import com.jysoft.weChat.util.Wechatconfig;
import com.jysoft.weChat.vo.ContentVo;
import com.jysoft.weChat.vo.TemplateMessageVo;
import com.nationalelectirc.Constant.Constant;
import com.nationalelectirc.utils.RestResult;
import com.nationalelectirc.utils.Utils;
import com.nationalelectric.greenH5.utils.SmsUtils;
import com.sgcc.uap.persistence.IHibernateDao;
import com.sgcc.uap.persistence.util.SqlFileUtil;
import net.sf.json.JSONObject;
/**
* @author yeping
* @date 2019-10-15
* @功能 微信服务号功能集中管理类
*/
@Service
public class WechatService {
@Autowired
private IHibernateDao hibernateDao;
/******************************************* 模板推送功能 ************************************************************/
/**
* 模板消息推送功能
*
* @param alarmResult
*
* @throws Exception
*/
// public void sendTemplateMessage(IFResult alarmResult) throws Exception {
//
// List<Map<String, String>> openidList = findUserOpnid();
//
// String access_token = Wechatconfig.accessToken.getTokenName();
// String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
//
// Map<String, ContentVo> data = new HashMap<String, ContentVo>();
//
// data.put("first", new ContentVo(alarmResult.getContent(), "#173177"));
// data.put("keyword2", new ContentVo(alarmResult.getDate(), "#black"));
// data.put("keyword1", new ContentVo(alarmResult.getAlarmType(), "#black"));
// data.put("remark", new ContentVo("查看详情", "#173177"));
// // xp0b_vaeeWC2I6SM6FvC0kBoocRrhyJBK41HenGmUhw 模板ID
//
// for (Map<String, String> one : openidList) {
// String openId = ParseUtil.parseText(one.get("OPEN_ID"));
// if (openId.length() > 0) {
// TemplateMessageVo temp = new TemplateMessageVo(openId, "xp0b_vaeeWC2I6SM6FvC0kBoocRrhyJBK41HenGmUhw", Wechatconfig.DNS + Wechatconfig.messageUrl + "?warnId=" + alarmResult.getAlarmID(), "#173177", data);
// temp.setData(data);
// String jsonString = JSONObject.fromObject(temp).toString().replace("day", "Day");
// String result = HttpUtil.postHttpsResponse(url, jsonString);
// System.out.println(result);
// }
// }
// }
// 需要推送预警用户(推送给所有注册用户)
// TODO
@SuppressWarnings({ "unchecked", "deprecation" })
public List<Map<String, String>> findUserOpnid() {
List<Map<String, String>> list = hibernateDao.queryForListWithSql(SqlFileUtil.get("findOpenid"));
return list;
}
/******************************************* 微信注册页面功能 ************************************************************/
/**
* @author YEP
* @date 2019-11-01
* @desc 保存用户信息
*/
public RestResult saveWechatUser(String name, String phone, String openid, String IDNumber, String headimgUrl) {
// 验证验证码时效性
// Map<String, Object> map = (Map<String, Object>) request.getSession().getAttribute(yzmid);
// long time = ParseUtil.parseLong(map.get("time"));
// if (new Date().getTime() - time > 60000) {
//
// return new RestResult("0", "验证码过期");
// }
// String oldCode = ParseUtil.parseText(map.get("code"));
// if (!yzm.equals(oldCode)) {
// return new RestResult("0", "验证码输入不正确");
// }
// 根据手机号码查找用户查找到用户后将用户的openid保存到数据库
String findOneSql = "select count(1) from green_user_info a where a.is_deleted='N' and a.idnumber=?";
System.out.println(findOneSql);
int count = hibernateDao.queryForIntWithSql(findOneSql,new Object[] {IDNumber});
if (count > 0) {
// 更新用户信息
String updateUserSql = "update green_user_info set wxopenid=?, mobile=?,real_name=?,wx_profile=?,gmt_modified=sysdate() where idnumber=? and is_deleted='N'";
hibernateDao.executeSqlUpdate(updateUserSql, new Object[] { openid, phone, name, IDNumber ,headimgUrl});
} else {
// 插入用户信息
String insertUserSql = "insert into green_user_info (id,real_name,mobile,idnumber,wxopenid,wx_profile,is_deleted,gmt_created) values (?,?,?,?,?,?,'N',sysdate())";
String id = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
hibernateDao.executeSqlUpdate(insertUserSql, new Object[] { id, name, phone, IDNumber, openid ,headimgUrl});
}
return new RestResult("1", "用户注册成功");
}
@SuppressWarnings({ "deprecation", "unchecked" })
public Map<String, Object> findUserByOpenid(String openid) {
Map<String, Object> rMap = hibernateDao.queryForMapWithSql(SqlFileUtil.get("findUserByOpenid"), new Object[] { openid });
return rMap;
}
public RestResult getCodeInfo(String phone) {
// 验证码
String code = MessageUtil.getVerifyCode();
String key = UUID.randomUUID().toString().replaceAll("-","");
try {
Map<String, Object> res = new HashMap<String, Object>();
res.put("time", new Date().getTime());
res.put("code", code);
Utils.map.put(key, res);
//session.setAttribute(key, res);
// 短信发送
String content = new String("验证码:" + code + "。如非本人操作,请忽略此短信。");
String state = SmsUtils.sendSms(phone, content);
System.out.println(code);
return new RestResult(state, key);
} catch (Exception e) {
e.printStackTrace();
return new RestResult(Constant.FAILED, key);
}
}
/**
* 模板消息推送功能
*
* @param data:推送内容tempId推送模板ID;openid 推送者openiddetailUrl详细信息条状urlcolor跳转详情url
*
* @throws Exception
*/
public void sendTemplateMessage(Map<String, ContentVo> data, String tempId, String openid, String detailUrl, String color) throws Exception {
String access_token = Wechatconfig.accessToken.getTokenName();
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
if (openid.length() > 0) {
TemplateMessageVo temp = new TemplateMessageVo(openid, tempId, detailUrl, color, data);
temp.setData(data);
String jsonString = JSONObject.fromObject(temp).toString().replace("day", "Day");
String result = HttpUtil.postHttpsResponse(url, jsonString);
System.out.println(result);
}
}
// 模版消息推送公用版
public void publishMsg(Map<String, ContentVo> data, String toUserOpenId, Integer type) throws Exception {
// String tempId = ""; // 模版id
// String detailUrl = Wechatconfig.DETAIL_URL; // 详情地址
// if (type.equals(1)) { // 访客申请 推送给 被访人
// tempId = Wechatconfig.VISITOR_REQ_TEMP;
// detailUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + Wechatconfig.appId + "&response_type=code" + "&scope=snsapi_userinfo" + "&redirect_uri="//
// //+ URLEncoder.encode(Wechatconfig.DNS + Wechatconfig.fksqUrl+"?openId='"+toUserOpenId+"'") + "&state=STATE#wechat_redirect";
// + URLEncoder.encode(Wechatconfig.DNS + Wechatconfig.zhhqUrl) + "&state=STATE#wechat_redirect";
// } else if (type.equals(2)) { // 被访人审核后 通知 访客审核结果
// tempId = Wechatconfig.VISITOR_RES_TEMP;
// detailUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + Wechatconfig.appId + "&response_type=code" + "&scope=snsapi_userinfo" + "&redirect_uri="//
// + URLEncoder.encode(Wechatconfig.DNS + Wechatconfig.zhhqUrl) + "&state=STATE#wechat_redirect";
// } else if (type.equals(3)) {
// tempId = Wechatconfig.USER_UPD_TEMP; // 用户注册成功 通知 管理员审核
// detailUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + Wechatconfig.appId + "&response_type=code" + "&scope=snsapi_userinfo" + "&redirect_uri="//
// + URLEncoder.encode(Wechatconfig.DNS + Wechatconfig.zhhqUrl) + "&state=STATE#wechat_redirect";
// }
// else if (type.equals(4)) {
// tempId = Wechatconfig.CAR_PARK_TIP; // 用户注册成功 通知 管理员审核
// detailUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + Wechatconfig.appId + "&response_type=code" + "&scope=snsapi_userinfo" + "&redirect_uri="//
// + URLEncoder.encode(Wechatconfig.DNS + Wechatconfig.zhhqUrl) + "&state=STATE#wechat_redirect";
// }
// else if (type.equals(5)) {
// tempId = Wechatconfig.VISITOR_OVER; // 用户注册成功 通知 管理员审核
// detailUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + Wechatconfig.appId + "&response_type=code" + "&scope=snsapi_userinfo" + "&redirect_uri="//
// + URLEncoder.encode(Wechatconfig.DNS + Wechatconfig.zhhqUrl) + "&state=STATE#wechat_redirect";
// }else if(type.equals(6)){
// tempId = Wechatconfig.SERVICE_NOTICE;//服务通知 (理发、取车、评价)
// detailUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + Wechatconfig.appId + "&response_type=code" + "&scope=snsapi_userinfo" + "&redirect_uri="//
// + URLEncoder.encode(Wechatconfig.DNS + Wechatconfig.zhhqUrl) + "&state=STATE#wechat_redirect";
// }
// else if(type.equals(7)){
// tempId = Wechatconfig.PICKUP_GOODS;//服务通知 (理发、取车、评价)
// detailUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + Wechatconfig.appId + "&response_type=code" + "&scope=snsapi_userinfo" + "&redirect_uri="//
// + URLEncoder.encode(Wechatconfig.DNS + Wechatconfig.zhhqUrl) + "&state=STATE#wechat_redirect";
// }
// else if(type.equals(8)){
// tempId = Wechatconfig.CHANGE_APPOINTMENT;
// detailUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + Wechatconfig.appId + "&response_type=code" + "&scope=snsapi_userinfo" + "&redirect_uri="//
// + URLEncoder.encode(Wechatconfig.DNS + Wechatconfig.zhhqUrl) + "&state=STATE#wechat_redirect";
// }
// if (!tempId.equals("")) {
// String access_token = Wechatconfig.accessToken.getTokenName();
// if (access_token == null || access_token.equals("")) {
// Wechatconfig.accessToken = AccessTokenServlet.getAccessToken();
// access_token = Wechatconfig.accessToken.getTokenName();
// }
// String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
// if (toUserOpenId.length() > 0) {
// TemplateMessageVo temp = new TemplateMessageVo(toUserOpenId, tempId, detailUrl, "#173177", data);
// temp.setData(data);
// String jsonString = JSONObject.fromObject(temp).toString().replace("day", "Day");
// String result = HttpUtil.postHttpsResponse(url, jsonString);
//
// System.out.println(result);
// JSONObject rJsonObject = JSONObject.fromObject(result);
// if (rJsonObject.getInt("errcode") == 0) {
// System.out.println("success");
// } else if (rJsonObject.getInt("errcode") == 40001 || rJsonObject.getInt("errcode") == 41001) {
// System.out.println("token过期 重新获取 ");
// Wechatconfig.accessToken = AccessTokenServlet.getAccessToken();
// access_token = Wechatconfig.accessToken.getTokenName();
// url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
// TemplateMessageVo temp1 = new TemplateMessageVo(toUserOpenId, tempId, detailUrl, "#173177", data);
// temp.setData(data);
// String jsonString1 = JSONObject.fromObject(temp1).toString().replace("day", "Day");
// String result1 = HttpUtil.postHttpsResponse(url, jsonString1);
// System.out.println("token 重新获取后发送消息结果:"+result1);
// }
// }
// }
}
}