package com.org.yxa.utils;
import com.alibaba.fastjson.JSONObject;
import com.org.frame.util.GlobalStatic;
import com.org.frame.util.SsdbUtils;
import com.org.frame.util.SysUtils;
import com.org.frame.util.ssdb.SSDB;
import com.org.yxa.utils.wxentity.TemplateDataVo;
import com.org.yxa.utils.wxentity.WxMssVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
public class WeChatService {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Autowired
private RestTemplate restTemplate;
/**
*
* @param access_token app的token
* @param openid 用户openid
* @param formId 表单ID
* @param templateId 模板ID
* @param keywords {与模板字段一一对应}
* @return
*/
public String pushOneUser(String openid, String templateId,String[] keywords) {
RaedProperties prop = new RaedProperties();
String get_time = prop.getProperty("get_time");
String access_token = prop.getProperty("access_token");
//prop.writeProperties("get_time", "1111111111111111");
//prop.writeProperties("access_token", "222222222222222");
//如果get_time为空则从新获取或者时间超过2小时
if(StringUtils.isEmpty(get_time)){
access_token = getAccess_token();
}else {
long nownum = System.currentTimeMillis();
long oldnum = Long.parseLong(get_time);
long num = nownum - oldnum;
if(num > 7000*1000) {
access_token = getAccess_token();
}
}
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send" +
"?access_token=" + access_token;
//拼接推送的模版
WxMssVo wxMssVo = new WxMssVo();
wxMssVo.setTouser(openid);//用户openid
//wxMssVo.setForm_id(formId);//formId
wxMssVo.setTemplate_id(templateId);//模版id
Map<String, TemplateDataVo> m = new HashMap<>();
//封装数据
if(keywords.length>0){
for(int i=1;i<=keywords.length;i++){
TemplateDataVo keyword = new TemplateDataVo();
keyword.setValue(keywords[i-1]);
if(i==1) {
m.put("thing3", keyword);
}else if(i==2) {
m.put("name1", keyword);
}else {
m.put("date2", keyword);
}
}
TemplateDataVo keyword = new TemplateDataVo();
keyword.setValue("通知中心");
m.put("thing4", keyword);
wxMssVo.setData(m);
}else{
System.out.print("keywords长度为空");
return null;
}
if(restTemplate==null){
restTemplate = new RestTemplate();
}
ResponseEntity<String> responseEntity =
restTemplate.postForEntity(url, wxMssVo, String.class);
System.out.print("小程序推送结果={}");
return responseEntity.getBody();
}
/*
* 获取access_token
* appid和appsecret到小程序后台获取,当然也可以让小程序开发人员给你传过来
* */
public String getAccess_token() {
//获取access_token
String appid = "wx3491e618eab9033e";
String appsecret = "8f50b93084d4513cff11294da173cfc7";
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
"&appid=" + appid + "&secret=" + appsecret;
if(restTemplate==null){
restTemplate = new RestTemplate();
}
String json = restTemplate.getForObject(url, String.class);
JSONObject myJson = JSONObject.parseObject(json);
RaedProperties prop = new RaedProperties();
prop.writeProperties("access_token", myJson.get("access_token").toString());
prop.writeProperties("get_time", System.currentTimeMillis()+"");
return myJson.get("access_token").toString();
}
// public static void main(String[] args) {
// System.out.println(new WeChatService().getAccess_token());
//
// WeChatService weChatUtil = new WeChatService();
// String values[] ={"Jack方","2019-5-8 10:10:10","xxx有限公司","JAVA开发","xx区xx广场xx号","请带好入职材料"};
// weChatUtil.pushOneUser(weChatUtil.getAccess_token()
// ,"o_fh25E0IufW7NIpezUReODfVH68","ec76b8b81cd04cf6b464bb0adf309d3b","zv0IsYDpJxgKWLHGUy8FEv0ajtJqkfhWTsFWiM7zzSU"
// ,values);
// }
}