Commit db1217a4 authored by qinhaitao's avatar qinhaitao

feat: 🎸 sign模块

parent 8773c72d
/** @format */
import { services, preCheck, registeInfos, preUpdate, checkParams } from '../decorator/common/'
import { resultsModel } from '../sdk'
import { CommonSignService } from '../service/common'
import { STAT_TYPE } from '../constants'
import { checkActivityTime, checkInviteId, checkHelpRecord, checkNewVip, checkSign } from '../utils/common/check'
import { updateHelpRecord, updateVip } from '../utils/common/update/'
export default class SignController {
/**
* 签到
*/
@checkParams(['activityId', 'subscribeSignDay?'])
@services([CommonSignService])
@preCheck([checkSign])
async doSign(
context: IContext<{
activityId: string
subscribeSignDay?: string
}>,
{ userInfo }: IControllerInfos,
[signService]: [CommonSignService]
) {
const { activityId, subscribeSignDay } = context.data
const result = await signService.doSign(userInfo, activityId, subscribeSignDay)
return resultsModel.success(result)
}
}
......@@ -8,6 +8,8 @@ export const AWARDS_DB_NAME = 'c_awards_info'
export const JOIN_DB_NAME = 'c_user_join'
export const SIGN_DB_NAME = 'c_user_sign'
export const STAT_DB_NAME = 'c_stats'
export const ERROR_LOG_DB_NAME = 'error_log'
......
......@@ -7,6 +7,7 @@ import CommonAwardsService from './awards.service'
import CommonTaskService from './task.service'
import CommonStatService from './stat.service'
import CommonGameService from './game.service'
import CommonSignService from './sign.service'
const common = {
CommonAccessService,
......@@ -15,7 +16,8 @@ const common = {
CommonAwardsService,
CommonTaskService,
CommonStatService,
CommonGameService
CommonGameService,
CommonSignService
}
export default common
......@@ -27,5 +29,6 @@ export {
CommonAwardsService,
CommonTaskService,
CommonStatService,
CommonGameService
CommonGameService,
CommonSignService
}
/**
* 基本信息
*
* @format
*/
import { BaseDao, TBAPIS } from '../../sdk'
import { SIGN_DB_NAME } from '../../db'
import { ACTIVITY_STATUS } from '../../constants'
import UserService from './user.service'
import { getToday } from '../../utils'
import { CODE_TYPES } from '../../errorCode'
export default class SignService extends UserService {
context: IContext<any>
signdao: IBaseDao
constructor(context: IContext<any>) {
super(context)
this.signdao = new BaseDao(context, SIGN_DB_NAME)
}
async doSign(userInfo: IUserInfo, activityId: string, subscribeSignDay = '') {
const { openId } = this.context
const { userNick } = userInfo
const today = getToday()
const signResult = await this.signdao.insertOne<IUserSign>({
activityId,
openId,
userNick,
isSignLater: !!subscribeSignDay,
createDay: today,
signTime: subscribeSignDay ? new Date(subscribeSignDay) : new Date(),
signDay: subscribeSignDay || today,
createTime: new Date()
})
if (!signResult) return CODE_TYPES.SYSTEM_ERROR
return {
signId: signResult
}
}
async getSignList(activityId: string) {
const { openId } = this.context
const signInfoList: ISignInfoAggregate[] = await this.signdao.aggregate(
{ $match: { activityId, openId } },
{ $project: { d: { $dayOfYear: '$signTime' }, y: { $year: '$signTime' }, signTime: '$signTime' } }
)
return signInfoList
}
}
/** @format */
interface IUserSign {
activityId: string
openId: string
userNick: string
createTime: Date
createDay: string
signTime: Date
signDay: string
isSignLater?: boolean // 是否补签
}
interface ISignInfoAggregate {
_id?: string
d: number
y: number
signTime: Date
}
/** @format */
import { resultsModel } from '../../../sdk'
import { CODE_TYPES } from '../../../errorCode'
import { dbCount } from '../mongodb'
import { SIGN_DB_NAME } from '../../../db'
import { getToday } from '../getToday'
export default async function checkSign(
context: IContext<{
activityId: string
subscribeSignDay?: string
}>,
{ userInfo }: IControllerInfos
) {
const { activityId, subscribeSignDay } = context.data
const { openId } = context
const todaySigned = await dbCount(context, SIGN_DB_NAME, {
activityId,
openId,
signDay: subscribeSignDay || getToday()
})
if (todaySigned) return resultsModel.error(CODE_TYPES.PARAMS_ERROR, '今日已签到~')
}
......@@ -12,6 +12,7 @@ import checkVip from './checkVip'
import checkUserInfo, { checkGameTimes } from './checkUserInfo'
import checkJoinId from './checkJoinId'
import checkOpenPrizeStatus from './checkOpenPrizeStatus'
import checkSign from './checkSign'
const check = {
checkActivityTime,
......@@ -29,7 +30,8 @@ const check = {
checkUserInfo,
checkJoinId,
checkGameTimes,
checkOpenPrizeStatus
checkOpenPrizeStatus,
checkSign
}
export default check
......@@ -50,5 +52,6 @@ export {
checkUserInfo,
checkJoinId,
checkGameTimes,
checkOpenPrizeStatus
checkOpenPrizeStatus,
checkSign
}
/** @format */
import * as dayjs from 'dayjs'
import { uniq } from 'lodash'
import { getToday } from './getToday'
const isLeapYear = (year: number) => (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0
// 获取最大连续签到天数
export const getMaxContinusSignDays = (signInfoList: ISignInfoAggregate[]): number => {
const minYear = Math.min.apply(
null,
signInfoList.map(v => +v.y)
)
const list: number[] = signInfoList
.map(v => {
const gap = (isLeapYear ? 366 : 365) * (v.y - minYear)
return gap + v.d
})
.sort((a, b) => a - b)
if (list.length === 0) return 0
let m = uniq(list.sort((a, b) => a - b))
let max = 0
for (let i = 0; i < m.length; i++) {
let idx = 0
while (m[i + 1] - m[i] === 1) {
idx++
i++
max = Math.max(idx, max)
}
}
return max + 1
}
// 获取某天是否签到
export const getSignedByDay = (signInfoList: ISignInfoAggregate[], day = getToday()): boolean => {
return signInfoList.some(v => dayjs(v.signTime).format('yyyy/MM/dd') === day)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment