Vestmore_GO/server/app/internal/handlers/actions/auth.go

254 lines
6.2 KiB
Go
Raw Normal View History

2024-04-07 10:12:24 +00:00
package actions
import (
2024-04-11 10:04:03 +00:00
"strings"
2024-04-12 02:20:29 +00:00
"time"
2024-04-11 10:04:03 +00:00
2024-04-07 10:12:24 +00:00
"github.com/gin-gonic/gin"
2024-04-11 10:04:03 +00:00
"github.com/iapologizewhenimwrong/Vestmore_GO/model"
2024-04-09 10:17:08 +00:00
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/auth"
2024-04-09 05:38:29 +00:00
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/basic"
2024-04-09 10:17:08 +00:00
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/email"
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/encryption_decryption"
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/log"
2024-04-07 10:12:24 +00:00
)
2024-04-09 10:17:08 +00:00
var CompanyKey = "vestmore-bjwl"
2024-04-07 10:12:24 +00:00
// @Action base/getToken
// Base_GetToken
// action: string;
// app_market: string;
// lang: string;
// token: string;
2024-04-12 02:20:29 +00:00
func BaseGetToken(ctx *gin.Context, param *BaseGetTokenParam) (resp *basic.Response) {
2024-04-07 10:12:24 +00:00
ctx.ShouldBind(param)
2024-04-08 10:13:01 +00:00
// model.Models.KillaraCustomerModel.Find()
2024-04-07 10:12:24 +00:00
log.Println()
2024-04-12 02:20:29 +00:00
return resp.Success()
2024-04-07 10:12:24 +00:00
}
// @Action account/loginWithTelephonePassword
// AccountLoginWithTelephonePassword
2024-04-11 10:04:03 +00:00
// randstr: string;
// sign: string;
2024-04-07 10:12:24 +00:00
// telephone: string;
2024-04-11 10:04:03 +00:00
// version: string;.0.14
// token: string;
// country_code: string; 86
// password: string;
// action: string;
// device: string;,12
// app_market: uint64;
// email: string;
// timestamp: int64;
2024-04-12 02:20:29 +00:00
func AccountLoginWithTelephonePassword(ctx *gin.Context, param *AccountLoginWithTelephonePasswordParam) (resp *basic.Response) {
2024-04-07 10:12:24 +00:00
// ctx.ShouldBind()
2024-04-08 10:13:01 +00:00
// model.Models.KillaraCustomerModel.Find()
2024-04-11 10:04:03 +00:00
if param.CountryCode == "" {
2024-04-12 02:20:29 +00:00
resp.ErrorMsg(1, "country_code 参数缺失")
2024-04-11 10:04:03 +00:00
return
}
if param.Telephone == "" {
2024-04-12 02:20:29 +00:00
resp.ErrorMsg(1, "telephone 参数缺失")
2024-04-11 10:04:03 +00:00
return
}
if param.Password == "" {
2024-04-12 02:20:29 +00:00
resp.ErrorMsg(1, "password 参数缺失")
2024-04-11 10:04:03 +00:00
return
}
telephone := strings.TrimSpace(param.Telephone)
password := strings.TrimSpace(param.Password)
countryCode := strings.TrimSpace(param.CountryCode)
// 假设 modelCustomer 和 modelCustomerToken 是对应的服务接口
var customer *model.KillaraCustomer
var err error
customer, err = model.Models.KillaraCustomerModel.GetCustomerByTelephoneForBackEnd(telephone)
if err != nil {
2024-04-12 02:20:29 +00:00
resp.ErrorMsg(1, err.Error())
2024-04-11 10:04:03 +00:00
return
}
if customer == nil {
customer, err = model.Models.KillaraCustomerModel.GetCustomerByCode(telephone)
if err != nil {
2024-04-12 02:20:29 +00:00
resp.ErrorMsg(1, err.Error())
2024-04-11 10:04:03 +00:00
return
}
}
if customer == nil {
2024-04-12 02:20:29 +00:00
return resp.ErrorMsg(1, "账号未注册")
2024-04-11 10:04:03 +00:00
}
if *customer.CountryCode != countryCode {
2024-04-12 02:20:29 +00:00
return resp.ErrorMsg(1, "电话号码国家不正确")
2024-04-11 10:04:03 +00:00
}
if *customer.Status != 1 {
2024-04-12 02:20:29 +00:00
return resp.ErrorMsg(1, "账号已禁用,无法登录")
2024-04-11 10:04:03 +00:00
}
if !auth.CheckPassword(*customer.Password, *customer.Salt, *customer.RandomPassword, password) {
2024-04-12 02:20:29 +00:00
return resp.ErrorMsg(1, "账号或密码错误")
2024-04-11 10:04:03 +00:00
}
customerID := *customer.CustomerId
err = model.Models.KillaraCustomerTokenModel.UpdateTokenCustomerID(param.Token, customerID)
if err != nil {
2024-04-12 02:20:29 +00:00
return resp.ErrorMsg(1, err.Error())
2024-04-11 10:04:03 +00:00
}
var deviceCode string
if param.Device != "" {
deviceCode = param.Device
}
var version string
if param.Version != "" {
version = param.Version
}
var ip string
addr := strings.Split(ctx.Request.RemoteAddr, ":")
if len(addr) > 0 {
ip = addr[0]
}
log.Println(deviceCode, version, ip)
2024-04-12 02:20:29 +00:00
data := &model.KillaraCustomerDevice{
CustomerId: &customerID,
Device: &deviceCode,
Version: &version,
Ip: &ip,
AppMarket: &param.AppMarket,
Date: basic.TimePtr(time.Now()),
}
2024-04-11 10:04:03 +00:00
2024-04-12 02:20:29 +00:00
// 假设 insertCustomerDevice 是对应的服务接口
2024-04-11 10:04:03 +00:00
// insertCustomerDevice(data)
2024-04-12 02:20:29 +00:00
err = model.Models.KillaraCustomerModel.InsertCustomerDevice(data)
if err != nil {
return resp.ErrorErr(1, err)
}
2024-04-11 10:04:03 +00:00
// // 假设 clearDuplicateToken 是对应的服务接口
// clearDuplicateToken(customerID, param.Token)
2024-04-12 02:20:29 +00:00
model.Models.KillaraCustomerTokenModel.ClearDuplicateToken(customerID, param.Token, 1)
2024-04-11 10:04:03 +00:00
// return map[string]interface{}{
// "success": true,
// "error_code": 0,
// "error_text": "",
// "data": make(map[string]interface{}),
// }, nil
2024-04-08 10:13:01 +00:00
// log.Println(param)
2024-04-12 02:20:29 +00:00
return resp.Success()
2024-04-07 10:12:24 +00:00
}
// @Action account/registerSmsCode
// AccountRegisterSmsCode
// action: string;
// country_code?: string;
// telephone?: string;
// token: string;
2024-04-12 02:20:29 +00:00
func AccountRegisterSmsCode(ctx *gin.Context, param *AccountRegisterSmsCodeParam) (resp *basic.Response) {
2024-04-07 10:12:24 +00:00
// ctx.ShouldBind()
log.Println()
2024-04-12 02:20:29 +00:00
return resp.Success()
2024-04-07 10:12:24 +00:00
}
// @Action account/forgetSmsCode
// AccountForgetSmsCode
// action: string;
// country_code?: string;
// telephone?: string;
// token: string;
2024-04-12 02:20:29 +00:00
func AccountForgetSmsCode(ctx *gin.Context, param *AccountForgetSmsCodeParam) (resp *basic.Response) {
2024-04-07 10:12:24 +00:00
// ctx.ShouldBind()
log.Println()
2024-04-12 02:20:29 +00:00
return resp.Success()
2024-04-07 10:12:24 +00:00
}
2024-04-09 10:17:08 +00:00
type RegisterValidEmailCode struct {
Code string
Email string
AppMarket int
}
2024-04-07 10:12:24 +00:00
// @Action account/registerEmailCode
// AccountRegisterEmailCode
2024-04-09 05:38:29 +00:00
// randstr: string;
// sign: string;
// action: string;
// app_market: int;
// email: string;
// timestamp: int64;
// token: string;
2024-04-12 02:20:29 +00:00
func AccountRegisterEmailCode(ctx *gin.Context, param *AccountRegisterEmailCodeParam) (resp *basic.Response) {
2024-04-09 10:17:08 +00:00
log.Println(param)
if !email.IsEmailValid(param.Email) {
2024-04-12 02:20:29 +00:00
return resp.Error(basic.ErrEmailFormat)
2024-04-09 10:17:08 +00:00
}
gcm := encryption_decryption.NewSecretGCM[RegisterValidEmailCode](CompanyKey)
code := auth.GenerateVerificationCode()
codetoken := &RegisterValidEmailCode{
Code: code,
Email: param.Email,
AppMarket: param.AppMarket,
}
tokenstr, err := gcm.Encrypt(codetoken)
if err != nil {
2024-04-12 02:20:29 +00:00
return resp.Error(basic.ErrEncGcm)
2024-04-09 10:17:08 +00:00
}
2024-04-08 10:13:01 +00:00
2024-04-09 10:17:08 +00:00
resp.Success(map[string]any{
"token": tokenstr,
})
2024-04-12 02:20:29 +00:00
return resp.Success()
2024-04-07 10:12:24 +00:00
}
// @Action member/alterPassword
// MemberAlterPassword
// action: string;
// confirm_password: string;
// new_password: string;
// old_password: string;
// token?: string;
2024-04-12 02:20:29 +00:00
func MemberAlterPassword(ctx *gin.Context, param *MemberAlterPasswordParam) (resp *basic.Response) {
2024-04-07 10:12:24 +00:00
// ctx.ShouldBind()
log.Println()
2024-04-12 02:20:29 +00:00
return resp.Success()
2024-04-07 10:12:24 +00:00
}
// @Action account/loginWithEmailPassword
// AccountLoginWithEmailPassword
2024-04-09 05:38:29 +00:00
// password: string;
// randstr: string;
// sign: string;
// action: string;
// device: string;
// version: string;
// app_market: int;
// email: string;
// timestamp: int64;
// token: string;
2024-04-12 02:20:29 +00:00
func AccountLoginWithEmailPassword(ctx *gin.Context, param *AccountLoginWithEmailPasswordParam) (resp *basic.Response) {
2024-04-09 05:38:29 +00:00
log.Println(param)
2024-04-12 02:20:29 +00:00
return resp.Success()
2024-04-07 10:12:24 +00:00
}