|
|
@@ -1,20 +1,25 @@
|
|
|
package controllers
|
|
|
|
|
|
import (
|
|
|
+ "crypto/md5"
|
|
|
+ "encoding/hex"
|
|
|
"encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "github.com/spf13/viper"
|
|
|
"io"
|
|
|
+ "io/ioutil"
|
|
|
"log"
|
|
|
"math/rand"
|
|
|
"net/http"
|
|
|
+ "sort"
|
|
|
+ "strings"
|
|
|
"the12animal/configFiles"
|
|
|
"the12animal/errorcode"
|
|
|
"the12animal/models"
|
|
|
"the12animal/server"
|
|
|
"the12animal/timedata"
|
|
|
"time"
|
|
|
-
|
|
|
- "github.com/gin-gonic/gin"
|
|
|
- "github.com/spf13/viper"
|
|
|
)
|
|
|
|
|
|
type UserController struct {
|
|
|
@@ -27,10 +32,12 @@ type UserController struct {
|
|
|
}
|
|
|
|
|
|
type consumercodeReq struct {
|
|
|
- Code string `json:"code"`
|
|
|
- NickName string `json:"nick_name"`
|
|
|
- PicUrl string `json:"pic_url"` //头像路由
|
|
|
- Phone string `json:"phone"` //头像路由
|
|
|
+ Code string `json:"code"`
|
|
|
+ NickName string `json:"nick_name"`
|
|
|
+ PicUrl string `json:"pic_url"` //头像路由
|
|
|
+ Phone string `json:"phone"` //头像路由
|
|
|
+ Latitude float64 `json:"latitude"`
|
|
|
+ Longitude float64 `json:"longitude"`
|
|
|
}
|
|
|
|
|
|
func (U *UserController) Init() {
|
|
|
@@ -68,7 +75,7 @@ func (U *UserController) RunResurrection(g *gin.Context) {
|
|
|
}
|
|
|
|
|
|
openid := g.GetHeader("token")
|
|
|
- resurrectionCount := U.C.GetResurrectionCount(openid)
|
|
|
+ resurrectionCount := U.C.GetResurrectionCount(openid) - 1
|
|
|
resurrectionConfig := U.Resurrection
|
|
|
var resurrectionJson []float32
|
|
|
json.Unmarshal([]byte(resurrectionConfig), &resurrectionJson)
|
|
|
@@ -94,6 +101,25 @@ func (U *UserController) RunResurrection(g *gin.Context) {
|
|
|
g.JSON(200, back)
|
|
|
}
|
|
|
|
|
|
+func (U *UserController) ClearResurrectionCount(g *gin.Context) {
|
|
|
+ openid := g.GetHeader("token")
|
|
|
+ resurrectionCount := U.C.GetResurrectionCount(openid)
|
|
|
+ var resurrectionJson []float32
|
|
|
+ json.Unmarshal([]byte(U.Resurrection), &resurrectionJson)
|
|
|
+
|
|
|
+ if resurrectionCount-1 < len(resurrectionJson) {
|
|
|
+ errRequest(g, "未达到清空条件", 1)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ U.C.ClearResurrection(openid)
|
|
|
+ back := BackData{}
|
|
|
+
|
|
|
+ back.Code = 0
|
|
|
+ back.Data = ""
|
|
|
+ g.JSON(200, back)
|
|
|
+}
|
|
|
+
|
|
|
type resurrectionData struct {
|
|
|
Count int `json:"resurrection_count"`
|
|
|
Resurrection string `json:"resurrection"`
|
|
|
@@ -122,7 +148,7 @@ func (U *UserController) Login(g *gin.Context) {
|
|
|
code, msg, wxMsg := U.wxLogin(req.Code)
|
|
|
if code == 0 {
|
|
|
encrypt := &models.Encrypt{}
|
|
|
- phoneNumber := encrypt.DecryptData("0x5c4Ac697C62c20808105134FA2036900B1f8AFDE", req.Phone)
|
|
|
+ phoneNumber := encrypt.DecryptData(viper.GetString("encrypt"), req.Phone)
|
|
|
back := BackData{}
|
|
|
nothas := U.C.UserHasOrNot(wxMsg.Openid)
|
|
|
var (
|
|
|
@@ -130,7 +156,22 @@ func (U *UserController) Login(g *gin.Context) {
|
|
|
errorcode int
|
|
|
)
|
|
|
if nothas {
|
|
|
- addr1, addr2 := U.getClientAddr(g.ClientIP())
|
|
|
+ //addr1, addr2 := U.getClientAddr(g.ClientIP())
|
|
|
+ var addr1 = ""
|
|
|
+ var addr2 = ""
|
|
|
+ if req.Latitude != -999999999 {
|
|
|
+ addr1, addr2 = U.getLocationAddr(req.Latitude, req.Longitude)
|
|
|
+ }
|
|
|
+
|
|
|
+ if addr1 == "" {
|
|
|
+ addr1, addr2 = U.getClientAddr(g.ClientIP())
|
|
|
+ }
|
|
|
+
|
|
|
+ if addr1 == "" {
|
|
|
+ //如果定位和IP地址都无法获得,则判定位域外
|
|
|
+ addr1 = "域外"
|
|
|
+ }
|
|
|
+
|
|
|
user, errorcode = U.C.NewLoginRecord3(req.NickName, req.PicUrl, wxMsg.Openid, phoneNumber, wxMsg.SessionKey, addr1, addr2)
|
|
|
} else {
|
|
|
user, errorcode = U.C.LoginRecord3(req.NickName, req.PicUrl, wxMsg.Openid, phoneNumber, wxMsg.SessionKey)
|
|
|
@@ -150,6 +191,89 @@ func (U *UserController) Login(g *gin.Context) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+type TencentMapResponse struct {
|
|
|
+ Status int `json:"status"`
|
|
|
+ Message string `json:"message"`
|
|
|
+ Result struct {
|
|
|
+ Address string `json:"address"`
|
|
|
+ AddressComponent struct {
|
|
|
+ Province string `json:"province"`
|
|
|
+ City string `json:"city"`
|
|
|
+ } `json:"address_component"`
|
|
|
+ } `json:"result"`
|
|
|
+}
|
|
|
+
|
|
|
+func (U *UserController) getLocationAddr(latitude float64, longitude float64) (string, string) {
|
|
|
+ mapKey := viper.GetString("map_key")
|
|
|
+ mapSk := viper.GetString("map_sk")
|
|
|
+ url := "https://apis.map.qq.com/ws/geocoder/v1"
|
|
|
+ tencentData := TencentMapResponse{}
|
|
|
+ // 构建签名
|
|
|
+ params := map[string]string{
|
|
|
+ "key": mapKey,
|
|
|
+ "location": fmt.Sprintf("%f,%f", latitude, longitude),
|
|
|
+ }
|
|
|
+ sign := signRequest("/ws/geocoder/v1", params, mapSk)
|
|
|
+
|
|
|
+ // 发送HTTP请求
|
|
|
+ requestURL := fmt.Sprintf("%s?%s&sig=%s", url, buildQueryString(params), sign)
|
|
|
+ response, err := http.Get(requestURL)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Failed to make HTTP request:", err)
|
|
|
+ return "", ""
|
|
|
+ }
|
|
|
+ defer response.Body.Close()
|
|
|
+
|
|
|
+ body, err := ioutil.ReadAll(response.Body)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Failed to read response body:", err)
|
|
|
+ return "", ""
|
|
|
+ }
|
|
|
+
|
|
|
+ //fmt.Println(string(body))
|
|
|
+ err = json.Unmarshal(body, &tencentData)
|
|
|
+ if err != nil {
|
|
|
+ return "", ""
|
|
|
+ }
|
|
|
+
|
|
|
+ province := replaceAddr(tencentData.Result.AddressComponent.Province)
|
|
|
+ city := replaceAddr(tencentData.Result.AddressComponent.City)
|
|
|
+ return province, city
|
|
|
+}
|
|
|
+
|
|
|
+func replaceAddr(data string) string {
|
|
|
+ data1 := strings.Replace(data, "省", "", -1)
|
|
|
+ data2 := strings.Replace(data1, "市", "", -1)
|
|
|
+ return data2
|
|
|
+}
|
|
|
+
|
|
|
+func signRequest(url string, params map[string]string, secretKey string) string {
|
|
|
+ keys := make([]string, 0, len(params))
|
|
|
+ for k := range params {
|
|
|
+ keys = append(keys, k)
|
|
|
+ }
|
|
|
+ sort.Strings(keys)
|
|
|
+
|
|
|
+ var kvPairs []string
|
|
|
+ for _, k := range keys {
|
|
|
+ v := params[k]
|
|
|
+ kvPairs = append(kvPairs, k+"="+v)
|
|
|
+ }
|
|
|
+ joinedParams := strings.Join(kvPairs, "&")
|
|
|
+
|
|
|
+ str := url + "?" + joinedParams + secretKey
|
|
|
+ hash := md5.Sum([]byte(str))
|
|
|
+ return hex.EncodeToString(hash[:])
|
|
|
+}
|
|
|
+
|
|
|
+func buildQueryString(params map[string]string) string {
|
|
|
+ var kvPairs []string
|
|
|
+ for k, v := range params {
|
|
|
+ kvPairs = append(kvPairs, k+"="+v)
|
|
|
+ }
|
|
|
+ return strings.Join(kvPairs, "&")
|
|
|
+}
|
|
|
+
|
|
|
type jsoninfo struct {
|
|
|
Ret string `json:"ret"`
|
|
|
Ip string `json:"ip"`
|
|
|
@@ -157,8 +281,9 @@ type jsoninfo struct {
|
|
|
}
|
|
|
|
|
|
func (U *UserController) getClientAddr(ipstring string) (string, string) {
|
|
|
- req, err := http.NewRequest("GET", "https://api.ip138.com/ip/?ip="+ipstring+"&datatype=jsonp", nil)
|
|
|
- req.Header.Add("token", viper.GetString("addrGetToken"))
|
|
|
+ url := "https://api.ip138.com/ip/?ip=" + ipstring + "&datatype=jsonp"
|
|
|
+ req, err := http.NewRequest("GET", url, nil)
|
|
|
+ req.Header.Add("token", viper.GetString("ip138_token"))
|
|
|
C := http.Client{}
|
|
|
resp, err := C.Do(req)
|
|
|
defer resp.Body.Close()
|
|
|
@@ -394,7 +519,7 @@ func (U *UserController) AdminLogin(g *gin.Context) {
|
|
|
errRequest(g, "参数错误", errorcode.PARAMETERERROR)
|
|
|
return
|
|
|
}
|
|
|
- if req.Account != "adminuser" || req.Passwd != "hjf16511826666" {
|
|
|
+ if req.Account != "adminuser" || req.Passwd != "jsbsl52666888" {
|
|
|
errRequest(g, "账号或密码错误", errorcode.PASSWDERROR)
|
|
|
return
|
|
|
}
|