Skip to content

Commit 0d25bd5

Browse files
committedNov 15, 2015
done all task
1 parent 4284370 commit 0d25bd5

13 files changed

+223
-40
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* AdviceCustomerListController
3+
*
4+
* @description :: Server-side logic for managing Advicecustomerlists
5+
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
6+
*/
7+
8+
let AdviceCustomerListController = {
9+
create : async(req,res) => {
10+
let user = await UserService.getLoginUser(req);
11+
let AdviceCustomerCheck = await db.AdviceCustomerList.findAll({
12+
where:{
13+
status: false
14+
},
15+
include: [
16+
{
17+
model: db.User,
18+
where: {
19+
id : user.id
20+
}
21+
}, {
22+
model: db.Product,
23+
where: {
24+
id: req.query.productid
25+
}
26+
}
27+
]
28+
});
29+
console.log(AdviceCustomerCheck);
30+
if(AdviceCustomerCheck.length > 0){
31+
res.ok("您已登記過");
32+
}
33+
else{
34+
let AdviceCustomerList = await db.AdviceCustomerList.create();
35+
AdviceCustomerList.setProduct(req.query.productid);
36+
AdviceCustomerList.setUser(user.id);
37+
res.ok("您已登記通知");
38+
}
39+
}
40+
};
41+
42+
module.exports = AdviceCustomerListController;

‎api/controllers/PromotionController.js

