Authored by yyq

del use less

'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const UserData = require('./user-data');
const helpers = global.yoho.helpers;
const path = require('path');
const _ = require('lodash');
const moment = require('moment');
// 使用 product中的分页逻辑
const pagerPath = path.join(global.appRoot, '/apps/product/models/public-handler.js');
const pager = require(pagerPath).handlePagerData;
const UNUSED = 'notuse';
const USED = 'use';
const INVALID = 'overtime';
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
getCouponsList(uid, type, page, limit) {
let userDataModel = new UserData(this.ctx);
return co(function*() {
let couponsInfo = yield userDataModel.getCouponsList(uid, type, page, limit);
let result = [],
coupons = _.get(couponsInfo, 'data.couponList');
if (!coupons) {
return {
list: result
};
}
if (coupons) {
coupons.forEach(function(item, i) {
result[i] = {};
result[i].id = item.couponId;
result[i].code = item.couponCode;
result[i].couponType = Number(item.couponType);
// 格式化有效日期 "couponValidity": "2016.03.15-2016.03.31"
let dates = item.couponValidity.split('-'),
extra = ['秒杀', '限定', '境外', '预售'],
limits = _.get(item, 'shopPriceLimits', '');
result[i].beginTime = dates[0];
result[i].endTime = dates[1];
if (!item.couponImageUrl) {
result[i].img = '//static.yohobuy.com/images/v2/activity/default_coupon.jpg';
} else {
result[i].img = item.couponImageUrl;
}
result[i].value = item.couponValue;
result[i].validity = item.couponValidity;
result[i].useRemark = item.couponDetailInfomation;
if (limits.indexOf('1') >= 0) {
extra.push('3折以下');
}
if (limits.indexOf('2') >= 0) {
extra.push('限量');
}
result[i].notes = item.notes;
result[i].proListUrl = helpers.urlFormat('', {cpc_id: item.couponId, coupon_code: item.couponCode,
phrase: encodeURIComponent('以下商品可使用 【' + item.couponDetailInfomation + '】优惠券')}, 'list');
result[i].rule = item.rule4ShortName || '';
result[i].overState = item.overState || '';
if (type === USED) {
result[i].orderNum = _.get(item, 'orderCode', '');
result[i].orderDetailUrl = helpers.urlFormat('/home/orders/detail',
{orderCode: item.orderCode || ''});
result[i].orderSum = _.get(item, 'orderPrice', 0).toFixed(2);
result[i].payment = _.get(item, 'actuallyPaid', 0).toFixed(2);
let date = item.usedTime ? moment(item.usedTime).format('YYYY-MM-DD H:m') : 0;
if (date) {
result[i].useTime = date;
} else {
result[i].useTime = '';
}
} else {
result[i].status = item.isValidity ? '可使用' : '已过期';
}
if (item.couponType) {
result[i].type = item.couponType;
}
});
}
let pageNum = pager(couponsInfo.data.total, {
page: page,
limit: limit,
type: type
});
return {
list: result,
pager: Object.assign({
count: couponsInfo.data.total || 0,
curPage: page,
totalPages: couponsInfo.data.totalPageNum
}, pageNum)
};
})();
}
couponsData(uid, params) {
let type = params.type || UNUSED;
let page = params.page || 1;
let limit = params.limit || 10;
let _this = this;
return co(function*() {
let coupons = yield _this.getCouponsList(uid, type, page, limit);
let data = {};
if (type === UNUSED) {
data.unUseCoupons = !coupons.list.length ? {empty: '您没有优惠券'} : coupons.list;
data.unUse = true;
} else if (type === USED) {
data.usedCoupons = !coupons.list.length ? {empty: '您没有优惠券'} : coupons.list;
data.used = true;
} else if (type === INVALID) {
data.noValidCoupons = !coupons.list.length ? {empty: '您没有优惠券'} : coupons.list;
data.noValid = true;
}
data.tabs = [
{
active: type === UNUSED ? true : false,
url: helpers.urlFormat('/home/coupons', {type: UNUSED}),
name: '未使用优惠券'
},
{
active: type === USED ? true : false,
url: helpers.urlFormat('/home/coupons', {type: USED}),
name: '已使用优惠券'
},
{
active: type === INVALID ? true : false,
url: helpers.urlFormat('/home/coupons', {type: INVALID}),
name: '已失效优惠券'
}
];
data.pager = coupons.pager;
return data;
})();
}
};
... ... @@ -39,20 +39,4 @@ module.exports = class extends global.yoho.BaseModel {
return this.get({data: Object.assign(options, params)});
}
getCouponsList(uid, type, page, limit) {
page = page || 1;
type = type || 'notuse';
limit = limit || 10;
let options = {
method: 'app.coupons.lists',
uid: uid,
type: type,
page: page,
limit: limit
};
return this.get({data: options});
}
};
... ...
<div class="me-coupons-page me-page yoho-page clearfix">
{{> path}}
{{> navigation}}
<div class="me-main">
<div class="coupons{{#if unUse}} un-use-coupons{{/if}}{{#if used}} used-coupons{{/if}}{{#if noValid}} no-valid-coupons{{/if}} block">
<h2 class="title">优惠券</h2>
{{> tabs}}
<div class="coupons-wrap">
<p class="coupons-header clearfix">
<span class="coupons-name">优惠券名称</span>
<span class="coupons-info">详细说明</span>
{{#if unUse}}
<span class="coupons-opt">操作</span>
{{/if}}
{{#if used}}
<span class="coupons-opt">订单号</span>
{{/if}}
{{#if noValid}}
<span class="coupons-opt">状态</span>
{{/if}}
</p>
{{# unUseCoupons}}
{{#unless empty}}
<div class="coupon" data-couponId="{{id}}">
<div class="coupons-card{{#isEqual couponType 5}} coupons-freight{{/isEqual}}">
<div class="coupons-card-header">
<p class="left-title">{{useRemark}}</p>
</div>
<div class="coupons-card-content">
<div class="coupons-card-row">
<div class="coupons-sht">
<p class="coupons-price">{{value}}</p>
{{#if rule}}
<p class="coupons-full" title="{{rule}}">{{rule}}</p>
{{/if}}
</div>
<div class="coupons-dtl">
<p>{{beginTime}} - {{endTime}}</p>
{{#if overState}}
<p class="red">({{overState}})</p>
{{/if}}
</div>
</div>
</div>
</div>
<div class="coupons-info">
<div class="coupons-text">
{{notes}}
</div>
</div>
<div class="coupons-opt">
<a class="btn use-btn" id="coupon-use" href="{{proListUrl}}" target="_black">立即使用</a>
</div>
</div>
{{/unless}}
{{/ unUseCoupons}}
{{# usedCoupons}}
{{#unless empty}}
<div class="coupon" data-couponId="{{id}}">
<div class="coupons-card coupons-use{{#isEqual couponType 5}} coupons-freight{{/isEqual}}">
<div class="coupons-card-header">
<p class="left-title">{{useRemark}}</p>
</div>
<div class="coupons-card-content">
<div class="coupons-card-row">
<div class="coupons-sht">
<p class="coupons-price">{{value}}</p>
{{#if rule}}
<p class="coupons-full" title="{{rule}}">{{rule}}</p>
{{/if}}
</div>
<div class="coupons-dtl">{{beginTime}} - {{endTime}}</div>
</div>
</div>
</div>
<div class="coupons-info">
<div class="coupons-text">
{{#categorys}}<p>限品类:{{.}}</p>{{/categorys}}
{{#brands}}<p>限品牌:{{.}}</p>{{/brands}}
{{#if isNoLimit}}<p>全场通用</p>{{/if}}
<p>特例商品({{extraPro}}等特例商品)暂不支持使用优惠券。</p>
{{#explains}}<p>说明:{{.}}</p>{{/explains}}
</div>
</div>
<div class="coupons-opt">
<a href="{{orderDetailUrl}}">
<p>{{orderNum}}</p>
<p>{{useTime}}</p>
</a>
</div>
</div>
{{/unless}}
{{/ usedCoupons}}
{{# noValidCoupons}}
{{#unless empty}}
<div class="coupon" data-couponId="{{id}}">
<div class="coupons-card coupons-invalid">
<div class="coupons-card-header">
<p class="left-title">{{useRemark}}</p>
</div>
<div class="coupons-card-content">
<div class="coupons-card-row">
<div class="coupons-sht">
<p class="coupons-price">{{value}}</p>
{{#if rule}}
<p class="coupons-full" title="{{rule}}">{{rule}}</p>
{{/if}}
</div>
<div class="coupons-dtl">{{beginTime}} - {{endTime}}</div>
</div>
</div>
</div>
<div class="coupons-info">
<div class="coupons-text">
{{#categorys}}<p>限品类:{{.}}</p>{{/categorys}}
{{#brands}}<p>限品牌:{{.}}</p>{{/brands}}
{{#if isNoLimit}}<p>全场通用</p>{{/if}}
<p>特例商品({{extraPro}}等特例商品)暂不支持使用优惠券。</p>
{{#explains}}<p>说明:{{.}}</p>{{/explains}}
</div>
</div>
<div class="coupons-opt">
<a class="btn overtime-btn">过期</a>
</div>
</div>
{{/unless}}
{{/ noValidCoupons}}
</div>
{{#if unUseCoupons}}
{{#unless unUseCoupons.empty}}
{{> pager}}
{{^}}
{{#with unUseCoupons}}
{{> empty}}
{{/with}}
{{/unless}}
{{/if}}
{{#if usedCoupons}}
{{#unless usedCoupons.empty}}
{{> pager}}
{{^}}
{{#with usedCoupons}}
{{> empty}}
{{/with}}
{{/unless}}
{{/if}}
{{#if noValidCoupons}}
{{#unless noValidCoupons.empty}}
{{> pager}}
{{^}}
{{#with noValidCoupons}}
{{> empty}}
{{/with}}
{{/unless}}
{{/if}}
</div>
</div>
</div>