Browse Source

小助手方式微信code登录

xingzai 3 years ago
parent
commit
fbf10e53e0
5 changed files with 119 additions and 2 deletions
  1. 65 0
      controllers/wechat.go
  2. 28 0
      models/cygx_user_record.go
  3. 1 0
      models/db.go
  4. 16 0
      services/wechat.go
  5. 9 2
      utils/config.go

+ 65 - 0
controllers/wechat.go

@@ -640,3 +640,68 @@ func (this *WechatController) UpdateWxAccesstoken() {
 	br.Ret = 200
 	br.Success = true
 }
+
+// @Title 微信登录小助手接口
+// @Description 微信登录小助手接口
+// @Param   Code   query   string  true       "微信唯一编码code"
+// @Success 200 {object} models.WxLoginResp
+// @router /loginByxzs [get]
+func (this *WechatCommonController) WechatLoginByxzs() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	code := this.GetString("Code")
+	if code == "" {
+		br.Msg = "参数错误"
+		br.ErrMsg = "Code 为空"
+		return
+	}
+
+	item, err := services.WxGetUserOpenIdByCodeXzs(code)
+	if err != nil {
+		br.Msg = "获取用户信息失败"
+		br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
+		return
+	}
+	if item.Errcode != 0 {
+		br.Msg = "获取用户信息失败"
+		br.ErrMsg = "获取access_token 失败 errcode:" + strconv.Itoa(item.Errcode) + " ;errmsg:" + item.Errmsg
+		return
+	}
+	openId := item.Openid
+	if openId == "" {
+		br.Msg = "获取用户信息失败"
+		br.ErrMsg = "获取openid失败,openid:" + item.Openid
+		return
+	}
+	unionId := item.Unionid
+	if unionId == "" {
+		br.Msg = "获取用户信息失败"
+		br.ErrMsg = "获取unionid失败,unionid:" + item.Openid
+		return
+	}
+	total, err := models.GetCygxUserRecordCount(openId)
+	if err != nil {
+		br.Msg = "获取用户信息失败"
+		br.ErrMsg = "查询数量失败,Err:" + err.Error()
+		return
+	}
+	if total == 0 {
+		items := new(models.CygxUserRecord)
+		items.OpenId = openId
+		items.UnionId = unionId
+		items.CreateTime = time.Now()
+		_, err = models.AddCygxUserRecord(items)
+		if err != nil {
+			br.Msg = "获取用户信息失败"
+			br.ErrMsg = "添加openid失败,Err:" + err.Error()
+			return
+		}
+	}
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "获取成功"
+	br.Data = item
+}

+ 28 - 0
models/cygx_user_record.go

@@ -0,0 +1,28 @@
+package models
+
+import (
+	"rdluck_tools/orm"
+	"time"
+)
+
+type CygxUserRecord struct {
+	UserRecordId int       `orm:"column(user_record_id);pk" description:"id"`
+	OpenId       string    `description:"用户openid,最大长度:32"`
+	UnionId      string    `description:"用户unionid,最大长度:64"`
+	CreateTime   time.Time `description:"提交建议时间"`
+}
+
+//添加优化建议
+func AddCygxUserRecord(item *CygxUserRecord) (lastId int64, err error) {
+	o := orm.NewOrm()
+	lastId, err = o.Insert(item)
+	return
+}
+
+//获取数量
+func GetCygxUserRecordCount(openId string) (count int, err error) {
+	o := orm.NewOrm()
+	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_user_record WHERE open_id=? `
+	err = o.Raw(sqlCount, openId).QueryRow(&count)
+	return
+}

+ 1 - 0
models/db.go

@@ -64,5 +64,6 @@ func init() {
 		new(CygxReportHistoryRecord),
 		new(CygxActivityMeetDetailLog),
 		new(CygxArticleAuthor),
+		new(CygxUserRecord),
 	)
 }

+ 16 - 0
services/wechat.go

@@ -37,6 +37,22 @@ func WxGetUserOpenIdByCode(code string) (item *WxAccessToken, err error) {
 	return
 }
 
+//小助手
+func WxGetUserOpenIdByCodeXzs(code string) (item *WxAccessToken, err error) {
+	if code == "" {
+		err = errors.New("code is empty")
+		return nil, err
+	}
+	requestUrl := `https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code`
+	requestUrl = fmt.Sprintf(requestUrl, utils.WxAppIdXzs, utils.WxAppSecretXzs, code)
+	result, err := http.Get(requestUrl)
+	if err != nil {
+		return nil, err
+	}
+	err = json.Unmarshal(result, &item)
+	return
+}
+
 type WxToken struct {
 	AccessToken string `json:"access_token"`
 	ExpiresIn   int    `json:"expires_in"`

+ 9 - 2
utils/config.go

@@ -20,8 +20,11 @@ var (
 //微信配置信息
 var (
 	WxId        string //微信原始ID
-	WxAppId     string
-	WxAppSecret string
+	WxAppId     string //查研观向小程序
+	WxAppSecret string //查研观向小程序
+
+	WxAppIdXzs     string //查研观向小助手公众号
+	WxAppSecretXzs string //查研观向小助手公众号
 
 	WxMsgTemplateIdApply           string //申请待处理
 	WxMsgTemplateIdApplyCancel     string //预约取消提醒
@@ -73,6 +76,10 @@ func init() {
 		fmt.Println(Re)
 		panic(Re)
 	}
+
+	WxAppIdXzs = "wxb7cb8a15abad5b8e"                   //查研观向小助手
+	WxAppSecretXzs = "4dd35cd1598b27bd1dc9a3b299b289fa" //查研观向小助手
+
 	OnlineTime = "2021-06-01 00:00:01" //上线时间
 	SummaryArticleId = 1000000         //手动添加的纪要库开始ID
 	WxMsgTemplateIdAskMsgMobileAll = "15557270714,18767183922,18621268829"