You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
1.9 KiB
88 lines
1.9 KiB
package users
|
|
|
|
import (
|
|
"recook/internal/back"
|
|
"recook/internal/dbc"
|
|
"recook/internal/define"
|
|
"recook/internal/model/user"
|
|
service "recook/internal/service/app"
|
|
"recook/tools"
|
|
"regexp"
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type wxBindingParam struct {
|
|
UserId uint `json:"userId"`
|
|
Code string `json:"code" validate:"required"`
|
|
}
|
|
|
|
/**
|
|
手机号注册之后绑定微信用
|
|
|
|
*/
|
|
func WxBinding(c *gin.Context) {
|
|
var p wxBindingParam
|
|
err := tools.ParseParams(&p, c)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
|
|
wxUserInfo, err := service.GetInfoByCode(p.Code)
|
|
if err != nil {
|
|
back.Err(c, "wechat err:"+err.Error())
|
|
return
|
|
}
|
|
// 拿到 userInfo 去更新数据库中的信息
|
|
var userInfo user.Information
|
|
dbc.DB.Find(&userInfo, p.UserId)
|
|
if userInfo.ID <= 0 {
|
|
back.Fail(c, "用户不存在")
|
|
return
|
|
}
|
|
|
|
if len(userInfo.WxUnionId) > 0 {
|
|
back.Fail(c, "该账号,已经绑定了微信")
|
|
return
|
|
}
|
|
|
|
sql := `select count(*) from recook_user_info where wx_union_id=? and wx_app_open_id =?`
|
|
var is_count int
|
|
dbc.DB.Raw(sql, wxUserInfo.UnionId, wxUserInfo.OpenID).Count(&is_count)
|
|
if is_count > 0 {
|
|
back.Fail(c, "该微信号已经绑定了其他账号")
|
|
return
|
|
}
|
|
beego.Info(sql, is_count)
|
|
|
|
reg := regexp.MustCompile(`瑞库客\d+`)
|
|
res := reg.FindAllString(userInfo.Nickname, -1)
|
|
if len(res) >= 0 {
|
|
wxUserInfo.Nickname = userInfo.Nickname // 昵称 前缀是瑞库客的是系统自动生成的,会更新
|
|
}
|
|
if userInfo.HeadImgUrl != define.DefaultHeadImage {
|
|
wxUserInfo.HeadImgUrl = userInfo.HeadImgUrl
|
|
}
|
|
|
|
if err = dbc.DB.Model(&userInfo).Update(user.Information{
|
|
Nickname: wxUserInfo.Nickname,
|
|
HeadImgUrl: wxUserInfo.HeadImgUrl,
|
|
WxUnionId: wxUserInfo.UnionId,
|
|
WxAppOpenId: wxUserInfo.OpenID,
|
|
Gender: wxUserInfo.Sex,
|
|
}).Error; err != nil {
|
|
back.Err(c, "数据错误:"+err.Error())
|
|
return
|
|
}
|
|
|
|
// not good
|
|
dbc.DB.Find(&userInfo, p.UserId)
|
|
|
|
login(c, &userInfo)
|
|
return
|
|
|
|
}
|