|
|
@@ -1,30 +1,36 @@
|
|
|
package com.uas.platform.b2c.common.weixin.service.impl;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
import com.uas.platform.b2c.common.account.dao.UserDao;
|
|
|
import com.uas.platform.b2c.common.account.model.Enterprise;
|
|
|
import com.uas.platform.b2c.common.account.model.User;
|
|
|
import com.uas.platform.b2c.common.weixin.model.MessageModel;
|
|
|
-import com.uas.platform.b2c.common.weixin.model.TemplateData;
|
|
|
-import com.uas.platform.b2c.common.weixin.model.TemplateDataDetail;
|
|
|
-import com.uas.platform.b2c.common.weixin.model.TemplateMessage;
|
|
|
+import com.uas.platform.b2c.common.weixin.model.req.AuthTokenParams;
|
|
|
+import com.uas.platform.b2c.common.weixin.model.req.AuthUserParams;
|
|
|
+import com.uas.platform.b2c.common.weixin.model.req.WechatTemplateMsg;
|
|
|
+import com.uas.platform.b2c.common.weixin.model.resp.AccessToken;
|
|
|
+import com.uas.platform.b2c.common.weixin.model.resp.AuthAccessToken;
|
|
|
+import com.uas.platform.b2c.common.weixin.model.resp.AuthUserInfo;
|
|
|
+import com.uas.platform.b2c.common.weixin.model.resp.TemplateMsgResult;
|
|
|
import com.uas.platform.b2c.common.weixin.service.WeChatService;
|
|
|
+import com.uas.platform.b2c.common.weixin.util.HttpReqUtil;
|
|
|
import com.uas.platform.b2c.common.weixin.util.WeChatUtil;
|
|
|
import com.uas.platform.core.exception.IllegalOperatorException;
|
|
|
import com.uas.platform.core.util.serializer.FlexJsonUtils;
|
|
|
-import com.uas.sso.common.util.HttpUtil;
|
|
|
import com.uas.sso.entity.UserAccount;
|
|
|
import com.uas.sso.entity.UserView;
|
|
|
import com.uas.sso.util.AccountUtils;
|
|
|
-import net.sf.json.JSONArray;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.ui.ModelMap;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
-import java.util.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.TreeMap;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* @author liusw
|
|
|
@@ -36,14 +42,23 @@ public class WeChatServiceImpl implements WeChatService{
|
|
|
@Autowired
|
|
|
private UserDao userDao;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate redisTemplate;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存到 redis 里的过期时间(second)
|
|
|
+ */
|
|
|
+ private static final Integer ACCESS_TOKEN_EXPIRES_IN = 3600;
|
|
|
+
|
|
|
@Override
|
|
|
public ModelMap getWxUserInfo(String code, String state) {
|
|
|
ModelMap result = new ModelMap();
|
|
|
if (!StringUtils.isEmpty(code)) {
|
|
|
- JSONObject userInfo = getAccessTokenByCode(code);
|
|
|
- result.put("nickname", userInfo.getString("nickname"));
|
|
|
- result.put("headimgurl", userInfo.getString("headimgurl"));
|
|
|
- String openid = userInfo.getString("openid");
|
|
|
+ AuthUserInfo userInfo = getAccessTokenByCode(code);
|
|
|
+ result.put("nickname", userInfo.getNickname());
|
|
|
+ result.put("headimgurl", userInfo.getHeadimgurl());
|
|
|
+ String openid = userInfo.getOpenid();
|
|
|
+
|
|
|
result.put("openid", openid);
|
|
|
User user = userDao.findUsersByOpenId(openid);
|
|
|
if (StringUtils.isEmpty(user)) {
|
|
|
@@ -74,53 +89,37 @@ public class WeChatServiceImpl implements WeChatService{
|
|
|
@Override
|
|
|
public ModelMap sendTemplateMessage(List<MessageModel> messages) {
|
|
|
ModelMap result = new ModelMap();
|
|
|
- String url = WeChatUtil.GET_ACCESS_TOKEN;
|
|
|
- url = url.replace("APPID", WeChatUtil.APPID);
|
|
|
- url = url.replace("SECRET", WeChatUtil.APPSECRET);
|
|
|
- try {
|
|
|
- HttpUtil.ResponseWrap res = HttpUtil.doGet(url);
|
|
|
- JSONObject jsonObject = JSON.parseObject(res.getContent());
|
|
|
- String access_token = jsonObject.getString("access_token");
|
|
|
- String sendTemplateUrl = WeChatUtil.SEND_TEMPLATE_URL.replace("ACCESS_TOKEN", access_token);
|
|
|
- // 模板内容设置
|
|
|
- TemplateMessage templateMessage = new TemplateMessage();
|
|
|
- templateMessage.setTemplate_id(WeChatUtil.INQUIRY_TEMPLATE_ID);
|
|
|
- TemplateDataDetail first = new TemplateDataDetail();
|
|
|
- TemplateDataDetail keyword1 = new TemplateDataDetail();
|
|
|
- TemplateDataDetail keyword2 = new TemplateDataDetail();
|
|
|
- TemplateDataDetail keyword3 = new TemplateDataDetail();
|
|
|
- TemplateDataDetail remark = new TemplateDataDetail();
|
|
|
- for (MessageModel messageModel : messages) {
|
|
|
- User user = userDao.findOne(messageModel.getReceiverUu());
|
|
|
- if (!StringUtils.isEmpty(user.getOpenId())) {
|
|
|
- templateMessage.setUrl("https://www.usoftmall.com/mobile/applyPurchase/list/businessOpportunity?enuu=" + messageModel.getReceiverEnuu());
|
|
|
- Map<String, TemplateDataDetail> data = new HashMap<>();
|
|
|
- templateMessage.setTouser(user.getOpenId());
|
|
|
- keyword1.setValue(messageModel.getName());
|
|
|
- keyword1.setColor("#173177");
|
|
|
- data.put("keyword1", keyword1);
|
|
|
- keyword2.setValue("点击查看详情");
|
|
|
- keyword2.setColor("#173177");
|
|
|
- data.put("keyword2", keyword2);
|
|
|
- keyword3.setColor("#173177");
|
|
|
- keyword3.setValue("点击查看详情");
|
|
|
- data.put("keyword3", keyword3);
|
|
|
- first.setValue("尊敬的用户,您所在的企业(" + messageModel.getName() + ")有一条新的询价单");
|
|
|
- remark.setValue(messageModel.getContent());
|
|
|
- data.put("first", first);
|
|
|
- data.put("remark", remark);
|
|
|
- templateMessage.setData(data);
|
|
|
- HttpUtil.doPost(sendTemplateUrl, FlexJsonUtils.toJsonDeep(templateMessage));
|
|
|
- }
|
|
|
+ List<TemplateMsgResult> templateMsgResults = new ArrayList<>();
|
|
|
+
|
|
|
+ TreeMap<String, TreeMap<String, String>> params = new TreeMap<>();
|
|
|
+
|
|
|
+ // 获取操作 api 需要的 access_token
|
|
|
+ String access_token = getAccessToken();
|
|
|
+ String sendTemplateUrl = WeChatUtil.SEND_TEMPLATE_URL.replace("ACCESS_TOKEN", access_token);
|
|
|
+ // 模板内容设置
|
|
|
+ for (MessageModel messageModel : messages) {
|
|
|
+ User user = userDao.findOne(messageModel.getReceiverUu());
|
|
|
+ if (!StringUtils.isEmpty(user.getOpenId())) {
|
|
|
+ WechatTemplateMsg wechatTemplateMsg = new WechatTemplateMsg();
|
|
|
+ wechatTemplateMsg.setTemplate_id(WeChatUtil.INQUIRY_TEMPLATE_ID);
|
|
|
+
|
|
|
+ params.put("first", WechatTemplateMsg.item("尊敬的用户,您所在的企业(" + messageModel.getName() + ")有一条新的询价单", null));
|
|
|
+ params.put("keyword1", WechatTemplateMsg.item(messageModel.getName(), "#173177"));
|
|
|
+ params.put("keyword2", WechatTemplateMsg.item("点击查看详情", "#173177"));
|
|
|
+ params.put("keyword3", WechatTemplateMsg.item("点击查看详情", "#173177"));
|
|
|
+ params.put("remark", WechatTemplateMsg.item(messageModel.getContent(), null));
|
|
|
+
|
|
|
+ wechatTemplateMsg.setUrl("https://www.usoftmall.com/mobile/applyPurchase/list/businessOpportunity?enuu=" + messageModel.getReceiverEnuu());
|
|
|
+ wechatTemplateMsg.setTouser(user.getOpenId());
|
|
|
+ wechatTemplateMsg.setData(params);
|
|
|
+ String json = HttpReqUtil.doPost(sendTemplateUrl, FlexJsonUtils.toJsonDeep(wechatTemplateMsg));
|
|
|
+
|
|
|
+ TemplateMsgResult templateMsgResult = FlexJsonUtils.fromJson(json, TemplateMsgResult.class);
|
|
|
+ templateMsgResults.add(templateMsgResult);
|
|
|
}
|
|
|
- result.put("success", true);
|
|
|
- return result;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- result.put("success", false);
|
|
|
- result.put("message", e.getMessage());
|
|
|
- return result;
|
|
|
}
|
|
|
+ result.put("data", templateMsgResults);
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -174,36 +173,39 @@ public class WeChatServiceImpl implements WeChatService{
|
|
|
* 通过code获取用户openId
|
|
|
* @param code
|
|
|
*/
|
|
|
- private JSONObject getAccessTokenByCode(String code) {
|
|
|
- String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
|
|
|
- url = url.replace("APPID", WeChatUtil.APPID);
|
|
|
- url = url.replace("SECRET", WeChatUtil.APPSECRET);
|
|
|
- url = url.replace("CODE", code);
|
|
|
- try {
|
|
|
- HttpUtil.ResponseWrap res = HttpUtil.doGet(url);
|
|
|
- JSONObject jsonObject = JSON.parseObject(res.getContent());
|
|
|
- String accessToken = jsonObject.getString("access_token");
|
|
|
- String openId = jsonObject.getString("openid");
|
|
|
- String refreshToken = jsonObject.getString("refresh_token");
|
|
|
- JSONObject userInfo = getUserInfo(accessToken, openId);
|
|
|
- return userInfo;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return null;
|
|
|
+ private AuthUserInfo getAccessTokenByCode(String code) {
|
|
|
+ AuthTokenParams authTokenParams = new AuthTokenParams(WeChatUtil.APPID, WeChatUtil.APPSECRET, code, "authorization_code");
|
|
|
+ String json = HttpReqUtil.doGet(WeChatUtil.GET_OAUTH_TOKEN_URL, authTokenParams.getParams());
|
|
|
+ AuthAccessToken authAccessToken = FlexJsonUtils.fromJson(json, AuthAccessToken.class);
|
|
|
+ String accessToken = authAccessToken.getAccess_token();
|
|
|
+ String openId = authAccessToken.getOpenid();
|
|
|
+ AuthUserInfo userInfo = getUserInfo(accessToken, openId);
|
|
|
+ return userInfo;
|
|
|
}
|
|
|
|
|
|
- private JSONObject getUserInfo(String accessToken, String openId) {
|
|
|
- String url = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
|
|
|
- url = url.replace("ACCESS_TOKEN", accessToken);
|
|
|
- url = url.replace("OPENID", openId);
|
|
|
- try {
|
|
|
- HttpUtil.ResponseWrap res = HttpUtil.doGet(url);
|
|
|
- JSONObject jsonObject = JSON.parseObject(res.getContent());
|
|
|
- return jsonObject;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ private AuthUserInfo getUserInfo(String accessToken, String openId) {
|
|
|
+ // 返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语
|
|
|
+ AuthUserParams authUserParams = new AuthUserParams(accessToken, openId, "zh_CN");
|
|
|
+ String json = HttpReqUtil.doGet(WeChatUtil.SNS_USERINFO_URL, authUserParams.getParams());
|
|
|
+ AuthUserInfo authUserInfo = FlexJsonUtils.fromJson(json, AuthUserInfo.class);
|
|
|
+ return authUserInfo;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取调用api所需的access_token
|
|
|
+ * @exception Exception 获取 access_token 失败抛出异常
|
|
|
+ */
|
|
|
+ private String getAccessToken () {
|
|
|
+ String access_token = (String) redisTemplate.opsForValue().get("WX_ACCESS_TOKEN");
|
|
|
+ if (!StringUtils.isEmpty(access_token)) {
|
|
|
+ return access_token;
|
|
|
}
|
|
|
- return null;
|
|
|
+ AuthTokenParams authTokenParams = new AuthTokenParams(WeChatUtil.APPID, WeChatUtil.APPSECRET,"client_credential");
|
|
|
+ String json = HttpReqUtil.doGet(WeChatUtil.GET_ACCESS_TOKEN, authTokenParams.getParams());
|
|
|
+ AccessToken accessToken = FlexJsonUtils.fromJson(json, AccessToken.class);
|
|
|
+ access_token = accessToken.getAccess_token();
|
|
|
+ redisTemplate.opsForValue().set("WX_ACCESS_TOKEN", access_token, ACCESS_TOKEN_EXPIRES_IN, TimeUnit.SECONDS);
|
|
|
+ return access_token;
|
|
|
}
|
|
|
}
|