From b295dafa80b3ece864e9375d50000fa2a2132a23 Mon Sep 17 00:00:00 2001
From: zzs <zzs0@zzs0.com>
Date: Fri, 2 Sep 2016 14:50:26 +0800
Subject: [PATCH] 消息完成大部分

---
 apps/home/controllers/message.js          |  22 ++++++++++++++++++++++
 apps/home/models/message.js               | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
 apps/home/router.js                       |   2 ++
 apps/home/views/action/message-detail.hbs |   2 +-
 apps/home/views/action/message.hbs        |   6 +++++-
 public/js/home/message.page.js            | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 246 insertions(+), 27 deletions(-)
 create mode 100644 public/js/home/message.page.js

diff --git a/apps/home/controllers/message.js b/apps/home/controllers/message.js
index 4b9243a..6a7d3ca 100644
--- a/apps/home/controllers/message.js
+++ b/apps/home/controllers/message.js
@@ -7,6 +7,7 @@ const _getList = (req, res, next) => {
     model.getList({
         page: req.body.page,
         size: 20,
+        uid: req.user.uid,
     }).then(result => {
         let options = {
             module: 'home',
@@ -28,6 +29,7 @@ const _msgDetail = (req, res, next) => {
         page: req.body.page,
         size: 1000,
         msgid: req.query.id,
+        uid: req.user.uid,
     }).then(result => {
         let options = {
             pageHeader: headerModel.setNav({
@@ -47,4 +49,24 @@ exports.index = (req, res, next) => {
     } else {
         _msgDetail(req, res, next);
     }
+}
+
+exports.ajaxDelMes = (req, res, next) => {
+    model.delMsg({
+        uid: req.user.uid,
+        msgid: req.body.id,
+    }).then(result => {
+        res.send(result);
+        res.end();
+    }).catch(next);
+}
+
+exports.pickCoupon = (req, res, next) => {
+    model.pickCoupon({
+        uid: req.user.uid,
+        couponId: req.body.id,
+    }).then(result => {
+        res.send(result);
+        res.end();
+    }).catch(next);
 }
\ No newline at end of file
diff --git a/apps/home/models/message.js b/apps/home/models/message.js
index ab361e9..73a59a4 100644
--- a/apps/home/models/message.js
+++ b/apps/home/models/message.js
@@ -1,17 +1,25 @@
 // 消息 by acgpiano
 "use strict";
 
+const moment = require('moment');
 const api = global.yoho.API;
 
+//格式化时间
+const _formatTime = (time) => {
+    return moment(time).format('HH:mm:ss');
+}
+
 //获取列表页
 const _list = (result) => {
     let final = [];
     if (result.data && result.data.list) {
+        //PHP排序了,应该不用
         for (let item of result.data.list) {
             final.push({
                 id: item.id,
                 isNotReaded: item.is_read === 'Y' ? false : true,
                 title: item.title,
+                time: `${item.create_date} ${_formatTime(item.create_time*1000)}`,
             });
         }
     }
@@ -19,41 +27,43 @@ const _list = (result) => {
 }
 
 //获取生日信息
-
 const _getBirthCouponById = (uid) => {
-        return api.get('', {
-            method: 'app.promotion.queryBirthCoupon',
-            uid: uid,
-            couponType: 4,
-        });
-    }
-    //获取详情页
-const _detail = (result, id, uid) => {
+    let arr = [];
+    return api.get('', {
+        method: 'app.promotion.queryBirthCoupon',
+        uid: uid,
+        couponType: 4,
+    }).then(result => {
+        if (result && result['data']) {
+            for (let item of result['data']) {
+                arr.push({
+                    'id': item['id'] ? item['id'] : '',
+                    'remark': item['couponName'] ? item['couponName'] : '',
+                    // 'useTime': item['body']['use_time'] ? item['body']['use_time'] : '',
+                    // 'pickTime': item['body']['collar_time'] ? item['body']['collar_time'] : '',
+                    'canPick': true
+                });
+            }
+        }
+        return arr;
+    });
+}
+
+//获取详情页
+const _detail = (result, id, couponData) => {
     let final = {};
     if (result.data && result.data.list) {
         for (let item of result.data.list) {
             if (item.id === Number(id) && item.type !== 'showGetCoin' && item.type !== 'notice') {
                 final.sender = item.from;
                 final.title = item.title;
-                final.time = 11; //时间
+                final.time = `${item.create_date} ${_formatTime(item.create_time*1000)}`; //时间
 
                 //判断消息类型
                 switch (item['type']) {
                     case 'pullCoupon':
                         //领取生日券消息
-                        final['coupons'] = [];
-                        coupondata = _getBirthCouponById(uid); //获取优惠券信息
-                        if (coupondata && coupondata['data']) {
-                            for (let item of coupondata['data']) {
-                                final['coupons'].push({
-                                    'id': item['id'] ? item['id'] : '',
-                                    'remark': item['couponName'] ? item['couponName'] : '',
-                                    'useTime': item['body']['use_time'] ? item['body']['use_time'] : '',
-                                    'pickTime': item['body']['collar_time'] ? item['body']['collar_time'] : '',
-                                    'canPick': true
-                                });
-                            }
-                        }
+                        final['coupons'] = couponData; //获取优惠券信息
                         break;
                     case 'button':
                         //促销活动
@@ -88,21 +98,75 @@ const _detail = (result, id, uid) => {
     return final;
 }
 
+//获取列表或详情
 const getList = (params) => {
     return api.get('', {
         method: 'app.inbox.getlist',
         page: params.page || 1,
         size: params.size || 10,
-        uid: params.uid || 15184104,
+        uid: params.uid || 123,
     }).then(result => {
         if (params.msgid) {
-            return _detail(result, params.msgid, params.uid);
+            // 绕的一比
+            return _getBirthCouponById(params.uid || 20000382).then(couponData => {
+                return _detail(result, params.msgid, couponData);
+            });
         } else {
             return _list(result);
         }
     });
 }
 
+//删除消息
+const delMsg = (params) => {
+    return api.get('', {
+        method: 'app.inbox.delmessage',
+        uid: params.uid || 20000382,
+        id: params.msgid,
+    }).then(result => {
+        if(result && result.code === 200){
+            return {
+                code: 200
+            }
+        }
+        else {
+            return {
+                code: 400,
+                message: '出错啦~',
+            }
+        }
+    });
+}
+
+//领取优惠券
+const pickCoupon = (params) => {
+    return api.get('', {
+        method: 'app.promotion.getCoupon',
+        uid: params.uid || 20000382,
+        couponId: params.couponId,
+    }).then(result => {
+        if(result && result.code === 200) {
+            return {
+                code: 200
+            }
+        }
+        else if(result.code === 401){
+            return {
+                code: 401,
+                message: '优惠券已经领取',
+            }
+        }
+        else{
+            return {
+                code: 400,
+                message: '出错啦~',
+            }
+        }
+    });
+}
+
 module.exports = {
     getList,
+    delMsg,
+    pickCoupon,
 }
\ No newline at end of file
diff --git a/apps/home/router.js b/apps/home/router.js
index 2acf523..b990bb1 100644
--- a/apps/home/router.js
+++ b/apps/home/router.js
@@ -79,4 +79,6 @@ router.get('/helpDetail', help.helpDetail);
 
 // 消息
 router.use('/message', message.index);
+router.post('/ajaxDelMes', message.ajaxDelMes);
+router.post('/pickCoupon', message.pickCoupon);
 module.exports = router;
diff --git a/apps/home/views/action/message-detail.hbs b/apps/home/views/action/message-detail.hbs
index 4f1fb71..e9d6740 100644
--- a/apps/home/views/action/message-detail.hbs
+++ b/apps/home/views/action/message-detail.hbs
@@ -44,7 +44,7 @@
     {{#sale}}
     <!-- 促销消息 -->
     <div class="sale-img">
-        <img src="{{image}}">
+        <img src="{{image image}}">
     </div>
     <br />
     <br />
diff --git a/apps/home/views/action/message.hbs b/apps/home/views/action/message.hbs
index 3c5c8c6..4955def 100644
--- a/apps/home/views/action/message.hbs
+++ b/apps/home/views/action/message.hbs
@@ -1,6 +1,7 @@
 <div class="massage-page yoho-page">
+    {{#if list}}
     {{#list}}
-    <div class="massage-list">
+    <div class="massage-list" data-id="{{id}}">
         {{#if isNotReaded}}
         <strong></strong>
         {{/if}}
@@ -9,4 +10,7 @@
         <i class="iconfont del">&#xe621;</i>
     </div>
     {{/list}}
+    {{else}}
+    <p style="text-align:center">您没有消息</p>
+    {{/if}}
 </div>
\ No newline at end of file
diff --git a/public/js/home/message.page.js b/public/js/home/message.page.js
new file mode 100644
index 0000000..ea12f72
--- /dev/null
+++ b/public/js/home/message.page.js
@@ -0,0 +1,127 @@
+// 消息列表 by acgpiano
+
+var $ = require('yoho-jquery'),
+    loading = require('../plugin/loading'),
+    dialog = require('./dialog');
+
+var $page = $('.massage-page'),
+	page = 1,
+    stopLoading = false;
+
+function massageAJAX(page) {
+    loading.showLoadingMask();
+    $.ajax({
+        type: 'POST',
+        url: '/home/message',
+        dataType: 'html',
+        data: {
+            page: page,
+        },
+        success: function(data) {
+            if ($(data).find('p').html() === '您没有消息') {
+                stopLoading = true;
+                window.rePosFooter();
+                loading.hideLoadingMask();
+                return;
+            }
+
+            $('.massage-page').append($(data).html());
+             window.rePosFooter();
+             loading.hideLoadingMask();
+        }
+    });
+}
+
+function scrollHandler() {
+    if (!stopLoading && ($(window).scrollTop() + $(window).height() > $('body').height() - 100)) {
+        page++;
+        massageAJAX(page);
+    }
+}
+
+require('../common.js');
+
+$(window).scroll(function() {
+    !location.search && window.requestAnimationFrame(scrollHandler);
+});
+
+// 删除消息
+$page.on('touchstart', '.del', function() {
+    var $good = $(this).closest('.massage-list'),
+        $id = $good.data('id');
+
+    dialog.showDialog({
+        dialogText: '确定删除这条消息吗?',
+        hasFooter: {
+            leftBtnText: '取消',
+            rightBtnText: '确定'
+        }
+    }, function() {
+
+        $.ajax({
+            type: 'POST',
+            url: '/home/ajaxDelMes',
+            data: {
+                id: $id
+            },
+            success: function(data) {
+                if (data.code === 200) {
+                    dialog.showDialog({
+                        dialogText: '删除消息成功',
+                        autoHide: 2000,
+                        fast: true
+                    });
+                    setTimeout(function() {
+                        window.history.go(0);
+                    }, 1200);
+                } else {
+                    dialog.showDialog({
+                        dialogText: data.message,
+                        autoHide: 2000,
+                        fast: true
+                    });
+                }
+            }
+        });
+    });
+});
+
+var $page = $('.massage-page');
+
+var pickBusy = false;
+
+// 领取生日券
+$page.on('touchstart', '.pick-coupon-btn', function() {
+    if (pickBusy) {
+        return;
+    }
+    pickBusy = true;
+
+    var $id = $(this).data('id');
+
+    $.ajax({
+        type: 'POST',
+        url: '/home/pickCoupon',
+        data: {
+            id: $id
+        },
+        success: function(data) {
+            if (data.code === 200) {
+                dialog.showDialog({
+                    dialogText: '领取成功',
+                    autoHide: 2000,
+                    fast: true
+                });
+                $(this).removeClass('pick-coupon-btn');
+            }
+
+            pickBusy = false;
+
+            dialog.showDialog({
+                dialogText: data.message,
+                autoHide: 2000,
+                fast: true
+            });
+        }
+    });
+});
\ No newline at end of file
--
libgit2 0.24.0