This commit is contained in:
邹宗楠
2022-08-11 13:49:12 +08:00
parent 8a118b092f
commit 7d874eca22
13 changed files with 537 additions and 224 deletions

View File

@@ -14,7 +14,12 @@ import (
wxLogin "git.rosy.net.cn/jx-print/model/app_model"
"git.rosy.net.cn/jx-print/services/api"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
"time"
)
@@ -168,3 +173,42 @@ func PKCS7UnPadding(origData []byte) (bs []byte) {
}
return
}
// Jxc4UserToken 全局变量,缓存菜市管理系统token
var Jxc4UserToken = map[string]string{"token": "", "expirationTime": ""}
// GetJxc4Token 获取京西菜市token
func (u *UserLogin) GetJxc4Token() (string, error) {
if Jxc4UserToken["token"] != "" && Jxc4UserToken["expirationTime"] != "" {
timeBegin, _ := strconv.ParseInt(Jxc4UserToken["expirationTime"], 10, 64)
timeNow := time.Now().Unix()
if timeBegin-timeNow > 0 { // 没过期
return Jxc4UserToken["token"], nil
}
}
//TODO 参数为登录菜市管理系统的管理员用户账号:目前账号为 15141938808 密码111111
params := map[string]interface{}{"authType": "localpass", "authIDType": "mobile", "authID": "15141938808", "authSecret": "96e79218965eb72c92a549dd5a330112"}
retVal := make(url.Values)
for k, v := range params {
retVal.Set(k, fmt.Sprint(v))
}
strings.NewReader(utils.Map2URLValues(params).Encode())
resp, err := http.Post("https://www.jxc4.com/v2/auth2/Login", "application/x-www-form-urlencoded", strings.NewReader(retVal.Encode()))
if err != nil {
return "", err
}
defer resp.Body.Close()
bodyResp, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
jxc4User := &model.AutoGenerated{}
if err := json.Unmarshal(bodyResp, jxc4User); err != nil {
return "", err
}
Jxc4UserToken["token"] = jxc4User.Data.Token
Jxc4UserToken["expirationTime"] = strconv.FormatInt(time.Now().Unix(), 64)
return jxc4User.Data.Token, nil
}