Merge branches 'develop' and 'xzj' of https://gitee.com/darlk/ShengTangManage into xzj
This commit is contained in:
		@@ -0,0 +1,47 @@
 | 
			
		||||
package com.stdiet.custom.domain;
 | 
			
		||||
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import org.apache.commons.lang3.builder.ToStringBuilder;
 | 
			
		||||
import org.apache.commons.lang3.builder.ToStringStyle;
 | 
			
		||||
import com.stdiet.common.annotation.Excel;
 | 
			
		||||
import com.stdiet.common.core.domain.BaseEntity;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 微信销售账号对象 sys_wx_sale_account
 | 
			
		||||
 * 
 | 
			
		||||
 * @author wonder
 | 
			
		||||
 * @date 2021-01-29
 | 
			
		||||
 */
 | 
			
		||||
@Data
 | 
			
		||||
public class SysWxSaleAccount extends BaseEntity
 | 
			
		||||
{
 | 
			
		||||
    private static final long serialVersionUID = 1L;
 | 
			
		||||
 | 
			
		||||
    /** $column.columnComment */
 | 
			
		||||
    private Long id;
 | 
			
		||||
 | 
			
		||||
    /** 账号名称 */
 | 
			
		||||
    @Excel(name = "账号名称")
 | 
			
		||||
    private String nickName;
 | 
			
		||||
 | 
			
		||||
    /** 账号id */
 | 
			
		||||
    @Excel(name = "账号id")
 | 
			
		||||
    private Long accountId;
 | 
			
		||||
 | 
			
		||||
    /** 微信号 */
 | 
			
		||||
    @Excel(name = "微信号")
 | 
			
		||||
    private String wxId;
 | 
			
		||||
 | 
			
		||||
    /** 手机号 */
 | 
			
		||||
    @Excel(name = "手机号")
 | 
			
		||||
    private String phone;
 | 
			
		||||
 | 
			
		||||
    private String imgUrl;
 | 
			
		||||
 | 
			
		||||
    private String mediaId;
 | 
			
		||||
 | 
			
		||||
    private String remark;
 | 
			
		||||
 | 
			
		||||
    private Integer count;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,15 @@
 | 
			
		||||
package com.stdiet.custom.domain.wechat;
 | 
			
		||||
 | 
			
		||||
import com.alibaba.fastjson.annotation.JSONField;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
 | 
			
		||||
import java.io.Serializable;
 | 
			
		||||
 | 
			
		||||
@Data
 | 
			
		||||
public class WxAccessToken implements Serializable {
 | 
			
		||||
    @JSONField(name = "access_token")
 | 
			
		||||
    private String accessToken;
 | 
			
		||||
 | 
			
		||||
    @JSONField(name = "expires_in")
 | 
			
		||||
    private Integer expiresIn;
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,16 @@
 | 
			
		||||
package com.stdiet.custom.domain.wechat;
 | 
			
		||||
 | 
			
		||||
import com.alibaba.fastjson.annotation.JSONField;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
 | 
			
		||||
import java.io.Serializable;
 | 
			
		||||
 | 
			
		||||
@Data
 | 
			
		||||
public class WxFileUploadResult implements Serializable {
 | 
			
		||||
 | 
			
		||||
    @JSONField(name = "media_id")
 | 
			
		||||
    private String mediaId;
 | 
			
		||||
 | 
			
		||||
    @JSONField(name = "url")
 | 
			
		||||
    private String url;
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,61 @@
 | 
			
		||||
package com.stdiet.custom.mapper;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import com.stdiet.custom.domain.SysWxSaleAccount;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 微信销售账号Mapper接口
 | 
			
		||||
 * 
 | 
			
		||||
 * @author wonder
 | 
			
		||||
 * @date 2021-01-29
 | 
			
		||||
 */
 | 
			
		||||
public interface SysWxSaleAccountMapper 
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * 查询微信销售账号
 | 
			
		||||
     * 
 | 
			
		||||
     * @param id 微信销售账号ID
 | 
			
		||||
     * @return 微信销售账号
 | 
			
		||||
     */
 | 
			
		||||
    public SysWxSaleAccount selectSysWxSaleAccountById(Long id);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 查询微信销售账号列表
 | 
			
		||||
     * 
 | 
			
		||||
     * @param sysWxSaleAccount 微信销售账号
 | 
			
		||||
     * @return 微信销售账号集合
 | 
			
		||||
     */
 | 
			
		||||
    public List<SysWxSaleAccount> selectSysWxSaleAccountList(SysWxSaleAccount sysWxSaleAccount);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 新增微信销售账号
 | 
			
		||||
     * 
 | 
			
		||||
     * @param sysWxSaleAccount 微信销售账号
 | 
			
		||||
     * @return 结果
 | 
			
		||||
     */
 | 
			
		||||
    public int insertSysWxSaleAccount(SysWxSaleAccount sysWxSaleAccount);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 修改微信销售账号
 | 
			
		||||
     * 
 | 
			
		||||
     * @param sysWxSaleAccount 微信销售账号
 | 
			
		||||
     * @return 结果
 | 
			
		||||
     */
 | 
			
		||||
    public int updateSysWxSaleAccount(SysWxSaleAccount sysWxSaleAccount);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 删除微信销售账号
 | 
			
		||||
     * 
 | 
			
		||||
     * @param id 微信销售账号ID
 | 
			
		||||
     * @return 结果
 | 
			
		||||
     */
 | 
			
		||||
    public int deleteSysWxSaleAccountById(Long id);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 批量删除微信销售账号
 | 
			
		||||
     * 
 | 
			
		||||
     * @param ids 需要删除的数据ID
 | 
			
		||||
     * @return 结果
 | 
			
		||||
     */
 | 
			
		||||
    public int deleteSysWxSaleAccountByIds(Long[] ids);
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,64 @@
 | 
			
		||||
package com.stdiet.custom.service;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import com.stdiet.common.core.domain.AjaxResult;
 | 
			
		||||
import com.stdiet.custom.domain.SysWxSaleAccount;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 微信销售账号Service接口
 | 
			
		||||
 * 
 | 
			
		||||
 * @author wonder
 | 
			
		||||
 * @date 2021-01-29
 | 
			
		||||
 */
 | 
			
		||||
public interface ISysWxSaleAccountService 
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * 查询微信销售账号
 | 
			
		||||
     * 
 | 
			
		||||
     * @param id 微信销售账号ID
 | 
			
		||||
     * @return 微信销售账号
 | 
			
		||||
     */
 | 
			
		||||
    public SysWxSaleAccount selectSysWxSaleAccountById(Long id);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 查询微信销售账号列表
 | 
			
		||||
     * 
 | 
			
		||||
     * @param sysWxSaleAccount 微信销售账号
 | 
			
		||||
     * @return 微信销售账号集合
 | 
			
		||||
     */
 | 
			
		||||
    public List<SysWxSaleAccount> selectSysWxSaleAccountList(SysWxSaleAccount sysWxSaleAccount);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 新增微信销售账号
 | 
			
		||||
     * 
 | 
			
		||||
     * @param sysWxSaleAccount 微信销售账号
 | 
			
		||||
     * @return 结果
 | 
			
		||||
     */
 | 
			
		||||
    public int insertSysWxSaleAccount(SysWxSaleAccount sysWxSaleAccount);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 修改微信销售账号
 | 
			
		||||
     * 
 | 
			
		||||
     * @param sysWxSaleAccount 微信销售账号
 | 
			
		||||
     * @return 结果
 | 
			
		||||
     */
 | 
			
		||||
    public int updateSysWxSaleAccount(SysWxSaleAccount sysWxSaleAccount);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 批量删除微信销售账号
 | 
			
		||||
     * 
 | 
			
		||||
     * @param ids 需要删除的微信销售账号ID
 | 
			
		||||
     * @return 结果
 | 
			
		||||
     */
 | 
			
		||||
    public int deleteSysWxSaleAccountByIds(Long[] ids);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 删除微信销售账号信息
 | 
			
		||||
     * 
 | 
			
		||||
     * @param id 微信销售账号ID
 | 
			
		||||
     * @return 结果
 | 
			
		||||
     */
 | 
			
		||||
    public int deleteSysWxSaleAccountById(Long id);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
package com.stdiet.custom.service;
 | 
			
		||||
 | 
			
		||||
import com.stdiet.common.core.domain.AjaxResult;
 | 
			
		||||
import com.stdiet.custom.domain.WxXmlData;
 | 
			
		||||
 | 
			
		||||
import javax.servlet.http.HttpServletRequest;
 | 
			
		||||
@@ -16,4 +17,6 @@ public interface ISysWxService {
 | 
			
		||||
    public String wxCheckAuth(String signature, String timestamp, String nonce, String echostr);
 | 
			
		||||
 | 
			
		||||
    public String autoResponse(HttpServletRequest request);
 | 
			
		||||
 | 
			
		||||
    public AjaxResult getAccessToken();
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,93 @@
 | 
			
		||||
package com.stdiet.custom.service.impl;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import com.stdiet.custom.mapper.SysWxSaleAccountMapper;
 | 
			
		||||
import com.stdiet.custom.domain.SysWxSaleAccount;
 | 
			
		||||
import com.stdiet.custom.service.ISysWxSaleAccountService;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 微信销售账号Service业务层处理
 | 
			
		||||
 * 
 | 
			
		||||
 * @author wonder
 | 
			
		||||
 * @date 2021-01-29
 | 
			
		||||
 */
 | 
			
		||||
@Service
 | 
			
		||||
public class SysWxSaleAccountServiceImpl implements ISysWxSaleAccountService 
 | 
			
		||||
{
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private SysWxSaleAccountMapper sysWxSaleAccountMapper;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 查询微信销售账号
 | 
			
		||||
     * 
 | 
			
		||||
     * @param id 微信销售账号ID
 | 
			
		||||
     * @return 微信销售账号
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public SysWxSaleAccount selectSysWxSaleAccountById(Long id)
 | 
			
		||||
    {
 | 
			
		||||
        return sysWxSaleAccountMapper.selectSysWxSaleAccountById(id);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 查询微信销售账号列表
 | 
			
		||||
     * 
 | 
			
		||||
     * @param sysWxSaleAccount 微信销售账号
 | 
			
		||||
     * @return 微信销售账号
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<SysWxSaleAccount> selectSysWxSaleAccountList(SysWxSaleAccount sysWxSaleAccount)
 | 
			
		||||
    {
 | 
			
		||||
        return sysWxSaleAccountMapper.selectSysWxSaleAccountList(sysWxSaleAccount);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 新增微信销售账号
 | 
			
		||||
     * 
 | 
			
		||||
     * @param sysWxSaleAccount 微信销售账号
 | 
			
		||||
     * @return 结果
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public int insertSysWxSaleAccount(SysWxSaleAccount sysWxSaleAccount)
 | 
			
		||||
    {
 | 
			
		||||
        return sysWxSaleAccountMapper.insertSysWxSaleAccount(sysWxSaleAccount);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 修改微信销售账号
 | 
			
		||||
     * 
 | 
			
		||||
     * @param sysWxSaleAccount 微信销售账号
 | 
			
		||||
     * @return 结果
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public int updateSysWxSaleAccount(SysWxSaleAccount sysWxSaleAccount)
 | 
			
		||||
    {
 | 
			
		||||
        return sysWxSaleAccountMapper.updateSysWxSaleAccount(sysWxSaleAccount);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 批量删除微信销售账号
 | 
			
		||||
     * 
 | 
			
		||||
     * @param ids 需要删除的微信销售账号ID
 | 
			
		||||
     * @return 结果
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public int deleteSysWxSaleAccountByIds(Long[] ids)
 | 
			
		||||
    {
 | 
			
		||||
        return sysWxSaleAccountMapper.deleteSysWxSaleAccountByIds(ids);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 删除微信销售账号信息
 | 
			
		||||
     * 
 | 
			
		||||
     * @param id 微信销售账号ID
 | 
			
		||||
     * @return 结果
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public int deleteSysWxSaleAccountById(Long id)
 | 
			
		||||
    {
 | 
			
		||||
        return sysWxSaleAccountMapper.deleteSysWxSaleAccountById(id);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
package com.stdiet.custom.service.impl;
 | 
			
		||||
 | 
			
		||||
import com.stdiet.common.core.domain.AjaxResult;
 | 
			
		||||
import com.stdiet.common.utils.StringUtils;
 | 
			
		||||
import com.stdiet.custom.domain.WxXmlData;
 | 
			
		||||
import com.stdiet.custom.service.ISysWxService;
 | 
			
		||||
@@ -29,7 +30,7 @@ public class SysWxServiceImpl implements ISysWxService {
 | 
			
		||||
            WxXmlData resultXmlData = new WxXmlData();
 | 
			
		||||
            resultXmlData.setToUserName(wxData.getFromUserName());  //收到的消息是谁发来的再发给谁
 | 
			
		||||
            resultXmlData.setFromUserName(wxData.getToUserName());  //
 | 
			
		||||
            if (!StringUtils.isEmpty(wxData.getEvent())) {
 | 
			
		||||
            if (wxData.getMsgType().equals(WechatMessageUtil.MESSAGE_EVENT)) {
 | 
			
		||||
                if (wxData.getEvent().equals(WechatMessageUtil.MESSAGE_EVENT_SUBSCRIBE)) {
 | 
			
		||||
                    resultXmlData.setMsgType("text");
 | 
			
		||||
                    resultXmlData.setCreateTime(System.currentTimeMillis());
 | 
			
		||||
@@ -37,7 +38,7 @@ public class SysWxServiceImpl implements ISysWxService {
 | 
			
		||||
                } else if (wxData.getEvent().equals(WechatMessageUtil.MESSAGE_EVENT_UNSUBSCRIBE)) {
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
            } else if (wxData.getContent().equalsIgnoreCase("vip")) {
 | 
			
		||||
            } else if (wxData.getMsgType().equals(WechatMessageUtil.MESSAGE_TEXT)) {
 | 
			
		||||
                resultXmlData.setMsgType("text");
 | 
			
		||||
                resultXmlData.setCreateTime(System.currentTimeMillis());
 | 
			
		||||
                resultXmlData.setContent("<a href=\"https://my.openwrite.cn/code/generate?blogId=18931-1576559666626-322\">点击该链接,获取博客解锁验证码</a>");
 | 
			
		||||
@@ -55,4 +56,10 @@ public class SysWxServiceImpl implements ISysWxService {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public AjaxResult getAccessToken() {
 | 
			
		||||
        return AjaxResult.success(WxTokenUtils.fetchAccessToken());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,193 @@
 | 
			
		||||
package com.stdiet.custom.utils;
 | 
			
		||||
 | 
			
		||||
import java.io.*;
 | 
			
		||||
import java.net.HttpURLConnection;
 | 
			
		||||
import java.net.URL;
 | 
			
		||||
import java.net.URLEncoder;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Iterator;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * [url=home.php?mod=space&uid=49329]@author[/url] Sunlight
 | 
			
		||||
 */
 | 
			
		||||
public class HttpPostUtil {
 | 
			
		||||
    private URL url;
 | 
			
		||||
    private HttpURLConnection conn;
 | 
			
		||||
    private String boundary = "--------httppost123";
 | 
			
		||||
    private HashMap<String, String> textParams = new HashMap<String, String>();
 | 
			
		||||
    private HashMap<String, File> fileparams = new HashMap<String, File>();
 | 
			
		||||
    private DataOutputStream outputStream;
 | 
			
		||||
 | 
			
		||||
    public HttpPostUtil(String url) throws Exception {
 | 
			
		||||
        this.url = new URL(url);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 重新设置要请求的服务器地址,即上传文件的地址。
 | 
			
		||||
     *
 | 
			
		||||
     * @param url
 | 
			
		||||
     * @throws Exception
 | 
			
		||||
     */
 | 
			
		||||
    public void setUrl(String url) throws Exception {
 | 
			
		||||
        this.url = new URL(url);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 增加一个普通字符串数据到form表单数据中
 | 
			
		||||
     *
 | 
			
		||||
     * @param name
 | 
			
		||||
     * @param value
 | 
			
		||||
     */
 | 
			
		||||
    public void addParameter(String name, String value) {
 | 
			
		||||
        textParams.put(name, value);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 增加一个文件到form表单数据中
 | 
			
		||||
     *
 | 
			
		||||
     * @param name
 | 
			
		||||
     * @param value
 | 
			
		||||
     */
 | 
			
		||||
    public void addParameter(String name, File value) {
 | 
			
		||||
        fileparams.put(name, value);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 清空所有已添加的form表单数据
 | 
			
		||||
     */
 | 
			
		||||
    public void clearAllParameters() {
 | 
			
		||||
        textParams.clear();
 | 
			
		||||
        fileparams.clear();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 发送数据到服务器,返回一个字节包含服务器的返回结果的数组
 | 
			
		||||
     *
 | 
			
		||||
     * @return
 | 
			
		||||
     * @throws Exception
 | 
			
		||||
     */
 | 
			
		||||
    public String send() throws Exception {
 | 
			
		||||
        initConnection();
 | 
			
		||||
        conn.connect();
 | 
			
		||||
        outputStream = new DataOutputStream(conn.getOutputStream());
 | 
			
		||||
        writeFileParams();
 | 
			
		||||
        writeStringParams();
 | 
			
		||||
        paramsEnd();
 | 
			
		||||
        int code = conn.getResponseCode();
 | 
			
		||||
        if (code == 200) {
 | 
			
		||||
            InputStream in = conn.getInputStream();
 | 
			
		||||
            ByteArrayOutputStream out = new ByteArrayOutputStream();
 | 
			
		||||
            byte[] buf = new byte[1024 * 8];
 | 
			
		||||
            int len;
 | 
			
		||||
            while ((len = in.read(buf)) != -1) {
 | 
			
		||||
                out.write(buf, 0, len);
 | 
			
		||||
            }
 | 
			
		||||
            conn.disconnect();
 | 
			
		||||
            String s = new String(out.toByteArray(), "utf-8");
 | 
			
		||||
            return s;
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 文件上传的connection的一些必须设置
 | 
			
		||||
     *
 | 
			
		||||
     * @throws Exception
 | 
			
		||||
     */
 | 
			
		||||
    private void initConnection() throws Exception {
 | 
			
		||||
        conn = (HttpURLConnection) this.url.openConnection();
 | 
			
		||||
        conn.setDoOutput(true);
 | 
			
		||||
        conn.setUseCaches(false);
 | 
			
		||||
        conn.setConnectTimeout(10000); // 连接超时为10秒
 | 
			
		||||
        conn.setRequestMethod("POST");
 | 
			
		||||
        conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 普通字符串数据
 | 
			
		||||
     *
 | 
			
		||||
     * @throws Exception
 | 
			
		||||
     */
 | 
			
		||||
    private void writeStringParams() throws Exception {
 | 
			
		||||
        Set<String> keySet = textParams.keySet();
 | 
			
		||||
        for (Iterator<String> it = keySet.iterator(); it.hasNext(); ) {
 | 
			
		||||
            String name = it.next();
 | 
			
		||||
            String value = textParams.get(name);
 | 
			
		||||
            outputStream.writeBytes("--" + boundary + "\r\n");
 | 
			
		||||
            outputStream.writeBytes("Content-Disposition: form-data; name=" + name + "\r\n");
 | 
			
		||||
            outputStream.writeBytes("\r\n");
 | 
			
		||||
            outputStream.writeBytes(encode(value) + "\r\n");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 文件数据
 | 
			
		||||
     *
 | 
			
		||||
     * @throws Exception
 | 
			
		||||
     */
 | 
			
		||||
    private void writeFileParams() throws Exception {
 | 
			
		||||
        Set<String> keySet = fileparams.keySet();
 | 
			
		||||
        for (Iterator<String> it = keySet.iterator(); it.hasNext(); ) {
 | 
			
		||||
            String name = it.next();
 | 
			
		||||
            File value = fileparams.get(name);
 | 
			
		||||
            outputStream.writeBytes("--" + boundary + "\r\n");
 | 
			
		||||
            outputStream.writeBytes("Content-Disposition: form-data; name=" + name + "; filename=" + encode(value.getName()) + "\r\n");
 | 
			
		||||
            outputStream.writeBytes("Content-Type: " + getContentType(value) + "\r\n");
 | 
			
		||||
            outputStream.writeBytes("\r\n");
 | 
			
		||||
            outputStream.write(getBytes(value));
 | 
			
		||||
            outputStream.writeBytes("\r\n");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 获取文件的上传类型,图片格式为image/png,image/jpeg等。非图片为application /octet-stream
 | 
			
		||||
     *
 | 
			
		||||
     * @param f
 | 
			
		||||
     * @return
 | 
			
		||||
     * @throws Exception
 | 
			
		||||
     */
 | 
			
		||||
    private String getContentType(File f) throws Exception {
 | 
			
		||||
        return "application/octet-stream";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 把文件转换成字节数组
 | 
			
		||||
     *
 | 
			
		||||
     * @param f
 | 
			
		||||
     * @return
 | 
			
		||||
     * @throws Exception
 | 
			
		||||
     */
 | 
			
		||||
    private byte[] getBytes(File f) throws Exception {
 | 
			
		||||
        FileInputStream in = new FileInputStream(f);
 | 
			
		||||
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 | 
			
		||||
        byte[] b = new byte[1024];
 | 
			
		||||
        int n;
 | 
			
		||||
        while ((n = in.read(b)) != -1) {
 | 
			
		||||
            out.write(b, 0, n);
 | 
			
		||||
        }
 | 
			
		||||
        in.close();
 | 
			
		||||
        return out.toByteArray();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 添加结尾数据
 | 
			
		||||
     *
 | 
			
		||||
     * @throws Exception
 | 
			
		||||
     */
 | 
			
		||||
    private void paramsEnd() throws Exception {
 | 
			
		||||
        outputStream.writeBytes("--" + boundary + "--" + "\r\n");
 | 
			
		||||
        outputStream.writeBytes("\r\n");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 对包含中文的字符串进行转码,此为UTF-8。服务器那边要进行一次解码
 | 
			
		||||
     *
 | 
			
		||||
     * @param value
 | 
			
		||||
     * @return
 | 
			
		||||
     * @throws Exception
 | 
			
		||||
     */
 | 
			
		||||
    private String encode(String value) throws Exception {
 | 
			
		||||
        return URLEncoder.encode(value, "UTF-8");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,9 +1,17 @@
 | 
			
		||||
package com.stdiet.custom.utils;
 | 
			
		||||
 | 
			
		||||
import com.alibaba.fastjson.JSONObject;
 | 
			
		||||
import com.stdiet.common.core.redis.RedisCache;
 | 
			
		||||
import com.stdiet.common.utils.StringUtils;
 | 
			
		||||
import com.stdiet.common.utils.http.HttpUtils;
 | 
			
		||||
import com.stdiet.custom.domain.WxXmlData;
 | 
			
		||||
import com.stdiet.custom.domain.wechat.WxAccessToken;
 | 
			
		||||
import com.stdiet.custom.domain.wechat.WxFileUploadResult;
 | 
			
		||||
import com.thoughtworks.xstream.XStream;
 | 
			
		||||
import org.apache.commons.io.IOUtils;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.InputStream;
 | 
			
		||||
import java.nio.charset.StandardCharsets;
 | 
			
		||||
import java.security.MessageDigest;
 | 
			
		||||
@@ -11,8 +19,48 @@ import java.security.NoSuchAlgorithmException;
 | 
			
		||||
 | 
			
		||||
public class WxTokenUtils {
 | 
			
		||||
 | 
			
		||||
    public static final String KEY_ACCESS_TOKEN="wx:access_token";
 | 
			
		||||
    public static final String KEY_ACCESS_TOKEN_WATHER="wx:access_token_watcher";
 | 
			
		||||
 | 
			
		||||
    // 与接口配置信息中的Token要一致
 | 
			
		||||
    private static String token = "shengtangdiet";
 | 
			
		||||
    //    private static String appId = "wx4a9c1fc9dba53202";
 | 
			
		||||
//    private static String appSecret = "fff029ade5d3575df755f4cf9e52f8da";
 | 
			
		||||
    private static String appId = "wxaf10fe560ea043a0";
 | 
			
		||||
    private static String appSecret = "afb47e477337df23b7562c3c1f965826";
 | 
			
		||||
    private static String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token";
 | 
			
		||||
    private static String uploadMaterialUrl = "https://api.weixin.qq.com/cgi-bin/material/add_material";
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public static WxAccessToken fetchAccessToken() {
 | 
			
		||||
        try {
 | 
			
		||||
            String resStr = HttpUtils.sendGet(tokenUrl, "grant_type=client_credential&appid=" + appId + "&secret=" + appSecret);
 | 
			
		||||
            if (StringUtils.isEmpty(resStr)) {
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
            JSONObject obj = JSONObject.parseObject(resStr);
 | 
			
		||||
 | 
			
		||||
            WxAccessToken token = JSONObject.toJavaObject(obj, WxAccessToken.class);
 | 
			
		||||
            return token;
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static WxFileUploadResult uploadImage(String filePath, String accessToken) {
 | 
			
		||||
        try {
 | 
			
		||||
            String url = uploadMaterialUrl + "?access_token" + accessToken + "&type=image";
 | 
			
		||||
            HttpPostUtil post = new HttpPostUtil(url);
 | 
			
		||||
            post.addParameter("media", new File(filePath));
 | 
			
		||||
            String resultStr = post.send();
 | 
			
		||||
            JSONObject obj = JSONObject.parseObject(resultStr);
 | 
			
		||||
            WxFileUploadResult result = JSONObject.toJavaObject(obj, WxFileUploadResult.class);
 | 
			
		||||
            return result;
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 验证签名
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user