|
@@ -1,7 +1,9 @@
|
|
|
package user
|
|
|
|
|
|
import (
|
|
|
+ "eta_mini_ht_api/common/component/config"
|
|
|
logger "eta_mini_ht_api/common/component/log"
|
|
|
+ "eta_mini_ht_api/common/contants"
|
|
|
"eta_mini_ht_api/common/exception"
|
|
|
authUtils "eta_mini_ht_api/common/utils/auth"
|
|
|
"eta_mini_ht_api/controllers"
|
|
@@ -12,11 +14,14 @@ type AuthController struct {
|
|
|
controllers.BaseController
|
|
|
}
|
|
|
|
|
|
+const ChinaAreaCode = "86"
|
|
|
+
|
|
|
// LoginReq 获取验证码请求
|
|
|
type LoginReq struct {
|
|
|
Code string `json:"code"`
|
|
|
VerifyCode string `json:"verify_code"`
|
|
|
Mobile string `json:"mobile"`
|
|
|
+ AreaCode string `json:"area_code"`
|
|
|
}
|
|
|
|
|
|
// Login 小程序登录接口
|
|
@@ -30,7 +35,12 @@ func (a *AuthController) Login() {
|
|
|
result = a.InitWrapData("登录失败")
|
|
|
loginReq := new(LoginReq)
|
|
|
a.GetPostParams(loginReq)
|
|
|
- if !authUtils.IsValidMobile(loginReq.Mobile) {
|
|
|
+ if loginReq.AreaCode == "" || !checkValidAreaCode(loginReq.AreaCode) {
|
|
|
+ a.FailedResult("登录失败", result)
|
|
|
+ err = exception.New(exception.IllegalAreaCode)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if loginReq.AreaCode == ChinaAreaCode && !authUtils.IsValidMobile(loginReq.Mobile) {
|
|
|
a.FailedResult("登录失败", result)
|
|
|
err = exception.New(exception.IllegalPhoneNumber)
|
|
|
return
|
|
@@ -65,7 +75,8 @@ type LoginResp struct {
|
|
|
|
|
|
// SmsCodeReq 获取验证码请求
|
|
|
type SmsCodeReq struct {
|
|
|
- Mobile string `json:"mobile"`
|
|
|
+ Mobile string `json:"mobile"`
|
|
|
+ AreaCode string `json:"area_code"`
|
|
|
}
|
|
|
|
|
|
// SMSCode 小程序手机验证码接口
|
|
@@ -79,11 +90,15 @@ func (a *AuthController) SMSCode() {
|
|
|
result = a.InitWrapData("发送短信失败")
|
|
|
mobile := new(SmsCodeReq)
|
|
|
a.GetPostParams(mobile)
|
|
|
+ if mobile.AreaCode == "" || !checkValidAreaCode(mobile.AreaCode) {
|
|
|
+ a.FailedResult("登录失败", result)
|
|
|
+ err = exception.New(exception.IllegalAreaCode)
|
|
|
+ return
|
|
|
+ }
|
|
|
phoneNum := mobile.Mobile
|
|
|
- if !authUtils.IsValidMobile(phoneNum) {
|
|
|
+ if mobile.AreaCode == ChinaAreaCode && !authUtils.IsValidMobile(phoneNum) {
|
|
|
return result, exception.New(exception.IllegalPhoneNumber)
|
|
|
}
|
|
|
- //
|
|
|
//发送短息
|
|
|
err = auth.SendSMSCode(phoneNum)
|
|
|
if err != nil {
|
|
@@ -97,3 +112,95 @@ func (a *AuthController) SMSCode() {
|
|
|
return result, nil
|
|
|
})
|
|
|
}
|
|
|
+
|
|
|
+type RefreshTokenRes struct {
|
|
|
+ Token string `json:"token"`
|
|
|
+}
|
|
|
+
|
|
|
+// RefreshToken 更新token
|
|
|
+// @Summary 更新token
|
|
|
+// @Success 200 {object} controllers.BaseResponse
|
|
|
+// @Description 更新token
|
|
|
+// @router /refreshToken [get]
|
|
|
+func (a *AuthController) RefreshToken(code string) {
|
|
|
+ controllers.Wrap(&a.BaseController, func() (result *controllers.WrapData, err error) {
|
|
|
+ result = a.InitWrapData("刷新token失败")
|
|
|
+ if code == "" {
|
|
|
+ logger.Error("code不能为空")
|
|
|
+ return result, exception.New(exception.WeChatCodeEmpty)
|
|
|
+ }
|
|
|
+ //刷新token
|
|
|
+ token, err := auth.RefreshToken(code)
|
|
|
+ if err != nil {
|
|
|
+ logger.Error("刷新token失败:%v", err)
|
|
|
+ a.FailedResult("刷新token失败", result)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ a.SuccessResult("刷新token成功", RefreshTokenRes{
|
|
|
+ Token: token,
|
|
|
+ }, result)
|
|
|
+ return
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+// AreaCodes 小程序手机验证码接口
|
|
|
+// @Summary 获取手机验证码
|
|
|
+// @Param mobile body SmsCodeReq true "小程序手机验证码接口"
|
|
|
+// @Success 200 {object} controllers.BaseResponse
|
|
|
+// @Description 用户发送手机验证码
|
|
|
+// @router /areaCodes [get]
|
|
|
+func (a *AuthController) AreaCodes() {
|
|
|
+ controllers.Wrap(&a.BaseController, func() (result *controllers.WrapData, err error) {
|
|
|
+ result = a.InitWrapData("获取区号失败")
|
|
|
+ list, err := auth.GetAreaCodes()
|
|
|
+ if err != nil {
|
|
|
+ a.FailedResult("获取区号失败", result)
|
|
|
+ }
|
|
|
+ a.SuccessResult("获取区号成功", list, result)
|
|
|
+ return
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+type WXAppidResp struct {
|
|
|
+ AppId string
|
|
|
+}
|
|
|
+
|
|
|
+// WXAppid 获取APPID
|
|
|
+// @Summary 获取APPID
|
|
|
+// @Success 200 {object} controllers.BaseResponse
|
|
|
+// @Description 获取APPID
|
|
|
+// @router /wxAppid [get]
|
|
|
+func (a *AuthController) WXAppid() {
|
|
|
+ controllers.Wrap(&a.BaseController, func() (result *controllers.WrapData, err error) {
|
|
|
+ result = a.InitWrapData("获取AppId失败")
|
|
|
+ appid := config.GetConfig(contants.WECHAT).(*config.WechatConfig).GetAppid()
|
|
|
+ if err != nil {
|
|
|
+ a.FailedResult("获取AppId失败", result)
|
|
|
+ }
|
|
|
+ a.SuccessResult("获取AppId成功", WXAppidResp{
|
|
|
+ AppId: appid,
|
|
|
+ }, result)
|
|
|
+ return
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+// Test 获取APPID
|
|
|
+// @Success 200 {object} controllers.BaseResponse
|
|
|
+// @Description 获取APPID
|
|
|
+// @router /test [get]
|
|
|
+func (a *AuthController) Test() {
|
|
|
+ auth.GetValidAreaCodes()
|
|
|
+}
|
|
|
+
|
|
|
+func checkValidAreaCode(areaCode string) bool {
|
|
|
+ list := auth.GetValidAreaCodes()
|
|
|
+ if areaCode == "" || len(list) == 0 {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ for _, code := range list {
|
|
|
+ if areaCode == code {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false
|
|
|
+}
|