package utils

import (
	"crypto/md5"
	"encoding/hex"
	"gin-vue-admin/global"
	"strconv"
	"time"
)

func GetIpaddr(ip string) GetIpaddrReply {
	url := global.GVA_CONFIG.Ipaddr.Url

	data := map[string]string{}
	data["ip"] = ip

	resp := GetIpaddrPostUrl(url, data, global.GVA_CONFIG.Ipaddr.Appcode, 1)

	reply := new(GetIpaddrReply)

	UnserislizeJson(resp, reply)

	return *reply
}

type GetIpaddrReq struct {
	Ip string `json:"ip"`
}
type GetIpaddrReply struct {
	Code   int    `json:"code"`
	Msg    string `json:"msg"`
	TaskNo string `json:"taskNo"`
	Data   IpData `json:"data"`
}

type IpData struct {
	Country   string `json:"country"`
	CountryID string `json:"country_id"`
	Area      string `json:"area"`
	Region    string `json:"region"`
	RegionID  string `json:"region_id"`
	City      string `json:"city"`
	CityID    string `json:"city_id"`
	IP        string `json:"ip"`
	LongIP    string `json:"long_ip"`
	Isp       string `json:"isp"`
}

func SendMobileCode(action, mobile, code string) string {
	url := "http://39.101.175.217:8088" + "/v2sms.aspx"
	nowtime := strconv.FormatInt(time.Now().Unix(), 10)

	h := md5.New()
	h.Write([]byte("dthydthy12345" + nowtime))
	sign := hex.EncodeToString(h.Sum(nil))

	content := "【顶图DR】您的验证码为：" + code

	postData := make(map[string]string)
	postData["action"] = action
	postData["rt"] = "json"
	postData["userid"] = "47"
	postData["timestamp"] = nowtime
	postData["sign"] = string(sign)
	postData["mobile"] = mobile
	postData["content"] = content
	postData["sendTime"] = ""
	postData["extno"] = ""

	return PostWithFormData("POST", url, &postData, "")
}
