|
@@ -1,8 +1,10 @@
|
|
|
package youngee_talent_service
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
+ "github.com/gogf/gf/encoding/gjson"
|
|
|
"net/http"
|
|
|
"youngmini_server/app/dao"
|
|
|
"youngmini_server/app/model"
|
|
@@ -23,10 +25,36 @@ type WxLoginResult struct {
|
|
|
ErrMsg string `json:"errmsg"`
|
|
|
}
|
|
|
|
|
|
+// 定义 Watermark 结构体
|
|
|
+type Watermark struct {
|
|
|
+ Timestamp int `json:"timestamp"`
|
|
|
+ AppID string `json:"appid"`
|
|
|
+}
|
|
|
+
|
|
|
+// 定义 PhoneInfo 结构体
|
|
|
+type PhoneInfo struct {
|
|
|
+ PhoneNumber string `json:"phoneNumber"`
|
|
|
+ PurePhoneNumber string `json:"purePhoneNumber"`
|
|
|
+ CountryCode string `json:"countryCode"`
|
|
|
+ Watermark Watermark `json:"watermark"`
|
|
|
+}
|
|
|
+
|
|
|
+// 定义响应结构体 GetPhoneRes
|
|
|
+type GetPhoneRes struct {
|
|
|
+ ErrCode int `json:"errcode"`
|
|
|
+ ErrMsg string `json:"errmsg"`
|
|
|
+ PhoneInfo PhoneInfo `json:"phone_info"`
|
|
|
+}
|
|
|
+
|
|
|
const (
|
|
|
- urlformat = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code"
|
|
|
+ urlgetphone = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s"
|
|
|
+ urlformat = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code"
|
|
|
)
|
|
|
|
|
|
+type GetPhoneRequestBody struct {
|
|
|
+ Code string `json:"code"`
|
|
|
+}
|
|
|
+
|
|
|
func WxLogin(r *ghttp.Request) *TalentHttpResult {
|
|
|
|
|
|
l := youngee_talent_model.WxLoginInfo{}
|
|
@@ -59,8 +87,11 @@ func WxLogin(r *ghttp.Request) *TalentHttpResult {
|
|
|
return &TalentHttpResult{Code: -4, Msg: fmt.Sprintf("errCode:%d, errmsg:%s", wxResp.ErrCode, wxResp.ErrMsg)}
|
|
|
}
|
|
|
|
|
|
+ //为了获得手机号
|
|
|
+ phoneNum := getPhoneNum(l.GetPhoneCode)
|
|
|
+
|
|
|
// 根据openid查询达人信息
|
|
|
- rec, err := g.DB().Model("youngee_talent_info").One("talent_wx_openid", wxResp.OpenId)
|
|
|
+ rec, err := g.DB().Model("youngee_talent_info").One("talent_phone_number", phoneNum)
|
|
|
if err != nil {
|
|
|
return &TalentHttpResult{Code: -5, Msg: "get talent info failed"}
|
|
|
}
|
|
@@ -69,6 +100,7 @@ func WxLogin(r *ghttp.Request) *TalentHttpResult {
|
|
|
if rec != nil && rec[dao.YoungeeTalentInfo.Columns.InBlacklist].Int() > 0 {
|
|
|
return &TalentHttpResult{Code: -6, Msg: "in black list"}
|
|
|
}
|
|
|
+
|
|
|
//获得登录后的返回结果对象
|
|
|
res := youngee_talent_model.LoginResultData{}
|
|
|
var newTalentId string
|
|
@@ -77,19 +109,21 @@ func WxLogin(r *ghttp.Request) *TalentHttpResult {
|
|
|
// 首先生成达人唯一id
|
|
|
newTalentId = utils.GetUuid.GetTalentId()
|
|
|
talentInfo := model.YoungeeTalentInfo{
|
|
|
- Id: newTalentId,
|
|
|
- TalentWxOpenid: wxResp.OpenId,
|
|
|
- Avatar: l.Avatar,
|
|
|
- TalentWxNickname: l.Nickname,
|
|
|
- Canwithdraw: 0,
|
|
|
- Income: 0,
|
|
|
- Withdrawing: 0,
|
|
|
- Withdrawed: 0,
|
|
|
- LastLoginDate: gtime.Now(),
|
|
|
- CreateDate: gtime.Now(),
|
|
|
+ Id: newTalentId,
|
|
|
+ TalentWxOpenid: wxResp.OpenId,
|
|
|
+ Avatar: l.Avatar,
|
|
|
+ TalentWxNickname: l.Nickname,
|
|
|
+ TalentPhoneNumber: phoneNum, //插入手机号
|
|
|
+ Canwithdraw: 0,
|
|
|
+ Income: 0,
|
|
|
+ Withdrawing: 0,
|
|
|
+ Withdrawed: 0,
|
|
|
+ LastLoginDate: gtime.Now(),
|
|
|
+ CreateDate: gtime.Now(),
|
|
|
}
|
|
|
res.Avatar = l.Avatar
|
|
|
res.Nickname = l.Nickname
|
|
|
+ res.Phone = phoneNum //接口返回手机号
|
|
|
_, err = g.DB().Model(dao.YoungeeTalentInfo.Table).Data(talentInfo).Insert()
|
|
|
if err != nil {
|
|
|
return &TalentHttpResult{Code: -7, Msg: "get talentId failed"}
|
|
@@ -99,6 +133,7 @@ func WxLogin(r *ghttp.Request) *TalentHttpResult {
|
|
|
newTalentId = rec["id"].String()
|
|
|
res.Avatar = rec["avatar"].String()
|
|
|
res.Nickname = rec["talent_wx_nickname"].String()
|
|
|
+ res.Phone = rec["talent_phone_number"].String()
|
|
|
// 更新达人最近登录时间
|
|
|
_, err = g.DB().Model(dao.YoungeeTalentInfo.Table).Data(
|
|
|
g.Map{
|
|
@@ -134,7 +169,45 @@ func WxLogin(r *ghttp.Request) *TalentHttpResult {
|
|
|
return &TalentHttpResult{Code: 0, Msg: "success", Data: res}
|
|
|
}
|
|
|
|
|
|
-func IsLogin(r *ghttp.Request) *TalentHttpResult {
|
|
|
+func getPhoneNum(code string) string {
|
|
|
+ //获取access_token
|
|
|
+ accessToken, err := getAndCacheWxAccessToken()
|
|
|
+ fmt.Println("accesstoken===>", accessToken)
|
|
|
+ //post获取电话号码
|
|
|
+ url := fmt.Sprintf(urlgetphone, accessToken)
|
|
|
+ fmt.Println("url ", url)
|
|
|
+
|
|
|
+ getPhoneRequest := GetPhoneRequestBody{
|
|
|
+ Code: code,
|
|
|
+ }
|
|
|
+ jsonBody, err := gjson.Encode(getPhoneRequest)
|
|
|
|
|
|
+ // 发送 POST 请求
|
|
|
+ response, err := http.Post(url, "application/json", bytes.NewBuffer(jsonBody))
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("post getphoneNum failed")
|
|
|
+ }
|
|
|
+ defer response.Body.Close()
|
|
|
+
|
|
|
+ // 解码微信服务端传来的信息
|
|
|
+ var getphoneResp = GetPhoneRes{}
|
|
|
+ decoder := json.NewDecoder(response.Body)
|
|
|
+ if err = decoder.Decode(&getphoneResp); err != nil {
|
|
|
+ fmt.Printf("decode json from wx fail")
|
|
|
+ }
|
|
|
+
|
|
|
+ if getphoneResp.ErrCode != 0 {
|
|
|
+ fmt.Printf("错误码:%d, 错误信息:%s", getphoneResp.ErrCode, getphoneResp.ErrMsg)
|
|
|
+ fmt.Sprintf("errCode:%d, errmsg:%s", getphoneResp.ErrCode, getphoneResp.ErrMsg)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 打印解析后的结构体内容
|
|
|
+ fmt.Println("获取号码接口响应====》", getphoneResp.PhoneInfo.PhoneNumber)
|
|
|
+
|
|
|
+ return getphoneResp.PhoneInfo.PhoneNumber
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func IsLogin(r *ghttp.Request) *TalentHttpResult {
|
|
|
return &TalentHttpResult{Code: 0, Msg: "success!"}
|
|
|
}
|