修改默认的收货地址
获取用户当前收货地址 id 以及用户 id
更新当前用户的所有收货地址的默认收货地址状态为 0
更新当前收货地址的默认收货地址状态为 1
router
router.get(‘/user/changeDefaultAddress’, initMiddleware, userauthMiddleware, controller.default.address.changeDefaultAddress);
controller
app/controller/default/address.js
async changeDefaultAddress() {
const uid = this.ctx.service.cookies.get(‘userinfo’)._id;
var id = this.ctx.request.query.id;
await this.ctx.model.Address.updateMany({uid: uid}, {default_address: 0});
await this.ctx.model.Address.updateMany({uid: uid, “_id”: id}, {default_address: 1});
this.ctx.body = {
success: true,
msg: ‘ 更新默认收货地址成功 ’
};
}
view
app/view/default/checkout.html
$(“#addressList .J_addressItem”).click(function() {
$(this).addClass(‘selected’).siblings().removeClass(‘selected’);
var id = $(this).attr(‘data-id’); // 收货地址的_id
$.get(‘/user/changeDefaultAddress?id=’ + id, function(response) {
//console.log(response);
})
})
效果
修改收货地址
router
router.get(‘/user/getOneAddressList’, initMiddleware, userauthMiddleware, controller.default.address.getOneAddressList);
router.post(‘/user/editAddress’, initMiddleware, userauthMiddleware, controller.default.address.editAddress);
controller
app/controller/default/address.js
获取一个收货地址
async getOneAddressList() {
const uid = this.ctx.service.cookies.get(‘userinfo’)._id;
var id = this.ctx.request.query.id;
var result = await this.ctx.model.Address.find({uid: uid, “_id”: id});
this.ctx.body = {
success: true,
result: result,
};
}
编辑收货地址
async editAddress() {
const uid = this.ctx.service.cookies.get(‘userinfo’)._id;
const id = this.ctx.request.body.id;
const name = this.ctx.request.body.name;
const phone = this.ctx.request.body.phone;
const address = this.ctx.request.body.address;
const zipcode = this.ctx.request.body.zipcode;
await this.ctx.model.Address.updateOne({“_id”: id, “uid”: uid}, {name, phone, address, zipcode});
const addressList = await this.ctx.model.Address.find({uid}).sort({default_address: -1});
this.ctx.body = {
success: true,
result: addressList,
};
}
效果