DefaultWeiXinBeanDefinition.java 3.67 KB
package com.topdraw.business.process.domian.weixin;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.topdraw.exception.BadRequestException;
import com.topdraw.utils.StringUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Date;
import java.util.UUID;

@Data
@Component
public class DefaultWeiXinBeanDefinition implements WeiXinBeanDefinition {

    //
    private String appid;

    private String openId;

    private String code;

    private String token;

    private String secret;

    private String unionId;

    private String nickname;

    private String headImgUrl;

    private JSONObject userInfo;

    private String phoneNumber;

    @Value("${file.upload:upload}")
    private String filePath;

    public DefaultWeiXinBeanDefinition() {
    }

    public DefaultWeiXinBeanDefinition(String appId, String code, String unionId, String openId, JSONObject userInfoWxJo, String phone) {

        this.userInfo = userInfoWxJo;
        if (userInfo != null) {

            if (StringUtils.isNotBlank(userInfoWxJo.getString("unionId"))) {
                unionId = userInfoWxJo.getString("unionId");
            }

            if (StringUtils.isNotBlank(userInfoWxJo.getString("openId"))) {
                openId = userInfoWxJo.getString("openId");
            }

            headImgUrl = userInfoWxJo.getString("avatarUrl");

            if (StringUtils.isNotBlank(userInfoWxJo.getString("nickName"))) {
                nickname = Base64.getEncoder().encodeToString(userInfoWxJo.getString("nickName").getBytes(StandardCharsets.UTF_8));
            }

            String phoneNumber = userInfoWxJo.getString("phoneNumber");
            if (StringUtils.isBlank(phoneNumber)) {
                throw new BadRequestException("phoneNumber is null...");
            }

            this.phoneNumber = phoneNumber;

             if (StringUtils.isNotBlank(headImgUrl)) {

                 new Thread(() -> {
                     String s = UUID.randomUUID().toString();
                     File file = new File(System.getProperty("user.dir") + "/" + filePath + "/icon/" + DateUtil.format(new Date(), "yyyy-MM-dd"));
                     if (!file.exists()) {
                         file.mkdirs();
                     }

                     HttpUtil.downloadFile(headImgUrl, new File(System.getProperty("user.dir") + "/" + filePath + "/icon/" + DateUtil.format(new Date(), "yyyy-MM-dd") + "/" + s + ".jpg"));

                     headImgUrl = filePath + "/icon/" + DateUtil.format(new Date(), "yyyy-MM-dd") + "/" + s + ".jpg";
                 }).start();

            }

        }

        this.unionId = unionId;
        this.phoneNumber = phone;
        this.openId = openId;
        this.appid = appId;
        this.code = code;
    }

    @Override
    public String getAppId() {
        return this.appid;
    }

    @Override
    public String getCode() {
        return this.code;
    }

    @Override
    public String getToken() {
        return this.token;
    }

    @Override
    public String getSecret() {
        return this.secret;
    }

    @Override
    public String getOpenId() {
        return this.openId;
    }

    @Override
    public String getUnionId() {
        return this.unionId;
    }

    @Override
    public String getNickname() {
        return this.nickname;
    }

    @Override
    public String getHeadImgUrl() {
        return this.headImgUrl;
    }

    @Override
    public JSONObject getUserInfo() {
        return this.userInfo;
    }
}