Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
taxus authored and taxus committed Jan 9, 2018
1 parent 8f40884 commit a158804
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 30 deletions.
26 changes: 16 additions & 10 deletions app_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package main

import (

// pad "fresh/proto/ad"
// pcategory "fresh/proto/category"
// pgoods "fresh/proto/goods"
// pcart "fresh/proto/cart"
// pregion "fresh/proto/region"

"log"
"testing"

"github.com/kataras/iris/httptest"
)

// pad "fresh/proto/ad"
// pcategory "fresh/proto/category"
// pgoods "fresh/proto/goods"
// pcart "fresh/proto/cart"
// pregion "fresh/proto/region"

// porder "fresh/proto/order"

var app = newApp()
Expand Down Expand Up @@ -266,11 +265,18 @@ var app = newApp()
// log.Printf("[TestUpdateUserAddress] %v", data)
// }

func TestDeleteUserAddress(t *testing.T) {
// func TestDeleteUserAddress(t *testing.T) {
// e := httptest.New(t, app)
//
// body := e.DELETE("/address/8/delete").WithHeader("token", "00a1c0366b96e5c3bfff8bd1d85fa557").Expect().Status(httptest.StatusOK).Body()
// log.Printf("[TestDeleteUserAddress] %v", body)
// }

func TestUserAddressSetDefault(t *testing.T) {
e := httptest.New(t, app)

body := e.DELETE("/address/8/delete").WithHeader("token", "00a1c0366b96e5c3bfff8bd1d85fa557").Expect().Status(httptest.StatusOK).Body()
log.Printf("[TestDeleteUserAddress] %v", body)
body := e.PATCH("/address/814/setDefault").WithHeader("token", "00a1c0366b96e5c3bfff8bd1d85fa557").Expect().Status(httptest.StatusOK).Body()
log.Printf("[TestSetUserAddressDefault] %v", body)
}

// func TestCreateOrder(t *testing.T) {
Expand Down
6 changes: 1 addition & 5 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ development:
username: root
password: root
database: fresh
<<<<<<< HEAD
maxConns: 1000
=======
maxConns: 1
>>>>>>> 263be310110abd1ea512ca4d6593e4da196a3767
maxConns: 10
idleConns: 1
encoding: utf8

Expand Down
20 changes: 20 additions & 0 deletions controllers/address_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ func (c *addressController) Update(ctx iris.Context) {
c.WriteProto(ctx, result)
}

// SetDefault 设置默认收货地址
func (c *addressController) SetDefault(ctx iris.Context) {
u, err := model.LoadUserBy(ctx.GetHeader("token"))
if err != nil {
ctx.Text(err.Error())
return
}

id, _ := ctx.Params().GetInt("id")
ua, err := model.LoadAddress(uint32(id), u.ID)
if err != nil {
ctx.Text(err.Error())
}

if err := ua.SetDefault(); err != nil {
ctx.Text(err.Error())
}
ctx.WriteString("0")
}

func (c *addressController) createOrUpdate(ctx iris.Context) (*model.UserAddress, error) {
params := &paddress.CreateParam{}
if err := c.ReadProto(ctx, params); err != nil {
Expand Down
27 changes: 27 additions & 0 deletions model/user_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ func LoadDefaultAddress(userID uint32) (*UserAddress, error) {
return ua, nil
}

// LoadAddress 最普通获取地址方式
func LoadAddress(id, userID uint32) (*UserAddress, error) {
query := "SELECT * FROM tp_user_address WHERE address_id = ? AND user_id = ?"
ua := &UserAddress{}
err := DataSource.Session.QueryRow(query, id, userID).Scan(ua.Values()...)
if err != nil {
return nil, fmt.Errorf("[LoadAddress] %v", err)
}
return ua, nil
}

// Create 创建
func (u *UserAddress) Create(userID uint32) error {
u.UserID = userID
Expand Down Expand Up @@ -114,6 +125,22 @@ func (u *UserAddress) Delete() error {
return err
}

// SetDefault 设置默认地址
func (u *UserAddress) SetDefault() error {
return DataSource.TxExec(func(tx *sql.Tx) error {
update := `UPDATE tp_user_address SET is_default = 0 WHERE user_id = ? AND is_default = 1`
_, err := tx.Exec(update, u.UserID)
if err == nil {
update = `UPDATE tp_user_address SET is_default = 1 WHERE address_id = ?`
_, err = tx.Exec(update, u.ID)
}
if err != nil {
err = fmt.Errorf("[UserAddress.SetDefault] %v", err)
}
return err
})
}

func (u *UserAddress) Fields() []interface{} {
return []interface{}{
&u.ID,
Expand Down
7 changes: 7 additions & 0 deletions model/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package model
// log.Printf("%v \n %s", u, token)
// userID = u.ID
// }

//
// func TestLoadUserAddress(t *testing.T) {
// uas, err := LoadUserAddress(userID)
Expand All @@ -28,3 +29,9 @@ package model
// log.Printf("%v", ua)
// }
// }

// func TestSetDefaultAddress(t *testing.T) {
// a, err := LoadAddress(831, userID)
// checkErr(err)
// checkErr(a.SetDefault())
// }
1 change: 1 addition & 0 deletions router/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (r *Route) Init(app *iris.Application) {
address.Post("/create", ctr.AddressController.Create)
address.Put("/{id:int}/update", ctr.AddressController.Update)
address.Delete("/{id:int}/delete", ctr.AddressController.Delete)
address.Patch("/{id:int}/setDefault", ctr.AddressController.SetDefault)

goods := app.Party("/goods")
goods.Get("/new", ctr.GoodsController.NewGoods)
Expand Down
18 changes: 10 additions & 8 deletions web/pages/goods-details/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,12 @@ Page({

addToCart: function(cb){
var self = this;
if (this.data.specs && !this.data.canSubmit) {
if (!this.data.canSubmit){
wx.showModal({
title: '提示',
content: '请选择商品规格!',
showCancel: false
})
}
if (this.data.specs.length > 0 && !this.data.canSubmit) {
wx.showModal({
title: '提示',
content: '请选择商品规格!',
showCancel: false
})
this.bindGuiGeTap();
return;
}
Expand Down Expand Up @@ -327,6 +325,10 @@ Page({
}
});

if (cart.length == 0) {
cart.push({id: self.data.id, num: self.data.buyNumber});
}

return cart;
},

Expand Down
2 changes: 1 addition & 1 deletion web/pages/order-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Page({
var orderId = e.currentTarget.dataset.id;
console.log(orderId);
wx.navigateTo({
url: "/pages/order-details/index?id=" + orderID
url: "/pages/order-details/index?id=" + orderId
})
},

Expand Down
9 changes: 3 additions & 6 deletions web/pages/select-address/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ Page({
selectTap: function (e) {
var id = e.currentTarget.dataset.id;
wx.request({
url: 'https://api.it120.cc/'+ app.globalData.subDomain +'/user/shipping-address/update',
data: {
token:app.globalData.token,
id:id,
isDefault:'true'
},
url: app.globalData.domain + '/address/' + id +'/setDefault',
method: "PATCH",
header: {token: app.globalData.token},
success: (res) =>{
wx.navigateBack({})
}
Expand Down

0 comments on commit a158804

Please sign in to comment.