+27-14
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ let PromotionController = {
131131
try {
132132
let query = req.query;
133133
let queryObj = {};
134-
134+
queryObj.$or = [];
135135
console.log('==== discountAddItem query ====', query);
136136

137137
if(!query.hasOwnProperty("productIds"))
@@ -143,8 +143,22 @@ let PromotionController = {
143143

144144
let brands = await db.Brand.findAll();
145145

146-
if(query.keyword)
147-
queryObj.name = { 'like': '%'+query.keyword+'%'};
146+
if(query.keyword){
147+
let eachKeywords = query.keyword.split('+');
148+
for (var i=0; i<eachKeywords.length; i++) {
149+
let keyword = eachKeywords[i];
150+
151+
queryObj.$or.push({ name: { $like: '%'+keyword+'%' } });
152+
queryObj.$or.push({ description: { $like: '%'+keyword+'%' } });
153+
queryObj.$or.push({ country: { $like: '%'+keyword+'%' } });
154+
queryObj.$or.push({ spec: { $like: '%'+keyword+'%' } });
155+
156+
queryObj.$or.push(['`ProductGm`.`name` like ?', '%'+keyword+'%']);
157+
queryObj.$or.push(['`ProductGm`.`explain` like ?', '%'+keyword+'%']);
158+
queryObj.$or.push(['`ProductGm`.`usage` like ?', '%'+keyword+'%']);
159+
queryObj.$or.push(['`ProductGm`.`notice` like ?', '%'+keyword+'%']);
160+
}
161+
}
148162
else
149163
query.keyword = ''
150164

@@ -165,14 +179,16 @@ let PromotionController = {
165179

166180
console.log('------------ page');
167181
console.log(offset);
168-
182+
console.log("123hihihi",queryObj);
169183
let products = await db.Product.findAndCountAll({
170-
where: queryObj,
184+
subQuery: false,
171185
include: [{
186+
required: true,
172187
model: db.ProductGm
173188
}],
174-
offset: offset,
175-
limit: limit
189+
where: queryObj,
190+
limit: limit,
191+
order: [['id', 'ASC']]
176192
});
177193

178194
products.rows = await PromotionService.productPriceTransPromotionPrice(new Date(), products.rows);
@@ -212,17 +228,14 @@ let PromotionController = {
212228

213229
console.log('=== isCreatePromotion ===', isCreatePromotion);
214230
if(isCreatePromotion){
215-
let products = await db.Product.findAndCountAll({
231+
let products = await db.Product.findAll({
216232
where: {
217233
id: query.productIds
218234
},
219-
include:[{
220-
model: db.ProductGm,
221-
}],
222235
offset: offset,
223-
limit: limit,
224-
subQuery: false
236+
limit: limit
225237
});
238+
226239
if(products == null)
227240
products = []
228241

@@ -261,7 +274,7 @@ let PromotionController = {
261274
promotion.Products = products;
262275
}
263276
}
264-
277+
promotion.products = await PromotionService.productPriceTransPromotionPrice(promotion.startDate,promotion.Products);
265278
console.log('=== promotion ===', promotion);
266279

267280
let view = "";

‎api/controllers/ShopController.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ let ShopController = {
4747
}]
4848
});
4949

50-
50+
5151
products = await PromotionService.productPriceTransPromotionPrice(new Date(), promotion.Products);
5252

5353
res.view('main/flash', {
@@ -66,10 +66,12 @@ let ShopController = {
6666
query.dateEnd = moment().format();
6767
query.dateFrom = moment().subtract(3, 'months').format();
6868
}
69-
69+
7070
let productsWithCount = await ProductService.productQuery(query, offset, limit);
71+
7172
let products = productsWithCount.rows || [];
7273
// sails.log.info('=== shop products ===',products);
74+
7375
products = await PromotionService.productPriceTransPromotionPrice(new Date(), products);
7476

7577
let brands = await db.Brand.findAll({order: 'weight ASC',});
@@ -179,10 +181,6 @@ let ShopController = {
179181

180182
productGm = productGm.dataValues;
181183

182-
// console.log('------ productGM -----')
183-
// console.log(productGm)
184-
// console.log('------ productGM end -----')
185-
186184
product = (await PromotionService.productPriceTransPromotionPrice(new Date(), [product]))[0];
187185

188186
product = product.dataValues;
@@ -232,7 +230,7 @@ let ShopController = {
232230
}
233231

234232
else {
235-
// product.price = '$ ' + UtilService.numberFormat(product.price);
233+
236234
let resData = {
237235
dpt: dpt,
238236
dptSub: dptSub,

‎api/db/AdviceCustomerList.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
module.exports = function(sequelize, DataTypes) {
3+
4+
var AdviceCustomerList = sequelize.define('AdviceCustomerList', {
5+
//通知狀況
6+
status: {
7+
type:DataTypes.BOOLEAN,
8+
allowNull: false,
9+
defaultValue: false
10+
}
11+
}, {
12+
classMethods: {
13+
associate: (models) => {
14+
AdviceCustomerList.belongsTo(models.Product);
15+
AdviceCustomerList.belongsTo(models.User);
16+
}
17+
}
18+
});
19+
20+
return AdviceCustomerList;
21+
};

‎api/db/Message.coffee

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ module.exports = (sequelize, DataTypes) ->
1515
'newPassword',
1616
'verification',
1717
'shopCode',
18-
'contactUs'
18+
'contactUs',
19+
'adviceCustomer'
1920
)
2021
from: DataTypes.STRING
2122
to: DataTypes.STRING
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module.exports = {
2+
sendEmail: async (req, res) => {
3+
let AdviceCustomerList = await db.AdviceCustomerList.findAll({
4+
where:{
5+
status: false
6+
},
7+
include: [
8+
{
9+
model: db.User
10+
}, {
11+
model: db.Product,
12+
where: {
13+
id: req
14+
}
15+
}
16+
]
17+
});
18+
19+
for(var i = 0; i < AdviceCustomerList.length;i++){
20+
let link = await UrlHelper.resolve(`shop/products/${AdviceCustomerList[i].Product.ProductGmId}/${AdviceCustomerList[i].Product.id}`,true);
21+
let adviceCustomer = AdviceCustomerList[i];
22+
23+
let messageConfig = await CustomMailerService.adviceCustomerMail({adviceCustomer, link});
24+
let message = await db.Message.create(messageConfig);
25+
await CustomMailerService.sendMail(message);
26+
AdviceCustomerList[i].status = true;
27+
AdviceCustomerList[i].save();
28+
}
29+
}
30+
}

‎api/services/CustomMailerService.js

+22
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,29 @@ module.exports = {
240240
throw e;
241241
}
242242
},
243+
adviceCustomerMail: async({adviceCustomer,link}) => {
244+
try {
245+
var checkForgotTpl = sails.config.mail.templete.adviseCustomer;
246+
var email = adviceCustomer.User.email;
247+
var mailSendConfig = {...checkForgotTpl, from: sails.config.mail.config.from, to: email};
248+
mailSendConfig.subject = sprintf(mailSendConfig.subject, {
249+
fullName: adviceCustomer.User.username
250+
});
251+
252+
mailSendConfig.html = sprintf(mailSendConfig.html, {
253+
fullName: adviceCustomer.User.username,
254+
link: link,
255+
storeName: sails.config.store.name,
256+
serviceMail: sails.config.store.serviceMail,
257+
});
243258

259+
mailSendConfig.type = 'adviceCustomer';
260+
261+
return mailSendConfig;
262+
} catch (e) {
263+
throw e;
264+
}
265+
},
244266
newPasswordMail: async({user,passport}) =>{
245267
try {
246268
var newPasswordTpl = sails.config.mail.templete.newPassword;

‎api/services/ProductService.js

+3
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ module.exports = {
163163
product.spec = updateProduct.spec;
164164
product.color = good.color;
165165
product.productNumber = good.productNumber;
166+
if(product.stockQuantity == 0 && good.stockQuantity > 0){
167+
await AdviceCustomerListService.sendEmail(product.id);
168+
}
166169
product.stockQuantity = good.stockQuantity;
167170
product.description = good.description;
168171
product.isPublish = (good.isPublish == "false") ? false : true;

‎api/services/PromotionService.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,10 @@ module.exports = {
200200
productPriceTransPromotionPrice: async(date, products) => {
201201
try {
202202
// find all promotions within a specific date
203-
203+
console.log("date:=====",date);
204204
// check each prduct
205205
if(!products.length) return products;
206-
206+
207207
let productIds = products.map((product) => product.id)
208208

209209
let findPromotions = await db.Promotion.findAll({

‎assets/javascripts/common/discountCountdown.js

+37
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,39 @@
11
(function ($) {
2+
var isMobile = ( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) ? true : false;
3+
$(".notice-to-buy").on("click", function(e){
4+
var productid = $(".notice-to-buy").data("productid");
5+
$.ajax({
6+
url : '/user/loginStatus',
7+
type: "get",
8+
data : null,
9+
success:function(data, textStatus, jqXHR)
10+
{
11+
console.log('=== data ==>',data.loginStatus);
12+
if(data.loginStatus){
13+
$.ajax({
14+
url : '/user/AdviceCustomerListController',
15+
type: "get",
16+
data : {
17+
productid : productid
18+
},
19+
success:function(data, textStatus, jqXHR)
20+
{
21+
alert(data);
22+
}
23+
});
24+
}else{
25+
if (isMobile) {
26+
alert('請先登入帳號密碼')
27+
return location.href='/user/login';
28+
}
29+
30+
$('#modal-login').modal('show')
31+
}
32+
}
33+
});
34+
});
35+
36+
237

338
var interval = 1000;
439
var runTimer = function (differ, target) {
@@ -35,4 +70,6 @@
3570
runTimer(duration, self);
3671
});
3772

73+
74+
3875
}(jQuery));

‎config/mail.coffee

+23
Original file line numberDiff line numberDiff line change
@@ -299,5 +299,28 @@ module.exports.mail = {
299299
</body></html>"""
300300
},
301301

302+
adviseCustomer:{
303+
sendBy: 'email',
304+
subject: '%(fullName)s 貨到通知信',
305+
html: """<html><body>
306+
<br /><p> 親愛的 %(fullName)s 您好!!</p>
307+
<br />
308+
<br />您於i+DEAL創而有意平台登記缺貨通知的商品已到貨,請參考下方是您已期待許久的商品 !:
309+
<br />
310+
<br /><%(link)s
311+
<br />趕緊把心怡已久的商品帶回家吧 !
312+
<br />
313+
<br />此為系統信件,請勿直接回覆此信件
314+
<br />
315+
<br />---
316+
<br />
317+
<br />%(storeName)s | 好物慢慢選
318+
<br />
319+
<br />客服信箱:%(serviceMail)s
320+
<br />
321+
<br />上班時間:週一至週五,10.00AM - 5.00PM,比照國定休假日
322+
</body></html>"""
323+
}
324+
302325
}
303326
}

‎config/routes.coffee

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ module.exports.routes = {
7979
'get /user/cart/removeAdditionalPurchases' : 'UserController.removeAdditionalPurchases'
8080
'get /user/loginStatus' : 'UserController.loginStatus'
8181
'get /user/cart-step-2' : 'ShopController.cartStep2'
82+
'get /user/AdviceCustomerListController' : 'AdviceCustomerListController.create'
8283

8384
'get /admin/department' : 'DptController.list'
8485
'post /admin/department/update': 'DptController.update'

‎views/main/shopProduct.jade

+8-16
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,6 @@ block body
1313
shop-product-carousel
1414
==================================================
1515
#carousel-shop-product.carousel(data-ride='carousel')
16-
//- ol.carousel-indicators.carousel-indicators-v
17-
//- li.active(data-target='#carousel-shop-product', data-slide-to='0')
18-
//- li(data-target='#carousel-shop-product', data-slide-to='1')
19-
//- // /carousel-indicators
20-
//- .carousel-inner.carousel-inner-v(role='listbox')
21-
//- .item.active
22-
//- img.img-full(src='images/demo/JC1121-set-My-Mug-blue-2.jpg', alt='First slide image', href='#carousel-shop-product', data-slide='next')
23-
//- // /item
24-
//- .item
25-
//- img.img-full(src='images/demo/JC1121-set-My-Mug-blue-22.jpg', alt='Second slide image', href='#carousel-shop-product', data-slide='next')
26-
//- // /item
2716
- if (coverPhotos && coverPhotos.length > 0)
2817
ol.carousel-indicators.carousel-indicators-v.ac.active
2918
each photo, index in coverPhotos
@@ -126,12 +115,15 @@ block body
126115
// /input-group
127116
// /col-sm-4
128117
.col-sm-8
129-
- var disabled = (product.stockQuantity == 0) ? 'disabled' : '';
130118
- var productName = (product.name) ? "(" + product.name + ")" : "";
131-
a.btn.btn-black.border-radius-circle.btn-block.btn-lg.add-to-cart(class='#{disabled}',
132-
data-productGmId=productGm.id,, data-productId=product.id, data-photos=product.photos,
133-
data-brand=productGm.name, data-name="#{product.ProductGm.name}#{productName}" , data-brandname=_.find(brands, {id: product.ProductGm.BrandId}).name,
134-
data-price=product.price) 加入購物車
119+
- if(product.stockQuantity != 0)
120+
a.btn.btn-black.border-radius-circle.btn-block.btn-lg.add-to-cart(
121+
data-productGmId=productGm.id,, data-productId=product.id, data-photos=product.photos,
122+
data-brand=productGm.name, data-name="#{product.ProductGm.name}#{productName}" , data-brandname=_.find(brands, {id: product.ProductGm.BrandId}).name,
123+
data-price=product.price) 加入購物車
124+
- else
125+
a.btn.btn-gray.border-radius-circle.btn-block.btn-lg.notice-to-buy(data-productId=product.id
126+
) 缺貨中 - 我要貨到通知
135127
// /col-sm-8
136128
// /row
137129

0 commit comments

Comments
 (0)
Please sign in to comment.