2021SC@SDUSC
本周我们来看cmswing文件夹下的home.js,该文件主要负责用户前台网站页面的基础加载。
1.
async parseDocumentList(list, model_id) {
model_id = model_id || 1;
const attrList = await this.model('cmswing/attribute').get_model_attribute(model_id, false, 'id,name,type,extra');
// attrList=attrList[model_id];
// this.end(attrList);
// console.log(attrList);
if (think.isArray(list)) {
list.forEach((data, k) => {
// console.log(data);
for (const key in data) {
// console.log(key)
if (!think.isEmpty(attrList[key])) {
const extra = attrList[key]['extra'];
const type = attrList[key]['type'];
// console.log(extra);
if (type == 'select' || type == 'checkbox' || type == 'radio' || type == 'bool') {
// 枚举/多选/单选/布尔型
const options = parse_config_attr(extra);
const oparr = Object.keys(options);
if (options && in_array(data[key], oparr)) {
data[key] = options[data[key]];
}
} else if (type == 'date') { // 日期型
data[key] = dateformat('Y-m-d', data[key]);
} else if (type == 'datetime') { // 时间型
data[key] = dateformat('Y-m-d H:i', data[key]);
} else if (type === 'pics') {
data[key] = `<span class="thumb-sm"><img alt="..." src="${data[key]}" class="img-responsive img-thumbnail"></span>`;
}
}
}
data.model_id = model_id;
list[k] = data;
});
// console.log(222)
return list;
}
}
parseDocumentList负责处理文档列表的显示,list传递列表数据,model_id传递模型id。与admin类中的方法同理:通过if语句与变量type来区分列表中的数据是布尔型、日期型、时间型或其他。 2. 判断是否登陆是由weblogin方法嵌套islogin方法来实现的。
async islogin() {
const user = await this.session('webuser');
const res = think.isEmpty(user) ? false : user.uid;
return res;
}
islogin是用来登陆时设置一个变量user先判断前台是否登录,然后返回变量res来告诉weblogin方法是否已登录。
async weblogin() {
const islogin = await this.islogin();
if (!islogin) {
// 判断浏览客户端
if (this.isMobile) {
// 手机端直接跳转到登录页面
return this.redirect('/center/public/login');
} else {
return this.redirect('/cmswing/error/login');
}
}
}
若islogin返回的变量res的布尔值为否,则代表用户未登录,if语句可以执行,然后进入内部的if语句,来判断用户是在用pc端浏览还是在用手机端浏览,this.isMobile方法返回true,则代表用户是在用手机端浏览,直接跳转到登陆界面,否则返回错误界面。
3.
async category(id, field) {
const error = this.controller('cmswing/error');
id = id || 0;
field = field || '';
if (think.isEmpty(id)) {
// this.fail('没有指定数据分类!');
// this.http.error = new Error('没有指定数据分类!');
return error.noAction('没有指定数据分类!');
}
const cate = await this.model('cmswing/category').info(id, field);
// console.log(cate);
if (cate && cate.status == 1) {
switch (cate.display) {
case 0:
// this.fail('该分类禁止显示')
return error.noAction('该分类禁止显示!');
break;
// TODO:更多分类显示状态判断
default:
return cate;
}
} else {
return error.noAction('分类不存在或者被禁用!');
// this.fail("分类不存在或者被禁用!");
// this.http.error = new Error('分类不存在或者被禁用!');
// return think.statusAction(702, this.http);
}
}
用户登陆后前台要通过category方法来获取分类信息,先通过if语句判断用户的列表里是否有指定的数据分类,若没有则返回“没有指定数据分类”然后再通过变量cate判断分类状态,看分类是否被禁用或禁止显示。
4.
async shopCart() {
let cartdata = null;
if (this.is_login) {
const loadata = await this.session('cart_goods_item');
if (think.isEmpty(loadata)) {
cartdata = await this.model('cart').where({uid: this.user.uid}).select();
} else {
// loadata = JSON.parse(loadata);
for (const val of loadata) {
val.uid = this.user.uid;
// 验证原有的数据是否已经存在
const res = await this.model('cart').where({product_id: val.product_id, type: val.type, uid: this.user.uid}).select();
// console.log(res);
if (!think.isEmpty(res)) {
val.qty = Number(val.qty) + Number(res[0].qty);
val.id = res[0].id;
await this.model('cart').update(val);
} else {
await this.model('cart').add(val);
}
}
await this.session('cart_goods_item', null);
cartdata = await this.model('cart').where({uid: this.user.uid}).select();
}
} else {
cartdata = await this.session('cart_goods_item');
// if(cartdata){
// cartdata = JSON.parse(cartdata);
// }
}
// console.log(cartdata);
return cartdata;
}
该分类用来显示用户的购物车,用户在使用购物车添加商品或进行结算时,首先要判断用户是否登录,只有用户登陆了才能进行接下来的操作,当if语句判断用户购物车非空之后,先通过for循环以此判断购物车中原有的数据是否存在,将model方法的值传递给变量res,再通过isEmpty方法判断是否为空,将得到的布尔值返回给if语句,若返回的是flase,则说明不为空,可以进行购物车的相关操作。