package vip.xiaonuo.biz.api.wxapp.entity;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: WeiXinRes
 * @Package cn.stylefeng.guns.sys.modular.member.entity
 * @Description: (微信获取openid返回结果)
 * @Author: zhangfuqiang
 * @Date: 2020/5/18 17:20
 * @Version V1.0
 */
@Data
public class WeiXinRes {

    private Integer errcode;
    private String errmsg;
    private String openid;
    private String session_key;
    private String unionid;
    /**
     * 微信Code
     */
    @ApiModelProperty(value = "微信Code", position = 3)
    private String jsCode;


    Map<String, Object> param = new HashMap<>();

    public WeiXinRes() {
        param.put("-1", "系统繁忙，请稍候再试");
        param.put("0", "请求成功");
        param.put("40029", "code 无效");
        param.put("45011", "频率限制，每个用户每分钟100次");
    }

    /**
     * 结果转换
     *
     * @param res
     */
    public WeiXinRes weixinResgistRes(String res) {
        JSONObject result = JSONObject.parseObject(res);
        WeiXinRes weiXinRes = new WeiXinRes();

        if (ObjectUtil.isNotEmpty(result.get("openid"))) {
            weiXinRes.setOpenid(result.get("openid").toString());
            weiXinRes.setSession_key(result.get("session_key").toString());
            weiXinRes.setErrcode(0);
            if (ObjectUtil.isNotEmpty(result.get("unionid"))) {
                weiXinRes.setUnionid(result.get("unionid").toString());
            }
        } else if (ObjectUtil.isNotEmpty(result.get("errcode"))) {
            weiXinRes.setErrcode(Integer.parseInt(result.get("errcode").toString()));
            weiXinRes.setErrmsg(result.get("errmsg").toString());
        }
        return weiXinRes;
    }

    public void setErrcode(Integer errcode) {
        this.errcode = errcode;
    }

    public void setErrmsg(String errmsg) {
        this.errmsg = errmsg;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    public void setSession_key(String session_key) {
        this.session_key = session_key;
    }

    public void setUnionid(String unionid) {
        this.unionid = unionid;
    }

    public void setParam(Map<String, Object> param) {
        this.param = param;
    }

    public Integer getErrcode() {
        return errcode;
    }

    public String getErrmsg() {
        return errmsg;
    }

    public String getOpenid() {
        return openid;
    }

    public String getSession_key() {
        return session_key;
    }

    public String getUnionid() {
        return unionid;
    }

    public Map<String, Object> getParam() {
        return param;
    }

    @Override
    public String toString() {
        return "WeiXinRes{" +
                "errcode='" + errcode + '\'' +
                ", errmsg='" + errmsg + '\'' +
                ", openid='" + openid + '\'' +
                ", session_key='" + session_key + '\'' +
                ", unionid='" + unionid + '\'' +
                ", param=" + param +
                '}';
    }
}
