From 3e0c0ccd7dbddcd10c63e101d26db9749729a790 Mon Sep 17 00:00:00 2001 From: OldHawk Date: Tue, 15 May 2018 21:22:59 +0800 Subject: [PATCH] feat(check in): new feather of check in daily --- .../client/services/check.client.service.js | 24 ++++ .../controllers/check.server.controller.js | 110 ++++++++++++++++++ .../server/routes/check.server.routes.js | 8 ++ modules/core/client/app/trans-string-en.js | 12 +- modules/core/client/app/trans-string-zh.js | 12 +- .../controllers/home.client.controller.js | 91 ++++++++++++++- modules/core/client/less/home.less | 47 ++++++-- .../core/client/views/home.client.view.html | 40 +++++++ 8 files changed, 327 insertions(+), 17 deletions(-) create mode 100644 modules/check/client/services/check.client.service.js create mode 100644 modules/check/server/controllers/check.server.controller.js create mode 100644 modules/check/server/routes/check.server.routes.js diff --git a/modules/check/client/services/check.client.service.js b/modules/check/client/services/check.client.service.js new file mode 100644 index 00000000..b4577736 --- /dev/null +++ b/modules/check/client/services/check.client.service.js @@ -0,0 +1,24 @@ +(function () { + 'use strict'; + + // Users service used for communicating with the users REST endpoint + angular + .module('check.services') + .factory('CheckService', CheckService); + + CheckService.$inject = ['$resource']; + + function CheckService($resource) { + var Check = $resource('/api/check', {}, { + get: { + method: 'GET' + }, + update: { + method: 'PUT' + } + }); + + return Check; + } + +}()); diff --git a/modules/check/server/controllers/check.server.controller.js b/modules/check/server/controllers/check.server.controller.js new file mode 100644 index 00000000..8d40f850 --- /dev/null +++ b/modules/check/server/controllers/check.server.controller.js @@ -0,0 +1,110 @@ +'use strict'; + +/** + * Module dependencies + */ +var path = require('path'), + config = require(path.resolve('./config/config')), + mongoose = require('mongoose'), + errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller')), + moment = require('moment'), + User = mongoose.model('User'), + Check = mongoose.model('Check'), + scoreUpdate = require(path.resolve('./config/lib/score')).update; + +var mtDebug = require(path.resolve('./config/lib/debug')); +var scoreConfig = config.meanTorrentConfig.score; + +/** + * Show the current collection + */ +exports.get = function (req, res) { + if (!req.user) { + return res.status(403).json({ + message: 'SERVER.USER_IS_NOT_AUTHORIZED' + }); + } else { + Check.findOne({user: req.user._id}, function (err, ck) { + if (err) { + return res.status(422).send({ + message: errorHandler.getErrorMessage(err) + }); + } else { + if (!ck) { + return res.status(422).send({ + message: 'no check data founded' + }); + } else { + var oldCheckDate = moment(moment(ck.lastCheckedAt).format('YYYY-MM-DD')); + var now = moment(moment().format('YYYY-MM-DD')); + var diff = now.diff(oldCheckDate, 'days'); + + var nck = ck.toJSON(); + nck.todayIsDone = (diff === 0 ? true : false); + res.json(nck); + } + } + }); + } + +}; + +/** + * Update an collection + */ +exports.check = function (req, res) { + if (!req.user) { + return res.status(403).json({ + message: 'SERVER.USER_IS_NOT_AUTHORIZED' + }); + } else { + var score = 0; + Check.findOne({user: req.user._id}, function (err, ck) { + if (!ck) { + ck = new Check(); + ck.user = req.user._id; + ck.keepDays = 1; + ck.save(); + + score = scoreConfig.action.dailyCheckIn.dailyBasicScore + (ck.keepDays - 1) * scoreConfig.action.dailyCheckIn.dailyStepScore; + scoreUpdate(req, req.user, scoreConfig.action.dailyCheckIn, score); + + ck = ck.toJSON(); + ck.todayIsDone = true; + res.json(ck); + } else { + var oldCheckDate = moment(moment(ck.lastCheckedAt).format('YYYY-MM-DD')); + var now = moment(moment().format('YYYY-MM-DD')); + var diff = now.diff(oldCheckDate, 'days'); + + if (diff === 0) { + return res.status(422).json({ + message: 'SERVER.YOU_ALREADY_CHECK_IN' + }); + } else if (diff === 1) { + ck.keepDays += 1; + ck.lastCheckedAt = Date.now(); + ck.save(); + + score = scoreConfig.action.dailyCheckIn.dailyBasicScore + (ck.keepDays - 1) * scoreConfig.action.dailyCheckIn.dailyStepScore; + scoreUpdate(req, req.user, scoreConfig.action.dailyCheckIn, score); + + ck = ck.toJSON(); + ck.todayIsDone = true; + res.json(ck); + } else { + ck.keepDays = 1; + ck.lastCheckedAt = Date.now(); + ck.save(); + + score = scoreConfig.action.dailyCheckIn.dailyBasicScore + (ck.keepDays - 1) * scoreConfig.action.dailyCheckIn.dailyStepScore; + scoreUpdate(req, req.user, scoreConfig.action.dailyCheckIn, score); + + ck = ck.toJSON(); + ck.todayIsDone = true; + res.json(ck); + } + } + }); + } +}; diff --git a/modules/check/server/routes/check.server.routes.js b/modules/check/server/routes/check.server.routes.js new file mode 100644 index 00000000..05691f65 --- /dev/null +++ b/modules/check/server/routes/check.server.routes.js @@ -0,0 +1,8 @@ +'use strict'; + +module.exports = function (app) { + var check = require('../controllers/check.server.controller'); + + app.route('/api/check').get(check.get); + app.route('/api/check').put(check.check); +}; diff --git a/modules/core/client/app/trans-string-en.js b/modules/core/client/app/trans-string-en.js index c7135234..5231748f 100644 --- a/modules/core/client/app/trans-string-en.js +++ b/modules/core/client/app/trans-string-en.js @@ -132,6 +132,15 @@ } }, + CHECK: { + BTN_CHECK_IN: 'Check In', + CHECK_TOOLTIP: '#### `mine.pt` invites you to participate in the daily check in. Now check in to get `{{todayScore}}` scores and renew tomorrow to get `{{tomorrowScore}}` scores!', + CHECK_TODAY_NOT: '#### You have not checked in yet. Last check in at: `{{checkTime | date: "yyyy-MM-dd HH:mm"}}`. Now check in to get `{{todayScore}}` scores and renew tomorrow to get `{{tomorrowScore}}` scores!', + CHECK_TODAY_DONE: '#### You have checked in to `{{keepDays}}` days in a row, today check in at: `{{checkTime | date: "yyyy-MM-dd HH:mm"}}`, you have got `{{todayScore}}` scores and renew tomorrow to get `{{tomorrowScore}}` scores, please be careful not to interrupt!', + CHECK_SUCCESSFULLY: 'Check in successfully', + CHECK_ERROR: 'Check in failed' + }, + //element title/alt TITLE_ALT: { SEEDS: 'Seeds users', @@ -1609,7 +1618,8 @@ INFO_HASH_ALREADY_EXISTS: 'This info hash value is already exists', UPLOAD_ACCESS_DENY: 'System only accepts resources group to upload torrents', READ_TORRENT_FILE_FAILD: 'The torrent file parsing error. Please check your torrent file to check if some of the necessary information is missing', - MOVE_TORRENT_FILE_ERROR: 'The torrent file was moved incorrectly. Please do not repeat the submit operation quickly or the uploaded torrent file is missing.' + MOVE_TORRENT_FILE_ERROR: 'The torrent file was moved incorrectly. Please do not repeat the submit operation quickly or the uploaded torrent file is missing.', + YOU_ALREADY_CHECK_IN: 'You already checked in today' }, //server message string, content string support markdown and emoji diff --git a/modules/core/client/app/trans-string-zh.js b/modules/core/client/app/trans-string-zh.js index e9516cc3..7f69660c 100644 --- a/modules/core/client/app/trans-string-zh.js +++ b/modules/core/client/app/trans-string-zh.js @@ -132,6 +132,15 @@ } }, + CHECK: { + BTN_CHECK_IN: '我要签到', + CHECK_TOOLTIP: '#### `mine.pt` 邀请您参加每日签到,现在签到可获得 `{{todayScore}}` 积分,明日续签可获得 `{{tomorrowScore}}` 积分哦!', + CHECK_TODAY_NOT: '#### 您今日还未签到,上次签到:`{{checkTime | date: "yyyy-MM-dd HH:mm"}}`,现在签到可获得 `{{todayScore}}` 积分,明日续签到可获得 `{{tomorrowScore}}` 积分哦!', + CHECK_TODAY_DONE: '#### 您已连续签到 `{{keepDays}}` 天,今日签到时间:`{{checkTime | date: "yyyy-MM-dd HH:mm"}}`,已经获得 `{{todayScore}}` 积分,明日续签到可获得 `{{tomorrowScore}}` 积分,请注意不要中断哦!', + CHECK_SUCCESSFULLY: '签到成功', + CHECK_ERROR: '签到失败' + }, + //element title/alt TITLE_ALT: { SEEDS: '做种用户数', @@ -1609,7 +1618,8 @@ INFO_HASH_ALREADY_EXISTS: '该 info hash 值已经存在,不能重复提交', UPLOAD_ACCESS_DENY: '当前系统只接受资源制作小组上传种子', READ_TORRENT_FILE_FAILD: '种子文件解析错误,请检查您的种子文件,看是否缺少某些必要信息', - MOVE_TORRENT_FILE_ERROR: '种子文件移动错误,请不要快速重复进行提交操作或者上传的种子文件已丢失' + MOVE_TORRENT_FILE_ERROR: '种子文件移动错误,请不要快速重复进行提交操作或者上传的种子文件已丢失', + YOU_ALREADY_CHECK_IN: '您今天已经签过到了,不能重复签到' }, //server message string, content string support markdown and emoji diff --git a/modules/core/client/controllers/home.client.controller.js b/modules/core/client/controllers/home.client.controller.js index 5981f1bf..1b89df85 100644 --- a/modules/core/client/controllers/home.client.controller.js +++ b/modules/core/client/controllers/home.client.controller.js @@ -5,13 +5,13 @@ .module('core') .controller('HomeController', HomeController); - HomeController.$inject = ['$scope', '$state', '$translate', 'Authentication', 'TorrentsService', 'Notification', 'MeanTorrentConfig', + HomeController.$inject = ['$scope', '$state', '$translate', 'Authentication', 'TorrentsService', 'NotifycationService', 'MeanTorrentConfig', 'getStorageLangService', 'ForumsService', '$timeout', 'localStorageService', 'TopicsService', 'TorrentGetInfoServices', 'DebugConsoleService', - 'marked']; + 'marked', 'CheckService']; - function HomeController($scope, $state, $translate, Authentication, TorrentsService, Notification, MeanTorrentConfig, getStorageLangService, + function HomeController($scope, $state, $translate, Authentication, TorrentsService, NotifycationService, MeanTorrentConfig, getStorageLangService, ForumsService, $timeout, localStorageService, TopicsService, TorrentGetInfoServices, mtDebug, - marked) { + marked, CheckService) { var vm = this; vm.user = Authentication.user; vm.appConfig = MeanTorrentConfig.meanTorrentConfig.app; @@ -23,8 +23,10 @@ vm.announceConfig = MeanTorrentConfig.meanTorrentConfig.announce; vm.homeConfig = MeanTorrentConfig.meanTorrentConfig.home; vm.supportConfig = MeanTorrentConfig.meanTorrentConfig.support; + vm.scoreConfig = MeanTorrentConfig.meanTorrentConfig.score; vm.searchType = 'torrents'; + vm.checkData = undefined; /** * initBodyBackground @@ -323,5 +325,86 @@ return marked(ts, {sanitize: true}); }; + /** + * getMyCheckData + */ + vm.getMyCheckData = function () { + CheckService.get(function (res) { + mtDebug.info(res); + vm.checkData = res; + vm.openCheckTooltip(); + }, function (err) { + vm.checkData = false; + vm.openCheckTooltip(); + }); + }; + + /** + * checkIn + */ + vm.checkIn = function () { + CheckService.update(function (res) { + mtDebug.info(res); + vm.checkData = res; + NotifycationService.showSuccessNotify('CHECK.CHECK_SUCCESSFULLY'); + }, function (err) { + NotifycationService.showErrorNotify(err.data.message, 'CHECK.CHECK_ERROR'); + }); + }; + + /** + * getCheckTodayDoneMessage + * @returns {*} + */ + vm.getCheckTodayDoneMessage = function () { + var ts = $translate.instant('CHECK.CHECK_TODAY_DONE', { + keepDays: vm.checkData.keepDays, + checkTime: vm.checkData.lastCheckedAt, + todayScore: vm.scoreConfig.action.dailyCheckIn.dailyBasicScore + (vm.checkData.keepDays - 1) * vm.scoreConfig.action.dailyCheckIn.dailyStepScore, + tomorrowScore: vm.scoreConfig.action.dailyCheckIn.dailyBasicScore + vm.checkData.keepDays * vm.scoreConfig.action.dailyCheckIn.dailyStepScore + }); + + return marked(ts, {sanitize: false}); + }; + + /** + * getCheckTodayNotMessage + * @returns {*} + */ + vm.getCheckTodayNotMessage = function () { + var ts = $translate.instant('CHECK.CHECK_TODAY_NOT', { + checkTime: vm.checkData.lastCheckedAt, + todayScore: vm.scoreConfig.action.dailyCheckIn.dailyBasicScore + vm.checkData.keepDays * vm.scoreConfig.action.dailyCheckIn.dailyStepScore, + tomorrowScore: vm.scoreConfig.action.dailyCheckIn.dailyBasicScore + (vm.checkData.keepDays + 1) * vm.scoreConfig.action.dailyCheckIn.dailyStepScore + }); + + return marked(ts, {sanitize: false}); + }; + + /** + * getCheckTooltipMessage + * @returns {*} + */ + vm.getCheckTooltipMessage = function () { + var ts = $translate.instant('CHECK.CHECK_TOOLTIP', { + todayScore: vm.scoreConfig.action.dailyCheckIn.dailyBasicScore, + tomorrowScore: vm.scoreConfig.action.dailyCheckIn.dailyBasicScore + vm.scoreConfig.action.dailyCheckIn.dailyStepScore + }); + + return marked(ts, {sanitize: false}); + }; + + /** + * openCheckTooltip + */ + vm.openCheckTooltip = function () { + var e = $('.home-check-in'); + + $timeout(function () { + e.slideDown(800); + e.removeClass('panel-collapsed'); + }, 1000); + }; + } }()); diff --git a/modules/core/client/less/home.less b/modules/core/client/less/home.less index 1fa413c0..bdac4200 100644 --- a/modules/core/client/less/home.less +++ b/modules/core/client/less/home.less @@ -113,6 +113,30 @@ } } +.col-valign-middle { + @media (min-width: @screen-sm-min) { + display: table-cell; + vertical-align: middle; + float: none; + } +} + +.home-check-in { + padding: 10px 0; + h4, .h4 { + line-height: 1.8; + } + .check-in-btn { + margin-top: 10px; + @media(max-width: @screen-sm-max){ + margin-top: 20px; + } + @media(max-width: @screen-xs-max){ + margin-top: 15px; + } + } +} + .home-button-list { padding: 50px 0; @media (max-width: @screen-xs-max) { @@ -316,6 +340,18 @@ } } +.filter-check-in { + position: relative; + margin-top: 2px; + background-color: @mt-body-background-color; + background-color: rgba(255, 255, 255, .85); + transition: background-color .2s ease-in; + &:hover { + background-color: @mt-body-background-color; + background-color: rgba(255, 255, 255, .95); + } +} + .filter-copy { position: relative; line-height: 2; @@ -449,17 +485,6 @@ } } -.home-check-in { - background-color: #2a2a2a; - background-color: rgba(0,0,0, 0.5); - .check-in-title { - color: #fff; - font-size: 2em; - font-weight: 400; - margin-bottom: 0 !important; - } -} - .view-footer { color: #aaa; font-size: 12px; diff --git a/modules/core/client/views/home.client.view.html b/modules/core/client/views/home.client.view.html index 4df018de..4f03eda5 100644 --- a/modules/core/client/views/home.client.view.html +++ b/modules/core/client/views/home.client.view.html @@ -75,6 +75,46 @@ + +