Exemplo n.º 1
0
func (this *MainController) Post() {
	beego.Info("new business start  ...")
	var (
		transData    string
		w4Data       *libtool.ParamData
		req          *gjson.Json
		err          error
		bizApidCode  string
		bizMethod    string
		retMsg       string
		retCode      string
		newTransData string
	)
	//处理业务
	busiData := new(libtool.BusiData)
	busiData.Session = models.Orm.NewSession()
	defer busiData.Session.Close()
	//开启事务
	err = busiData.Session.Begin()
	if !libtool.CheckError(err, "开启事务失败") {
		beego.Error("开启事务失败: ", err)
		this.retData.Set("retCode", libtool.ERR_TRANS_BEGIN)
		goto BUSI_END
	}
	busiData.Response = gjson.New()
	//初始化返回数据节点
	this.retData = gjson.New()
	//获取交易数据
	transData = this.GetString("transData")
	if "" == transData {
		beego.Info("retCode: ", libtool.ERR_NO_DATA)
		this.retData.Set("retCode", libtool.ERR_NO_DATA)
		goto BUSI_END
	}
	beego.Info("transData: ", transData)

	//临时修改 将空格转换成加号
	newTransData = strings.Replace(transData, " ", "+", -1)
	w4Data = libtool.ParamTransData(newTransData)

	//检查数据报文长度
	if !libtool.CheckDataLen(w4Data) {
		beego.Error("报文长度不一致")
		this.retData.Set("retCode", libtool.ERR_DATA_LEN)
		goto BUSI_END
	}
	//数据JSON化
	req, err = gjson.NewJson([]byte(w4Data.TransData))
	if err != nil {
		beego.Error("解析报文错误: ", err)
		this.retData.Set("retCode", libtool.ERR_TRANS_DATA)
		goto BUSI_END
	}
	//设置业务请求数据
	busiData.Request = req
	beego.Info("busiREQ: ", libtool.InnerJsonToString(busiData.Request))
	bizApidCode, _ = busiData.Request.Get("bizApiCode").String()
	beego.Info("bizApiCode: ", bizApidCode)
	bizMethod = beego.AppConfig.String(bizApidCode)
	reflect.ValueOf(busiData).MethodByName(bizMethod).Call([]reflect.Value{})
	retMsg = libtool.InnerJsonToString(busiData.Response)
	busiData.Response, _ = gjson.NewJson([]byte(retMsg))
	//组织部分返回数据
	this.retData = busiData.Response
	//写返回报文头
	err = this.writeHeaderData(req)
	if err != nil {
		beego.Error("组织报文头错误: ", err)
		goto BUSI_END
	}
	retCode, err = busiData.Response.Get("retCode").String()
	if err != nil {
		beego.Error("biz err: 无返回码", err)
		this.retData.Set("retCode", libtool.ERR_NO_RETCODE)
		goto BUSI_END
	}
BUSI_END:
	retCode, _ = this.retData.Get("retCode").String()
	errorCode := new(models.ErrorCode)
	errorCode.ErrorId = retCode
	busiData.Session.Get(errorCode)
	this.retData.Set("retMsg", errorCode.ErrorDes)

	//提交事务&关闭数据库连接
	if retCode != libtool.RET_SUCC {
		busiData.Session.Rollback()
	} else {
		busiData.Session.Commit()
	}
	//数据写回
	this.writeBackData()
	beego.Info("new business end  ...")

	return
}
Exemplo n.º 2
0
//处理数据回写
func (this *MainController) writeBackData() {
	this.Ctx.Output.Header("Content-Type", "text/html;charset=UTF-8")
	beego.Info("返回的数据: ", libtool.InnerJsonToString(this.retData))
	retInfo := libtool.JsonToString(this.retData)
	this.Ctx.Output.Body([]byte(retInfo))
}