示例#1
0
/* 被动接收支付宝同步跳转的页面 */
func AlipayReturn(contro *beego.Controller) (int, string, string, string) {
	//列举全部传参
	type Params struct {
		Body        string `form:"body" json:"body"`                 //描述
		BuyerEmail  string `form:"buyer_email" json:"buyer_email"`   //买家账号
		BuyerId     string `form:"buyer_id" json:"buyer_id"`         //买家ID
		Exterface   string `form:"exterface" json:"exterface"`       //接口名称
		IsSuccess   string `form:"is_success" json:"is_success"`     //交易是否成功
		NotifyId    string `form:"notify_id" json:"notify_id"`       //通知校验id
		NotifyTime  string `form:"notify_time" json:"notify_time"`   //校验时间
		NotifyType  string `form:"notify_type" json:"notify_type"`   //校验类型
		OutTradeNo  string `form:"out_trade_no" json:"out_trade_no"` //在网站中唯一id
		PaymentType uint8  `form:"payment_type" json:"payment_type"` //支付类型
		SellerEmail string `form:"seller_email" json:"seller_email"` //卖家账号
		SellerId    string `form:"seller_id" json:"seller_id"`       //卖家id
		Subject     string `form:"subject" json:"subject"`           //商品名称
		TotalFee    string `form:"total_fee" json:"total_fee"`       //总价
		TradeNo     string `form:"trade_no" json:"trade_no"`         //支付宝交易号
		TradeStatus string `form:"trade_status" json:"trade_status"` //交易状态 TRADE_FINISHED或TRADE_SUCCESS表示交易成功
		Sign        string `form:"sign" json:"sign"`                 //签名
		SignType    string `form:"sign_type" json:"sign_type"`       //签名类型
	}

	//实例化参数
	param := &Params{}

	//解析表单内容,失败返回错误代码-3
	if err := contro.ParseForm(param); err != nil {
		return -3, "", "", ""
	}
	//如果最基本的网站交易号为空,返回错误代码-1
	if param.OutTradeNo == "" { //不存在交易号
		return -1, "", "", ""
	} else {
		//生成签名
		sign := sign(param)

		//对比签名是否相同
		if sign == param.Sign { //只有相同才说明该订单成功了
			//判断订单是否已完成
			if param.TradeStatus == "TRADE_FINISHED" || param.TradeStatus == "TRADE_SUCCESS" { //交易成功
				return 1, param.OutTradeNo, param.BuyerEmail, param.TradeNo
			} else { //交易未完成,返回错误代码-4
				return -4, "", "", ""
			}
		} else { //签名认证失败,返回错误代码-2
			return -2, "", "", ""
		}
	}

	//位置错误类型-5
	return -5, "", "", ""
}
示例#2
0
/* 后台操作数据表 */
func Restful(this BaseInterface, contro *beego.Controller) {
	ok, data := func() (bool, interface{}) {
		switch contro.GetString("type") {
		case "add": //增
			if err := contro.ParseForm(this); err != nil {
				return false, err.Error()
			}

			//数据验证
			valid := validation.Validation{}
			b, err := valid.Valid(this)
			if err != nil {
				return false, "验证参数解析失败"
			}

			if !b { //验证失败
				for _, err := range valid.Errors {
					return false, err.Key + " " + err.Message
				}
			}

			// 额外解析数组参数
			if _ok, _data := this.BaseFormStrings(contro); !_ok {
				return false, _data
			}

			if err := this.BaseInsert(); err != nil {
				return false, err.Error()
			}

			return true, "" // 插入成功
		case "delete": //删
			if _ok, _data := this.BaseSetId(contro.GetString("_id")); !_ok {
				return false, _data
			}

			err := this.BaseDelete()

			if err != nil {
				return false, err
			}

			return true, nil
		case "update": //改
			//根据id查询对象
			if _ok, _data := this.BaseFind(contro.GetString("_id")); !_ok {
				return false, _data
			}

			//解析请求参数->对象
			if err := contro.ParseForm(this); err != nil {
				return false, err.Error()
			}

			// 额外解析数组参数
			if _ok, _data := this.BaseFormStrings(contro); !_ok {
				return false, _data
			}

			//验证
			valid := validation.Validation{}
			b, err := valid.Valid(this)
			if err != nil {
				return false, "验证参数解析失败"
			}

			if !b { //验证失败
				for _, err := range valid.Errors {
					return false, err.Key + " " + err.Message
				}
			}

			if err := this.BaseUpdate(); err != nil {
				return false, err.Error()
			}
			return true, nil
		case "get": //查
			from, _ := contro.GetInt("from")
			number, _ := contro.GetInt("number")

			if number > 100 {
				return false, "最多查询100条"
			}

			if contro.GetString("like") != "" && contro.GetString("likeKey") != "" { // 模糊搜索
				var result []BaseInterface
				var count int

				if contro.GetString("likeMethod") == "like" {
					result = this.BaseSelectLike(from, number, contro.GetString("sort"), contro.GetString("likeKey"), contro.GetString("like"))
					count = this.BaseLikeCount(contro.GetString("likeKey"), contro.GetString("like"))
				} else if contro.GetString("likeMethod") == "accuracy" {
					result = this.BaseSelectAccuracy(from, number, contro.GetString("sort"), contro.GetString("likeKey"), contro.GetString("like"))
					count = this.BaseAccuracyCount(contro.GetString("likeKey"), contro.GetString("like"))
				}

				return true, map[string]interface{}{
					"lists": result,
					"count": count,
				}
			} else { // 普通查询
				if contro.GetString("filter") != "" {
					filter, _ := contro.GetInt("filter")
					count := this.BaseFilterCount(contro.GetString("filterKey"), filter)
					result := this.BaseFilterSelect(from, number, contro.GetString("sort"), contro.GetString("filterKey"), filter)

					return true, map[string]interface{}{
						"lists": result,
						"count": count,
					}
				} else {
					count := this.BaseCount()
					result := this.BaseSelect(from, number, contro.GetString("sort"))

					return true, map[string]interface{}{
						"lists": result,
						"count": count,
					}
				}
			}
		}
		return true, nil
	}()

	contro.Data["json"] = map[string]interface{}{
		"ok":   ok,
		"data": data,
	}
	contro.ServeJson()
}