|
|
@@ -0,0 +1,102 @@
|
|
|
+package cn.xyh.wx.service;
|
|
|
+
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import cn.xyh.common.config.WxAppConfig;
|
|
|
+import cn.xyh.common.exception.user.UserException;
|
|
|
+import cn.xyh.wx.domain.dto.WxAccessTokenDto;
|
|
|
+import cn.xyh.wx.domain.vo.WxMssVo;
|
|
|
+import cn.xyh.wx.domain.vo.WxTemplateDataVo;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class WeChatService {
|
|
|
+
|
|
|
+ private final WxAppConfig wxAppConfig;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param access_token app的token
|
|
|
+ * @param openid 用户openid
|
|
|
+ * @param formId 表单ID
|
|
|
+ * @param templateId 模板ID
|
|
|
+ * @param keywords {与模板字段一一对应}
|
|
|
+ */
|
|
|
+ public String pushOneUser(String access_token, String openid, String formId, String templateId, String[] keywords) {
|
|
|
+
|
|
|
+ //如果access_token为空则从新获取
|
|
|
+ if (StringUtils.isEmpty(access_token)) {
|
|
|
+ access_token = getAccess_token();
|
|
|
+ }
|
|
|
+ String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/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, WxTemplateDataVo> m = new HashMap<>();
|
|
|
+
|
|
|
+ //封装数据
|
|
|
+ if (keywords.length > 0) {
|
|
|
+ for (int i = 1; i <= keywords.length; i++) {
|
|
|
+ WxTemplateDataVo keyword = new WxTemplateDataVo();
|
|
|
+ keyword.setValue(keywords[i - 1]);
|
|
|
+ m.put("keyword" + i, keyword);
|
|
|
+ }
|
|
|
+ wxMssVo.setData(m);
|
|
|
+ } else {
|
|
|
+ log.error("keywords长度为空");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String data = HttpUtil.post(url, (Map<String, Object>) wxMssVo);
|
|
|
+ log.info("小程序推送结果" + data);
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 获取access_token
|
|
|
+ * appid和appsecret到小程序后台获取,当然也可以让小程序开发人员给你传过来
|
|
|
+ * */
|
|
|
+ public String getAccess_token() {
|
|
|
+ //获取access_token
|
|
|
+ String url = cn.xyh.common.utils.StringUtils.format(
|
|
|
+ "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}", wxAppConfig.getAppId(), wxAppConfig.getAppSecret());
|
|
|
+
|
|
|
+ String data = HttpUtil.get(url);
|
|
|
+ log.info("微信获取获取openid和unionid,返回结果:{}", data);
|
|
|
+ try {
|
|
|
+ WxAccessTokenDto dto = JSONUtil.toBean(data, WxAccessTokenDto.class);
|
|
|
+ if (dto.getErrcode() != null) {
|
|
|
+ String msg = "获取access_token异常,代码:" + dto.getErrcode() + "," + dto.getErrmsg();
|
|
|
+ log.error(msg);
|
|
|
+ throw new UserException(msg);
|
|
|
+ }
|
|
|
+ return dto.getAccess_token();
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取access_token异常", e);
|
|
|
+ throw new UserException("获取access_token异常");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /* 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);
|
|
|
+ }*/
|
|
|
+}
|
|
|
+
|