✨ init project
This commit is contained in:
103
repository/base.go
Normal file
103
repository/base.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.echol.cn/loser/logger/log"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 分页组件
|
||||
func page(current, size int) func(db *gorm.DB) *gorm.DB {
|
||||
return func(db *gorm.DB) *gorm.DB {
|
||||
if current == 0 {
|
||||
current = 1
|
||||
}
|
||||
if size < 1 {
|
||||
size = 10
|
||||
}
|
||||
// 计算偏移量
|
||||
offset := (current - 1) * size
|
||||
// 返回组装结果
|
||||
return db.Offset(offset).Limit(size)
|
||||
}
|
||||
}
|
||||
|
||||
// @title updateSortBefore
|
||||
// @description 更新之前处理序号
|
||||
// @param tx *gorm.DB "已开启的事务对象"
|
||||
// @param model any "模型对象"
|
||||
// @return error "错误信息"
|
||||
func updateSortBefore(tx *gorm.DB, tableName, id string, sort int, param string) (err error) {
|
||||
// 查出原来的排序号
|
||||
var oldSort int
|
||||
err = tx.Table(tableName).Select("sort").Where("id = ?", id).Scan(&oldSort).Error
|
||||
if err != nil {
|
||||
log.Errorf("查询老数据失败: %v", err.Error())
|
||||
return
|
||||
}
|
||||
// 如果相等,啥都不干
|
||||
if oldSort == sort {
|
||||
return nil
|
||||
}
|
||||
// 处理排序
|
||||
// 如果老的排序号小于新的,(老, 新]之间的排序号都要-1
|
||||
// 如果老的大于新的,[老, 新)排序号-1
|
||||
if oldSort < sort {
|
||||
// 老的小于新的,[老, 新) + 1
|
||||
sel := tx.Table(tableName).
|
||||
Where("sort <= ? AND sort > ?", sort, oldSort).
|
||||
Where("deleted_at IS NULL")
|
||||
if param != "" {
|
||||
sel.Where(param) // 自定义条件
|
||||
}
|
||||
err = sel.Update("sort", gorm.Expr("sort - 1")).Error
|
||||
} else {
|
||||
// 老的大于新的,[新, 老) + 1
|
||||
sel := tx.Table(tableName).
|
||||
Where("sort >= ? AND sort < ?", sort, oldSort).
|
||||
Where("deleted_at IS NULL")
|
||||
if param != "" {
|
||||
sel.Where(param) // 自定义条件
|
||||
}
|
||||
err = sel.Update("sort", gorm.Expr("sort + 1")).Error
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// @title createSortBefore
|
||||
// @description 新建之前处理序号
|
||||
// @param tx *gorm.DB "已开启的事务对象"
|
||||
// @param model any "模型对象"
|
||||
// @return error "错误信息"
|
||||
func createSortBefore(tx *gorm.DB, tableName string, sort int, param string) (err error) {
|
||||
// 处理排序,如果没有传,就会是在最前面
|
||||
sel := tx.Table(tableName).Where("sort >= ?", sort).
|
||||
Where("deleted_at IS NULL")
|
||||
if param != "" {
|
||||
sel.Where(param)
|
||||
}
|
||||
err = sel.Update("sort", gorm.Expr("sort + 1")).Error
|
||||
if err != nil {
|
||||
log.Errorf("处理前置排序失败:%v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// @title dealSortAfter
|
||||
// @description 处理序号之后
|
||||
// @param tx *gorm.DB "已开启的事务对象"
|
||||
// @param modelName string "表名"
|
||||
// @return error "错误信息"
|
||||
func dealSortAfter(tx *gorm.DB, modelName, param string) (err error) {
|
||||
// 保存成功,刷新排序
|
||||
if param != "" {
|
||||
param += " AND "
|
||||
}
|
||||
sql := fmt.Sprintf("UPDATE %s a, (SELECT (@i := @i + 1) i, id FROM %s WHERE %s deleted_at IS NULL order by sort ASC) i, "+
|
||||
"(SELECT @i := 0) ir SET a.sort = i.i, updated_at=now() WHERE a.id = i.id", modelName, modelName, param)
|
||||
err = tx.Exec(sql).Error
|
||||
if err != nil {
|
||||
log.Errorf("刷新排序失败: %v", err.Error())
|
||||
}
|
||||
return
|
||||
}
|
99
repository/user.go
Normal file
99
repository/user.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"Lee-WineList/client"
|
||||
"Lee-WineList/config"
|
||||
"Lee-WineList/model/entity"
|
||||
"Lee-WineList/model/resp"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.echol.cn/loser/logger/log"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type user struct {
|
||||
}
|
||||
|
||||
// User ...
|
||||
func User() *user {
|
||||
return &user{}
|
||||
}
|
||||
|
||||
// GetUser 查询单个用户信息
|
||||
func (user) GetUser(user *entity.User) (err error) {
|
||||
return client.MySQL.Take(&user, user).Error
|
||||
}
|
||||
|
||||
// Login Code登录
|
||||
func (u *user) Login(code string) *entity.User {
|
||||
var acsJson resp.Code2SessionResult
|
||||
acs := resp.Code2Session{
|
||||
Code: code,
|
||||
AppId: config.Scd.Tencent.MiniApp.AppId,
|
||||
AppSecret: config.Scd.Tencent.MiniApp.AppSecret,
|
||||
}
|
||||
api := "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code"
|
||||
res, err := http.DefaultClient.Get(fmt.Sprintf(api, acs.AppId, acs.AppSecret, acs.Code))
|
||||
if err != nil {
|
||||
fmt.Println("微信登录凭证校验接口请求错误")
|
||||
return nil
|
||||
}
|
||||
if err := json.NewDecoder(res.Body).Decode(&acsJson); err != nil {
|
||||
fmt.Println("decoder error...")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 查看用户是否已经存在
|
||||
rows := client.MySQL.Where("open_id = ?", acsJson.OpenId).First(&entity.User{}).RowsAffected
|
||||
if rows == 0 {
|
||||
// 不存在,添加用户
|
||||
fmt.Println(acsJson.OpenId)
|
||||
user := entity.User{
|
||||
OpenId: acsJson.OpenId,
|
||||
}
|
||||
row := client.MySQL.Create(&user).RowsAffected
|
||||
if row == 0 {
|
||||
fmt.Println("add app user error...")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return &entity.User{OpenId: acsJson.OpenId}
|
||||
}
|
||||
|
||||
// GetOrCreate 查询或创建用户
|
||||
func (user) GetOrCreate(user *entity.User) (err error) {
|
||||
err = client.MySQL.Take(&user, user).Error
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
// 如果是没查询到记录,则创建用户
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
// 用户不存在,创建用户
|
||||
user.Nickname = "用户"
|
||||
//user.Avatar = "https://hyxc-mini.oss-cn-beijing.aliyuncs.com/application/resource/miniapp/index/index-more-love.png"
|
||||
user.Avatar = "https://hyxc-mini.oss-cn-beijing.aliyuncs.com/avatar/mona-loading-dark.gif"
|
||||
if err = client.MySQL.Create(&user).Error; err != nil {
|
||||
log.Errorf("账号创建失败: %v", err)
|
||||
err = errors.New("登录失败")
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// CheckUnionIdIsExist 检查UnionId和OpenId是否存在
|
||||
func (user) CheckUnionIdIsExist(unionId, openId string) bool {
|
||||
var count int64
|
||||
err := client.MySQL.Model(&entity.User{}).Where("union_id = ? and open_id = ?", unionId, openId).Count(&count).Error
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return count > 0
|
||||
}
|
||||
|
||||
// UpdateUserInfo 更新普通用户信息
|
||||
func (user) UpdateUserInfo(e *entity.User) (err error) {
|
||||
return client.MySQL.Updates(&e).Error
|
||||
}
|
Reference in New Issue
Block a user