140 lines
3.7 KiB
Go
140 lines
3.7 KiB
Go
package user
|
|
|
|
import (
|
|
"common/remark"
|
|
"common/resultStatus"
|
|
"common/webServer"
|
|
"goutil/intUtil"
|
|
"goutil/securityUtil"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
func init() {
|
|
|
|
//注册接口
|
|
webServer.RegisterFunction(new(UserApi))
|
|
}
|
|
|
|
func init() {
|
|
moduleName := "UserApi"
|
|
desc := "用户接口"
|
|
author := "tangping"
|
|
mendor := ""
|
|
date := "2024-12-25"
|
|
remark.RegisterModuleRemark(moduleName, desc, author, mendor, date)
|
|
}
|
|
|
|
// UserApi 用户接口
|
|
type UserApi struct {
|
|
}
|
|
|
|
// ---------------------------------------- 接口 --------------------------------------------------
|
|
func init() {
|
|
moduleName := "UserApi"
|
|
methodName := "Register"
|
|
methodDesc := "注册用户"
|
|
methodAuthor := "tangping"
|
|
methodMendor := ""
|
|
methodDate := "2024-12-25 15:55:00"
|
|
methodInParam := []string{"string 账号,string:用户名称,string:用户密码,string:用户性别,string:用户生日,int64:用户手机号,string:用户邮箱,string:用户微信群,string:用户备注"}
|
|
methodOutParam := `
|
|
{
|
|
"Code '类型:int'": "响应结果的状态值",
|
|
"Message '类型:string'": "响应结果的状态值所对应的描述信息",
|
|
"Data '类型:interface{}'": "响应结果的数据"
|
|
{
|
|
"id '类型:int'": "用户id",
|
|
}
|
|
}`
|
|
remark.RegisterMethodRemark(moduleName, methodName, methodDesc, methodAuthor, methodMendor, methodDate, methodInParam, methodOutParam)
|
|
}
|
|
func (a *UserApi) Register(account string, name string, password string, sex int32, birthday string, phone int64, email string, wechatGroup string, describe string) (responseObj *webServer.ResponseObject) {
|
|
responseObj = webServer.GetInitResponseObj()
|
|
|
|
//校验参数
|
|
if account == "" || name == "" || password == "" || sex == 0 || birthday == "" || phone == 0 || email == "" || wechatGroup == "" {
|
|
responseObj.SetResultStatus(resultStatus.APIDataError)
|
|
return
|
|
}
|
|
|
|
//密码md5加密
|
|
password = securityUtil.Md5String(password, true)
|
|
|
|
//处理数据
|
|
UserModel := &User{
|
|
Account: account,
|
|
Name: name,
|
|
Password: password,
|
|
Sex: sex,
|
|
Birthday: birthday,
|
|
Phone: phone,
|
|
Email: email,
|
|
Describe: describe,
|
|
}
|
|
|
|
//添加到数据库
|
|
AddUser(UserModel)
|
|
|
|
resultMap := make(map[string]any)
|
|
resultMap["id"] = UserModel.ID
|
|
responseObj.SetData(resultMap)
|
|
return
|
|
}
|
|
|
|
// 用户登录
|
|
func init() {
|
|
moduleName := "UserApi"
|
|
methodName := "Login"
|
|
methodDesc := "用户登录"
|
|
methodAuthor := "tangping"
|
|
methodMendor := ""
|
|
methodDate := "2024-12-26 15:55:00"
|
|
methodInParam := []string{"string:账号,string:密码"}
|
|
methodOutParam := `
|
|
{
|
|
"Code '类型:int'": "响应结果的状态值",
|
|
"Message '类型:string'": "响应结果的状态值所对应的描述信息",
|
|
"Data '类型:interface{}'": "响应结果的数据"
|
|
{
|
|
"Token '类型:string'": "登录令牌",
|
|
}
|
|
}`
|
|
|
|
remark.RegisterMethodRemark(moduleName, methodName, methodDesc, methodAuthor, methodMendor, methodDate, methodInParam, methodOutParam)
|
|
}
|
|
|
|
func (a *UserApi) Login(account string, password string) (responseObj *webServer.ResponseObject) {
|
|
|
|
responseObj = webServer.GetInitResponseObj()
|
|
|
|
//验证参数
|
|
if account == "" || password == "" {
|
|
responseObj.SetResultStatus(resultStatus.APIDataError)
|
|
return
|
|
}
|
|
|
|
userData, err := Login(account, securityUtil.Md5String(password, true))
|
|
if err != nil {
|
|
responseObj.SetResultStatus(resultStatus.DataError)
|
|
return
|
|
}
|
|
|
|
//生成一个随机token
|
|
token, err := intUtil.ShuffleIntDigits(time.Now().UnixNano())
|
|
if err != nil {
|
|
responseObj.SetResultStatus(resultStatus.DataError)
|
|
return
|
|
}
|
|
|
|
//添加登录用户
|
|
webServer.AddLoginUserToken(token, &webServer.TokenInfo{Id: userData.ID, Account: account})
|
|
|
|
//UserData映射成map
|
|
resultMap := make(map[string]any)
|
|
resultMap["Token"] = strconv.FormatInt(token, 10)
|
|
|
|
responseObj.SetData(resultMap)
|
|
return
|
|
}
|