Example #1
0
// 修改邮费模板
//  NOTE: 需要指定 template.Id 字段
func (c *Client) MerchantExpressUpdate(template *express.DeliveryTemplate) error {
	if template == nil {
		return errors.New("template == nil")
	}

	token, err := c.Token()
	if err != nil {
		return err
	}
	_url := merchantExpressUpdateURL(token)

	var request = struct {
		TemplateId       int64                     `json:"template_id"`
		DeliveryTemplate *express.DeliveryTemplate `json:"delivery_template"`
	}{
		TemplateId:       template.Id,
		DeliveryTemplate: template,
	}

	template.Id = 0 // 请求的时候不携带这个参数

	var result Error
	if err = c.postJSON(_url, request, &result); err != nil {
		return err
	}

	if result.ErrCode != 0 {
		return &result
	}

	return nil
}
Example #2
0
// 增加邮费模板
//  NOTE: 无需指定 Id 字段
func (c *Client) MerchantExpressAddDeliveryTemplate(template *express.DeliveryTemplate) (templateId int64, err error) {
	if template == nil {
		err = errors.New("template == nil")
		return
	}

	template.Id = 0

	var request = struct {
		DeliveryTemplate *express.DeliveryTemplate `json:"delivery_template"`
	}{
		DeliveryTemplate: template,
	}

	var result struct {
		Error
		TemplateId int64 `json:"template_id"`
	}

	hasRetry := false
RETRY:
	token, err := c.Token()
	if err != nil {
		return
	}
	_url := merchantExpressAddURL(token)
	if err = c.postJSON(_url, request, &result); err != nil {
		return
	}

	switch result.ErrCode {
	case errCodeOK:
		templateId = result.TemplateId
		return

	case errCodeTimeout:
		if !hasRetry {
			hasRetry = true
			timeoutRetryWait()
			goto RETRY
		}
		fallthrough

	default:
		err = &result.Error
		return
	}
}
Example #3
0
// 修改邮费模板
//  NOTE: 需要指定 template.Id 字段
func (c *Client) MerchantExpressUpdateDeliveryTemplate(template *express.DeliveryTemplate) (err error) {
	if template == nil {
		return errors.New("template == nil")
	}

	var request = struct {
		TemplateId       int64                     `json:"template_id"`
		DeliveryTemplate *express.DeliveryTemplate `json:"delivery_template"`
	}{
		TemplateId:       template.Id,
		DeliveryTemplate: template,
	}

	template.Id = 0 // 请求的时候不携带这个参数

	var result Error

	hasRetry := false
RETRY:
	token, err := c.Token()
	if err != nil {
		return
	}
	_url := merchantExpressUpdateURL(token)
	if err = c.postJSON(_url, request, &result); err != nil {
		return
	}

	switch result.ErrCode {
	case errCodeOK:
		return

	case errCodeTimeout:
		if !hasRetry {
			hasRetry = true
			timeoutRetryWait()
			goto RETRY
		}
		fallthrough

	default:
		err = &result
		return
	}
}
Example #4
0
// 增加邮费模板
func (c *Client) MerchantExpressAdd(template *express.DeliveryTemplate) (templateId int64, err error) {
	if template == nil {
		err = errors.New("template == nil")
		return
	}

	template.Id = 0

	token, err := c.Token()
	if err != nil {
		return
	}
	_url := merchantExpressAddURL(token)

	var request = struct {
		DeliveryTemplate *express.DeliveryTemplate `json:"delivery_template"`
	}{
		DeliveryTemplate: template,
	}

	var result struct {
		Error
		TemplateId int64 `json:"template_id"`
	}
	if err = c.postJSON(_url, request, &result); err != nil {
		return
	}

	if result.ErrCode != 0 {
		err = &result.Error
		return
	}

	templateId = result.TemplateId
	return
}