Skip to content

Commit

Permalink
Merge pull request #86 from XiMo-210/main
Browse files Browse the repository at this point in the history
重构易校园接口至yxy-go
  • Loading branch information
XiMo-210 authored Nov 16, 2024
2 parents 656d4c8 + c4dcfb1 commit 3d9101e
Show file tree
Hide file tree
Showing 13 changed files with 301 additions and 369 deletions.
60 changes: 31 additions & 29 deletions app/apiException/apiException.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
95 changes: 21 additions & 74 deletions app/controllers/userController/bind.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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"`
Expand Down Expand Up @@ -91,93 +84,48 @@ 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
}
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 {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit 3d9101e

Please sign in to comment.