Files
jx-callback/controllers/auth_controller.go
2018-12-27 16:49:32 +08:00

166 lines
5.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package controllers
import (
"encoding/base64"
"fmt"
"net/http"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxcallback/auth"
_ "git.rosy.net.cn/jx-callback/business/jxcallback/auth/localpass" // 加载本地用户密码
"git.rosy.net.cn/jx-callback/business/jxcallback/auth/mobile"
"git.rosy.net.cn/jx-callback/business/jxcallback/auth/weixin"
"git.rosy.net.cn/jx-callback/globals"
"github.com/astaxie/beego"
)
type WeixinCallbackResult struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
// 认证相关API
type AuthController struct {
beego.Controller
}
var (
ErrParameterIsIllegal = "参数不全或不合法"
)
// @Title 给微信用的回调接口
// @Description 给微信用的回调接口,自己不能直接调用
// @Param code query string true "客户同意后得到的code"
// @Param block query string true "回调地址"
// @Param state query string false "微信回调的登录状态"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetWeiXinUserInfo [get]
func (c *AuthController) GetWeiXinUserInfo() {
retVal := &WeixinCallbackResult{}
var err error
code := c.GetString("code")
block := c.GetString("block")
state := c.GetString("state")
if block != "" {
if code != "" {
result, err2 := weixin.GetUserInfo(code, state)
if err = err2; err == nil {
retVal.Code = 1
retVal.Msg = "微信登录成功"
retVal.Data = result
} else {
retVal.Msg = err.Error()
}
} else {
retVal.Msg = "code为空"
}
} else {
retVal.Msg = "没有block"
}
c.Redirect(fmt.Sprintf("%s?info=%s", block, base64.StdEncoding.EncodeToString(utils.MustMarshal(retVal))), http.StatusTemporaryRedirect)
}
// @Title 登录接口
// @Description 登录接口
// @Param id formData string true "登录ID"
// @Param type formData string true "登录类型,当前支持[weixinsns,localpass]"
// @Param secret formData string true "不同登录类型的登录秘密"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /Login [post]
func (c *AuthController) Login() {
c.callLogin(func(params *tAuthLoginParams) (retVal interface{}, errCode string, err error) {
retVal, err = auth.Login(params.Id, params.Type, params.Secret)
return retVal, "", err
})
}
// @Title 登出接口
// @Description 登出接口
// @Param token header string true "认证token"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /Logout [delete]
func (c *AuthController) Logout() {
c.callLogout(func(params *tAuthLogoutParams) (retVal interface{}, errCode string, err error) {
err = auth.Logout(params.Token)
globals.SugarLogger.Debug(err)
return nil, "", err
})
}
// @Title 得到用户信息
// @Description 得到用户信息从token中
// @Param token header string true "认证token"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetUserInfo [get]
func (c *AuthController) GetUserInfo() {
c.callGetUserInfo(func(params *tAuthGetUserInfoParams) (retVal interface{}, errCode string, err error) {
retVal, err = auth.GetUserInfo(params.Token)
return retVal, "", err
})
}
// @Title 发送验证码
// @Description 发送验证码
// @Param mobile formData string true "手机号"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /SendMobileVerifyCode [post]
func (c *AuthController) SendMobileVerifyCode() {
c.callSendMobileVerifyCode(func(params *tAuthSendMobileVerifyCodeParams) (retVal interface{}, errCode string, err error) {
err = mobile.SendVerifyCode(params.Mobile)
return retVal, "", err
})
}
// @Title 绑定手机
// @Description 绑定手机
// @Param token header string true "认证token"
// @Param mobile formData string true "手机号"
// @Param code formData string true "验证码"
// @Param nickname formData string false "用户名"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /BindMobile [post]
func (c *AuthController) BindMobile() {
c.callBindMobile(func(params *tAuthBindMobileParams) (retVal interface{}, errCode string, err error) {
err = weixin.BindMobile(params.Token, params.Mobile, params.Code, params.Nickname)
return retVal, "", err
})
}
// @Title 绑定手机
// @Description 绑定手机(调用此方法前先需要以短信方式登录)
// @Param token header string true "认证token"
// @Param mobile formData string true "手机号,当前无用,置空"
// @Param code formData string true "小程序用户code"
// @Param nickname formData string false "用户名"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /MiniBindWeiXin [post]
func (c *AuthController) MiniBindWeiXin() {
c.callMiniBindWeiXin(func(params *tAuthMiniBindWeiXinParams) (retVal interface{}, errCode string, err error) {
err = weixin.AutherMini.BindWeiXin(params.Ctx, params.Code, params.Nickname)
return retVal, "", err
})
}
// @Title 解密小程序数据
// @Description 解密小程序数据
// @Param token header string true "认证token"
// @Param data formData string true "加密数据"
// @Param iv formData string true "iv"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /MiniDecrpytData [post]
func (c *AuthController) MiniDecrpytData() {
c.callMiniDecrpytData(func(params *tAuthMiniDecrpytDataParams) (retVal interface{}, errCode string, err error) {
retVal, err = weixin.AutherMini.DecryptData(params.Ctx, params.Data, params.Iv)
return retVal, "", err
})
}