|
@@ -1,7 +1,12 @@
|
|
|
package controllers
|
|
|
|
|
|
import (
|
|
|
+ "encoding/json"
|
|
|
"hongze/hongze_cygx/models"
|
|
|
+ "hongze/hongze_cygx/utils"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
//用户
|
|
@@ -9,6 +14,116 @@ type UserController struct {
|
|
|
BaseAuthController
|
|
|
}
|
|
|
|
|
|
+// @Title 登录
|
|
|
+// @Description 登录接口
|
|
|
+// @Param request body models.LoginReq true "type json string"
|
|
|
+// @Success 200 {object} models.LoginResp
|
|
|
+// @router /login [post]
|
|
|
+func (this *UserController) Login() {
|
|
|
+ br := new(models.BaseResponse).Init()
|
|
|
+ defer func() {
|
|
|
+ this.Data["json"] = br
|
|
|
+ this.ServeJSON()
|
|
|
+ }()
|
|
|
+ var req models.LoginReq
|
|
|
+ err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
|
|
|
+ if err != nil {
|
|
|
+ br.Msg = "参数解析异常!"
|
|
|
+ br.ErrMsg = "参数解析失败,Err:" + err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ user := this.User
|
|
|
+ if user == nil {
|
|
|
+ br.Msg = "请登录"
|
|
|
+ br.ErrMsg = "请登录"
|
|
|
+ br.Ret = 408
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ unionId := this.User.UnionId
|
|
|
+ userId := this.User.UserId
|
|
|
+ if unionId == "" {
|
|
|
+ br.Msg = "参数错误"
|
|
|
+ br.ErrMsg = "参数错误,unionId 为空"
|
|
|
+ return
|
|
|
+ }
|
|
|
+ newUserId := 0
|
|
|
+ if req.LoginType == 1 {
|
|
|
+ //BindMobile(openId, mobile string, userId, loginType int) (err error) {
|
|
|
+ req.Mobile = strings.Trim(req.Mobile, " ")
|
|
|
+ newUserId, err = models.PcBindMobile(unionId, req.Mobile, userId, req.LoginType)
|
|
|
+ } else if req.LoginType == 2 {
|
|
|
+ if req.Email == "" {
|
|
|
+ br.ErrMsg = "邮箱不能为空,请输入邮箱"
|
|
|
+ br.Msg = "邮箱不能为空,请输入邮箱"
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if !utils.ValidateEmailFormatat(req.Email) {
|
|
|
+ br.ErrMsg = "邮箱格式错误,请重新输入"
|
|
|
+ br.Msg = "邮箱格式错误,请重新输入"
|
|
|
+ return
|
|
|
+ }
|
|
|
+ newUserId, err = models.PcBindMobile(unionId, req.Email, userId, req.LoginType)
|
|
|
+ } else {
|
|
|
+ br.Msg = "无效的登录方式"
|
|
|
+ br.ErrMsg = "无效的登录方式,Err:" + err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var token string
|
|
|
+ tokenItem, err := models.GetTokenByUid(newUserId)
|
|
|
+ if err != nil && err.Error() != utils.ErrNoRow() {
|
|
|
+ br.Msg = "登录失败"
|
|
|
+ br.ErrMsg = "登录失败,获取token失败:" + err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if tokenItem == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
|
|
|
+ timeUnix := time.Now().Unix()
|
|
|
+ timeUnixStr := strconv.FormatInt(timeUnix, 10)
|
|
|
+ token := utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
|
|
|
+ //新增session
|
|
|
+ {
|
|
|
+ session := new(models.CygxSession)
|
|
|
+ session.OpenId = unionId
|
|
|
+ session.UnionId = unionId
|
|
|
+ session.UserId = userId
|
|
|
+ session.CreatedTime = time.Now()
|
|
|
+ session.LastUpdatedTime = time.Now()
|
|
|
+ session.ExpireTime = time.Now().AddDate(0, 1, 0)
|
|
|
+ session.AccessToken = token
|
|
|
+ err = models.AddSession(session)
|
|
|
+ if err != nil {
|
|
|
+ br.Msg = "登录失败"
|
|
|
+ br.ErrMsg = "登录失败,新增用户session信息失败:" + err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ token = tokenItem.AccessToken
|
|
|
+ }
|
|
|
+
|
|
|
+ //新增登录日志
|
|
|
+ {
|
|
|
+ loginLog := new(models.WxUserLog)
|
|
|
+ loginLog.UserId = userId
|
|
|
+ loginLog.OpenId = unionId
|
|
|
+ loginLog.Mobile = req.Mobile
|
|
|
+ loginLog.Email = req.Email
|
|
|
+ loginLog.CreateTime = time.Now()
|
|
|
+ loginLog.Handle = "wechat_user_login"
|
|
|
+ loginLog.Remark = token
|
|
|
+ go models.AddWxUserLog(loginLog)
|
|
|
+ }
|
|
|
+
|
|
|
+ resp := new(models.LoginResp)
|
|
|
+ resp.UserId = newUserId
|
|
|
+ resp.Authorization = token
|
|
|
+ br.Ret = 200
|
|
|
+ br.Success = true
|
|
|
+ br.Data = resp
|
|
|
+ br.Msg = "登录成功"
|
|
|
+}
|
|
|
+
|
|
|
// @Title 获取用户详情
|
|
|
// @Description 获取用户详情接口
|
|
|
// @Success 200 {object} models.UserDetail
|
|
@@ -42,7 +157,13 @@ func (this *UserController) Detail() {
|
|
|
var hasPermission bool
|
|
|
if companyItem.Status == "试用" || companyItem.Status == "永续" || companyItem.Status == "正式" {
|
|
|
hasPermission = true
|
|
|
-
|
|
|
+ permissionStr, err := models.GetCompanyPermission(companyItem.CompanyId)
|
|
|
+ if err != nil {
|
|
|
+ br.Msg = "获取信息失败"
|
|
|
+ br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ item.PermissionName = permissionStr
|
|
|
}
|
|
|
item.HasPermission = hasPermission
|
|
|
br.Ret = 200
|
|
@@ -50,3 +171,56 @@ func (this *UserController) Detail() {
|
|
|
br.Msg = "获取成功"
|
|
|
br.Data = item
|
|
|
}
|
|
|
+
|
|
|
+//
|
|
|
+//// @Title 绑定手机号或邮箱
|
|
|
+//// @Description 绑定手机号或邮箱
|
|
|
+//// @Param request body models.WxGetPhoneNumberReq true "type json string"
|
|
|
+//// @Success 200 {object} models.WxGetPhoneNumberResp
|
|
|
+//// @router /bind [post]
|
|
|
+//func (this *WechatController) Bind() {
|
|
|
+// br := new(models.BaseResponse).Init()
|
|
|
+// defer func() {
|
|
|
+// this.Data["json"] = br
|
|
|
+// this.ServeJSON()
|
|
|
+// }()
|
|
|
+// var req models.WxGetPhoneNumberReq
|
|
|
+// err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
|
|
|
+// if err != nil {
|
|
|
+// br.Msg = "参数解析异常!"
|
|
|
+// br.ErrMsg = "参数解析失败,Err:" + err.Error()
|
|
|
+// return
|
|
|
+// }
|
|
|
+// if req.EncryptedData == "" || req.Iv == "" {
|
|
|
+// br.Msg = "参数错误"
|
|
|
+// return
|
|
|
+// }
|
|
|
+// user := this.User
|
|
|
+// if user == nil {
|
|
|
+// br.Msg = "请登陆"
|
|
|
+// br.Ret = 408
|
|
|
+// return
|
|
|
+// }
|
|
|
+// sessionKey := user.SessionKey
|
|
|
+// wxMobile, err := weapp.DecryptMobile(sessionKey, req.EncryptedData, req.Iv)
|
|
|
+// if err != nil {
|
|
|
+// br.Msg = "解析用户手机号信息失败"
|
|
|
+// br.ErrMsg = "解析用户手机号信息失败,Err:" + err.Error()
|
|
|
+// return
|
|
|
+// }
|
|
|
+// err = models.ModifyUsersMobile(user.UserId, wxMobile.PurePhoneNumber)
|
|
|
+// if err != nil {
|
|
|
+// br.Msg = "获取失败"
|
|
|
+// br.ErrMsg = "获取失败,Err:" + err.Error()
|
|
|
+// return
|
|
|
+// }
|
|
|
+// resp := new(models.WxGetPhoneNumberResp)
|
|
|
+// resp.Authorization = this.Token
|
|
|
+// resp.PhoneNumber = wxMobile.PhoneNumber
|
|
|
+// resp.PurePhoneNumber = wxMobile.PurePhoneNumber
|
|
|
+// resp.CountryCode = wxMobile.CountryCode
|
|
|
+// br.Msg = "获取成功!"
|
|
|
+// br.Ret = 200
|
|
|
+// br.Success = true
|
|
|
+// br.Data = resp
|
|
|
+//}
|