Authored by yyq

message ajax

... ... @@ -34,7 +34,49 @@ const detail = (req, res, next) => {
}).catch(next);
};
const delMsg = (req, res, next) => {
let uid = req.user.uid;
let id = req.query.id;
if (!req.xhr || !uid) {
return next();
}
message.delMessage(uid, id).then(result => {
res.json(result);
}).catch(next);
};
const readMsg = (req, res, next) => {
let uid = req.user.uid;
let id = req.query.id;
if (!req.xhr || !uid) {
return next();
}
message.delMessage(uid, id).then(result => {
res.json(result);
}).catch(next);
};
const pickCoupon = (req, res, next) => {
let uid = req.user.uid;
let id = req.query.id;
if (!req.xhr || !uid) {
return next();
}
message.pickBirthCoupon(uid, id).then(result => {
res.json(result);
}).catch(next);
};
module.exports = {
index,
detail
detail,
delMsg,
readMsg,
pickCoupon
};
... ...
... ... @@ -21,7 +21,30 @@ const getMessageAsync = (uid, page, size) => {
page: page || 1,
size: size || 10
}, {code: 200});
};
const delMessageAsync = (uid, id) => {
return api.get('', {
method: 'app.inbox.delmessage',
uid: uid,
id: id
});
};
const readMessageAsync = (uid, id) => {
return api.get('', {
method: 'web.inbox.setread',
uid: uid,
ids: id
});
};
const getBirthCouponAsync = (uid, couponId) => {
return api.get('', {
method: 'app.promotion.getCoupon',
uid: uid,
couponId: couponId
});
};
/**
... ... @@ -92,7 +115,6 @@ const getMessageList = (uid, page, limit) => {
return co(process)();
};
/**
* 获取消息内容
* @function getMessageDetail
... ... @@ -175,7 +197,63 @@ const getMessageDetail = (uid, mid, page, limit) => {
return co(process)();
};
/**
* 删除用户消息
* @function getMessageDetail
* @param { Number } uid 用户uid
* @param { Number } mid 消息id
* @return { Object } 消息列表数据
*/
const delMessage = (uid, mid) => {
let process = function*() {
let resData = {code: 400, message: '删除失败'};
let result = yield delMessageAsync(uid, mid);
if (result.code === 200) {
Object.assign(resData, {
code: 200,
message: '删除成功'
});
}
return resData;
};
return co(process)();
};
/**
* 删除用户消息
* @function getMessageDetail
* @param { Number } uid 用户uid
* @param { Number } mid 消息id
* @return { Object } 消息列表数据
*/
const readMessage = (uid, mid) => {
let process = function*() {
let resData = {code: 400, message: '标记失败'};
let result = yield readMessageAsync(uid, mid);
if (result.code === 200) {
resData = result;
}
return resData;
};
return co(process)();
};
const pickBirthCoupon = (uid, id) => {
return getBirthCouponAsync(uid, id);
}
module.exports = {
getMessageList,
getMessageDetail
getMessageDetail,
delMessage,
readMessage,
pickBirthCoupon
};
... ...
... ... @@ -138,6 +138,9 @@ router.post('/complaints/cancel', complaintsController.cancel);
// 我的消息
router.get('/message', messageController.index);
router.get('/message/detail', messageController.detail);
router.get('/message/del', messageController.delMsg);
router.get('/message/read', messageController.readMsg);
router.get('/message/pickCoupon', messageController.pickCoupon);
// 我的退/换货
router.get('/returns', returnsController.index);
... ...
/**
* 我的信息
* @author: yyqing<yanqing.yang@yoho.cn>
* @date: 2016/3/21
*/
var $ = require('yoho-jquery');
var $container = $('#message-main-container'),
$msgCheck = $container.find('.msg-check'),
$readBtn = $container.find('.read-choose-btn'),
$checked;
var $pickBtn = $('.pick-coupon-btn');
var operationId = [];
var pickBusy = false;
require('./message-content');
function hasNew() {
var newMsg = false;
$checked = $container.find('.msg-check:checked');
operationId = [];
$checked.each(function() {
var $par = $(this).parent().parent();
operationId.push($(this).val());
if ($par.hasClass('new-msg')) {
newMsg = true;
}
});
if (newMsg) {
$readBtn.addClass('has-new');
} else {
$readBtn.removeClass('has-new');
}
}
function msgOperation(type, data) {
var url;
switch (type) {
case 'del':
url = '/home/message/del';
break;
case 'read':
url = '/home/message/read';
break;
}
if (!url) {
return;
}
$.ajax({
type: 'GET',
url: url,
data: data
}).then(function(jsonData) {
if (jsonData.code === 200) {
if (type === 'read') {
$checked.removeAttr('checked');
$checked.parent().parent().removeClass('new-msg');
hasNew();
} else {
window.location.reload()
}
} else {
alert(jsonData.message);
}
});
}
$container.on('change', 'input[type="checkbox"]', function() {
var checked = $(this).attr('checked');
if ($(this).hasClass('choose-all')) {
if (checked === 'checked') {
$msgCheck.attr('checked', true);
} else {
$msgCheck.removeAttr('checked');
}
}
hasNew();
});
$container.on('click', '.del-btn', function() {
if (confirm('您确定要删除这条短消息?')) {
msgOperation('del', $(this).data());
}
});
$container.on('click', '.del-choose-btn', function() {
if (!operationId.length) {
alert('请选中您要删除的消息');
return;
}
if (confirm('确定要删除您选中的消息')) {
msgOperation('del', {
id: operationId.join(',')
});
}
});
$container.on('click', '.read-choose-btn', function() {
if ($(this).hasClass('has-new')) {
msgOperation('read', {
id: operationId.join(',')
});
}
});
$pickBtn.click(function() {
var data = $(this).data();
if (pickBusy) {
return;
}
pickBusy = true;
$.ajax({
type: 'GET',
url: '/home/message/pickCoupon',
data: data
}).then(function(jsonData) {
var msg = jsonData.message;
if (jsonData.code === 200) {
msg = '领取成功';
$(this).removeClass('pick-coupon-btn');
}
pickBusy = false;
alert(msg);
});
});
\ No newline at end of file
... ...