乐趣区

egg(104)–egg之去结算页面收货地址的增加 修改 显示(上)

router
router.post(‘/user/addAddress’, initMiddleware, userauthMiddleware, controller.default.address.addAddress);
model
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;

const d = new Date();

const AddressSchema = new Schema({
uid: {type: Schema.Types.ObjectId},
name: {type: String},
phone: {type: Number},
address: {type: String},
zipcode: {type: String},
default_address: {type: Number, default: 1},
add_time: {
type: Number,
default: d.getTime(),
},
});

return mongoose.model(‘Address’, AddressSchema, ‘address’);
};
controller
增加地址
app/controller/default/address.js
async addAddress() {
const uid = this.ctx.service.cookies.get(‘userinfo’)._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;
const addressCount = await this.ctx.model.Address.find({uid}).count();
if (addressCount > 20) {
this.ctx.body = {
success: false,
result: ‘ 增加收货地址失败 收货地址数量超过限制 ’,
};
} else {
await this.ctx.model.Address.updateMany({uid}, {default_address: 0});
const addressModel = new this.ctx.model.Address({uid, name, phone, address, zipcode});
await addressModel.save();
const addressList = await this.ctx.model.Address.find({uid}).sort({default_address: -1});
this.ctx.body = {
success: true,
result: addressList,
};
}
}
显示地址
app/controller/default/buy.js
async checkout() {

// 获取购物车选中的商品

const orderList = [];
let allPrice = 0;
const cartList = this.service.cookies.get(‘cartList’);

if (cartList && cartList.length > 0) {

for (let i = 0; i < cartList.length; i++) {

if (cartList[i].checked) {
orderList.push(cartList[i]);

allPrice += cartList[i].price * cartList[i].num;
}

}

// 获取当前用户的所有收货地址

const uid = this.ctx.service.cookies.get(‘userinfo’)._id;
const addressList = await this.ctx.model.Address.find({uid}).sort({default_address: -1});

await this.ctx.render(‘default/checkout.html’, {
orderList,
allPrice,
addressList,
});

} else {
// 恶意操作
this.ctx.redirect(‘/cart’);
}

}
view
app/view/default/checkout.html
<div id=”addressList”>

<%for(var i=0;i<addressList.length;i++){%>

<div class=”address-item J_addressItem <%if(addressList[i].default_address){%>selected <%}%>” data-id=”<%=addressList[i]._id%>” data-name=”<%=addressList[i].name%>” data-phone=”<%=addressList[i].phone%>” data-address=”<%=addressList[i].address%>”>

<dl>
<dt> <em class=”uname”><%=addressList[i].name%></em> </dt>
<dd class=”utel”>
<%=addressList[i].phone%>
</dd>
<dd class=”uaddress”>
<%=addressList[i].address%>
</dd>
</dl>

<div class=”actions” data-id=”<%=addressList[i]._id%>”>
<a href=”javascript:void(0);” class=”modify addressModify”> 修改 </a>
</div>
</div>
<%}%>

</div>
<script>
$(function() {

$(‘#addAddressButton’).click(function() {

var name = $(‘#add_name’).val();
var phone = $(‘#add_phone’).val();
var address = $(‘#add_address’).val();
var zipcode = $(‘#add_zipcode’).val();

// 判断格式是否正确 - 自己完善
if (name == ” || phone == “” || address == “”) {

alert(‘ 格式不正确 ’)
return false;
}
//csrf
$.post(‘/user/addAddress’, {
name: name,
phone: phone,
address: address,
zipcode: zipcode
}, function(response) {

console.log(response);

var addressList = response.result;

var str = ”;

if (response.success == true) {

for (var i = 0; i < addressList.length; i++) {

if (addressList[i].default_address) {

str += ‘<div class=”address-item J_addressItem selected” data-id=”‘ + addressList[i]._id + ‘” data-name=”‘ + addressList[i].name + ‘” data-phone=”‘ + addressList[i].phone + ‘” data-address=”‘ + addressList[i].address + ‘” > ‘;
str += ‘<dl>’;
str += ‘<dt> <em class=”uname”>’ + addressList[i].name + ‘</em> </dt>’;
str += ‘<dd class=”utel”>’ + addressList[i].phone + ‘</dd>’;
str += ‘<dd class=”uaddress”>’ + addressList[i].address + ‘</dd>’;
str += ‘</dl>’;

str += ‘<div class=”actions”>’;
str += ‘<a href=”javascript:void(0);” data-id=”‘ + addressList[i]._id + ‘” class=”modify addressModify”> 修改 </a>’;
str += ‘</div>’;
str += ‘</div>’;

} else {

str += ‘<div class=”address-item J_addressItem” data-id=”‘ + addressList[i]._id + ‘” data-name=”‘ + addressList[i].name + ‘” data-phone=”‘ + addressList[i].phone + ‘” data-address=”‘ + addressList[i].address + ‘” > ‘;
str += ‘<dl>’;
str += ‘<dt> <em class=”uname”>’ + addressList[i].name + ‘</em> </dt>’;
str += ‘<dd class=”utel”>’ + addressList[i].phone + ‘</dd>’;
str += ‘<dd class=”uaddress”>’ + addressList[i].address + ‘</dd>’;
str += ‘</dl>’;

str += ‘<div class=”actions”>’;
str += ‘<a href=”javascript:void(0);” data-id=”‘ + addressList[i]._id + ‘” class=”modify addressModify”> 修改 </a>’;
str += ‘</div>’;
str += ‘</div>’;

}

}

// 增加

$(“#addressList”).html(str);

} else {
alert(‘ 增加失败 ’);
}

$(‘#addAddress’).modal(‘hide’);

})

})

})
</script>
效果

退出移动版