✨ 重新初始化项目
This commit is contained in:
9
repository/login.go
Normal file
9
repository/login.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package repository
|
||||
|
||||
type login struct {
|
||||
}
|
||||
|
||||
// Login ...
|
||||
func Login() *login {
|
||||
return &login{}
|
||||
}
|
23
repository/oauth2_client.go
Normal file
23
repository/oauth2_client.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"Lee-WineList/client"
|
||||
"Lee-WineList/model/entity"
|
||||
)
|
||||
|
||||
type oauth2Client struct{}
|
||||
|
||||
// OAuth2Client ...
|
||||
func OAuth2Client() *oauth2Client {
|
||||
return &oauth2Client{}
|
||||
}
|
||||
|
||||
// FinAll 查询所有客户端
|
||||
func (oauth2Client) FinAll(clients *[]entity.OAuth2Client) error {
|
||||
return client.MySQL.Find(&clients).Error
|
||||
}
|
||||
|
||||
// FindOne 查询某一个
|
||||
func (oauth2Client) FindOne(c *entity.OAuth2Client) error {
|
||||
return client.MySQL.Take(&c, c).Error
|
||||
}
|
@@ -2,15 +2,10 @@ 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 {
|
||||
@@ -26,54 +21,16 @@ 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
|
||||
//err = client.MySQL.Take(&user, user).Error
|
||||
err = client.MySQL.Model(&user).Where("phone = ?", user.Phone).First(&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("登录失败")
|
||||
@@ -86,7 +43,7 @@ func (user) GetOrCreate(user *entity.User) (err error) {
|
||||
// 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
|
||||
err := client.MySQL.Model(&entity.User{}).Where("wechat_union_id = ? and wechat_open_id = ?", unionId, openId).Count(&count).Error
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
40
repository/wine.go
Normal file
40
repository/wine.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"Lee-WineList/client"
|
||||
"Lee-WineList/model/entity"
|
||||
"Lee-WineList/model/param"
|
||||
"git.echol.cn/loser/logger/log"
|
||||
)
|
||||
|
||||
type wine struct {
|
||||
}
|
||||
|
||||
// Wine ...
|
||||
func Wine() *wine {
|
||||
return &wine{}
|
||||
}
|
||||
|
||||
// GetWineList 获取酒单列表
|
||||
func (w *wine) GetWineList(p param.GetWineList) (wines []entity.Wine, err error) {
|
||||
db := client.MySQL.Model(&wines).Preload("Materials").Scopes(page(p.Current, p.Size))
|
||||
|
||||
if p.UserId != 0 {
|
||||
db = db.Where("user_id = ?", p.UserId)
|
||||
}
|
||||
if p.Category != "" {
|
||||
db = db.Where("category = ?", p.Category)
|
||||
}
|
||||
err = db.Find(&wines).Error
|
||||
if err != nil {
|
||||
log.Errorf("获取酒单列表失败:%s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (w *wine) Add(p param.AddWine) (err error) {
|
||||
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user