Parcourir la source

新增绑定接口

rdluck il y a 4 ans
Parent
commit
00df30fe71
7 fichiers modifiés avec 130 ajouts et 144 suppressions
  1. 0 5
      controllers/base_auth.go
  2. 62 10
      controllers/wechat.go
  3. 1 1
      lastupdate.tmp
  4. 13 15
      models/wx_user.go
  5. 8 0
      routers/router.go
  6. 28 64
      swagger/swagger.json
  7. 18 49
      swagger/swagger.yml

+ 0 - 5
controllers/base_auth.go

@@ -34,11 +34,6 @@ func (this *BaseAuthController) Prepare() {
 	if method != "HEAD" {
 		if method == "POST" || method == "GET" {
 			authorization := this.Ctx.Input.Header("Authorization")
-			if authorization == "" {
-				cookie := this.Ctx.GetCookie("rddp_access_token")
-				utils.FileLog.Info("authorization:%s,cookie:%s", authorization, cookie)
-				authorization = cookie
-			}
 			this.Token=authorization
 			if authorization == "" {
 				this.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:Token is empty or account is empty"}, false, false)

+ 62 - 10
controllers/wechat.go

@@ -267,16 +267,15 @@ func (this *WechatController) GetUserInfo() {
 		token = tokenItem.AccessToken
 	}
 	resp := new(models.WxGetUserInfoResp)
-	resp.NickName = userInfo.Nickname
-	resp.Gender = userInfo.Gender
-	resp.Province = userInfo.Province
-	resp.Language = userInfo.Language
-	resp.Country = userInfo.Country
-	resp.City = userInfo.City
-	resp.AvatarUrl = userInfo.Avatar
-	resp.Unionid = userInfo.UnionID
-	resp.Appid = userInfo.Watermark.AppID
-	resp.Timestamp = userInfo.Watermark.Timestamp
+	//resp.NickName = userInfo.Nickname
+	//resp.Gender = userInfo.Gender
+	//resp.Province = userInfo.Province
+	//resp.Language = userInfo.Language
+	//resp.Country = userInfo.Country
+	//resp.City = userInfo.City
+	//resp.AvatarUrl = userInfo.Avatar
+	//resp.Appid = userInfo.Watermark.AppID
+	//resp.Timestamp = userInfo.Watermark.Timestamp
 	resp.Authorization = token
 	br.Msg = "获取成功!"
 	br.Ret = 200
@@ -334,3 +333,56 @@ func (this *WechatController) GetPhoneNumber() {
 	br.Success = true
 	br.Data = resp
 }
+
+
+// @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
+}

+ 1 - 1
lastupdate.tmp

@@ -1 +1 @@
-{"E:\\code\\go\\src\\hongze\\hongze_cygx\\controllers":1611901964288200000}
+{"E:\\code\\go\\src\\hongze\\hongze_cygx\\controllers":1611905527222172100}

+ 13 - 15
models/wx_user.go

@@ -135,21 +135,19 @@ func DeleteWxUserByUserId(userId int) (err error) {
 }
 
 type WxGetUserInfoResp struct {
-	UsersId       int       `orm:"column(id);pk"`
-	Mobile        string    `description:"手机号"`
-	NickName      string    `description:"昵称"`
-	Gender        int       `description:"用户性别 1:男性,2:女性,0:未知(默认)"`
-	CreateTime    time.Time `description:"注册时间"`
-	ModifyTime    time.Time `description:"修改时间"`
-	AvatarUrl     string    `description:"头像地址"`
-	OpenId        string    `description:"open_id"`
-	Unionid       string    `description:"unionid"`
-	City          string    `description:"城市"`
-	Province      string    `description:"省"`
-	Country       string    `description:"国家"`
-	Language      string    `description:"语言"`
-	Appid         string    `description:"Appid"`
-	Timestamp     int64     `description:"时间戳"`
+	//UsersId       int       `orm:"column(id);pk"`
+	//Mobile        string    `description:"手机号"`
+	//NickName      string    `description:"昵称"`
+	//Gender        int       `description:"用户性别 1:男性,2:女性,0:未知(默认)"`
+	//CreateTime    time.Time `description:"注册时间"`
+	//ModifyTime    time.Time `description:"修改时间"`
+	//AvatarUrl     string    `description:"头像地址"`
+	//City          string    `description:"城市"`
+	//Province      string    `description:"省"`
+	//Country       string    `description:"国家"`
+	//Language      string    `description:"语言"`
+	//Appid         string    `description:"Appid"`
+	//Timestamp     int64     `description:"时间戳"`
 	Authorization string    `description:"登陆凭证,后续接口调用时,带在请求头里面Key:Authorization"`
 }
 

+ 8 - 0
routers/router.go

@@ -9,10 +9,18 @@ package routers
 
 import (
 	"github.com/astaxie/beego"
+	"github.com/astaxie/beego/plugins/cors"
 	"hongze/hongze_cygx/controllers"
 )
 
 func init() {
+	beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
+		AllowAllOrigins:  true,
+		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
+		AllowHeaders:     []string{"Origin", "Account", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
+		ExposeHeaders:    []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type", "Sign"},
+		AllowCredentials: true,
+	}))
 	ns := beego.NewNamespace("/api",
 		beego.NSNamespace("/home",
 			beego.NSInclude(

+ 28 - 64
swagger/swagger.json

@@ -15,6 +15,34 @@
     },
     "basePath": "/api",
     "paths": {
+        "/wechat/bind": {
+            "post": {
+                "tags": [
+                    "wechat"
+                ],
+                "description": "绑定手机号或邮箱",
+                "operationId": "WechatController.绑定手机号或邮箱",
+                "parameters": [
+                    {
+                        "in": "body",
+                        "name": "request",
+                        "description": "type json string",
+                        "required": true,
+                        "schema": {
+                            "$ref": "#/definitions/models.WxGetPhoneNumberReq"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "",
+                        "schema": {
+                            "$ref": "#/definitions/models.WxGetPhoneNumberResp"
+                        }
+                    }
+                }
+            }
+        },
         "/wechat/getPhoneNumber": {
             "post": {
                 "tags": [
@@ -161,73 +189,9 @@
             "title": "WxGetUserInfoResp",
             "type": "object",
             "properties": {
-                "Appid": {
-                    "description": "Appid",
-                    "type": "string"
-                },
                 "Authorization": {
                     "description": "登陆凭证,后续接口调用时,带在请求头里面Key:Authorization",
                     "type": "string"
-                },
-                "AvatarUrl": {
-                    "description": "头像地址",
-                    "type": "string"
-                },
-                "City": {
-                    "description": "城市",
-                    "type": "string"
-                },
-                "Country": {
-                    "description": "国家",
-                    "type": "string"
-                },
-                "CreateTime": {
-                    "description": "注册时间",
-                    "type": "string",
-                    "format": "datetime"
-                },
-                "Gender": {
-                    "description": "用户性别 1:男性,2:女性,0:未知(默认)",
-                    "type": "integer",
-                    "format": "int64"
-                },
-                "Language": {
-                    "description": "语言",
-                    "type": "string"
-                },
-                "Mobile": {
-                    "description": "手机号",
-                    "type": "string"
-                },
-                "ModifyTime": {
-                    "description": "修改时间",
-                    "type": "string",
-                    "format": "datetime"
-                },
-                "NickName": {
-                    "description": "昵称",
-                    "type": "string"
-                },
-                "OpenId": {
-                    "description": "open_id",
-                    "type": "string"
-                },
-                "Province": {
-                    "description": "省",
-                    "type": "string"
-                },
-                "Timestamp": {
-                    "description": "时间戳",
-                    "type": "integer",
-                    "format": "int64"
-                },
-                "Unionid": {
-                    "description": "unionid",
-                    "type": "string"
-                },
-                "UsersId": {
-                    "type": "integer",
-                    "format": "int64"
                 }
             }
         },

+ 18 - 49
swagger/swagger.yml

@@ -11,6 +11,24 @@ info:
     url: http://www.apache.org/licenses/LICENSE-2.0.html
 basePath: /api
 paths:
+  /wechat/bind:
+    post:
+      tags:
+      - wechat
+      description: 绑定手机号或邮箱
+      operationId: WechatController.绑定手机号或邮箱
+      parameters:
+      - in: body
+        name: request
+        description: type json string
+        required: true
+        schema:
+          $ref: '#/definitions/models.WxGetPhoneNumberReq'
+      responses:
+        "200":
+          description: ""
+          schema:
+            $ref: '#/definitions/models.WxGetPhoneNumberResp'
   /wechat/getPhoneNumber:
     post:
       tags:
@@ -111,58 +129,9 @@ definitions:
     title: WxGetUserInfoResp
     type: object
     properties:
-      Appid:
-        description: Appid
-        type: string
       Authorization:
         description: 登陆凭证,后续接口调用时,带在请求头里面Key:Authorization
         type: string
-      AvatarUrl:
-        description: 头像地址
-        type: string
-      City:
-        description: 城市
-        type: string
-      Country:
-        description: 国家
-        type: string
-      CreateTime:
-        description: 注册时间
-        type: string
-        format: datetime
-      Gender:
-        description: 用户性别 1:男性,2:女性,0:未知(默认)
-        type: integer
-        format: int64
-      Language:
-        description: 语言
-        type: string
-      Mobile:
-        description: 手机号
-        type: string
-      ModifyTime:
-        description: 修改时间
-        type: string
-        format: datetime
-      NickName:
-        description: 昵称
-        type: string
-      OpenId:
-        description: open_id
-        type: string
-      Province:
-        description: 省
-        type: string
-      Timestamp:
-        description: 时间戳
-        type: integer
-        format: int64
-      Unionid:
-        description: unionid
-        type: string
-      UsersId:
-        type: integer
-        format: int64
   models.WxLoginResp:
     title: WxLoginResp
     type: object