loginserver.go 3.16 KB
package apis

import (
	. "loginserver/models"
	// "encoding/base64"
	// "encoding/json"
	// "github.com/tidwall/gjson"
	"github.com/gin-gonic/gin"
	// "io"
	// "io/ioutil"
	"log"
	"net/http"
	// "net/url"
	// "os"
	// "reflect"
	// "strconv"
	// "strings"
	// "time"
)

//登录接口
func UserLoginApi(c *gin.Context) {
	// buf := make([]byte, 1024)
	// n, _ := c.Request.Body.Read(buf)
	// log.Println(string(buf[0:n]))
	loginType := c.Request.FormValue("login_type")
	loginName := c.Request.FormValue("login_name")
	loginPassword := c.Request.FormValue("login_password")
	l := UserLoginInfo{
		Type:     loginType,
		Name:     loginName,
		Password: loginPassword,
	}
	if loginType == "" || (loginType == "normal" && (loginName == "" || loginPassword == "")) {
		log.Printf("%+v", l)
		ReturnRespones(c, "104002", "参数错误")
		return
	}

	result, err := l.UserLogin()
	if err != nil {
		ReturnRespones(c, result.(string), err.Error())
		return
	}
	cookie := &http.Cookie{
		Name:     "icesimbaToken",
		Value:    result.(map[string]interface{})["token"].(string),
		Path:     "/",
		HttpOnly: true,
	}
	res := map[string]string{
		"user_id": result.(map[string]interface{})["user_id"].(string),
	}
	http.SetCookie(c.Writer, cookie)
	ReturnRespones(c, "0", res)
	return
}

//注册接口
func UserRegisterApi(c *gin.Context) {
	registerType := c.Request.FormValue("register_type")
	registerName := c.Request.FormValue("register_name")
	registerPassword := c.Request.FormValue("register_password")
	r := UserRegisterInfo{
		Type:     registerType,
		Name:     registerName,
		Password: registerPassword,
	}
	if registerType == "" || registerName == "" || registerPassword == "" {
		log.Printf("%+v", r)
		ReturnRespones(c, "104002", "参数错误")
		return
	}
	result, err := r.UserRegister()
	if err != nil {
		log.Println(err)
		ReturnRespones(c, result.(string), err.Error())
		return
	}
	ReturnRespones(c, "0", result)
	return
}

//校验用户名接口
func CheckUserNameRepeatApi(c *gin.Context) {
	userName := c.Request.FormValue("user_name")
	if userName == "" {
		log.Println("用户名为空")
		ReturnRespones(c, "104002", "参数错误")
		return
	}
	result, err := CheckUserNameRepeat(userName)
	if err != nil {
		log.Println(err)
		ReturnRespones(c, result.(string), err.Error())
		return
	}
	ReturnRespones(c, "0", result)
	return
}

//修改密码接口
func ChangeUserPasswordApi(c *gin.Context) {
	userName := c.Request.FormValue("user_name")
	userOriginPassword := c.Request.FormValue("user_origin_password")
	userNewPassword := c.Request.FormValue("user_new_password")
	if userName == "" || userOriginPassword == "" || userNewPassword == "" {
		log.Println("userName:", userName, "userOriginPassword:", userOriginPassword, "userNewPassword:", userNewPassword)
		ReturnRespones(c, "104002", "参数错误")
		return
	}
	result, err := ChangeUserPassword(userName, userOriginPassword, userNewPassword)
	if err != nil {
		log.Println(err)
		ReturnRespones(c, result.(string), err.Error())
		return
	}
	ReturnRespones(c, "0", result)
	return
}

//返回函数
func ReturnRespones(c *gin.Context, code string, result interface{}) {
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"data": result,
	})
	return
}