diff --git a/app/apiException/apiException.go b/app/apiException/apiException.go index 78e0c0e..77d1850 100644 --- a/app/apiException/apiException.go +++ b/app/apiException/apiException.go @@ -9,35 +9,37 @@ type Error struct { } var ( - ServerError = NewError(http.StatusInternalServerError, 200500, "系统异常,请稍后重试!") - OpenIDError = NewError(http.StatusInternalServerError, 200500, "系统异常,请稍后重试!") - ParamError = NewError(http.StatusInternalServerError, 200501, "参数错误") - NotAdmin = NewError(http.StatusInternalServerError, 200502, "该用户无管理员权限") - UserNotFind = NewError(http.StatusInternalServerError, 200503, "该用户不存在") - NotLogin = NewError(http.StatusInternalServerError, 200503, "未登录") - NoThatPasswordOrWrong = NewError(http.StatusInternalServerError, 200504, "密码错误") - HttpTimeout = NewError(http.StatusInternalServerError, 200505, "系统异常,请稍后重试!") - RequestError = NewError(http.StatusInternalServerError, 200506, "系统异常,请稍后重试!") - NotBindYxy = NewError(http.StatusInternalServerError, 200507, "该手机号或用户未绑定或未注册易校园") - UserAlreadyExisted = NewError(http.StatusInternalServerError, 200508, "该用户已激活") - WrongVerificationCode = NewError(http.StatusInternalServerError, 200509, "验证码错误") - StudentNumAndIidError = NewError(http.StatusInternalServerError, 200510, "该学号或身份证不存在或者不匹配,请重新输入") - PwdError = NewError(http.StatusInternalServerError, 200511, "密码长度必须在6~20位之间") - ReactiveError = NewError(http.StatusInternalServerError, 200512, "该通行证已经存在,请重新输入") - StudentIdError = NewError(http.StatusInternalServerError, 200513, "学号格式不正确,请重新输入") - YxySessionExpired = NewError(http.StatusInternalServerError, 200514, "一卡通登陆过期,请稍后再试") - YxyNeedCaptcha = NewError(http.StatusInternalServerError, 200515, "请输入验证码") - WrongCaptcha = NewError(http.StatusInternalServerError, 200516, "图形验证码错误") - WrongPhoneNum = NewError(http.StatusInternalServerError, 200517, "手机号格式不正确") - ImgTypeError = NewError(http.StatusInternalServerError, 200518, "图片类型有误") - PersonalInfoNotFill = NewError(http.StatusInternalServerError, 200519, "请先填写个人基本信息") - StockNotEnough = NewError(http.StatusInternalServerError, 200520, "物资库存不足") - RecordAlreadyExisted = NewError(http.StatusInternalServerError, 200521, "该用户已经申请过该物资") - RecordAlreadyRejected = NewError(http.StatusInternalServerError, 200522, "含有已经被驳回的申请,请重新选择") - NotBorrowingRecord = NewError(http.StatusInternalServerError, 200523, "含有非借用中的记录,请重新选择") - NotInit = NewError(http.StatusNotFound, 200404, http.StatusText(http.StatusNotFound)) - NotFound = NewError(http.StatusNotFound, 200404, http.StatusText(http.StatusNotFound)) - Unknown = NewError(http.StatusInternalServerError, 300500, "系统异常,请稍后重试!") + ServerError = NewError(http.StatusInternalServerError, 200500, "系统异常,请稍后重试!") + OpenIDError = NewError(http.StatusInternalServerError, 200500, "系统异常,请稍后重试!") + ParamError = NewError(http.StatusInternalServerError, 200501, "参数错误") + NotAdmin = NewError(http.StatusInternalServerError, 200502, "该用户无管理员权限") + UserNotFind = NewError(http.StatusInternalServerError, 200503, "该用户不存在") + NotLogin = NewError(http.StatusInternalServerError, 200503, "未登录") + NoThatPasswordOrWrong = NewError(http.StatusInternalServerError, 200504, "密码错误") + HttpTimeout = NewError(http.StatusInternalServerError, 200505, "系统异常,请稍后重试!") + RequestError = NewError(http.StatusInternalServerError, 200506, "系统异常,请稍后重试!") + NotBindYxy = NewError(http.StatusInternalServerError, 200507, "该手机号或用户未绑定或未注册易校园") + UserAlreadyExisted = NewError(http.StatusInternalServerError, 200508, "该用户已激活") + WrongVerificationCode = NewError(http.StatusInternalServerError, 200509, "手机验证码错误,错误3次将锁定15分钟") + StudentNumAndIidError = NewError(http.StatusInternalServerError, 200510, "该学号或身份证不存在或者不匹配,请重新输入") + PwdError = NewError(http.StatusInternalServerError, 200511, "密码长度必须在6~20位之间") + ReactiveError = NewError(http.StatusInternalServerError, 200512, "该通行证已经存在,请重新输入") + StudentIdError = NewError(http.StatusInternalServerError, 200513, "学号格式不正确,请重新输入") + YxySessionExpired = NewError(http.StatusInternalServerError, 200514, "一卡通登陆过期,请稍后再试") + YxyNeedCaptcha = NewError(http.StatusInternalServerError, 200515, "请输入验证码") + WrongCaptcha = NewError(http.StatusInternalServerError, 200516, "图形验证码错误") + WrongPhoneNum = NewError(http.StatusInternalServerError, 200517, "手机号格式不正确") + ImgTypeError = NewError(http.StatusInternalServerError, 200518, "图片类型有误") + PersonalInfoNotFill = NewError(http.StatusInternalServerError, 200519, "请先填写个人基本信息") + StockNotEnough = NewError(http.StatusInternalServerError, 200520, "物资库存不足") + RecordAlreadyExisted = NewError(http.StatusInternalServerError, 200521, "该用户已经申请过该物资") + RecordAlreadyRejected = NewError(http.StatusInternalServerError, 200522, "含有已经被驳回的申请,请重新选择") + NotBorrowingRecord = NewError(http.StatusInternalServerError, 200523, "含有非借用中的记录,请重新选择") + SendVerificationCodeLimit = NewError(http.StatusInternalServerError, 200524, "短信发送超限,请1分钟后再试") + CampusMismatch = NewError(http.StatusInternalServerError, 200525, "暂无该校区绑定信息") + NotInit = NewError(http.StatusNotFound, 200404, http.StatusText(http.StatusNotFound)) + NotFound = NewError(http.StatusNotFound, 200404, http.StatusText(http.StatusNotFound)) + Unknown = NewError(http.StatusInternalServerError, 300500, "系统异常,请稍后重试!") ) func OtherError(message string) *Error { diff --git a/app/controllers/userController/bind.go b/app/controllers/userController/bind.go index c7b50f0..09ea111 100644 --- a/app/controllers/userController/bind.go +++ b/app/controllers/userController/bind.go @@ -1,16 +1,14 @@ package userController import ( - "context" - "github.com/gin-gonic/gin" - "github.com/google/uuid" - "time" "wejh-go/app/apiException" "wejh-go/app/services/sessionServices" "wejh-go/app/services/userServices" "wejh-go/app/services/yxyServices" "wejh-go/app/utils" - "wejh-go/config/redis" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" ) type bindForm struct { @@ -21,11 +19,6 @@ type phoneForm struct { PhoneNum string `json:"phoneNum"` } -type captchaForm struct { - Captcha string `json:"captcha"` - PhoneNum string `json:"phoneNum"` -} - type loginForm struct { PhoneNum string `json:"phoneNum"` Code string `json:"code"` @@ -91,66 +84,21 @@ func BindOauthPassword(c *gin.Context) { utils.JsonSuccessResponse(c, nil) } -// SendVerificationCode 这一函数实际上不再被使用 -func SendVerificationCode(c *gin.Context) { - var postForm phoneForm - err := c.ShouldBindJSON(&postForm) - if err != nil { - _ = c.AbortWithError(200, apiException.ParamError) - return - } - user, err := sessionServices.GetUserSession(c) - if err != nil { - _ = c.AbortWithError(200, apiException.NotLogin) - return - } - u := uuid.New() - deviceId := u.String() - userServices.SetDeviceID(user, deviceId) - data, err := yxyServices.GetSecurityToken(deviceId) - if err != nil { - _ = c.AbortWithError(200, apiException.ServerError) - return - } - if yxyServices.CheckToken("SecurityToken" + user.Username) { - yxyServices.DelToken("SecurityToken" + user.Username) - } - yxyServices.SetToken("SecurityToken"+user.Username, data.Token) - if data.Level == 1 { - _ = c.AbortWithError(200, apiException.YxyNeedCaptcha) - return - } - err = yxyServices.SendVerificationCode(data.Token, deviceId, "", postForm.PhoneNum) - if err == apiException.WrongCaptcha || err == apiException.NotBindYxy { - _ = c.AbortWithError(200, err) - return - } else if err != nil { - _ = c.AbortWithError(200, apiException.ServerError) - return - } - utils.JsonSuccessResponse(c, nil) -} - +// 待废弃 func GetCaptcha(c *gin.Context) { - user, err := sessionServices.GetUserSession(c) + _, err := sessionServices.GetUserSession(c) if err != nil { _ = c.AbortWithError(200, apiException.NotLogin) return } - u := uuid.New() - deviceId := u.String() - redis.RedisClient.Set(context.Background(), user.Username+"_device_id", deviceId, time.Minute*5) + deviceId := uuid.New().String() data, err := yxyServices.GetSecurityToken(deviceId) if err != nil { _ = c.AbortWithError(200, apiException.ServerError) return } - if yxyServices.CheckToken("SecurityToken" + user.Username) { - yxyServices.DelToken("SecurityToken" + user.Username) - } - yxyServices.SetToken("SecurityToken"+user.Username, data.Token) - token := &data.Token - img, err := yxyServices.GetCaptchaImage(user.DeviceID, *token) + securityToken := &data.SecurityToken + img, err := yxyServices.GetCaptchaImage(deviceId, *securityToken) if err != nil { _ = c.AbortWithError(200, apiException.ServerError) return @@ -158,26 +106,26 @@ func GetCaptcha(c *gin.Context) { utils.JsonSuccessResponse(c, img) } -func SendVerificationCodeByCaptcha(c *gin.Context) { - var postForm captchaForm +func SendVerificationCode(c *gin.Context) { + var postForm phoneForm err := c.ShouldBindJSON(&postForm) if err != nil { _ = c.AbortWithError(200, apiException.ParamError) return } - user, err := sessionServices.GetUserSession(c) + _, err = sessionServices.GetUserSession(c) if err != nil { _ = c.AbortWithError(200, apiException.NotLogin) return } - token, err := yxyServices.GetToken("SecurityToken" + user.Username) + deviceId := uuid.New().String() + data, err := yxyServices.GetSecurityToken(deviceId) if err != nil { - _ = c.AbortWithError(200, apiException.YxyNeedCaptcha) + _ = c.AbortWithError(200, apiException.ServerError) return } - deviceId, err := redis.RedisClient.Get(context.Background(), user.Username+"_device_id").Result() - err = yxyServices.SendVerificationCode(*token, deviceId, postForm.Captcha, postForm.PhoneNum) - if err == apiException.WrongCaptcha || err == apiException.NotBindYxy { + err = yxyServices.SendVerificationCode(data.SecurityToken, deviceId, postForm.PhoneNum) + if err == apiException.WrongCaptcha || err == apiException.WrongPhoneNum || err == apiException.SendVerificationCodeLimit || err == apiException.NotBindYxy { _ = c.AbortWithError(200, err) return } else if err != nil { @@ -199,15 +147,14 @@ func LoginYxy(c *gin.Context) { _ = c.AbortWithError(200, apiException.NotLogin) return } - deviceId, err := redis.RedisClient.Get(context.Background(), user.Username+"_device_id").Result() - if err != nil { - _ = c.AbortWithError(200, apiException.ServerError) - return - } + deviceId := uuid.New().String() uid, err := yxyServices.LoginByCode(postForm.Code, deviceId, postForm.PhoneNum) - if err != nil { + if err == apiException.WrongVerificationCode || err == apiException.WrongPhoneNum { _ = c.AbortWithError(200, err) return + } else if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return } userServices.SetDeviceID(user, deviceId) userServices.DecryptUserKeyInfo(user) diff --git a/app/controllers/yxyController/electricityController/electricityController.go b/app/controllers/yxyController/electricityController/electricityController.go index 57915ec..c4f4901 100644 --- a/app/controllers/yxyController/electricityController/electricityController.go +++ b/app/controllers/yxyController/electricityController/electricityController.go @@ -1,35 +1,53 @@ package electricityController import ( - "github.com/gin-gonic/gin" "wejh-go/app/apiException" "wejh-go/app/models" "wejh-go/app/services/sessionServices" "wejh-go/app/services/yxyServices" "wejh-go/app/utils" + + "github.com/gin-gonic/gin" ) type recordForm struct { - Page string `json:"page"` + Page string `form:"page" json:"page"` + Campus string `form:"campus" json:"campus"` +} + +type CampusForm struct { + Campus string `form:"campus"` } func GetBalance(c *gin.Context) { + var postForm CampusForm + err := c.ShouldBindQuery(&postForm) + if err != nil { + _ = c.AbortWithError(200, apiException.ParamError) + return + } user, err := sessionServices.GetUserSession(c) if err != nil { _ = c.AbortWithError(200, apiException.NotLogin) return } - token, err := yxyServices.Auth(user.YxyUid) + token, err := yxyServices.GetElecAuthToken(user.YxyUid) if err != nil { _ = c.AbortWithError(200, apiException.ServerError) return } - electricityBalance, err := yxyServices.ElectricityBalance(*token) - if err != nil { + if postForm.Campus != "mgs" { + postForm.Campus = "zhpf" + } + balance, err := yxyServices.ElectricityBalance(*token, postForm.Campus) + if err == apiException.CampusMismatch { + _ = c.AbortWithError(200, err) + return + } else if err != nil { _ = c.AbortWithError(200, apiException.ServerError) return } - utils.JsonSuccessResponse(c, electricityBalance) + utils.JsonSuccessResponse(c, balance) } func GetRechargeRecords(c *gin.Context) { @@ -44,63 +62,70 @@ func GetRechargeRecords(c *gin.Context) { _ = c.AbortWithError(200, apiException.NotLogin) return } - token, err := yxyServices.Auth(user.YxyUid) + token, err := yxyServices.GetElecAuthToken(user.YxyUid) if err != nil { _ = c.AbortWithError(200, apiException.ServerError) return } - roomInfo, err := yxyServices.Bind(*token) - if err != nil { + if postForm.Campus != "mgs" { + postForm.Campus = "zhpf" + } + roomStrConcat, err := yxyServices.GetElecRoomStrConcat(*token, postForm.Campus, user.YxyUid) + if err == apiException.CampusMismatch { + _ = c.AbortWithError(200, err) + return + } else if err != nil { _ = c.AbortWithError(200, apiException.ServerError) return } - records, err := yxyServices.ElectricityRechargeRecords( - *token, - (*roomInfo)["area_id"].(string), - (*roomInfo)["building_code"].(string), - (*roomInfo)["floor_code"].(string), - (*roomInfo)["room_code"].(string), - postForm.Page) - if err != nil { + records, err := yxyServices.ElectricityRechargeRecords(*token, postForm.Campus, postForm.Page, *roomStrConcat) + if err == apiException.CampusMismatch { + _ = c.AbortWithError(200, err) + return + } else if err != nil { _ = c.AbortWithError(200, apiException.ServerError) return } - utils.JsonSuccessResponse(c, records) + utils.JsonSuccessResponse(c, records.List) } func GetConsumptionRecords(c *gin.Context) { - user, err := sessionServices.GetUserSession(c) + var postForm CampusForm + err := c.ShouldBindQuery(&postForm) if err != nil { - _ = c.AbortWithError(200, apiException.NotLogin) + _ = c.AbortWithError(200, apiException.ParamError) return } - token, err := yxyServices.Auth(user.YxyUid) + user, err := sessionServices.GetUserSession(c) if err != nil { - _ = c.AbortWithError(200, apiException.ServerError) + _ = c.AbortWithError(200, apiException.NotLogin) return } - roomInfo, err := yxyServices.Bind(*token) + token, err := yxyServices.GetElecAuthToken(user.YxyUid) if err != nil { _ = c.AbortWithError(200, apiException.ServerError) return } - electricityBalance, err := yxyServices.ElectricityBalance(*token) - if err != nil { + if postForm.Campus != "mgs" { + postForm.Campus = "zhpf" + } + roomStrConcat, err := yxyServices.GetElecRoomStrConcat(*token, postForm.Campus, user.YxyUid) + if err == apiException.CampusMismatch { + _ = c.AbortWithError(200, err) + return + } else if err != nil { _ = c.AbortWithError(200, apiException.ServerError) return } - records, err := yxyServices.ElectricityConsumptionRecords( - *token, - (*roomInfo)["area_id"].(string), - (*roomInfo)["building_code"].(string), - (*roomInfo)["floor_code"].(string), - (*roomInfo)["room_code"].(string), - (*electricityBalance)["md_type"].(string)) - if err != nil { + records, err := yxyServices.ElectricityConsumptionRecords(*token, postForm.Campus, *roomStrConcat) + if err == apiException.CampusMismatch { + _ = c.AbortWithError(200, err) + return + } else if err != nil { _ = c.AbortWithError(200, apiException.ServerError) return } - utils.JsonSuccessResponse(c, records) + utils.JsonSuccessResponse(c, records.List) } func InsertLowBatteryQuery(c *gin.Context) { diff --git a/app/controllers/yxyController/schoolCardController/schoolCardController.go b/app/controllers/yxyController/schoolCardController/schoolCardController.go index 47275c1..26e1ea6 100644 --- a/app/controllers/yxyController/schoolCardController/schoolCardController.go +++ b/app/controllers/yxyController/schoolCardController/schoolCardController.go @@ -1,11 +1,12 @@ package schoolCardController import ( - "github.com/gin-gonic/gin" "wejh-go/app/apiException" "wejh-go/app/services/sessionServices" "wejh-go/app/services/yxyServices" "wejh-go/app/utils" + + "github.com/gin-gonic/gin" ) type recordForm struct { @@ -18,7 +19,7 @@ func GetBalance(c *gin.Context) { _ = c.AbortWithError(200, apiException.NotLogin) return } - balance, err := yxyServices.GetCardBalance(user.DeviceID, user.YxyUid) + balance, err := yxyServices.GetCardBalance(user.DeviceID, user.YxyUid, user.PhoneNum) if err == apiException.YxySessionExpired { _ = c.AbortWithError(200, err) return @@ -41,14 +42,13 @@ func GetConsumptionRecord(c *gin.Context) { _ = c.AbortWithError(200, apiException.NotLogin) return } - balance, err := yxyServices.GetCardConsumptionRecord(user.DeviceID, user.YxyUid, postForm.QueryTime) - if err == apiException.YxySessionExpired || - err == apiException.ParamError { + records, err := yxyServices.GetCardConsumptionRecord(user.DeviceID, user.YxyUid, user.PhoneNum, postForm.QueryTime) + if err == apiException.YxySessionExpired || err == apiException.ParamError { _ = c.AbortWithError(200, err) return } else if err != nil { _ = c.AbortWithError(200, apiException.ServerError) return } - utils.JsonSuccessResponse(c, balance) + utils.JsonSuccessResponse(c, records.List) } diff --git a/app/services/yxyServices/bindService.go b/app/services/yxyServices/bindService.go index 89f9245..d0aa330 100644 --- a/app/services/yxyServices/bindService.go +++ b/app/services/yxyServices/bindService.go @@ -1,50 +1,26 @@ package yxyServices import ( - "fmt" - "github.com/mitchellh/mapstructure" "net/url" - "strings" "wejh-go/app/apiException" "wejh-go/config/api/yxyApi" + + "github.com/mitchellh/mapstructure" ) type securityToken struct { - Level int `json:"level"` - Token string `json:"token"` + Level int `json:"level" mapstructure:"level"` + SecurityToken string `json:"security_token" mapstructure:"security_token"` } type captcha struct { - Img string `json:"img"` + Img string `json:"img" mapstructure:"img"` } type userInfo struct { - UID string `json:"uid"` - Token string `json:"token"` - DeviceID string `json:"device_id"` - Sex int `json:"sex"` - SchoolCode string `json:"school_code"` - SchoolName string `json:"school_name"` - SchoolClasses int `json:"school_classes"` - SchoolNature int `json:"school_nature"` - UserName string `json:"user_name"` - UserType string `json:"user_type"` - JobNo string `json:"job_no"` - UserIdcard string `json:"user_idcard"` - IdentityNo string `json:"identity_no"` - UserClass string `json:"user_class"` - RealNameStatus int `json:"real_name_status"` - RegiserTime string `json:"regiser_time"` - BindCardStatus int `json:"bind_card_status"` - LastLogin string `json:"last_login"` - TestAccount int `json:"test_account"` - IsNew int `json:"is_new"` - CreateStatus int `json:"create_status"` - Platform string `json:"platform"` - BindCardRate int `json:"bind_card_rate"` - Points int `json:"points"` - SchoolIdentityType int `json:"school_identity_type"` - ExtraJSON string `json:"extra_json"` + UID string `json:"uid" mapstructure:"uid"` + Token string `json:"token" mapstructure:"token"` + BindCardStatus int `json:"bind_card_status" mapstructure:"bind_card_status"` } func GetSecurityToken(deviceId string) (*securityToken, error) { @@ -68,14 +44,14 @@ func GetSecurityToken(deviceId string) (*securityToken, error) { return &data, nil } -func GetCaptchaImage(deviceId, token string) (*string, error) { +func GetCaptchaImage(deviceId, securityToken string) (*string, error) { params := url.Values{} Url, err := url.Parse(string(yxyApi.CaptchaImage)) if err != nil { return nil, err } params.Set("device_id", deviceId) - params.Set("security_token", token) + params.Set("security_token", securityToken) Url.RawQuery = params.Encode() urlPath := Url.String() resp, err := FetchHandleOfGet(yxyApi.YxyApi(urlPath)) @@ -90,26 +66,27 @@ func GetCaptchaImage(deviceId, token string) (*string, error) { return &data.Img, nil } -func SendVerificationCode(securityToken, deviceId, captcha, phoneNum string) error { - var form map[string]string - form = make(map[string]string) +func SendVerificationCode(securityToken, deviceId, phoneNum string) error { + form := make(map[string]string) form["phone_num"] = phoneNum form["security_token"] = securityToken - form["captcha"] = captcha + form["captcha"] = "" form["device_id"] = deviceId resp, err := FetchHandleOfPost(form, yxyApi.SendVerificationCode) if err != nil { return err } - if resp.Code != 0 { - msgSplit := strings.Split(resp.Msg, "; ") - if len(msgSplit) > 1 && msgSplit[1] == "encryptedDeviceId不一致" { - fmt.Println(msgSplit[1]) - return apiException.ServerError - } else { - return apiException.WrongCaptcha - } + + if resp.Code == 110003 { + return apiException.WrongCaptcha + } else if resp.Code == 110005 { + return apiException.WrongPhoneNum + } else if resp.Code == 110006 { + return apiException.SendVerificationCodeLimit + } else if resp.Code != 0 { + return apiException.ServerError } + m := resp.Data.(map[string]interface{}) if m["user_exists"] == false { return apiException.NotBindYxy @@ -118,8 +95,7 @@ func SendVerificationCode(securityToken, deviceId, captcha, phoneNum string) err } func LoginByCode(code, deviceId, phoneNum string) (*string, error) { - var form map[string]string - form = make(map[string]string) + form := make(map[string]string) form["phone_num"] = phoneNum form["code"] = code form["device_id"] = deviceId @@ -127,13 +103,15 @@ func LoginByCode(code, deviceId, phoneNum string) (*string, error) { if err != nil { return nil, err } - if resp.Code == 403 { + + if resp.Code == 110007 || resp.Code == 110008 { return nil, apiException.WrongVerificationCode - } else if resp.Code == 500 { + } else if resp.Code == 110005 { return nil, apiException.WrongPhoneNum } else if resp.Code != 0 { return nil, apiException.ServerError } + var data userInfo err = mapstructure.Decode(resp.Data, &data) if err != nil { @@ -142,22 +120,19 @@ func LoginByCode(code, deviceId, phoneNum string) (*string, error) { return &data.UID, nil } -func SilentLogin(deviceId, uid string) error { - var form map[string]string - form = make(map[string]string) +func SilentLogin(deviceId, uid, phoneNum string) error { + form := make(map[string]string) form["uid"] = uid form["device_id"] = deviceId + form["phone_num"] = phoneNum resp, err := FetchHandleOfPost(form, yxyApi.SlientLogin) if err != nil { return err } - if resp.Code == 403 { - fmt.Println(resp.Msg) + if resp.Code == 100101 || resp.Code == 100102 { return apiException.YxySessionExpired } else if resp.Code != 0 { return apiException.ServerError } - var data userInfo - err = mapstructure.Decode(resp.Data, &data) - return err + return nil } diff --git a/app/services/yxyServices/cacheService.go b/app/services/yxyServices/cacheService.go new file mode 100644 index 0000000..7015875 --- /dev/null +++ b/app/services/yxyServices/cacheService.go @@ -0,0 +1,61 @@ +package yxyServices + +import ( + "context" + "time" + r "wejh-go/config/redis" + + "golang.org/x/sync/singleflight" + + "github.com/go-redis/redis/v8" +) + +var ( + ctx = context.Background() + g singleflight.Group +) + +func GetElecRoomStrConcat(token, campus, yxyUid string) (*string, error) { + cacheKey := "elec:room_str_concat:" + campus + ":" + yxyUid + cachedRoomStrConcat, err := r.RedisClient.Get(ctx, cacheKey).Result() + if err == redis.Nil { + balance, err := ElectricityBalance(token, campus) + if err != nil { + return nil, err + } + err = r.RedisClient.Set(ctx, cacheKey, balance.RoomStrConcat, 7*24*time.Hour).Err() + if err != nil { + return nil, err + } + return &balance.RoomStrConcat, nil + } else if err != nil { + return nil, err + } + return &cachedRoomStrConcat, nil +} + +func GetElecAuthToken(yxyUid string) (*string, error) { + cacheKey := "elec:auth_token:" + yxyUid + cachedToken, err := r.RedisClient.Get(ctx, cacheKey).Result() + if err == redis.Nil { + // 使用 singleflight 防止缓存击穿 + token, err, _ := g.Do(cacheKey, func() (interface{}, error) { + t, e := Auth(yxyUid) + if e != nil { + return nil, e + } + e = r.RedisClient.Set(ctx, cacheKey, *t, 7*24*time.Hour).Err() + if e != nil { + return nil, e + } + return t, nil + }) + if err != nil { + return nil, err + } + return token.(*string), nil + } else if err != nil { + return nil, err + } + return &cachedToken, nil +} diff --git a/app/services/yxyServices/electricityService.go b/app/services/yxyServices/electricityService.go index 703dbf4..131b9ef 100644 --- a/app/services/yxyServices/electricityService.go +++ b/app/services/yxyServices/electricityService.go @@ -1,76 +1,35 @@ package yxyServices import ( - "github.com/mitchellh/mapstructure" "net/url" "wejh-go/app/apiException" "wejh-go/config/api/yxyApi" + + "github.com/mitchellh/mapstructure" ) type AuthResp struct { - Token string `json:"token"` - ID string `json:"id"` - MobilePhone string `json:"mobile_phone"` - Sex int `json:"sex"` - Platform string `json:"platform"` - ThirdOpenid string `json:"third_openid"` - SchoolCode string `json:"school_code"` - SchoolName string `json:"school_name"` - UserName string `json:"user_name"` - UserType string `json:"user_type"` - JobNo string `json:"job_no"` - UserIDCard string `json:"user_id_card"` - UserClass string `json:"user_class"` - BindCardStatus int `json:"bind_card_status"` + Token string `json:"token" mapstructure:"token"` } -type RoomInfo struct { - ID string `json:"id"` - SchoolCode string `json:"school_code"` - SchoolName string `json:"school_name"` - AreaID string `json:"area_id"` - AreaName string `json:"area_name"` - BuildingCode string `json:"building_code"` - BuildingName string `json:"building_name"` - FloorCode string `json:"floor_code"` - FloorName string `json:"floor_name"` - RoomCode string `json:"room_code"` - RoomName string `json:"room_name"` - BindType string `json:"bind_type"` - CreateTime string `json:"create_time"` +type ElecBalance struct { + DisplayRoomName string `json:"display_room_name" mapstructure:"display_room_name"` + RoomStrConcat string `json:"room_str_concat" mapstructure:"room_str_concat"` + Soc float64 `json:"soc" mapstructure:"surplus"` } -type EleBalance struct { - SchoolCode string `json:"school_code"` - AreaID string `json:"area_id"` - BuildingCode string `json:"building_code"` - FloorCode string `json:"floor_code"` - RoomCode string `json:"room_code"` - DisplayRoomName string `json:"display_room_name"` - Soc float64 `json:"soc"` - SocAmount float64 `json:"soc_amount"` - Surplus float64 `json:"surplus"` - SurplusAmount float64 `json:"surplus_amount"` - Subsidy int `json:"subsidy"` - SubsidyAmount int `json:"subsidy_amount"` - MdType string `json:"md_type"` - MdName string `json:"md_name"` - RoomStatus string `json:"room_status"` +type RechargeRecords struct { + List []struct { + Money string `json:"money" mapstructure:"money"` + Datetime string `json:"datetime" mapstructure:"datetime"` + } `json:"list" mapstructure:"list"` } -type RechargeRecords []struct { - RoomDm string `json:"room_dm"` - Datetime string `json:"datetime"` - BuyType string `json:"buy_type"` - UsingType string `json:"using_type"` - Money string `json:"money"` - IsSend string `json:"is_send"` -} - -type EleConsumptionRecords []struct { - RoomDm string `json:"room_dm"` - Datetime string `json:"datetime"` - Used string `json:"used"` +type EleConsumptionRecords struct { + List []struct { + Used string `json:"used" mapstructure:"usage"` + Datetime string `json:"datetime" mapstructure:"datetime"` + } `json:"list" mapstructure:"list"` } func Auth(uid string) (*string, error) { @@ -94,89 +53,86 @@ func Auth(uid string) (*string, error) { return &data.Token, nil } -func Bind(token string) (*map[string]interface{}, error) { +func ElectricityBalance(token, campus string) (*ElecBalance, error) { params := url.Values{} - Url, err := url.Parse(string(yxyApi.Bind)) + Url, err := url.Parse(string(yxyApi.ElectricityBalance)) if err != nil { return nil, err } params.Set("token", token) + params.Set("campus", campus) Url.RawQuery = params.Encode() urlPath := Url.String() resp, err := FetchHandleOfGet(yxyApi.YxyApi(urlPath)) if err != nil { return nil, err } - m := resp.Data.(map[string]interface{}) - return &m, nil -} - -func ElectricityBalance(token string) (*map[string]interface{}, error) { - params := url.Values{} - Url, err := url.Parse(string(yxyApi.ElectricityBalance)) - if err != nil { - return nil, err + if resp.Code == 110102 { + return nil, apiException.CampusMismatch + } else if resp.Code != 0 { + return nil, apiException.ServerError } - params.Set("token", token) - Url.RawQuery = params.Encode() - urlPath := Url.String() - resp, err := FetchHandleOfGet(yxyApi.YxyApi(urlPath)) + var data ElecBalance + err = mapstructure.Decode(resp.Data, &data) if err != nil { return nil, err } - if resp.Data == nil { - return nil, apiException.ServerError - } - m := resp.Data.(map[string]interface{}) - return &m, nil + return &data, nil } -func ElectricityRechargeRecords(token, areaId, buildingCode, floorCode, roomCode, page string) (*[]interface{}, error) { +func ElectricityRechargeRecords(token, campus, page, roomStrConcat string) (*RechargeRecords, error) { params := url.Values{} Url, err := url.Parse(string(yxyApi.RechargeRecords)) if err != nil { return nil, err } params.Set("token", token) - params.Set("area_id", areaId) - params.Set("building_code", buildingCode) - params.Set("floor_code", floorCode) - params.Set("room_code", roomCode) + params.Set("campus", campus) params.Set("page", page) + params.Set("room_str_concat", roomStrConcat) Url.RawQuery = params.Encode() urlPath := Url.String() resp, err := FetchHandleOfGet(yxyApi.YxyApi(urlPath)) if err != nil { return nil, err } - if resp.Data == nil { + if resp.Code == 110103 { + return nil, apiException.CampusMismatch + } else if resp.Code != 0 { return nil, apiException.ServerError } - m := resp.Data.([]interface{}) - return &m, nil + var data RechargeRecords + err = mapstructure.Decode(resp.Data, &data) + if err != nil { + return nil, err + } + return &data, nil } -func ElectricityConsumptionRecords(token, areaId, buildingCode, floorCode, roomCode, mdType string) (*[]interface{}, error) { +func ElectricityConsumptionRecords(token, campus, roomStrConcat string) (*EleConsumptionRecords, error) { params := url.Values{} Url, err := url.Parse(string(yxyApi.ElectricityConsumption)) if err != nil { return nil, err } params.Set("token", token) - params.Set("area_id", areaId) - params.Set("building_code", buildingCode) - params.Set("floor_code", floorCode) - params.Set("room_code", roomCode) - params.Set("md_type", mdType) + params.Set("campus", campus) + params.Set("room_str_concat", roomStrConcat) Url.RawQuery = params.Encode() urlPath := Url.String() resp, err := FetchHandleOfGet(yxyApi.YxyApi(urlPath)) if err != nil { return nil, err } - if resp.Data == nil { + if resp.Code == 110103 { + return nil, apiException.CampusMismatch + } else if resp.Code != 0 { return nil, apiException.ServerError } - m := resp.Data.([]interface{}) - return &m, nil + var data EleConsumptionRecords + err = mapstructure.Decode(resp.Data, &data) + if err != nil { + return nil, err + } + return &data, nil } diff --git a/app/services/yxyServices/schoolCardService.go b/app/services/yxyServices/schoolCardService.go index 556eccb..f8d9862 100644 --- a/app/services/yxyServices/schoolCardService.go +++ b/app/services/yxyServices/schoolCardService.go @@ -1,29 +1,26 @@ package yxyServices import ( - "github.com/mitchellh/mapstructure" "net/url" "wejh-go/app/apiException" "wejh-go/config/api/yxyApi" -) -const ZJUTSchoolCode = "10337" + "github.com/mitchellh/mapstructure" +) type BalanceResp struct { - Balance string `json:"balance"` + Balance string `json:"balance" mapstructure:"balance"` } -type ConsumptionRecords []struct { - Type string `json:"type"` - FeeName string `json:"fee_name"` - Time string `json:"time"` - SerialNo string `json:"serial_no"` - Money string `json:"money"` - DealTime string `json:"deal_time"` - Address string `json:"address"` +type ConsumptionRecords struct { + List []struct { + Address string `json:"address" mapstructure:"address"` + Money string `json:"money" mapstructure:"money"` + Time string `json:"time" mapstructure:"time"` + } `json:"list" mapstructure:"list"` } -func GetCardBalance(deviceId, uid string) (*string, error) { +func GetCardBalance(deviceId, uid, phoneNum string) (*string, error) { params := url.Values{} Url, err := url.Parse(string(yxyApi.CardBalance)) if err != nil { @@ -31,17 +28,20 @@ func GetCardBalance(deviceId, uid string) (*string, error) { } params.Set("device_id", deviceId) params.Set("uid", uid) - params.Set("school_code", ZJUTSchoolCode) Url.RawQuery = params.Encode() urlPath := Url.String() - _ = SilentLogin(deviceId, uid) + _ = SilentLogin(deviceId, uid, phoneNum) resp, err := FetchHandleOfGet(yxyApi.YxyApi(urlPath)) if err != nil { return nil, err } - if resp.Code != 0 { + + if resp.Code == 100101 || resp.Code == 100102 { return nil, apiException.YxySessionExpired + } else if resp.Code != 0 { + return nil, apiException.ServerError } + var data BalanceResp err = mapstructure.Decode(resp.Data, &data) if err != nil { @@ -50,7 +50,7 @@ func GetCardBalance(deviceId, uid string) (*string, error) { return &data.Balance, nil } -func GetCardConsumptionRecord(deviceId, uid, queryTime string) (ConsumptionRecords, error) { +func GetCardConsumptionRecord(deviceId, uid, phoneNum, queryTime string) (*ConsumptionRecords, error) { params := url.Values{} Url, err := url.Parse(string(yxyApi.ConsumptionRecords)) if err != nil { @@ -59,27 +59,26 @@ func GetCardConsumptionRecord(deviceId, uid, queryTime string) (ConsumptionRecor params.Set("device_id", deviceId) params.Set("uid", uid) params.Set("query_time", queryTime) - params.Set("school_code", ZJUTSchoolCode) Url.RawQuery = params.Encode() urlPath := Url.String() - _ = SilentLogin(deviceId, uid) + _ = SilentLogin(deviceId, uid, phoneNum) resp, err := FetchHandleOfGet(yxyApi.YxyApi(urlPath)) if err != nil { return nil, err } - if resp.Code == 204 { - return nil, nil - } else if resp.Code == 500 { + + if resp.Code == 100101 || resp.Code == 100102 { return nil, apiException.YxySessionExpired - } else if resp.Code == 403 { + } else if resp.Code == 100002 { return nil, apiException.ParamError } else if resp.Code != 0 { return nil, apiException.ServerError } + var data ConsumptionRecords err = mapstructure.Decode(resp.Data, &data) if err != nil { return nil, err } - return data, nil + return &data, nil } diff --git a/app/services/yxyServices/tokenService.go b/app/services/yxyServices/tokenService.go deleted file mode 100644 index d90218c..0000000 --- a/app/services/yxyServices/tokenService.go +++ /dev/null @@ -1,34 +0,0 @@ -package yxyServices - -import ( - "context" - "time" - "wejh-go/config/redis" -) - -var ctx = context.Background() - -func GetToken(key string) (*string, error) { - val, err := redis.RedisClient.Get(ctx, key).Result() - if err != nil { - return nil, err - } - return &val, nil -} - -func SetToken(key, value string) { - redis.RedisClient.Set(ctx, key, value, 15*time.Minute) -} - -func CheckToken(key string) bool { - intCmd := redis.RedisClient.Exists(ctx, key) - if intCmd.Val() == 1 { - return true - } else { - return false - } -} - -func DelToken(key string) { - redis.RedisClient.Del(ctx, key) -} diff --git a/config/api/yxyApi/yxyApi.go b/config/api/yxyApi/yxyApi.go index 0314403..cab3e14 100644 --- a/config/api/yxyApi/yxyApi.go +++ b/config/api/yxyApi/yxyApi.go @@ -7,16 +7,15 @@ var YxyHost = config.Config.GetString("yxy.host") type YxyApi string const ( - SecurityToken YxyApi = "v1/campus/login/security_token" - CaptchaImage YxyApi = "v1/campus/login/captcha_image" - SendVerificationCode YxyApi = "v1/campus/login/send_verification_code" - LoginByCode YxyApi = "v1/campus/login/by_code" - CardBalance YxyApi = "v1/campus/user/card_balance" - ConsumptionRecords YxyApi = "v1/campus/user/consumption_records" - Auth YxyApi = "v1/app/auth" - Bind YxyApi = "v1/app/electricity/bind" - ElectricityBalance YxyApi = "v1/app/electricity/subsidy/by_user" - RechargeRecords YxyApi = "v1/app/electricity/recharge/by_room" - ElectricityConsumption YxyApi = "v1/app/electricity/consumption" - SlientLogin YxyApi = "v1/campus/login/silent" + SecurityToken YxyApi = "api/v1/login/security-token" + CaptchaImage YxyApi = "api/v1/login/captcha-image" + SendVerificationCode YxyApi = "api/v1/login/send-code" + LoginByCode YxyApi = "api/v1/login/code" + SlientLogin YxyApi = "api/v1/login/silent" + CardBalance YxyApi = "api/v1/card/balance" + ConsumptionRecords YxyApi = "api/v1/card/consumption-records" + Auth YxyApi = "api/v1/electricity/auth" + ElectricityBalance YxyApi = "api/v1/electricity/surplus" + RechargeRecords YxyApi = "api/v1/electricity/recharge-records" + ElectricityConsumption YxyApi = "api/v1/electricity/usage-records" ) diff --git a/config/router/userRouter.go b/config/router/userRouter.go index ea467e7..bfddc48 100644 --- a/config/router/userRouter.go +++ b/config/router/userRouter.go @@ -26,9 +26,9 @@ func userRouterInit(r *gin.RouterGroup) { { bind.POST("/zf", userController.BindZFPassword) bind.POST("/library", userController.BindLibraryPassword) - bind.POST("/yxy/send/verification", userController.SendVerificationCode) - bind.POST("/yxy/send/captcha", userController.SendVerificationCodeByCaptcha) - bind.POST("/yxy/get/captcha", userController.GetCaptcha) + bind.POST("/yxy/get/captcha", userController.GetCaptcha) // 待废弃 + bind.POST("/yxy/send/captcha", userController.SendVerificationCode) // 兼容用,待删除 + bind.POST("/yxy/send/code", userController.SendVerificationCode) bind.POST("/yxy/login", userController.LoginYxy) bind.POST("/oauth", userController.BindOauthPassword) } diff --git a/go.mod b/go.mod index 5b87874..adae961 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( github.com/ugorji/go v1.2.5 // indirect github.com/xuri/excelize/v2 v2.8.0 go.opentelemetry.io/otel v0.20.0 // indirect + golang.org/x/sync v0.1.0 gopkg.in/ini.v1 v1.62.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gorm.io/driver/mysql v1.0.6 diff --git a/go.sum b/go.sum index d00d385..beb386b 100644 --- a/go.sum +++ b/go.sum @@ -406,6 +406,7 @@ golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=