Browse Source

查研观向小助手Token

xingzai 3 years ago
parent
commit
86de85dc45
7 changed files with 126 additions and 18 deletions
  1. 85 10
      controllers/wechat.go
  2. 1 0
      models/db.go
  3. 30 0
      models/session.go
  4. 1 0
      models/user.go
  5. 2 1
      models/wx_user.go
  6. 4 4
      routers/commentsRouter_controllers.go
  7. 3 3
      utils/config.go

+ 85 - 10
controllers/wechat.go

@@ -114,12 +114,36 @@ func (this *WechatCommonController) WechatLoginByxzs() {
 	//		return
 	//	}
 	//}
+	token := utils.MD5(unionId)
 	user, err := models.GetWxUserItemByUserUnionId(unionId)
 	if err != nil && err.Error() != utils.ErrNoRow() {
 		br.Msg = "获取用户信息失败"
 		br.ErrMsg = "获取本地用户信息失败,Err:" + err.Error()
 		return
 	}
+
+	totalToken, err := models.GetXzsSessionCountByToken(token)
+	if err != nil {
+		br.Msg = "获取用户信息失败"
+		br.ErrMsg = "查询数量失败,Err:" + err.Error()
+		return
+	}
+	if totalToken == 0 {
+		itemsSession := new(models.CygxXzsSession)
+		itemsSession.UnionId = unionId
+		itemsSession.AccessToken = token
+		itemsSession.CreatedTime = time.Now()
+		if user != nil {
+			itemsSession.UserId = user.UserId
+		}
+		err = models.AddCygxXzsSession(itemsSession)
+		if err != nil {
+			br.Msg = "获取用户信息失败"
+			br.ErrMsg = "添加Token失败,Err:" + err.Error()
+			return
+		}
+	}
+
 	if user == nil {
 		resp.HasPermission = 3
 	} else {
@@ -140,9 +164,14 @@ func (this *WechatCommonController) WechatLoginByxzs() {
 			resp.RealName = user.RealName
 			resp.HasPermission = 2
 		}
-		//_ = models.UpdateUserRecord(items)
+		if resp.Headimgurl == "" {
+			resp.Headimgurl = user.HeadimgurlRecord
+		}
 	}
-	resp.Headimgurl = wxUserInfo.Headimgurl
+	if resp.Headimgurl == "" {
+		resp.Headimgurl = utils.HeadimgurlDefault
+	}
+	resp.Token = token
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "获取成功"
@@ -197,19 +226,65 @@ func (this *WechatCommonController) GetWxSign() {
 	br.Data = resp
 }
 
-// @Title 路由2
-// @Description 路由2
-// @Param   Url   query   string  true       "url地址"
-// @router /hello2 [get]
-func (this *WechatCommonController) TesrR2() {
+// @Title 获取用户详情
+// @Description 获取用户详情接口
+// @Param   Token   query   string  true       "Token"
+// @Success 200 {object}  models.UserDetailByUserLogin
+// @router /user/detail [get]
+func (this *WechatCommonController) UserInfo() {
 	br := new(models.BaseResponse).Init()
 	defer func() {
 		this.Data["json"] = br
 		this.ServeJSON()
 	}()
-	getUrl := this.GetString("Url")
+	token := this.GetString("Token")
+	if token == "" {
+		br.Msg = "参数错误"
+		br.ErrMsg = "Token 为空"
+		return
+	}
+	resp := new(models.UserDetailByUserLogin)
+	session, err := models.GetUnionidByToken(token)
+	if err != nil {
+		br.Msg = "获取用户信息失败"
+		br.ErrMsg = "查询session失败,Err:" + err.Error()
+		return
+	}
+	unionId := session.UnionId
+	user, err := models.GetWxUserItemByUserUnionId(unionId)
+	if err != nil && err.Error() != utils.ErrNoRow() {
+		br.Msg = "获取用户信息失败"
+		br.ErrMsg = "获取本地用户信息失败,Err:" + err.Error()
+		return
+	}
+	if user == nil {
+		resp.HasPermission = 3
+	} else {
+		permissionStr, err := models.GetCompanyPermission(user.CompanyId)
+		if err != nil {
+			br.Msg = "获取信息失败"
+			br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
+			return
+		}
+		if permissionStr != "" {
+			resp.Permission = permissionStr
+			resp.Mobile = user.Mobile
+			resp.RealName = user.RealName
+			resp.CompanyName = user.CompanyName
+			resp.HasPermission = 1
+		} else {
+			resp.Mobile = user.Mobile
+			resp.RealName = user.RealName
+			resp.HasPermission = 2
+		}
+		resp.Headimgurl = user.HeadimgurlRecord
+	}
+	if resp.Headimgurl == "" {
+		resp.Headimgurl = utils.HeadimgurlDefault
+	}
+	resp.Token = utils.MD5(unionId)
 	br.Ret = 200
 	br.Success = true
-	br.Msg = "路由2"
-	br.Data = getUrl
+	br.Msg = "获取成功"
+	br.Data = resp
 }

+ 1 - 0
models/db.go

@@ -25,5 +25,6 @@ func init() {
 		new(WxUserCode),
 		new(UserRecord),
 		new(CygxUserRecord),
+		new(CygxXzsSession),
 	)
 }

+ 30 - 0
models/session.go

@@ -51,3 +51,33 @@ func GetTokenByOpenId(openId string) (item *CygxSession, err error) {
 	err = o.Raw(sql, openId).QueryRow(&item)
 	return
 }
+
+type CygxXzsSession struct {
+	SessionId   int `orm:"column(session_id);pk"`
+	UserId      int
+	UnionId     string
+	AccessToken string
+	CreatedTime time.Time
+}
+
+//添加用户session信息
+func AddCygxXzsSession(item *CygxXzsSession) (err error) {
+	o := orm.NewOrm()
+	_, err = o.Insert(item)
+	return
+}
+
+func GetXzsSessionCountByToken(token string) (count int, err error) {
+	sql := `SELECT COUNT(1) AS count FROM cygx_xzs_session WHERE access_token=? LIMIT 1 `
+	o := orm.NewOrm()
+	err = o.Raw(sql, token).QueryRow(&count)
+	return
+}
+
+//获取用户token详情
+func GetUnionidByToken(token string) (item *CygxXzsSession, err error) {
+	sql := `SELECT * FROM cygx_xzs_session WHERE access_token=?  LIMIT 1 `
+	o := orm.NewOrm()
+	err = o.Raw(sql, token).QueryRow(&item)
+	return
+}

+ 1 - 0
models/user.go

@@ -181,6 +181,7 @@ type UserDetailByUserLogin struct {
 	CompanyName   string `description:"公司名称"`
 	Permission    string `description:"拥有权限分类,多个用英文逗号分隔"`
 	HasPermission int    `description:"1:有该行业权限,正常展示,2:无权限,非潜在客户,3:未在小程序授权用户信息 等"`
+	Token         string `description:"Token"`
 }
 
 func GetCompanyPermission(companyId int) (permission string, err error) {

+ 2 - 1
models/wx_user.go

@@ -68,6 +68,7 @@ type WxUserItem struct {
 	BindAccount         string    `description:"绑定时的账号"`
 	Email               string    `description:"邮箱"`
 	Headimgurl          string    `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
+	HeadimgurlRecord    string    `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
 	ApplyMethod         int       `description:"0:未申请,1:已付费客户申请试用,2:非客户申请试用"`
 	FirstLogin          int       `description:"是否第一次登陆"`
 	IsFreeLogin         int       `description:"是否免登陆,true:免登陆,false:非免登陆"`
@@ -252,7 +253,7 @@ func UPdateUserCountryCode(item *WxUserItem) (err error) {
 }
 
 func GetWxUserItemByUserUnionId(unionId string) (item *WxUserItem, err error) {
-	sql := `SELECT a.*,b.company_name FROM wx_user AS a
+	sql := `SELECT a.*,r.headimgurl as headimgurl_record, b.company_name FROM wx_user AS a
 			INNER JOIN company AS b on a.company_id=b.company_id
 			INNER JOIN user_record  as  r ON r.user_id = a.user_id 
 			WHERE r.union_id=?

+ 4 - 4
routers/commentsRouter_controllers.go

@@ -36,8 +36,8 @@ func init() {
 
     beego.GlobalControllerRouter["hongze/hongze_cygxzs/controllers:WechatCommonController"] = append(beego.GlobalControllerRouter["hongze/hongze_cygxzs/controllers:WechatCommonController"],
         beego.ControllerComments{
-            Method: "TesrR2",
-            Router: "/hello2",
+            Method: "WechatLoginByxzs",
+            Router: "/loginByxzs",
             AllowHTTPMethods: []string{"get"},
             MethodParams: param.Make(),
             Filters: nil,
@@ -45,8 +45,8 @@ func init() {
 
     beego.GlobalControllerRouter["hongze/hongze_cygxzs/controllers:WechatCommonController"] = append(beego.GlobalControllerRouter["hongze/hongze_cygxzs/controllers:WechatCommonController"],
         beego.ControllerComments{
-            Method: "WechatLoginByxzs",
-            Router: "/loginByxzs",
+            Method: "UserInfo",
+            Router: "/user/detail",
             AllowHTTPMethods: []string{"get"},
             MethodParams: param.Make(),
             Filters: nil,

+ 3 - 3
utils/config.go

@@ -26,12 +26,12 @@ var (
 
 	WxPublicAppId     string //查研观向小助手公众号
 	WxPublicAppSecret string //查研观向小助手公众号
+	HeadimgurlDefault string //默认头像
 )
 
 func init() {
 
-
-	RunMode,err := web.AppConfig.String("run_mode")
+	RunMode, err := web.AppConfig.String("run_mode")
 	if RunMode == "" {
 		RunMode = "release"
 		configPath := `/home/code/config/hongze_cygxzs/conf/app.conf`
@@ -62,7 +62,7 @@ func init() {
 
 	WxPublicAppId = "wxb7cb8a15abad5b8e"                   //查研观向小助手
 	WxPublicAppSecret = "f425ba2863084249722af1e2a5cfffd3" //查研观向小助手
-
+	HeadimgurlDefault = "https://hongze.oss-cn-shanghai.aliyuncs.com/static/images/202202/20220225/XFBBOUmDC5AXkfxnHiuqKpPtoofH.png"
 }
 
 //http://webapi.brilliantstart.cn/api/