初始
This commit is contained in:
200
internal/app/wxApplet/controller/wx_login.go
Normal file
200
internal/app/wxApplet/controller/wx_login.go
Normal file
@ -0,0 +1,200 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/crypto/gmd5"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/util/grand"
|
||||
"github.com/medivhzhan/weapp/v3"
|
||||
"github.com/medivhzhan/weapp/v3/auth"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/common/coryCommon"
|
||||
"github.com/tiger1103/gfast/v3/api/wxApplet/wxApplet"
|
||||
systemController "github.com/tiger1103/gfast/v3/internal/app/system/controller"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/model/do"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/model/entity"
|
||||
se "github.com/tiger1103/gfast/v3/internal/app/system/service"
|
||||
wxDao "github.com/tiger1103/gfast/v3/internal/app/wxApplet/dao"
|
||||
wxModel "github.com/tiger1103/gfast/v3/internal/app/wxApplet/model/do"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/wxApplet/service"
|
||||
"github.com/tiger1103/gfast/v3/library/libUtils"
|
||||
"github.com/tiger1103/gfast/v3/library/liberr"
|
||||
)
|
||||
|
||||
type WXLoginController struct {
|
||||
systemController.BaseController
|
||||
}
|
||||
|
||||
var WXLogin = new(WXLoginController)
|
||||
|
||||
func (e *WXLoginController) BusLogin(ctx context.Context, req *wxApplet.LoginReq) (res *wxApplet.LoginRes, err error) {
|
||||
res = &wxApplet.LoginRes{}
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
appId, err := g.Cfg().Get(gctx.New(), "wx.appId")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
appSecret, err := g.Cfg().Get(gctx.New(), "wx.appSecret")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
sdk := weapp.NewClient(appId.String(), appSecret.String())
|
||||
cli := sdk.NewAuth()
|
||||
//// 登录凭证校验
|
||||
rsp, err := cli.Code2Session(&auth.Code2SessionRequest{
|
||||
Appid: appId.String(),
|
||||
Secret: appSecret.String(),
|
||||
JsCode: req.Code,
|
||||
GrantType: "authorization_code",
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if rsp.ErrCode != 0 {
|
||||
err = rsp.GetResponseError()
|
||||
return
|
||||
}
|
||||
user := wxApplet.BusConstructionUserAddReq{}
|
||||
user.Openid = rsp.Openid
|
||||
res.Openid = user.Openid
|
||||
value, err := dao.BusConstructionUser.Ctx(ctx).Where("openid=?", user.Openid).Fields("head_icon").Value()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
result := 0
|
||||
if value != nil {
|
||||
result = 1
|
||||
}
|
||||
// 上传微信头像
|
||||
str, err := coryCommon.UploadFileTwo(ctx, req.HeadIcon, coryCommon.Helmet)
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
replace := strings.Replace(str, "resource/public", "/wxfile", 1)
|
||||
// 删除以前的头像
|
||||
if strings.Contains(value.String(), "/wxfile/") {
|
||||
s := strings.Replace(value.String(), "/wxfile/", coryCommon.GetCWD()+"/resource/public/", 1)
|
||||
os.Remove(filepath.ToSlash(s))
|
||||
}
|
||||
if result == 0 {
|
||||
// user.HeadIcon = req.HeadIcon
|
||||
//user.NickName = req.NickName
|
||||
//user.HeadIcon = replace
|
||||
_, err := service.BusConstructionUser().Add(ctx, &user)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
_, err = dao.BusConstructionUser.Ctx(ctx).Data(g.Map{
|
||||
//"head_icon": req.HeadIcon,
|
||||
"nick_name": req.NickName,
|
||||
"head_icon": replace,
|
||||
}).Where("openid=?", user.Openid).Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
res.Token = gettoken(ctx, user.Openid, user)
|
||||
return
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
//func gettoken(ctx context.Context, openid string, user wxApplet.BusConstructionUserAddReq) string {
|
||||
// key := gmd5.MustEncryptString(openid)
|
||||
// token, err := se.GfToken().GenerateToken(ctx, key+key, user)
|
||||
// fmt.Println("创建的token", token)
|
||||
// fmt.Println(err)
|
||||
// token = "Bearer " + token
|
||||
// fmt.Println("更改的-----", token)
|
||||
// return token
|
||||
//}
|
||||
|
||||
func gettoken(ctx context.Context, openid string, user wxApplet.BusConstructionUserAddReq) string {
|
||||
encryptedKey := gmd5.MustEncryptString(openid)
|
||||
|
||||
key := openid + "-" + encryptedKey
|
||||
authToken, _ := se.GfToken().GenerateToken(ctx, key, user)
|
||||
|
||||
return "Bearer " + authToken
|
||||
}
|
||||
|
||||
// IsBindApp 是否绑定 App
|
||||
func (e *WXLoginController) IsBindApp(ctx context.Context, req *wxApplet.IsBindAppReq) (*wxApplet.IsBindAppRes, error) {
|
||||
exist, err := dao.BusConstructionUser.Ctx(ctx).As("bcu").
|
||||
InnerJoin("sys_user AS su", `bcu.user_name COLLATE utf8mb4_general_ci = su.user_name COLLATE utf8mb4_general_ci
|
||||
AND bcu.phone COLLATE utf8mb4_general_ci = su.mobile COLLATE utf8mb4_general_ci`).
|
||||
Where("bcu.openid", req.Openid).
|
||||
Count()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 如果已绑定 App 则返回 1,否则返回 0
|
||||
if exist > 0 {
|
||||
return &wxApplet.IsBindAppRes{IsBind: 1}, nil
|
||||
}
|
||||
|
||||
return &wxApplet.IsBindAppRes{IsBind: 0}, nil
|
||||
}
|
||||
|
||||
// AppletBindApp 小程序绑定 App
|
||||
func (e *WXLoginController) AppletBindApp(ctx context.Context, req *wxApplet.AppletBindAppReq) (*wxApplet.AppletBindAppRes, error) {
|
||||
// 获取小程序用户信息
|
||||
busConstructUser := entity.BusConstructionUser{}
|
||||
if err := dao.BusConstructionUser.Ctx(ctx).Where(dao.BusConstructionUser.Columns().Openid, req.Openid).Scan(&busConstructUser); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 判断当前用户的手机号和用户名是否为空
|
||||
if busConstructUser.Phone == "" || busConstructUser.UserName == "" {
|
||||
return nil, fmt.Errorf("当前用户的手机号和用户名为空")
|
||||
}
|
||||
|
||||
exist, err := dao.SysUser.Ctx(ctx).Where(dao.SysUser.Columns().UserNickname, busConstructUser.UserName).
|
||||
Where(dao.SysUser.Columns().Mobile, busConstructUser.Phone).Count()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 判断是否已绑定 App
|
||||
if exist > 0 {
|
||||
return nil, fmt.Errorf("当前用户已经绑定了 App")
|
||||
}
|
||||
|
||||
passwordSalt := grand.S(10) // 密码盐值
|
||||
encryptedPassword := libUtils.EncryptPassword(req.Password, passwordSalt) // 加密密码
|
||||
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
// 创建用户
|
||||
userId, err := dao.SysUser.Ctx(ctx).InsertAndGetId(do.SysUser{
|
||||
UserName: busConstructUser.UserName, // 用户名
|
||||
UserNickname: busConstructUser.NickName, // 用户昵称
|
||||
Mobile: busConstructUser.Phone, // 手机号
|
||||
UserPassword: encryptedPassword, // 密码
|
||||
UserSalt: passwordSalt, // 密码盐
|
||||
Sex: busConstructUser.Sex, // 性别
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "创建用户失败")
|
||||
|
||||
// 关联后台项目
|
||||
_, err = wxDao.SysUserProjectRelevancy.Ctx(ctx).Insert(wxModel.SysUserProjectRelevancy{
|
||||
UserId: userId, // 用户 ID
|
||||
ProjectId: busConstructUser.ProjectId, // 项目 ID
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "关联后台管理项目失败")
|
||||
})
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
Reference in New Issue
Block a user