jyy
2021-03-08 ebeb41f94fdf23cc5e5d74cfbdc0a076fb747052
修改微商城产品bug
5 files modified
184 ■■■■ changed files
zq-erp/pom.xml 2 ●●● patch | view | raw | blame | history
zq-erp/src/main/java/com/matrix/component/tools/WxTempLateMsgUtil.java 108 ●●●●● patch | view | raw | blame | history
zq-erp/src/main/java/com/matrix/system/common/interceptor/HostInterceptor.java 17 ●●●● patch | view | raw | blame | history
zq-erp/src/main/java/com/matrix/system/shopXcx/api/WeChatApiTools.java 50 ●●●● patch | view | raw | blame | history
zq-erp/src/main/java/com/matrix/system/shopXcx/api/WxUserAction.java 7 ●●●●● patch | view | raw | blame | history
zq-erp/pom.xml
@@ -390,7 +390,7 @@
                    <exclude>config/test/*</exclude>
                    <exclude>config/xcx/*</exclude>
                    <!---->
                    <!-- -->
                    <exclude>config/config.json</exclude>
                    <exclude>config/application.properties</exclude>
                    <exclude>config/system.properties</exclude>
zq-erp/src/main/java/com/matrix/component/tools/WxTempLateMsgUtil.java
@@ -1,15 +1,7 @@
package com.matrix.component.tools;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.system.shopXcx.api.WeChatApiTools;
import net.sf.json.JSONObject;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 小程序消息模板
@@ -141,87 +133,7 @@
    public static int sendWxTemplateMsg(List<String> valueList, String openId,
                                        String page, String tempId, String formId) {
        if (StringUtils.isBlank(openId)) {
            return 0;
        }
        String access_token = "";
        try {
            access_token = WeChatApiTools.getAccessToken();
        } catch (IOException e1) {
            LogUtil.error("获取accesstoken失败", e1.getMessage());
        }
        // 发送模板URL
        String url = WxApiConstants.XCX_TEMPLATEMSG_SEN.replace("ACCESS_TOKEN", access_token);
        WxTemplate temp = new WxTemplate();
        // 跳转地址
        if (StringUtils.isNotBlank(page)) {
            temp.setPage(page);
        }
        temp.setTouser(openId);
        // 消息模版ID
        temp.setTemplateId(tempId);
        Map<String, Object> tempLateMsgMap = new HashMap<String, Object>();
        // 设置模板消息的值
        for (int i = 0; i < valueList.size(); i++) {
            TemplateData keyword = new TemplateData();
            keyword.setValue(valueList.get(i));
            tempLateMsgMap.put(TEMP_KEYWORD + (i + 1), keyword);
        }
        temp.setData(tempLateMsgMap);
        String jsonString = JSONObject.fromObject(temp).toString();
        LogUtil.info("组装模板消息={}", jsonString);
        HttpRequest reqObj = new HttpRequest();
        HttpResponse result = null;
        try {
            result = reqObj.sendHttpsPost(url, null, jsonString);
        } catch (IOException e) {
            LogUtil.error("发送模板通知出错了", e);
        }
        JSONObject json = JSONObject.fromObject(result.getDataString());
        LogUtil.info("发送模板消息结果={}", json);
        int res = 0;
        if (json == null) {
            return res;
        } else {
            res = json.getInt(ERRCODE);
            if (res == Integer.parseInt(SEND_ERROR_40001)) {
                String new_access_token = "";
                try {
                    new_access_token = WeChatApiTools.getAccessToken();
                } catch (IOException e1) {
                    LogUtil.error("获取new_access_token失败", e1.getMessage());
                }
                url = WxApiConstants.XCX_TEMPLATEMSG_SEN.replace("ACCESS_TOKEN", new_access_token);
                try {
                    result = reqObj.sendHttpsPost(url, null, jsonString);
                } catch (IOException e) {
                    LogUtil.error("发送new_access_token模板通知出错了", e);
                }
                json = JSONObject.fromObject(result.getDataString());
                if (json == null) {
                    return res;
                } else {
                    res = json.getInt(ERRCODE);
                    if (res != 0) {
                        // 添加发送失败 信息
                        LogUtil.info("发送new_access_token模板消息错误错误 errcode:{} errmsg:{}" + "," + json.getInt("errcode")
                                + "," + json.getString("errmsg"));
                    } else {
                        return 1;
                    }
                }
            } else if (res != 0) {
                // 添加发送失败 信息
                LogUtil.info("发送模板消息错误错误 errcode:{} errmsg:{}" + "," + json.getInt("errcode") + ","
                        + json.getString("errmsg"));
            } else {
                return 1;
            }
        }
        LogUtil.info("模板消息发送结果:" + res);
        return 0;
    }
@@ -231,25 +143,7 @@
     * @param temp {temp}
     */
    public static void  sendSubscribeMsg(WxSubscribeDto temp) {
        String access_token = "";
        try {
            access_token = WeChatApiTools.getAccessToken();
        } catch (IOException e1) {
            LogUtil.error("获取accesstoken失败", e1.getMessage());
        }
        // 发送模板URL
        String url = WxApiConstants.XCX_TEMPLATEMSG_SEN.replace("ACCESS_TOKEN", access_token);
        String jsonString = JSONObject.fromObject(temp).toString();
        LogUtil.info("组装模板消息={}", jsonString);
        HttpRequest reqObj = new HttpRequest();
        HttpResponse result = null;
        try {
            result = reqObj.sendHttpsPost(url, null, jsonString);
        } catch (IOException e) {
            LogUtil.error("发送模板通知出错了", e);
        }
        JSONObject json = JSONObject.fromObject(result.getDataString());
        LogUtil.info("发送模板消息结果={}", json);
    }
