This commit is contained in:
arraykeys
2019-08-08 17:13:34 +08:00
parent e8e5966a8c
commit f0bf2d5fec
2885 changed files with 1195993 additions and 12 deletions
+101
View File
@@ -0,0 +1,101 @@
package controllers
import (
"strings"
"github.com/golang-collections/lib.go/validation/validator"
"github.com/astaxie/beego"
"anytunnel/at-web/app/utils"
"anytunnel/at-web/app/business"
)
type AuthorController struct {
BaseUserController
}
// login
func (this *AuthorController) Login() {
this.viewLayoutTitle("AnyTunnelCloud", "author/login", "login")
}
// register
func (this *AuthorController) Register() {
this.viewLayoutTitle("AnyTunnelCloud", "author/register", "login")
}
//register save
func (this *AuthorController) Signin() {
username := strings.Trim(this.GetString("username"), "")
password := strings.Trim(this.GetString("password"), "")
email := strings.Trim(this.GetString("email"), "")
if(username == "") {
this.jsonError("注册失败:用户名不能为空")
}
if(password == "") {
this.jsonError("注册失败:密码不能为空")
}
if(email == "") {
this.jsonError("注册失败:邮箱不能为空")
}
if(!validator.IsEmail(email)) {
this.jsonError("注册失败:邮箱格式错误")
}
data := map[string]string{
"username": username,
"password": password,
"email": email,
}
user, err := business.NewBase().PostRequest("create_user", data, nil);
if(err != nil) {
this.jsonError("注册失败")
}
userMap := user.(map[string]interface{})
userId := utils.NewConvert().FloatToString(userMap["user_id"].(float64), 'f', 0, 64)
//保存session和cookie
this.refreshSession(userId)
this.jsonSuccess("恭喜,注册成功", nil, "/user/index")
}
//login save
func (this *AuthorController) Signup() {
username := strings.Trim(this.GetString("username"), "")
password := strings.Trim(this.GetString("password"), "")
if(username == "") {
this.jsonError("登录失败:用户名不能为空")
}
if(password == "") {
this.jsonError("登录失败:密码不能为空")
}
data := map[string]string{
"username":username,
}
user, err := business.NewBase().GetRequest("get_user_by_name", data)
if(err != nil) {
this.jsonError("登录失败:server error")
}
userMap := user.(map[string]interface{})
if(len(userMap) == 0) {
this.jsonError("登录失败:用户名不存在或密码错误")
}
if(userMap["is_forbidden"].(string) == "1") {
this.jsonError("登录失败:该用户已被屏蔽")
}
if(userMap["password"].(string) != utils.NewEncrypt().Md5Encode(password)) {
this.jsonError("登录失败:用户名或密码错误")
}
//保存session和cookie
this.refreshSession(userMap["user_id"].(string))
this.jsonSuccess("恭喜,登录成功", nil, "/user/index")
}
func (this *AuthorController) Logout() {
passport := beego.AppConfig.String("login.passport")
this.Ctx.SetCookie(passport, "")
this.SetSession("user", nil)
this.redirect("/");
}
+121
View File
@@ -0,0 +1,121 @@
package controllers
import (
"encoding/json"
"strings"
"github.com/astaxie/beego"
)
type BaseController struct {
beego.Controller
}
func (this *BaseController) viewLayoutTitle(title, viewName, layout string) {
this.Layout = "layout/" + layout + ".html"
this.TplName = viewName + ".html"
this.Data["title"] = title
this.Render()
}
func (this *BaseController) viewLayout(viewName, layout string) {
this.Layout = "layout/" + layout + ".html"
this.TplName = viewName + ".html"
this.Data["title"] = ""
this.Render()
}
func (this *BaseController) view(viewName string) {
this.Layout = "layout/default.html"
this.TplName = viewName + ".html"
this.Data["title"] = ""
this.Render()
}
func (this *BaseController) viewError(errorMessage string, data ...interface{}) {
this.Layout = "layout/page.html"
errorType := "500"
if len(data) > 0 {
errorType = data[0].(string)
}
this.TplName = "error/" + errorType + ".html"
this.Data["title"] = "system error"
this.Data["errorMessage"] = errorMessage
this.Render()
}
func (this *BaseController) viewTitle(title, viewName string) {
this.Layout = "layout/default.html"
this.TplName = viewName + ".html"
this.Data["title"] = title
this.Render()
}
func (this *BaseController) jsonSuccess(message interface{}, data ...interface{}) {
url := ""
sleep := 500
var _data interface{}
if len(data) > 0 {
_data = data[0]
}
if len(data) > 1 {
url = data[1].(string)
}
if len(data) > 2 {
sleep = data[2].(int)
}
this.Data["json"] = JSONResponse{
Code: 1,
Message: message,
Data: _data,
Redirect: map[string]interface{}{
"url": url,
"sleep": sleep,
},
}
//this.ServeJSON()
j, err := json.MarshalIndent(this.Data["json"], "", "\t")
if err != nil {
this.Abort(err.Error())
} else {
this.Abort(string(j))
}
}
func (this *BaseController) jsonError(message interface{}, data ...interface{}) {
url := ""
sleep := 500
var _data interface{}
if len(data) > 0 {
_data = data[0]
}
if len(data) > 1 {
url = data[1].(string)
}
if len(data) > 2 {
sleep = data[2].(int)
}
this.Data["json"] = JSONResponse{
Code: 0,
Message: message,
Data: _data,
Redirect: map[string]interface{}{
"url": url,
"sleep": sleep,
},
}
j, err := json.MarshalIndent(this.Data["json"], "", " \t")
if err != nil {
this.Abort(err.Error())
} else {
this.Abort(string(j))
}
}
//获取用户IP地址
func (this *BaseController) getClientIp() string {
s := strings.Split(this.Ctx.Request.RemoteAddr, ":")
return s[0]
}
//302跳转
func (this *BaseController) redirect(url string) {
this.Redirect(url, 302)
this.StopRun()
}
+118
View File
@@ -0,0 +1,118 @@
package controllers
import (
"strings"
"github.com/astaxie/beego"
"anytunnel/at-web/app/utils"
"anytunnel/at-web/app/business"
"fmt"
)
type JSONResponse struct {
Code int `json:"code"`
Message interface{} `json:"message"`
Data interface{} `json:"data"`
Redirect map[string]interface{} `json:"redirect"`
}
type BaseUserController struct {
BaseController
loginUser map[string]interface{}
controllerName string
methodName string
}
//验证登录
func (this *BaseUserController) isLogin() bool {
controllerName, actionName := this.GetControllerAndAction()
this.controllerName = strings.ToLower(controllerName[0 : len(controllerName)-10])
this.methodName = actionName
//忽略 /author /error
if(this.controllerName == "author" || this.controllerName == "error") {
return true;
}
passport := beego.AppConfig.String("login.passport")
cookie := this.Ctx.GetCookie(passport)
//cookie 失效
if(cookie == "") {
return false
}
user := this.GetSession("user")
//session 失效
if(user == nil) {
return false
}
cookieValue, _ := utils.NewEncrypt().Base64Decode(cookie)
if(cookieValue == "") {
return false
}
identifyList := strings.Split(cookieValue, "@")
username := identifyList[0]
identify := identifyList[1]
userValue := user.(map[string]interface{})
//对比cookie 和 session username
if(username != userValue["username"].(string)) {
return false
}
//对比客户端UAG and IP
if(identify != utils.NewEncrypt().Md5Encode(this.Ctx.Request.UserAgent() + this.getClientIp() + userValue["password"].(string))) {
return false
}
this.loginUser = userValue;
//this.refreshSession()
//success
return true
}
func (this *BaseUserController) Prepare() {
if !this.isLogin() {
this.Redirect("/author/login", 302)
this.StopRun()
}
this.Layout = "layout/default.html"
}
func (this *BaseUserController) checkAccess() {
}
//重置 session 和 cookie
func (this *BaseUserController) refreshSession(userId string) {
data := map[string]string {
"user_id" : userId,
}
user, err := business.NewBase().GetRequest("get_user_by_id", data)
if(err != nil) {
fmt.Println("重置session错误")
}
if(len(user.(map[string]interface{})) > 0) {
username := user.(map[string]interface{})["username"].(string)
password := user.(map[string]interface{})["password"].(string)
//重置 session
this.SetSession("user", user)
//重置 cookie
identify := utils.NewEncrypt().Md5Encode(this.Ctx.Request.UserAgent() + this.getClientIp() + password)
passportValue := utils.NewEncrypt().Base64Encode(username + "@" + identify)
passport := beego.AppConfig.String("login.passport")
cookieTime := beego.AppConfig.String("login.cookie.time")
this.Ctx.SetCookie(passport, passportValue, cookieTime)
}
this.loginUser = user.(map[string]interface{});
}
//get user_id
func (this *BaseUserController) getUserId() string {
return this.loginUser["user_id"].(string)
}
//get username
func (this *BaseUserController) getUsername() string {
return this.loginUser["username"].(string)
}
//get email
func (this *BaseUserController) getEmail() string {
return this.loginUser["email"].(string)
}
+124
View File
@@ -0,0 +1,124 @@
package controllers
import (
"anytunnel/at-web/app/utils"
"strings"
"anytunnel/at-web/app/business"
)
type ClientController struct {
BaseUserController
}
func (this *ClientController) List() {
userId := this.getUserId()
keyword := this.GetString("keyword")
data := map[string]string {
"user_id": userId,
"keyword": keyword,
}
clientValues, err:= business.NewBase().GetRequest("client_list_uri", data)
if(err != nil) {
this.viewError("request error")
}
this.Data["clientValues"] = clientValues
this.Data["keyword"] = keyword
this.viewLayoutTitle("client列表", "client/list", "page")
}
//add client
func (this *ClientController) Add() {
this.Data["clientValue"] = map[string]string{
"client_id": "0",
"name": "",
"token": "",
}
this.viewLayoutTitle("添加client", "client/form", "page")
}
//save client
func (this *ClientController) Save() {
name := strings.Trim(this.GetString("name"), "");
data := map[string]string {
"name": name,
"token": utils.NewMisc().RandString(32),
"user_id": this.getUserId(),
}
_, err := business.NewBase().PostRequest("client_create_uri", data, nil)
if(err != nil) {
this.jsonError(err.Error())
}
this.jsonSuccess("添加Client成功", nil, "/client/list");
}
//edit client
func (this *ClientController) Edit() {
clientId := this.GetString("client_id")
if(clientId == "") {
this.viewError("client_id error")
}
data := map[string]string{
"client_id":clientId,
}
clientValues, err:= business.NewBase().GetRequest("client_info_uri", data)
if(err != nil) {
this.viewError(err.Error())
}
this.Data["clientValue"] = clientValues
this.viewLayoutTitle("修改client", "client/form", "page")
}
//save client
func (this *ClientController) Modify() {
name := this.GetString("name");
clientId := this.GetString("client_id");
data := map[string]string{
"name": name,
"client_id":clientId,
}
_, err := business.NewBase().PostRequest("client_update_uri", data, nil)
if(err != nil) {
this.jsonError(err.Error())
}
this.jsonSuccess("修改Client成功", nil, "/client/list");
}
//delete client
func (this *ClientController) Delete() {
clientId := this.GetString("client_id")
data := map[string]string{
"client_id": clientId,
}
_, err := business.NewBase().GetRequest("client_delete_uri", data)
if(err != nil) {
this.jsonError(err.Error())
}
this.jsonSuccess("删除client成功", nil, "client/list")
}
//reset token
func (this *ClientController) ResetToken() {
clientId := this.GetString("client_id");
data := map[string]string{
"token": utils.NewMisc().RandString(32),
"client_id": clientId,
}
_, err := business.NewBase().PostRequest("client_update_uri", data, nil)
if(err != nil) {
this.jsonError(err.Error())
}
this.jsonSuccess("重置Token成功", nil, "/client/list");
}
+14
View File
@@ -0,0 +1,14 @@
package controllers
type MainController struct {
BaseController
}
func (this *MainController) Index() {
isLogin := "1"
if(this.GetSession("user") == nil) {
isLogin = "0"
}
this.Data["isLogin"] = isLogin
this.viewLayoutTitle("AnyTunnelCloud", "web/index", "index")
}
+124
View File
@@ -0,0 +1,124 @@
package controllers
import (
"anytunnel/at-web/app/utils"
"strings"
"anytunnel/at-web/app/business"
)
type ServerController struct {
BaseUserController
}
func (this *ServerController) List() {
userId := this.getUserId()
keyword := this.GetString("keyword")
data := map[string]string {
"user_id": userId,
"keyword": keyword,
}
serverValues, err:= business.NewBase().GetRequest("server_list_uri", data)
if(err != nil) {
this.viewError("request error")
}
this.Data["serverValues"] = serverValues
this.Data["keyword"] = keyword
this.viewLayoutTitle("server列表", "server/list", "page")
}
//add server
func (this *ServerController) Add() {
this.Data["serverValue"] = map[string]string{
"server_id": "0",
"name": "",
"token": "",
}
this.viewLayoutTitle("添加server", "server/form", "page")
}
//save server
func (this *ServerController) Save() {
name := strings.Trim(this.GetString("name"), "");
data := map[string]string {
"name": name,
"token": utils.NewMisc().RandString(32),
"user_id": this.getUserId(),
}
_, err := business.NewBase().PostRequest("server_create_uri", data, nil)
if(err != nil) {
this.jsonError(err.Error())
}
this.jsonSuccess("添加Server成功", nil, "/server/list");
}
//edit server
func (this *ServerController) Edit() {
serverId := this.GetString("server_id")
if(serverId == "") {
this.viewError("server_id error")
}
data := map[string]string{
"server_id":serverId,
}
serverValues, err:= business.NewBase().GetRequest("server_info_uri", data)
if(err != nil) {
this.viewError(err.Error())
}
this.Data["serverValue"] = serverValues
this.viewLayoutTitle("修改server", "server/form", "page")
}
//save server
func (this *ServerController) Modify() {
name := this.GetString("name");
serverId := this.GetString("server_id");
data := map[string]string{
"name": name,
"server_id":serverId,
}
_, err := business.NewBase().PostRequest("server_update_uri", data, nil)
if(err != nil) {
this.jsonError(err.Error())
}
this.jsonSuccess("修改Server成功", nil, "/server/list");
}
//delete server
func (this *ServerController) Delete() {
serverId := this.GetString("server_id")
data := map[string]string{
"server_id": serverId,
}
_, err := business.NewBase().GetRequest("server_delete_uri", data)
if(err != nil) {
this.jsonError(err.Error())
}
this.jsonSuccess("删除server成功", nil, "server/list")
}
//reset token
func (this *ServerController) ResetToken() {
serverId := this.GetString("server_id");
data := map[string]string{
"token": utils.NewMisc().RandString(32),
"server_id": serverId,
}
_, err := business.NewBase().PostRequest("server_update_uri", data, nil)
if(err != nil) {
this.jsonError(err.Error())
}
this.jsonSuccess("重置Token成功", nil, "/server/list");
}
+321
View File
@@ -0,0 +1,321 @@
package controllers
import (
"anytunnel/at-web/app/business"
"strings"
"anytunnel/at-web/app/utils"
"fmt"
)
const MODE_BASE = "0"
const MODE_SENIOR = "1"
const MODE_SPECIAL = "2"
type TunnelController struct {
BaseUserController
}
//tunnel list
func (this *TunnelController) List() {
userId := this.getUserId()
keyword := this.GetString("keyword")
data := map[string]string {
"user_id" : userId,
"keyword" : keyword,
}
tunnelValues, err := business.NewBase().GetRequest("tunnel_list_uri", data)
if(err != nil) {
this.viewError("request error")
}
this.Data["tunnelValues"] = tunnelValues
this.Data["keyword"] = keyword
this.viewLayoutTitle("隧道列表", "tunnel/list", "page")
}
//tunnel add 1. tunnel mode
func (this *TunnelController) Mode() {
userId := this.getUserId()
data := map[string]string{
"user_id": userId,
}
roles, err := business.NewBase().GetRequest("get_user_role", data)
if(err != nil) {
this.viewError("request error " + err.Error())
}
modeBase := "0"
modeSenior := "0"
modeSpecial := "0"
for _, role := range roles.([]interface{}) {
role := role.(map[string]interface{})
tunnelMode := strings.Split(role["tunnel_mode"].(string), ",")
for _, tunnel := range tunnelMode {
if(tunnel == MODE_BASE) {
modeBase = "1"
}
if(tunnel == MODE_SENIOR) {
modeSenior = "1"
}
if(tunnel == MODE_SPECIAL) {
modeSpecial = "1"
}
}
}
this.Data["modeBase"] = modeBase
this.Data["modeSenior"] = modeSenior
this.Data["modeSpecial"] = modeSpecial
//获取所有的 server
this.viewLayoutTitle("选择模式", "tunnel/mode", "page")
}
//tunnel add 2. tunnel cluster
func (this *TunnelController) Cluster() {
clusterId := this.GetString("cluster_id", "");
mode := this.GetString("mode", "0");
data := map[string]string{
"user_id": this.getUserId(),
"mode": mode,
}
regionClusters, err := business.NewBase().GetRequest("user_cluster_list",data)
if(err != nil) {
this.viewError("request error " + err.Error())
}
this.Data["regionClusters"] = regionClusters
this.Data["clusterId"] = clusterId
this.Data["mode"] = mode
this.viewLayoutTitle("选择节点", "tunnel/cluster", "page")
}
//tunnel add 2. tunnel add
func (this *TunnelController) Add() {
if(this.Ctx.Request.Method != "POST") {
this.viewError("request error")
}
clusterId := this.GetString("cluster_id", "");
mode := this.GetString("mode", "0");
if(clusterId == "") {
this.viewError("没有选择节点")
}
isHaveClient := "0"
isHaveServer := "0"
//基础模式,只能部署client
if(mode == MODE_BASE) {
isHaveClient = "1"
}
//高级模式,只能部署client和server
if(mode == MODE_SENIOR) {
isHaveClient = "1"
isHaveServer = "1"
}
//特殊模式,只能部署server
if(mode == MODE_SPECIAL) {
isHaveServer = "1"
}
userId := this.getUserId()
data := map[string]string{
"user_id": userId,
}
clientValues := []interface{}{}
serverValues := []interface{}{}
systemClients := []interface{}{}
systemServers := []interface{}{}
systemServer := map[string]interface{}{}
systemClient := map[string]interface{}{}
//获取系统在线client
res, _ := business.NewBase().GetRequest("online_client_by_clusterId", map[string]string{
"cluster_id": clusterId,
})
if(res != nil) {
systemClients = res.([]interface{})
}
//获取系统在线server
res, _ = business.NewBase().GetRequest("online_server_by_clusterId", map[string]string{
"cluster_id": clusterId,
})
if(res != nil) {
systemClients = res.([]interface{})
}
if(isHaveClient == "1") {
res, _ := business.NewBase().GetRequest("client_list_uri", data)
clientValues = res.([]interface{})
//存在,取交集
if(len(systemClients) > 0) {
for index, clientValue := range clientValues {
for _, systemClient := range systemClients {
if(clientValue.(map[string]interface{})["client_id"].(string) == systemClient.(map[string]interface{})["cs_id"].(string)) {
clientValues = append(clientValues[:index], clientValues[index+1:]...)
break
}
}
}
}
}else {
//随机获取一个系统 client
systemClient = utils.NewMisc().RandSlice(systemClients).(map[string]interface{})
}
if(isHaveServer == "1") {
res, _ := business.NewBase().GetRequest("server_list_uri", data)
serverValues = res.([]interface{})
//存在,取交集
if(len(systemServers) > 0) {
for index, serverValue := range serverValues {
for _, systemServer := range systemServers {
if(serverValue.(map[string]string)["client_id"] == systemServer.(map[string]string)["cs_id"]) {
serverValues = append(serverValues[:index], serverValues[index+1:]...)
break
}
}
}
}
}else {
//随机获取一个系统 server
systemServer = utils.NewMisc().RandSlice(systemServers).(map[string]interface{})
}
this.Data["isHaveClient"] = isHaveClient
this.Data["isHaveServer"] = isHaveServer
this.Data["clientValues"] = clientValues
this.Data["serverValues"] = serverValues
this.Data["systemClient"] = systemClient
this.Data["systemServer"] = systemServer
this.Data["clusterId"] = clusterId
this.Data["mode"] = mode
//获取所有的 server
this.viewLayoutTitle("添加隧道", "tunnel/form", "page")
}
func (this *TunnelController) Save() {
if(this.Ctx.Request.Method != "POST") {
this.viewError("request error")
}
name := this.GetString("name", "");
serverId := this.GetString("server_id", "");
serverListenIp := this.GetString("server_listen_ip", "");
serverListenPort := this.GetString("server_listen_port", "");
clientId := this.GetString("client_id", "");
clientLocalHost := this.GetString("client_local_host", "");
clientLocalPort := this.GetString("client_local_port", "");
mode := this.GetString("mode", "0");
clusterId := this.GetString("cluster_id", "0");
userId := this.getUserId()
data := map[string]string{
"name": strings.Trim(name, ""),
"server_id": strings.Trim(serverId, ""),
"server_listen_ip": strings.Trim(serverListenIp, ""),
"server_listen_port": strings.Trim(serverListenPort, ""),
"client_id": strings.Trim(clientId, ""),
"client_local_host": strings.Trim(clientLocalHost, ""),
"client_local_port": strings.Trim(clientLocalPort, ""),
"mode": mode,
"user_id": userId,
"protocol": "1",
"cluster_id": clusterId,
}
res, err := business.NewBase().PostRequest("tunnel_create_uri", data, nil)
if(err != nil) {
this.jsonError(err.Error())
}
fmt.Println(res)
this.jsonSuccess("添加隧道成功", nil, "/tunnel/list")
}
//启动隧道
func (this *TunnelController) Open() {
tunnelId := this.GetString("tunnel_id", "0");
if(tunnelId == "0") {
this.jsonError("tunnel_id error")
}
data := map[string]string{
"tunnel_id": tunnelId,
}
_, err := business.NewBase().GetRequest("tunnel_open_uri", data)
if(err != nil) {
this.jsonError(err.Error())
}
this.jsonSuccess("开启隧道成功", nil, "/tunnel/list")
}
//关闭隧道
func (this *TunnelController) Close() {
tunnelId := this.GetString("tunnel_id", "0");
if(tunnelId == "0") {
this.jsonError("tunnel_id error")
}
data := map[string]string{
"tunnel_id": tunnelId,
}
_, err := business.NewBase().GetRequest("tunnel_close_uri", data)
if(err != nil) {
this.jsonError(err.Error())
}
this.jsonSuccess("关闭隧道成功")
}
//删除隧道
func (this *TunnelController) Delete() {
tunnelId := this.GetString("tunnel_id", "0");
if(tunnelId == "0") {
this.jsonError("tunnel_id error")
}
data := map[string]string{
"tunnel_id": tunnelId,
}
_, err := business.NewBase().GetRequest("tunnel_delete_uri", data)
if(err != nil) {
this.jsonError(err.Error())
}
this.jsonSuccess("删除隧道成功")
}
//重启隧道
func (this *TunnelController) Refresh() {
tunnelId := this.GetString("tunnel_id", "0");
if(tunnelId == "0") {
this.jsonError("tunnel_id error")
}
data := map[string]string{
"tunnel_id": tunnelId,
}
//关闭
_, err := business.NewBase().GetRequest("tunnel_close_uri", data)
if(err != nil) {
this.jsonError(err.Error())
}
//打开
_, err = business.NewBase().GetRequest("tunnel_open_uri", data)
if(err != nil) {
this.jsonError(err.Error())
}
this.jsonSuccess("重启隧道成功", nil, "/tunnel/list")
}
+123
View File
@@ -0,0 +1,123 @@
package controllers
import (
utils "anytunnel/at-common"
"encoding/json"
"strings"
"github.com/astaxie/beego"
)
type UserController struct {
BaseUserController
}
//user center
func (this *UserController) Index() {
this.Data["userValue"] = this.loginUser
this.viewLayoutTitle("用户中心", "user/index", "user")
}
//user welcome
func (this *UserController) Welcome() {
this.viewLayoutTitle("默认首页", "user/welcome", "page")
}
//user profile
func (this *UserController) Profile() {
this.Data["userValue"] = this.loginUser
this.viewLayoutTitle("默认首页", "user/profile", "page")
}
//user password
func (this *UserController) Password() {
this.Data["userValue"] = this.loginUser
this.viewLayoutTitle("默认首页", "user/password", "page")
}
//user save
func (this *UserController) Save() {
userId := strings.Trim(this.GetString("user_id"), "")
nickname := strings.Trim(this.GetString("nickname"), "")
if userId == "" {
this.jsonError("user_id error")
}
updateUri := beego.AppConfig.String("user_update_uri")
if updateUri == "" {
this.jsonError("修改资料失败")
}
data := map[string]string{
"nickname": nickname,
"user_id": userId,
}
body, code, err := utils.HttpPost(updateUri, data, nil)
if code != 200 || err != nil {
this.jsonError("修改资料失败")
}
var results map[string]interface{}
json.Unmarshal(body, &results)
if results["code"].(float64) != 1 {
this.jsonError(results["message"].(string))
}
//更新session和cookie
this.refreshSession(userId)
this.jsonSuccess("修改资料成功", nil, "/user/profile")
}
//user repass
func (this *UserController) Repass() {
userId := strings.Trim(this.GetString("user_id"), "")
oldPass := strings.Trim(this.GetString("old_pass"), "")
newPass := strings.Trim(this.GetString("new_pass"), "")
confirmPass := strings.Trim(this.GetString("confirm_pass"), "")
if userId == "" {
this.jsonError("user_id error")
}
if oldPass == "" {
this.jsonError("旧密码错误")
}
if utils.NewEncrypt().Md5Encode(oldPass) != this.loginUser["password"].(string) {
this.jsonError("旧密码错误")
}
if newPass == "" {
this.jsonError("新密码不能为空")
}
if confirmPass == "" {
this.jsonError("确认密码不能为空")
}
if confirmPass != newPass {
this.jsonError("确认密码和新密码不一致")
}
updateUri := beego.AppConfig.String("user_update_uri")
if updateUri == "" {
this.jsonError("修改密码失败")
}
data := map[string]string{
"password": newPass,
"user_id": userId,
}
body, code, err := utils.HttpPost(updateUri, data, nil)
if code != 200 || err != nil {
this.jsonError("修改密码失败")
}
var results map[string]interface{}
json.Unmarshal(body, &results)
if results["code"].(float64) != 1 {
this.jsonError(results["message"].(string))
}
//更新session和cookie
this.refreshSession(userId)
this.jsonSuccess("修改密码成功", nil, "/user/password")
}