You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
307 lines
8.0 KiB
Go
307 lines
8.0 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/base64"
|
|
e "errors"
|
|
"fmt"
|
|
"github.com/xuri/excelize/v2"
|
|
"gold-shop/errors"
|
|
"gold-shop/global"
|
|
"gold-shop/model"
|
|
"gold-shop/request"
|
|
"gold-shop/request/manage"
|
|
"gold-shop/utils"
|
|
"gold-shop/utils/excel"
|
|
"gold-shop/utils/result"
|
|
"gold-shop/utils/tlpay"
|
|
"gold-shop/utils/tlpay/param"
|
|
"gorm.io/gorm"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
var UserService = userService{}
|
|
|
|
type userService struct {
|
|
}
|
|
|
|
func (userService) Login(mobile string, password string) (string, error) {
|
|
user := &model.User{}
|
|
err := global.DB.Where("mobile", mobile).First(user).Error
|
|
if e.Is(err, gorm.ErrRecordNotFound) {
|
|
return "", errors.NewBusinessError("手机号或密码错误")
|
|
}
|
|
if utils.PasswordVerify(password, user.PasswordHash) {
|
|
return utils.GenerateToken(user.ID, "user")
|
|
}
|
|
return "", errors.NewBusinessError("手机号或密码错误")
|
|
}
|
|
|
|
func (userService) Register(mobile string, password string) error {
|
|
user := &model.User{}
|
|
err := global.DB.Where("mobile", mobile).First(user).Error
|
|
if err == nil {
|
|
return errors.NewBusinessError("手机号已注册")
|
|
}
|
|
user.Mobile = mobile
|
|
passwordHash, _ := utils.PasswordHash(password)
|
|
user.PasswordHash = passwordHash
|
|
user.Status = 1
|
|
return global.DB.Save(user).Error
|
|
}
|
|
|
|
func (userService) GetUserInfo(userId int) *model.User {
|
|
user := &model.User{}
|
|
global.DB.Where("id", userId).First(user)
|
|
return user
|
|
}
|
|
|
|
func (userService) ModifyPassword(password string, oldPassword string, user *model.User) error {
|
|
if !utils.PasswordVerify(oldPassword, user.PasswordHash) {
|
|
return errors.NewBusinessError("旧密码错误")
|
|
}
|
|
passwordHash, _ := utils.PasswordHash(password)
|
|
user.PasswordHash = passwordHash
|
|
return global.DB.Save(user).Error
|
|
}
|
|
|
|
func (userService) GetUserData(user *model.User) result.Data {
|
|
var orderCount int64
|
|
global.DB.Model(&model.Order{}).Where("user_id", user.ID).Count(&orderCount)
|
|
data := result.Data{
|
|
"orderCount": orderCount,
|
|
}
|
|
return data
|
|
}
|
|
|
|
func (s *userService) GetUsers(req manage.UserQueryRequest) ([]model.User, int64) {
|
|
users := make([]model.User, req.PageSize)
|
|
offset := (req.Page - 1) * req.PageSize
|
|
var total int64
|
|
tx := s.buildQuery(req)
|
|
tx.Order("id desc").Offset(offset).Limit(req.PageSize).Find(&users)
|
|
tx.Model(&model.User{}).Count(&total)
|
|
return users, total
|
|
}
|
|
|
|
func (userService) SaveAlipayAccount(alipayAccount string, user *model.User) error {
|
|
err := global.DB.Model(&model.User{}).Where("id", user.ID).Update("alipay_account", alipayAccount).Error
|
|
return err
|
|
}
|
|
|
|
func (s *userService) buildQuery(req manage.UserQueryRequest) *gorm.DB {
|
|
tx := global.DB
|
|
if req.Mobile != "" {
|
|
tx = tx.Where("mobile", req.Mobile)
|
|
}
|
|
if req.Status != "" {
|
|
tx = tx.Where("status", req.Status)
|
|
}
|
|
return tx
|
|
}
|
|
|
|
func (s *userService) GenerateUsersExcel(req manage.UserQueryRequest) *excelize.File {
|
|
pageSize := 200
|
|
lastID := 0
|
|
tx := s.buildQuery(req)
|
|
f := excel.NewFile()
|
|
headers := []string{"用户ID", "手机号", "支付宝账号", "账户余额", "用户状态", "注册时间"}
|
|
excel.SetSimpleHeaders(headers, "Sheet1", f)
|
|
for {
|
|
users := make([]model.User, pageSize)
|
|
tx.Where("id > ?", lastID).Limit(pageSize).Find(&users)
|
|
count := len(users)
|
|
if count == 0 {
|
|
break
|
|
}
|
|
s.buildUserListExcel(users, "Sheet1", f)
|
|
lastIndex := count - 1
|
|
lastID = users[lastIndex].ID
|
|
}
|
|
return f
|
|
}
|
|
|
|
func (userService) buildUserListExcel(users []model.User, sheet string, f *excelize.File) {
|
|
row := 2
|
|
for _, user := range users {
|
|
col := 0
|
|
f.SetCellValue(sheet, excel.CellKey(row, &col), user.ID)
|
|
f.SetCellValue(sheet, excel.CellKey(row, &col), user.Mobile)
|
|
f.SetCellValue(sheet, excel.CellKey(row, &col), user.AlipayAccount)
|
|
f.SetCellValue(sheet, excel.CellKey(row, &col), user.Balance)
|
|
f.SetCellValue(sheet, excel.CellKey(row, &col), user.GetStatusText())
|
|
f.SetCellValue(sheet, excel.CellKey(row, &col), user.CreatedAt.Format(time.DateTime))
|
|
row++
|
|
}
|
|
}
|
|
|
|
func (s *userService) TlBindingApply(req request.TlBindingApplyRequest, user *model.User) (string, error) {
|
|
thpInfo, err := s.doTlBindingApply(req, user)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
_, err = s.createBankCard(req, user)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return thpInfo, nil
|
|
}
|
|
|
|
func (s *userService) doTlBindingApply(req request.TlBindingApplyRequest, user *model.User) (string, error) {
|
|
acctType := "00"
|
|
if req.AcctType != "" {
|
|
acctType = req.AcctType
|
|
}
|
|
p := param.AgreeApplyParam{}
|
|
p.Mobile = req.Mobile
|
|
p.IdNo = req.IdNo
|
|
p.Cvv2 = req.Cvv2
|
|
p.AcctNo = req.AcctNo
|
|
p.AcctName = req.AcctName
|
|
p.ValidDate = req.ValidDate
|
|
p.MerUserID = strconv.Itoa(user.ID)
|
|
p.IdType = "0"
|
|
p.AcctType = acctType
|
|
p.ReqIp = "127.0.0.1"
|
|
p.Version = "11"
|
|
|
|
r, err := tlpay.TLPay.AgreeApply(p)
|
|
fmt.Println(r)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if r.RetCode != "SUCCESS" {
|
|
return "", errors.NewBusinessError(r.RetMsg)
|
|
}
|
|
if r.TrxStatus != "1999" {
|
|
return "", errors.NewBusinessError(r.ErrMsg)
|
|
}
|
|
return base64.StdEncoding.EncodeToString([]byte(r.ThpInfo)), nil
|
|
}
|
|
|
|
func (userService) createBankCard(req request.TlBindingApplyRequest, user *model.User) (*model.UserBankCard, error) {
|
|
c := model.UserBankCard{}
|
|
err := global.DB.Where("user_id", user.ID).Where("account_no", req.AcctNo).First(&c).Error
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, err
|
|
}
|
|
if err == nil && c.Status == 1 {
|
|
return nil, errors.NewBusinessError("该卡号已绑定")
|
|
}
|
|
c.AccountName = req.AcctName
|
|
c.IdCardNo = req.IdNo
|
|
c.AccountNo = req.AcctNo
|
|
c.Mobile = req.Mobile
|
|
c.Status = 0
|
|
c.UserID = user.ID
|
|
err = global.DB.Save(&c).Error
|
|
if err != nil {
|
|
return nil, errors.NewBusinessError("保存数据失败")
|
|
}
|
|
return &c, nil
|
|
}
|
|
|
|
func (s *userService) TlBindingConfirm(req request.TlBindingConfirmRequest, user *model.User) (result.Data, error) {
|
|
c := model.UserBankCard{}
|
|
err := global.DB.Where("user_id", user.ID).Where("account_no", req.AcctNo).First(&c).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
data, err := s.doTlBindingConfirm(req, user)
|
|
if err != nil {
|
|
c.Status = 2
|
|
c.ErrorMessage = err.Error()
|
|
global.DB.Save(&c)
|
|
return nil, err
|
|
}
|
|
c.AgreeID = data["agreeId"].(string)
|
|
c.Status = 1
|
|
c.ErrorMessage = "成功"
|
|
c.BankName = data["bankName"].(string)
|
|
c.BankCode = data["bankCode"].(string)
|
|
global.DB.Save(&c)
|
|
return data, nil
|
|
}
|
|
|
|
func (userService) doTlBindingConfirm(req request.TlBindingConfirmRequest, user *model.User) (result.Data, error) {
|
|
acctType := "00"
|
|
if req.AcctType != "" {
|
|
acctType = req.AcctType
|
|
}
|
|
thpInfo, err := base64.StdEncoding.DecodeString(req.ThpInfo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
p := param.AgreeConfirmParam{}
|
|
p.Mobile = req.Mobile
|
|
p.IdNo = req.IdNo
|
|
p.Cvv2 = req.Cvv2
|
|
p.AcctNo = req.AcctNo
|
|
p.AcctName = req.AcctName
|
|
p.SmsCode = req.SmsCode
|
|
p.ThpInfo = string(thpInfo)
|
|
p.ValidDate = req.ValidDate
|
|
p.MerUserID = strconv.Itoa(user.ID)
|
|
p.IdType = "0"
|
|
p.AcctType = acctType
|
|
p.ReqIp = "127.0.0.1"
|
|
p.Version = "11"
|
|
|
|
r, err := tlpay.TLPay.AgreeConfirm(p)
|
|
fmt.Println(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if r.RetCode != "SUCCESS" {
|
|
return nil, errors.NewBusinessError(r.RetMsg)
|
|
}
|
|
if r.TrxStatus != "0000" {
|
|
return nil, errors.NewBusinessError(r.ErrMsg)
|
|
}
|
|
data := result.Data{"agreeId": r.AgreeID, "bankCode": r.BankCode, "bankName": r.BankName}
|
|
return data, nil
|
|
}
|
|
|
|
func (s *userService) GetBankCardByUserID(userID int) (*model.UserBankCard, error) {
|
|
c := model.UserBankCard{}
|
|
err := global.DB.Where("user_id", userID).Where("status", 1).First(&c).Error
|
|
fmt.Println(err)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &c, nil
|
|
}
|
|
|
|
func (s *userService) BankCardUnbind(user *model.User) error {
|
|
c := model.UserBankCard{}
|
|
err := global.DB.Where("user_id", user.ID).Where("status", 1).First(&c).Error
|
|
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return errors.NewBusinessError("银行卡不存在")
|
|
}
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
p := param.UnbindParam{}
|
|
p.AgreeID = c.AgreeID
|
|
p.ReqIp = "127.0.0.1"
|
|
p.Version = "11"
|
|
r, err := tlpay.TLPay.Unbind(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if r.RetCode != "SUCCESS" {
|
|
return errors.NewBusinessError(r.RetMsg)
|
|
}
|
|
|
|
c.Status = 3
|
|
err = global.DB.Save(&c).Error
|
|
if err != nil {
|
|
return errors.NewBusinessError("更新失败")
|
|
}
|
|
return nil
|
|
}
|