zq-erp/src/main/java/com/matrix/system/common/interceptor/HostInterceptor.java
@@ -1,13 +1,9 @@
package com.matrix.system.common.interceptor;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.matrix.system.common.bean.SysCompany;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
@@ -45,17 +41,8 @@
            LogUtil.debug("匹配到公司{}", company.getComName());
            return true;
        } else {
        // 获得请求的域名
        String host = WebUtil.getRequest().getServerName();
        //判断是否为debug模式
        if(MatrixConstance.DEBUG){
            String debugHost=request.getHeader("debugHost");
            if(StringUtils.isNotBlank(debugHost)){
                host=debugHost;
                WebUtil.getSession().removeAttribute(ATTR_COMPANY);
                LogUtil.debug("debugHost={}",host);
            }
        }
        // 获得请求的域名--由小程序直接传过来companyCode参数对应公司的网址
        String host = request.getHeader("companyCode");
        LogUtil.debug("当前请求域名{}", host);
            @SuppressWarnings("unchecked")
            Map<String, SysCompany> companyMap = (Map<String, SysCompany>) WebUtil.getServletContext()
zq-erp/src/main/java/com/matrix/system/shopXcx/api/WeChatApiTools.java
@@ -1,23 +1,25 @@
package com.matrix.system.shopXcx.api;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.PropertiesUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.component.tools.HttpRequest;
import com.matrix.component.tools.HttpResponse;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.PropertiesUtil;
import com.matrix.system.common.bean.BusParameterSettings;
import com.matrix.system.common.constance.AppConstance;
import com.matrix.system.common.dao.BusParameterSettingsDao;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
@Service
public class WeChatApiTools {
    /**
     * 小程序秘钥
     */
    private static final String XCX_SECRET = "xcx_secret";
    /**
     * 小程序appid
     */
    private static final String XCX_APPID = "xcx_appid";
    @Autowired
    private BusParameterSettingsDao busParameterSettingsDao;
    /**
     * 微信登录url
     */
@@ -33,8 +35,6 @@
     */
    private static String accessToken = "";
    private static String appid = "";
    private static String secret = "";
    /**
@@ -46,31 +46,27 @@
     * @param code
     * @return
     */
    public static String getXcxLoginUrl(String code) {
    public  String getXcxLoginUrl(String code,Long companyId) {
        String wechatLoginUrl = PropertiesUtil.getString(WECHAT_LOGIN_URL);
        return String.format(wechatLoginUrl, getAppid(), getSecret(), code);
        return String.format(wechatLoginUrl, getAppid(companyId), getSecret(companyId), code);
    }
    /**
     * 获取小程序APPId
     * @return
     */
    public static String  getAppid(){
        if(StringUtils.isBlank(appid)){
            appid = PropertiesUtil.getString(XCX_APPID);
        }
        return  appid;
    public  String  getAppid(Long companyId){
        BusParameterSettings appId = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.MINIPROGRAM_APPID, companyId);
        return  appId.getParamValue();
    }
    /**
     * 获取小程序秘钥
     * @return
     */
    public static String  getSecret(){
        if(StringUtils.isBlank(secret)){
            secret = PropertiesUtil.getString(XCX_SECRET);
        }
        return  secret;
    public  String  getSecret(Long companyId){
        BusParameterSettings secret = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.MINIPROGRAM_SECRET, companyId);
        return  secret.getParamValue();
    }
@@ -83,7 +79,7 @@
        accessToken = "";
    }
    public static String getAccessToken() throws IOException {
    public  String getAccessToken(Long companyId) throws IOException {
        if (isTokenInvalid()) {
            synchronized (accessToken) {
@@ -93,7 +89,7 @@
                    HttpResponse result = null;
                    result = reqObj
                            .sendHttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
                                    + getAppid() + "&secret=" + getSecret(), null);
                                    + getAppid(companyId) + "&secret=" + getSecret(companyId), null);
                    JSONObject json = JSONObject.fromObject(result.getDataString());
                    String access_token = json.getString("access_token");
                    accessToken = access_token;
zq-erp/src/main/java/com/matrix/system/shopXcx/api/WxUserAction.java
@@ -1,6 +1,5 @@
package com.matrix.system.shopXcx.api;
import com.alibaba.fastjson.JSON;
import com.matrix.biz.bean.BizUser;
import com.matrix.biz.dao.BizUserDao;
import com.matrix.biz.service.BizUserService;
@@ -12,6 +11,7 @@
import com.matrix.core.tools.StringUtils;
import com.matrix.core.web.BaseAction;
import com.matrix.system.common.constance.AppConstance;
import com.matrix.system.common.interceptor.HostInterceptor;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@@ -42,7 +42,8 @@
    @Autowired
    private BizUserDao bizUserDao;
    @Autowired
    WeChatApiTools weChatApiTools;
@@ -68,7 +69,7 @@
        AjaxResult res = new AjaxResult();
        LogUtil.info("code:{}" + code);
        if (StringUtils.isNotBlank(code)) {
            String requrl = WeChatApiTools.getXcxLoginUrl(code);
            String requrl = weChatApiTools.getXcxLoginUrl(code, HostInterceptor.getCompanyId());
            String reslutData = HttpCurlUtil.sendGetHttp(requrl, null);
            JSONObject json = JSONObject.fromObject(reslutData);
            LogUtil.debug("微信登录获取到登录信息={}", json);