Procházet zdrojové kódy

add: 微信小程序新版code获取手机号

hsun před 3 roky
rodič
revize
3644327da6
2 změnil soubory, kde provedl 60 přidání a 19 odebrání
  1. 18 4
      controller/wechat/wechat.go
  2. 42 15
      services/wx_app/wx_app.go

+ 18 - 4
controller/wechat/wechat.go

@@ -71,9 +71,10 @@ func Login(c *gin.Context) {
 
 // 消息解密请求参数
 type EncryptReq struct {
-	EncryptedData string
-	Iv            string
-	IsBind        bool `json:"isBind" description:"是否需要去授权绑定"`
+	Code			string	`description:"手机号获取凭证"`
+	EncryptedData 	string
+	Iv            	string
+	IsBind        	bool `json:"isBind" description:"是否需要去授权绑定"`
 }
 
 // GetEncryptInfo 消息解密
@@ -118,11 +119,24 @@ func GetEncryptInfo(c *gin.Context) {
 		return
 	}
 
+	purePhoneNumber := ""
+	if req.Code != "" {
+		// 新版code获取手机号
+		if phoneInfo, err := wx_app.GetPhoneNumber(req.Code); err != nil {
+			response.Fail("code获取手机号信息失败, Err:" + err.Error(), c)
+			return
+		} else {
+			purePhoneNumber = phoneInfo.PhoneInfo.PurePhoneNumber
+		}
+	} else {
+		purePhoneNumber = decryptData.PurePhoneNumber
+	}
+
 	//如果需要静默授权绑定,那么就去走静默绑定手机号流程
 	openId := userInfo.OpenID
 	//国际区号
 	countryCode, _ := strconv.Atoi(decryptData.CountryCode)
-	token, newUserInfo, err, errMsg := userService.BindWxUser(openId, decryptData.PurePhoneNumber, "", "", 3, countryCode, 1)
+	token, newUserInfo, err, errMsg := userService.BindWxUser(openId, purePhoneNumber, "", "", 3, countryCode, 1)
 	if err != nil {
 		if errMsg == "" {
 			errMsg = "登录失败"

+ 42 - 15
services/wx_app/wx_app.go

@@ -8,6 +8,7 @@ import (
 	"github.com/silenceper/wechat/v2/miniprogram/auth"
 	"github.com/silenceper/wechat/v2/miniprogram/config"
 	"github.com/silenceper/wechat/v2/miniprogram/encryptor"
+	"github.com/silenceper/wechat/v2/util"
 )
 
 //微信小程序配置信息
@@ -64,18 +65,44 @@ func GetDecryptInfo(sessionKey, encryptedData, iv string) (decryptData *encrypto
 	return
 }
 
-//func GetCode() {
-//	codeParms := qrcode.QRCoder{
-//		Page:      "",
-//		Path:      "",
-//		Width:     0,
-//		Scene:     "",
-//		AutoColor: false,
-//		LineColor: qrcode.Color{},
-//		IsHyaline: false,
-//	}
-//	//wxApp := GetWxApp()
-//	//qrCode := wxApp.GetQRCode()
-//	//qrCode.
-//	//qrCode.GetWXACodeUnlimit()
-//}
+// 微信小程序新版code获取手机号
+const (
+	getPhoneNumberURL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s"
+)
+
+type GetPhoneNumberResponse struct {
+	util.CommonError
+	PhoneInfo PhoneInfo `json:"phone_info"`
+}
+
+type PhoneInfo struct {
+	PhoneNumber     string `json:"phoneNumber"`     // 用户绑定的手机号
+	PurePhoneNumber string `json:"purePhoneNumber"` // 没有区号的手机号
+	CountryCode     string `json:"countryCode"`     // 区号
+	WaterMark       struct {
+		Timestamp int64  `json:"timestamp"`
+		AppID     string `json:"appid"`
+	} `json:"watermark"` // 数据水印
+}
+
+func GetPhoneNumber(code string) (result GetPhoneNumberResponse, err error) {
+	var response []byte
+	var (
+		at string
+	)
+	wechatClient := GetWxApp()
+	authClient := wechatClient.GetAuth()
+	if at, err = authClient.GetAccessToken(); err != nil {
+		return
+	}
+	body := map[string]interface{}{
+		"code": code,
+	}
+	if response, err = util.PostJSON(fmt.Sprintf(getPhoneNumberURL, at), body); err != nil {
+		return
+	}
+	if err = util.DecodeWithError(response, &result, "phonenumber.getPhoneNumber"); err != nil {
+		return
+	}
+	return
+}