Exemplo n.º 1
0
// Update User
func (p *PageUser) UpdateUser() {
	if p.Request.Method == "POST" {
		if _, ok := p.POST["ajax"]; ok {
			p.Hide = true
			mgoServer := Middleware.Get("db").(*utils.Mongo)

			username, ok := p.POST["username"]
			m := utils.M{
				"status":  "1",
				"message": "",
			}
			if !ok {
				m["status"] = "0"
				m["message"] = "修改用户时必须指定用户名!"
			}

			colQuerier := utils.M{"name": username}
			cnt, err := mgoServer.C(ColUser).Find(colQuerier).Count()
			if err != nil || cnt == 0 {
				m["status"] = "0"
				m["message"] = "用户不存在!"
			} else {
				email, emailOk := p.POST["email"]
				password, passwordOk := p.POST["password"]
				updateField, change := utils.M{}, utils.M{}

				if emailOk {
					updateField["email"] = email
				}
				if passwordOk {
					password = utils.Strings(password).Md5()
					updateField["password"] = password
				}
				if emailOk || passwordOk {
					updateField["update_time"] = time.Now().Unix()
					change["$set"] = updateField
					err := mgoServer.C(ColUser).Update(colQuerier, change)
					if err != nil {
						m["status"] = "0"
						m["message"] = "用户资料更新失败!"
					} else {
						m["message"] = "用户资料更新成功!"
					}
				} else {
					m["status"] = "0"
					m["message"] = "请输入需要更新的资料!"
				}
			}

			ret, _ := json.Marshal(m)
			p.ResponseWriter.Write(ret)

			return
		}
	}
}
Exemplo n.º 2
0
func (p *PageIndex) Login() {
	if p.Request.Method == "POST" {
		if _, ok := p.POST["ajax"]; ok {
			p.Hide = true
			mgoServer := Middleware.Get("db").(*utils.Mongo)

			m := utils.M{
				"status":  1,
				"message": "",
			}

			p.ResponseWriter.Header().Set("Content-Type", "application/json")
			p.ResponseWriter.Header().Set("Cache-Control", "no-store")

			username := p.POST["username"]
			password := p.POST["password"]
			passwordMd5 := utils.Strings(password).Md5()

			colQuerier := utils.M{"name": username, "status": 1, "delete": 0}
			colSelecter := utils.M{"password": 1}
			col := ModelUser{}
			var jres []byte
			err := mgoServer.C(ColUser).Find(colQuerier).Select(colSelecter).One(&col)
			if err != nil || col.Password == "" {
				m["status"] = -1
				m["message"] = "无此用户"
			} else {
				if passwordMd5 != col.Password {
					m["status"] = 0
					m["message"] = "密码错误"
				} else {
					m["back_url"] = ""
					if _, ok := p.GET["back_url"]; ok {
						m["back_url"] = p.GET["back_url"]
					}

					p.SESSION[p.M["SESSION_UNAME"].(string)] = username
					p.SESSION[p.M["SESSION_UKEY"].(string)] = passwordMd5
				}
			}

			jres, _ = json.Marshal(m)
			p.ResponseWriter.Write(jres)
			return
		}
	}
}
Exemplo n.º 3
0
// Create User
func (p *PageUser) CreateUser() {
	if p.Request.Method == "POST" {
		if _, ok := p.POST["ajax"]; ok {
			mgoServer := Middleware.Get("db").(*utils.Mongo)
			m := utils.M{
				"status":  1,
				"message": "",
			}
			p.Hide = true
			username := p.POST["username"]
			email := p.POST["email"]
			colQuerier := utils.M{"name": username, "delete": 0}
			var col ModelUser
			err := mgoServer.C(ColUser).Find(colQuerier).One(&col)

			if col.Name != "" || err == nil {
				m["status"] = 0
				m["message"] = "该用户名已存在"
			} else {
				password := utils.Strings(p.POST["password"]).Md5()
				tnow := time.Now()
				mgoServer.C(ColUser).Insert(&ModelUser{
					Email:       email,
					Name:        username,
					Password:    password,
					Status:      1,
					Create_time: tnow.Unix(),
					Update_time: tnow.Unix(),
				})
			}

			ret, _ := json.Marshal(m)
			p.ResponseWriter.Write(ret)

			return
		}
	}
}
Exemplo n.º 4
0
func (ar *ActiveRecord) getTableName(rowsSlicePtr interface{}) string {
	return utils.Strings(utils.Struct{rowsSlicePtr}.GetTypeName()).SnakeCasedName()
}