Beispiel #1
0
//处理实体的保存页请求
func dealEntitySave(_entity Entity, w http.ResponseWriter, r *http.Request) {

	m := make(map[string]interface{})

	employee := GetCurrentEmployee(r)

	if employee.UserId == "" {
		Log.Warn("用户未登陆")
		m["success"] = false
		m["code"] = 100
		m["msg"] = "用户未登陆"
		commonlib.OutputJson(w, m, " ")
		return
	}

	Log.Debug("路径:", r.URL.Path, "访问实体", _entity.Id, "的保存ajax请求")

	err := r.ParseForm()

	//异步请求绑定的组件Id
	componentId := r.FormValue("componentId")
	formpanel := runtimeComponentContain[componentId].(formPanel)

	if err != nil {
		m["success"] = false
		m["code"] = 100
		m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
		Log.Warn(err.Error())
		commonlib.OutputJson(w, m, " ")
		return
	}

	idString := r.FormValue(_entity.Pk)

	_model := new(Model)
	_model.Props = []*Prop{}

	imgElements := []element{}

	for _, formElement := range formpanel.Elements {
		_prop := new(Prop)

		if formElement.Type == "image" { //图片类型需要做多表处理
			imgElements = append(imgElements, formElement)
		} else if formElement.Type == "currentTime" { //当前时间,一般用于createTime
			_prop.Name = formElement.Field
			if formElement.Char14 == "true" {
				_prop.Value = time.Now().Format("20060102150405")
			} else {
				_prop.Value = time.Now().Format("2006-01-02 15:04:05")
			}
			_model.Props = append(_model.Props, _prop)
		} else {
			_prop.Name = formElement.Field
			_prop.Value = r.FormValue(formElement.Field)
			_model.Props = append(_model.Props, _prop)
		}
	}

	if idString != "" { //修改的情况
		id, err := strconv.Atoi(idString)

		if err != nil {
			m["success"] = false
			m["code"] = 100
			m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
			Log.Warn(err.Error())
			commonlib.OutputJson(w, m, " ")
			return
		}

		_model.Id = fmt.Sprint(id)

		err = modify(_entity, _model, formpanel.Elements)

		if err != nil {
			m["success"] = false
			m["code"] = 100
			m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
			commonlib.OutputJson(w, m, " ")
			return
		}

		m["success"] = true
		m["code"] = 200
		commonlib.OutputJson(w, m, " ")
		return
	} else {
		id, err := insert(_entity, _model, formpanel.Elements)

		for _, imgElment := range imgElements {

			filePath := r.FormValue(imgElment.Field)
			tmpFileName := ""

			if filePath != "" {
				imgEntity := getEntity(imgElment.ImageEntity)
				imageModel := new(Model)
				imageModel.Props = []*Prop{}

				fileNameProp := new(Prop)
				fileNameProp.Name = "filename"
				fileNameProp.Value = commonlib.SubstrByStEd(filePath, strings.LastIndex(filePath, "/")+1, strings.LastIndex(filePath, "."))
				tmpFileName = commonlib.Substr(filePath, strings.LastIndex(filePath, "/")+1, len(filePath))

				suffixProp := new(Prop)
				suffixProp.Name = "suffix"
				suffixProp.Value = commonlib.Substr(filePath, strings.LastIndex(filePath, ".")+1, len(filePath))

				refProp := new(Prop)
				refProp.Name = imgElment.Field
				refProp.Value = fmt.Sprint(id)

				imageModel.Props = append(imageModel.Props, fileNameProp)
				imageModel.Props = append(imageModel.Props, suffixProp)
				imageModel.Props = append(imageModel.Props, refProp)

				_, err := insert(imgEntity, imageModel, []element{
					element{
						Field: "filename",
					},
					element{
						Field: "suffix",
					},
					element{
						Field: imgElment.Field,
					},
				})

				if err != nil {
					m["success"] = false
					m["code"] = 100
					m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
					Log.Warn(err.Error())
					commonlib.OutputJson(w, m, " ")
					return
				}

				tmpFile, err := os.OpenFile("../tmp/"+tmpFileName, os.O_RDWR, 0777)

				if err != nil {
					m["success"] = false
					m["code"] = 100
					m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
					Log.Warn(err.Error())
					commonlib.OutputJson(w, m, " ")
					return
				}

				_, err = os.Stat(imgElment.ImagePath)

				if err != nil && os.IsNotExist(err) {
					Log.Info(imgElment.ImagePath, "文件夹不存在,创建")
					os.MkdirAll(imgElment.ImagePath, 0777)
				}

				disFile, err := os.Create(imgElment.ImagePath + "/" + tmpFileName)

				if err != nil {
					m["success"] = false
					m["code"] = 100
					m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
					Log.Warn(err.Error())
					commonlib.OutputJson(w, m, " ")
					return
				}

				io.Copy(disFile, tmpFile)

				os.Remove("../tmp/" + tmpFileName)
			}
		}

		if err != nil {
			m["success"] = false
			m["code"] = 100
			m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
			commonlib.OutputJson(w, m, " ")
			return
		}

		m["success"] = true
		m["code"] = 200
		commonlib.OutputJson(w, m, " ")
		return
	}

}
Beispiel #2
0
//处理实体的保存页请求
func dealEntitySave(entity Entity, w http.ResponseWriter, r *http.Request) {

	Log.Debug("路径:", r.URL.Path, "访问实体", entity.Id, "的保存ajax请求")

	err := r.ParseForm()

	//异步请求绑定的组件Id
	componentId := r.FormValue("componentId")
	formpanel := runtimeComponentContain[componentId].(FormPanel)

	m := make(map[string]interface{})

	if err != nil {
		m["success"] = false
		m["code"] = 100
		m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
		Log.Warn(err.Error())
		commonlib.OutputJson(w, m, " ")
		return
	}

	idString := r.FormValue(entity.Pk)

	model := new(Model)
	model.Props = []*Prop{}

	for _, element := range formpanel.Elements {
		prop := new(Prop)

		if element.Type == "image" { //图片类型需要做多表处理

			filePath := r.FormValue(element.Field)
			tmpFileName := ""

			if filePath != "" {
				imgEntity := GetEntity(element.ImageEntity)
				imageModel := new(Model)
				imageModel.Props = []*Prop{}

				fileNameProp := new(Prop)
				fileNameProp.Name = "filename"
				fileNameProp.Value = commonlib.SubstrByStEd(filePath, strings.LastIndex(filePath, "/")+1, strings.LastIndex(filePath, "."))
				tmpFileName = commonlib.Substr(filePath, strings.LastIndex(filePath, "/")+1, len(filePath))

				suffixProp := new(Prop)
				suffixProp.Name = "suffix"
				suffixProp.Value = commonlib.Substr(filePath, strings.LastIndex(filePath, ".")+1, len(filePath))

				imageModel.Props = append(imageModel.Props, fileNameProp)
				imageModel.Props = append(imageModel.Props, suffixProp)

				imgId, err := Insert(imgEntity, imageModel, []Element{
					Element{
						Field: "filename",
					},
					Element{
						Field: "suffix",
					},
				})

				if err != nil {
					m["success"] = false
					m["code"] = 100
					m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
					Log.Warn(err.Error())
					commonlib.OutputJson(w, m, " ")
					return
				}

				prop.Name = element.Field
				prop.Value = fmt.Sprint(imgId)
				model.Props = append(model.Props, prop)

				tmpFile, err := os.OpenFile("../tmp/"+tmpFileName, os.O_RDWR, 0777)

				if err != nil {
					m["success"] = false
					m["code"] = 100
					m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
					Log.Warn(err.Error())
					commonlib.OutputJson(w, m, " ")
					return
				}

				_, err = os.Stat(element.ImagePath)

				if err != nil && os.IsNotExist(err) {
					Log.Info(element.ImagePath, "文件夹不存在,创建")
					os.Mkdir(element.ImagePath, 0777)
				}

				disFile, err := os.Create(element.ImagePath + "/" + tmpFileName)

				if err != nil {
					m["success"] = false
					m["code"] = 100
					m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
					Log.Warn(err.Error())
					commonlib.OutputJson(w, m, " ")
					return
				}

				io.Copy(disFile, tmpFile)

				os.Remove("../tmp/" + tmpFileName)
			} else {
				prop.Name = element.Field
				prop.Value = fmt.Sprint(0)
				model.Props = append(model.Props, prop)
			}

		} else {
			prop.Name = element.Field
			prop.Value = r.FormValue(element.Field)
			model.Props = append(model.Props, prop)
		}
	}

	if idString != "" { //修改的情况
		id, err := strconv.Atoi(idString)

		if err != nil {
			m["success"] = false
			m["code"] = 100
			m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
			Log.Warn(err.Error())
			commonlib.OutputJson(w, m, " ")
			return
		}

		model.Id = id

		err = Modify(entity, model, formpanel.Elements)

		if err != nil {
			m["success"] = false
			m["code"] = 100
			m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
			commonlib.OutputJson(w, m, " ")
			return
		}

		m["success"] = true
		m["code"] = 200
		commonlib.OutputJson(w, m, " ")
		return
	} else {
		_, err = Insert(entity, model, formpanel.Elements)

		if err != nil {
			m["success"] = false
			m["code"] = 100
			m["msg"] = "出现错误,请联系IT部门,错误信息:" + err.Error()
			commonlib.OutputJson(w, m, " ")
			return
		}

		m["success"] = true
		m["code"] = 200
		commonlib.OutputJson(w, m, " ")
		return
	}

}