|
|
@@ -0,0 +1,177 @@
|
|
|
+package pick_address_controller
|
|
|
+
|
|
|
+import (
|
|
|
+ "fohow.com/apps/models/user_model"
|
|
|
+ "strconv"
|
|
|
+
|
|
|
+ "fohow.com/apps"
|
|
|
+ "github.com/astaxie/beego/context"
|
|
|
+
|
|
|
+ "fohow.com/apps/models/address_model"
|
|
|
+)
|
|
|
+
|
|
|
+var (
|
|
|
+ //不需要校验登录的Action
|
|
|
+ exceptCheckUserLoginAction = []string{"List", "DefaultPickAddress", "CreatePickAddress", "UpdatePickAddress", "DeletePickAddress", "SetDefault"}
|
|
|
+ exceptCheckWxUserLoginAction = []string{}
|
|
|
+)
|
|
|
+
|
|
|
+type PickAddressController struct {
|
|
|
+ apps.BaseController
|
|
|
+}
|
|
|
+
|
|
|
+func (self *PickAddressController) Init(ctx *context.Context, controllerName, actionName string, app interface{}) {
|
|
|
+ self.BaseController.Init(ctx, controllerName, actionName, app)
|
|
|
+ self.ExceptCheckUserLoginAction = exceptCheckUserLoginAction
|
|
|
+ self.ExceptCheckWxUserLoginAction = exceptCheckWxUserLoginAction
|
|
|
+}
|
|
|
+
|
|
|
+//获取用户地址列表
|
|
|
+func (self *PickAddressController) List() {
|
|
|
+ sort := self.GetString("sort")
|
|
|
+ if sort == "" {
|
|
|
+ sort = "created_at"
|
|
|
+ }
|
|
|
+ //uId := self.GetCurrentUserId()
|
|
|
+ wxUId := self.GetCurrentWxUserId()
|
|
|
+ pic_addresses := address_model.GetUserPickAddressList(wxUId, sort)
|
|
|
+ self.Data["json"] = pic_addresses
|
|
|
+ self.ServeJSON()
|
|
|
+}
|
|
|
+
|
|
|
+//取默认地址
|
|
|
+func (self *PickAddressController) DefaultPickAddress() {
|
|
|
+ //uId := self.GetCurrentUserId()
|
|
|
+ wxUId := self.GetCurrentWxUserId()
|
|
|
+ pic_address := address_model.GetUserDefaultPickAddress(wxUId)
|
|
|
+ self.Data["json"] = pic_address
|
|
|
+ self.ServeJSON()
|
|
|
+}
|
|
|
+
|
|
|
+func (self *PickAddressController) CreatePickAddress() {
|
|
|
+ contact := self.GetString("contact")
|
|
|
+ tel := self.GetString("tel")
|
|
|
+ pic_address := self.GetString("pic_address")
|
|
|
+ setDefault, _ := self.GetInt64("set_default")
|
|
|
+
|
|
|
+ if len(tel) != 11 {
|
|
|
+ self.ReturnError(404, apps.PhoneInvalid, "", nil)
|
|
|
+ }
|
|
|
+ if pic_address == "" || contact == "" || tel == "" {
|
|
|
+ self.ReturnError(404, apps.ParamsRequired, "", nil)
|
|
|
+ }
|
|
|
+ uId := self.GetCurrentUserId()
|
|
|
+ wxUId := self.GetCurrentWxUserId()
|
|
|
+
|
|
|
+ pic_addresses := address_model.GetPickAddressesByWxUId(wxUId)
|
|
|
+ /* if len(pic_addresses) >= 5 {
|
|
|
+ self.ReturnError(404, apps.UserPickAddressFull, "", nil)
|
|
|
+ }*/
|
|
|
+
|
|
|
+ //无默认地址的情况下,新增的地址为默认地址
|
|
|
+ hasDefaultAddr := false
|
|
|
+ for _, item := range pic_addresses {
|
|
|
+ if item.State == 1 {
|
|
|
+ hasDefaultAddr = true
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ item := address_model.CreatePickAddress(wxUId, uId, contact, tel, "")
|
|
|
+ if (setDefault == 1 || !hasDefaultAddr) && item != nil {
|
|
|
+ item.SetDefault()
|
|
|
+ item.State = 1
|
|
|
+ }
|
|
|
+ self.Data["json"] = item
|
|
|
+ self.ServeJSON()
|
|
|
+}
|
|
|
+
|
|
|
+func (self *PickAddressController) UpdatePickAddress() {
|
|
|
+ _id := self.Ctx.Input.Param(":id")
|
|
|
+ id, _ := strconv.ParseInt(_id, 10, 64)
|
|
|
+ item := address_model.GetUserPickAddressById(id)
|
|
|
+ //uId := self.GetCurrentUserId()
|
|
|
+ //if item.UserId != uId {
|
|
|
+ // self.ReturnError(404, apps.NoExist, "", nil)
|
|
|
+ //}
|
|
|
+ wxUId := self.GetCurrentWxUserId()
|
|
|
+ if item.WxUserId != wxUId {
|
|
|
+ self.ReturnError(404, apps.NoExist, "", nil)
|
|
|
+ }
|
|
|
+ contact := self.GetString("contact")
|
|
|
+ tel := self.GetString("tel")
|
|
|
+ pic_address := self.GetString("pic_address")
|
|
|
+ setDefault, _ := self.GetInt64("set_default")
|
|
|
+
|
|
|
+ if len(tel) != 11 {
|
|
|
+ self.ReturnError(404, apps.ParamsError, "", nil)
|
|
|
+ }
|
|
|
+ if pic_address == "" || contact == "" || tel == "" {
|
|
|
+ self.ReturnError(404, apps.ParamsRequired, "", nil)
|
|
|
+ }
|
|
|
+ item = item.Update(contact, tel, "")
|
|
|
+
|
|
|
+ if setDefault == 1 {
|
|
|
+ item.SetDefault()
|
|
|
+ item.State = 1
|
|
|
+ }
|
|
|
+
|
|
|
+ self.Data["json"] = item
|
|
|
+ self.ServeJSON()
|
|
|
+}
|
|
|
+
|
|
|
+func (self *PickAddressController) DeletePickAddress() {
|
|
|
+ _id := self.Ctx.Input.Param(":id")
|
|
|
+ id, _ := strconv.ParseInt(_id, 10, 64)
|
|
|
+ item := address_model.GetUserPickAddressById(id)
|
|
|
+ //uId := self.GetCurrentUserId()
|
|
|
+ //if item.UserId != uId {
|
|
|
+ // self.ReturnError(403, apps.PickAddressNotMatch, "", nil)
|
|
|
+ //}
|
|
|
+ wxUId := self.GetCurrentWxUserId()
|
|
|
+ if item.WxUserId != wxUId {
|
|
|
+ self.ReturnError(404, apps.NoExist, "", nil)
|
|
|
+ }
|
|
|
+ //如果删除的是默认地址,设置一个默认地址
|
|
|
+ if item.State == 1 {
|
|
|
+ allPickAddress := address_model.GetUserPickAddressList(wxUId, "id")
|
|
|
+ for _, pic_address := range allPickAddress {
|
|
|
+ if pic_address.State == 0 {
|
|
|
+ pic_address.State = 1
|
|
|
+ pic_address.Save()
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ go item.Delete()
|
|
|
+ self.ServeJSON()
|
|
|
+}
|
|
|
+
|
|
|
+func (self *PickAddressController) SetDefault() {
|
|
|
+ _id := self.Ctx.Input.Param(":id")
|
|
|
+ id, _ := strconv.ParseInt(_id, 10, 64)
|
|
|
+ //uId := self.GetCurrentUserId()
|
|
|
+ wxUId := self.GetCurrentWxUserId()
|
|
|
+ pic_address := address_model.GetPickAddressByWxUIdAndId(wxUId, id)
|
|
|
+
|
|
|
+ if pic_address != nil {
|
|
|
+ pic_address.SetDefault()
|
|
|
+ pic_address.State = 1
|
|
|
+ } else {
|
|
|
+ self.ReturnError(404, apps.NoExist, "", nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ self.Data["json"] = pic_address
|
|
|
+ self.ServeJSON()
|
|
|
+}
|
|
|
+
|
|
|
+//获取用户地址列表
|
|
|
+func (self *PickAddressController) PickDepartList() {
|
|
|
+ sort := self.GetString("sort")
|
|
|
+ if sort == "" {
|
|
|
+ sort = "created_at"
|
|
|
+ }
|
|
|
+ pic_departs := user_model.GetPickDepartList(sort)
|
|
|
+ self.Data["json"] = pic_departs
|
|
|
+ self.ServeJSON()
|
|
|
+}
|