✨ Init
This commit is contained in:
18
api/v1/app/enter.go
Normal file
18
api/v1/app/enter.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package app
|
||||
|
||||
import "miniapp/service"
|
||||
|
||||
type ApiGroup struct {
|
||||
UserApi
|
||||
LoginApi
|
||||
FavoriteApi
|
||||
VisionApi
|
||||
TodosApi
|
||||
}
|
||||
|
||||
var (
|
||||
userService = service.ServiceGroupApp.AppServiceGroup.UserService
|
||||
favoriteService = service.ServiceGroupApp.AppServiceGroup.FavoriteService
|
||||
visionService = service.ServiceGroupApp.AppServiceGroup.VisionService
|
||||
todosService = service.ServiceGroupApp.AppServiceGroup.TodesService
|
||||
)
|
80
api/v1/app/favorite.go
Normal file
80
api/v1/app/favorite.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"miniapp/global"
|
||||
"miniapp/model/app"
|
||||
"miniapp/model/app/request"
|
||||
r "miniapp/model/common/response"
|
||||
)
|
||||
|
||||
type FavoriteApi struct{}
|
||||
|
||||
// GetList 获取收藏列表
|
||||
func (f *FavoriteApi) GetList(ctx *gin.Context) {
|
||||
var p request.GetFavoriteList
|
||||
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
global.GVA_LOG.Error("获取收藏列表失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
// 获取收藏列表
|
||||
err, list, total := favoriteService.GetFavoriteList(p)
|
||||
if err != nil {
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
global.GVA_LOG.Error("获取收藏列表失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
r.OkWithData(r.PageResult{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: p.Page,
|
||||
PageSize: p.PageSize,
|
||||
}, ctx)
|
||||
}
|
||||
|
||||
// Create 创建收藏
|
||||
func (f *FavoriteApi) Create(ctx *gin.Context) {
|
||||
var p app.Favorite
|
||||
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
global.GVA_LOG.Error("创建收藏失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
// 创建收藏
|
||||
err := favoriteService.CreateFavorite(&p)
|
||||
if err != nil {
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
global.GVA_LOG.Error("创建收藏失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
r.Ok(ctx)
|
||||
}
|
||||
|
||||
// Delete 删除收藏
|
||||
func (f *FavoriteApi) Delete(ctx *gin.Context) {
|
||||
var p app.Favorite
|
||||
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
global.GVA_LOG.Error("删除收藏失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
// 删除收藏
|
||||
err := favoriteService.DeleteFavorite(&p)
|
||||
if err != nil {
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
global.GVA_LOG.Error("删除收藏失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
r.Ok(ctx)
|
||||
}
|
120
api/v1/app/login.go
Normal file
120
api/v1/app/login.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.echol.cn/loser/logger/log"
|
||||
"github.com/gin-gonic/gin"
|
||||
"miniapp/global"
|
||||
"miniapp/model/app/request"
|
||||
"miniapp/model/common/constant"
|
||||
r "miniapp/model/common/response"
|
||||
"miniapp/oauth2"
|
||||
"miniapp/service"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type LoginApi struct{}
|
||||
|
||||
// Login 登录
|
||||
func (l *LoginApi) Login(ctx *gin.Context) {
|
||||
var p request.Login
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
return
|
||||
}
|
||||
// 获取用户基础账号信息
|
||||
|
||||
userId, err := oauth2.OAuthServer.UserAuthorizationHandler(ctx.Writer, ctx.Request)
|
||||
if err != nil {
|
||||
log.Errorf("获取用户基础账号信息失败: %v", err)
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
return
|
||||
}
|
||||
// 重新组装登录参数
|
||||
ctx.Request.Form = url.Values{
|
||||
"username": {userId},
|
||||
"password": {p.Password},
|
||||
"scope": {"ALL"},
|
||||
"grant_type": {"password"},
|
||||
"nickName": {p.NickName},
|
||||
"avatarUrl": {p.AvatarUrl},
|
||||
}
|
||||
|
||||
// 参数解析成功,进行登录
|
||||
if err = oauth2.OAuthServer.HandleTokenRequest(ctx.Writer, ctx.Request); err != nil {
|
||||
log.Errorf("登录失败:%s", err.Error())
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
return
|
||||
}
|
||||
// 登录成功才更新登录时间
|
||||
if ctx.Writer.Status() == http.StatusOK {
|
||||
// 登录成功,更新登录时间和IP
|
||||
uid, _ := strconv.Atoi(userId)
|
||||
go service.ServiceGroupApp.AppServiceGroup.UserService.UpdateLastLoginInfo(uid)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Refresh 刷新登录Token
|
||||
func (l *LoginApi) Refresh(ctx *gin.Context) {
|
||||
var p request.RefreshToken
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
r.FailWithMessage("参数错误: "+err.Error(), ctx)
|
||||
return
|
||||
}
|
||||
|
||||
// 刷新Token
|
||||
if err := oauth2.OAuthServer.HandleTokenRequest(ctx.Writer, ctx.Request); err != nil {
|
||||
log.Errorf("Token数据返回失败: %v", err.Error())
|
||||
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// Logout 退出登录
|
||||
func (l *LoginApi) Logout(ctx *gin.Context) {
|
||||
log.Debug("退出登录啦")
|
||||
// Token字符串前缀
|
||||
const bearerSchema string = "Bearer "
|
||||
// 取出Token
|
||||
tokenHeader := ctx.Request.Header.Get("Authorization")
|
||||
tokenStr := tokenHeader[len(bearerSchema):]
|
||||
// 取出原始RedisKey
|
||||
baseDataId, err := global.GVA_REDIS.Get(context.Background(), constant.OAuth2RedisKey+tokenStr).Result()
|
||||
if err != nil {
|
||||
r.FailWithMessage("Token信息获取失败", ctx)
|
||||
return
|
||||
}
|
||||
baseDataStr, err := global.GVA_REDIS.Get(context.Background(), constant.OAuth2RedisKey+baseDataId).Result()
|
||||
if err != nil {
|
||||
r.FailWithMessage("Token信息获取失败", ctx)
|
||||
return
|
||||
}
|
||||
// 转换数据为Map
|
||||
tokenData := make(map[string]any)
|
||||
if err = json.Unmarshal([]byte(baseDataStr), &tokenData); err != nil {
|
||||
r.FailWithMessage("系统错误: "+err.Error(), ctx)
|
||||
return
|
||||
}
|
||||
// 删除Redis缓存的数据
|
||||
global.GVA_REDIS.Del(context.Background(), constant.OAuth2RedisKey+baseDataId)
|
||||
global.GVA_REDIS.Del(context.Background(), fmt.Sprintf("%v%v", constant.OAuth2RedisKey, tokenData["Access"]))
|
||||
global.GVA_REDIS.Del(context.Background(), fmt.Sprintf("%v%v", constant.OAuth2RedisKey, tokenData["Refresh"]))
|
||||
|
||||
r.Ok(ctx)
|
||||
}
|
||||
|
||||
func (l *LoginApi) GetWeChatToken(c *gin.Context) {
|
||||
wechaetToken, err := http.Get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3d38ce1103a82225&secret=3c41ca428b4d0f43cfaef6f567a1cc06")
|
||||
if err != nil {
|
||||
log.Errorf("获取微信Token失败: %v", err)
|
||||
r.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
r.OkWithData(wechaetToken, c)
|
||||
}
|
49
api/v1/app/todos.go
Normal file
49
api/v1/app/todos.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"miniapp/api"
|
||||
"miniapp/global"
|
||||
"miniapp/model/app"
|
||||
"miniapp/model/common"
|
||||
r "miniapp/model/common/response"
|
||||
)
|
||||
|
||||
type TodosApi struct{}
|
||||
|
||||
func (t TodosApi) GetUserTodos(ctx *gin.Context) {
|
||||
|
||||
var ue app.User
|
||||
if api.GetUser(ctx, &ue, false, true); ctx.IsAborted() {
|
||||
return
|
||||
}
|
||||
|
||||
list, err := todosService.GetUserTodos(ue.ID)
|
||||
if err != nil {
|
||||
r.FailWithMessage("获取Todo列表失败:"+err.Error(), ctx)
|
||||
global.GVA_LOG.Error("获取Todo列表失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
r.OkWithData(list, ctx)
|
||||
}
|
||||
|
||||
// UpdateTodo 更新Todo
|
||||
func (t TodosApi) UpdateTodo(ctx *gin.Context) {
|
||||
var todo common.UserTodo
|
||||
if err := ctx.ShouldBind(&todo); err != nil {
|
||||
r.FailWithMessage("参数错误:"+err.Error(), ctx)
|
||||
global.GVA_LOG.Error("参数错误", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
// 更新
|
||||
err := todosService.UpdateTodoById(&todo)
|
||||
if err != nil {
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
global.GVA_LOG.Error("更新失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
r.Ok(ctx)
|
||||
}
|
134
api/v1/app/user.go
Normal file
134
api/v1/app/user.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/logger/log"
|
||||
"github.com/gin-gonic/gin"
|
||||
"miniapp/api"
|
||||
"miniapp/global"
|
||||
"miniapp/model/app"
|
||||
"miniapp/model/app/request"
|
||||
"miniapp/model/app/response"
|
||||
r "miniapp/model/common/response"
|
||||
"miniapp/utils"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type UserApi struct {
|
||||
}
|
||||
|
||||
// GetUser 获取当前登录用户信息
|
||||
func (u *UserApi) GetUser(ctx *gin.Context) {
|
||||
// 取出当前登录用户
|
||||
var ue app.User
|
||||
if api.GetUser(ctx, &ue, false, true); ctx.IsAborted() {
|
||||
return
|
||||
}
|
||||
// 转换为VO
|
||||
var v response.UserVO
|
||||
v.ParseOrdinary(ue)
|
||||
|
||||
r.OkWithData(v, ctx)
|
||||
}
|
||||
|
||||
// BindingWeChat 绑定微信
|
||||
func (u *UserApi) BindingWeChat(ctx *gin.Context) {
|
||||
var p request.BindingWeChat
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
r.FailWithMessage("参数错误"+err.Error(), ctx)
|
||||
return
|
||||
}
|
||||
|
||||
// 取出当前登录用户
|
||||
var loginUser app.User
|
||||
if api.GetUser(ctx, &loginUser, true, true); ctx.IsAborted() {
|
||||
return
|
||||
}
|
||||
|
||||
// 解析出UnionId和OpenId
|
||||
unionId, openId, _, err := utils.WeChatUtils().GetWechatUnionId(p.Code)
|
||||
if err != nil {
|
||||
log.Errorf("获取微信UnionId失败:%s", err.Error())
|
||||
r.FailWithMessage("系统错误,请稍后再试", ctx)
|
||||
return
|
||||
}
|
||||
|
||||
// 解析成功,修改用户信息
|
||||
loginUser.WechatUnionId = &unionId
|
||||
loginUser.WechatOpenId = &openId
|
||||
if err = userService.UpdateUserInfo(&loginUser); err != nil {
|
||||
log.Errorf("修改用户信息失败:%s", err.Error())
|
||||
r.FailWithMessage("系统错误,请稍后再试", ctx)
|
||||
return
|
||||
}
|
||||
r.Ok(ctx)
|
||||
}
|
||||
|
||||
// UpdateUser 修改用户信息
|
||||
func (u *UserApi) UpdateUser(ctx *gin.Context) {
|
||||
var p request.ChangeUserInfo
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
r.FailWithMessage("参数错误: "+err.Error(), ctx)
|
||||
return
|
||||
}
|
||||
// 获取当前登录用户
|
||||
var loginUser app.User
|
||||
if api.GetUser(ctx, &loginUser, false, true); ctx.IsAborted() {
|
||||
return
|
||||
}
|
||||
// 修改资料
|
||||
|
||||
if p.Nickname != "" {
|
||||
loginUser.Nickname = p.Nickname
|
||||
}
|
||||
if p.Avatar != "" && strings.HasPrefix(p.Avatar, "http") {
|
||||
loginUser.Avatar = p.Avatar
|
||||
}
|
||||
if p.Phone != "" {
|
||||
loginUser.Phone = p.Phone
|
||||
}
|
||||
// 修改数据
|
||||
if err := userService.UpdateUserInfo(&loginUser); err != nil {
|
||||
log.Errorf("修改用户信息失败:%s", err.Error())
|
||||
r.FailWithMessage("修改用户信息失败"+err.Error(), ctx)
|
||||
return
|
||||
}
|
||||
// 操作成功,更新头像和昵称
|
||||
r.Ok(ctx)
|
||||
}
|
||||
|
||||
// UpdateUserHospital 修改用户医院信息
|
||||
func (u *UserApi) UpdateUserHospital(ctx *gin.Context) {
|
||||
var p request.ChangeUserHospital
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
r.FailWithMessage("参数错误: "+err.Error(), ctx)
|
||||
return
|
||||
}
|
||||
|
||||
// 修改数据
|
||||
if err := userService.UpdateUserHospital(&p); err != nil {
|
||||
log.Errorf("修改用户信息失败:%s", err.Error())
|
||||
r.FailWithMessage("修改用户信息失败"+err.Error(), ctx)
|
||||
return
|
||||
}
|
||||
// 操作成功,更新头像和昵称
|
||||
r.Ok(ctx)
|
||||
}
|
||||
|
||||
// GetInfo 获取用户信息
|
||||
func (u *UserApi) GetInfo(ctx *gin.Context) {
|
||||
id := ctx.Param("id")
|
||||
if id == "" {
|
||||
global.GVA_LOG.Error("参数错误")
|
||||
r.FailWithMessage("参数错误", ctx)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
userInfo, err := userService.GetUserInfo(id)
|
||||
if err != nil {
|
||||
log.Errorf("获取用户信息失败:%s", err.Error())
|
||||
r.FailWithMessage("获取用户信息失败"+err.Error(), ctx)
|
||||
return
|
||||
}
|
||||
r.OkWithData(userInfo, ctx)
|
||||
}
|
56
api/v1/app/vision.go
Normal file
56
api/v1/app/vision.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"miniapp/global"
|
||||
"miniapp/model/app"
|
||||
"miniapp/model/app/request"
|
||||
r "miniapp/model/common/response"
|
||||
)
|
||||
|
||||
type VisionApi struct{}
|
||||
|
||||
func (VisionApi) GetList(ctx *gin.Context) {
|
||||
var p request.VisionListRequest
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
r.FailWithMessage("参数错误:"+err.Error(), ctx)
|
||||
global.GVA_LOG.Error("参数错误", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
// 获取列表
|
||||
list, total, err := visionService.GetVisionList(p)
|
||||
if err != nil {
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
global.GVA_LOG.Error("获取列表失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
r.OkWithData(r.PageResult{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: p.Page,
|
||||
PageSize: p.PageSize,
|
||||
}, ctx)
|
||||
}
|
||||
|
||||
func (VisionApi) Create(ctx *gin.Context) {
|
||||
var p app.Vision
|
||||
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
global.GVA_LOG.Error("参数错误", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
// 创建
|
||||
err := visionService.CreateVision(&p)
|
||||
if err != nil {
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
global.GVA_LOG.Error("创建失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
r.Ok(ctx)
|
||||
}
|
Reference in New Issue
Block a user