Commit d30d2d07 authored by 王波's avatar 王波 Committed by qinhaitao

养成类模板提交

parent 1e833915
This diff is collapsed.
/** @format */
import { services, preCheck, registeInfos, preUpdate, checkParams } from '../decorator/common'
import { resultsModel } from '../sdk'
import { CommonUserService, CommonStatService, CommonTaskService, CommonGrowTaskService } from '../service/common'
import { STAT_TYPE } from '../constants'
import { checkActivityTime, checkInviteId, checkHelpRecord, checkNewVip } from '../utils/common/check'
import { updateHelpRecord, updateVip } from '../utils/common/update'
import { getTask } from '../utils'
export default class Share {
/**
* 完成分享
*/
@checkParams(['activityId', 'inviteId'])
@registeInfos(['vipInfo'])
@services([CommonGrowTaskService, CommonStatService])
@preCheck([checkActivityTime, checkInviteId, checkNewVip, checkHelpRecord])
@preUpdate([updateVip, updateHelpRecord])
async doHelp(
context: IContext<IParams>,
{ userInfo, activityInfo }: IControllerInfos,
[growTaskService, statService]: [CommonGrowTaskService, CommonStatService],
{ inviteUserInfo }: IPreCheckData
) {
// 如果邀请任务是每日的,则需要请求每日任务信息
const task = await getTask(context, inviteUserInfo.openId)
const result = await growTaskService.completeTask('invites', activityInfo, inviteUserInfo, task)
statService.addStat(STAT_TYPE.INITE_SUCCESS, userInfo)
return resultsModel.success(result)
}
}
/** @format */
import { checkParams, services, registeInfos, preCheck } from '../decorator/common'
import { resultsModel } from '../sdk'
import { CommonGrowTaskService, CommonBaseService } from '../service/common'
import { ITaskInfo } from '../service/common/task.service'
import {
checkActivityTime,
checkGrowTaskLimit,
checkExchangeCreditsTask,
checkRemainTimes,
checkInviteId
} from '../utils/common/check'
import { preUpdate } from '../decorator/common'
import { updateVip, updateSignGrowTask, updateGrowOrderGoods } from '../utils/common/update'
export default class Task {
/**
* 任务列表
*/
@checkParams(['activityId'])
@registeInfos(['vipInfo', 'task'])
@services([CommonGrowTaskService])
@preCheck([checkActivityTime])
@preUpdate([updateVip, updateSignGrowTask, updateGrowOrderGoods])
async getTaskList(
context: IContext<IParams>,
{ userInfo, activityInfo }: IControllerInfos,
[growTaskService]: [CommonGrowTaskService]
): Promise<IResult<{ list: ITaskInfo[] }>> {
// 读取B端 tasks字段配置自动生成任务列表
const result = await growTaskService.initTaskList(userInfo, activityInfo)
return resultsModel.success({
...result
})
}
/**
* 完成任务
*
* 商品相关任务 需要传itemId参数
* 积分兑换任务 需要传credits参数
*/
@checkParams(['activityId', 'taskType', 'itemId?', 'credits?'])
@registeInfos(['session', 'task'])
@services([CommonGrowTaskService])
@preCheck([checkActivityTime, checkGrowTaskLimit, checkExchangeCreditsTask])
async doCompleteTask(
context: IContext<IParams>,
{ userInfo, activityInfo, task }: IControllerInfos,
[growTaskService]: [CommonGrowTaskService]
) {
const { taskType } = context.data
// 更新user表
const result = await growTaskService.completeTask(taskType, activityInfo, userInfo, task)
return resultsModel.success(result)
}
/**
* 领取任务奖励
* 领取的奖励增加的key值 如: gameTimes 前端传入
*/
@checkParams(['activityId', 'taskType', 'rewardsKey'])
@services([CommonGrowTaskService])
@preCheck([checkActivityTime, checkRemainTimes])
async receiveTaskRewards(
context: IContext<IParams>,
{ userInfo }: IControllerInfos,
[growTaskService]: [CommonGrowTaskService]
): Promise<IResult<{ rewards: number }>> {
const { rewardsKey } = context.data
const { taskType } = context.data
const rewardsResult = await growTaskService.receiveTaskRewards(taskType, rewardsKey, userInfo)
return resultsModel.success(rewardsResult)
}
/**
* 获取收藏商品列表
*/
@checkParams(['activityId', 'itemIds'])
@registeInfos(['session'])
@services([CommonBaseService, CommonGrowTaskService])
async getCollectGoodsList(
context: IContext<IParams>,
{ session, userInfo }: IControllerInfos,
[commonBaseService, growTaskService]: [CommonBaseService, CommonGrowTaskService]
) {
const { itemIds } = context.data
const list = await commonBaseService.getItemListByIds(itemIds, session)
const listWithCollectStatus = await growTaskService.getItemListWithCollectStatus(list, userInfo)
return resultsModel.success(listWithCollectStatus)
}
}
...@@ -10,6 +10,8 @@ export const JOIN_DB_NAME = 'c_user_join' ...@@ -10,6 +10,8 @@ export const JOIN_DB_NAME = 'c_user_join'
export const SIGN_DB_NAME = 'c_user_sign' export const SIGN_DB_NAME = 'c_user_sign'
export const TASK_DB_NAME = 'c_user_tasks'
export const STAT_DB_NAME = 'c_stats' export const STAT_DB_NAME = 'c_stats'
export const ERROR_LOG_DB_NAME = 'error_log' export const ERROR_LOG_DB_NAME = 'error_log'
...@@ -19,3 +21,4 @@ export const SELLER_INFO_DB_NAME = 'a_seller_info' ...@@ -19,3 +21,4 @@ export const SELLER_INFO_DB_NAME = 'a_seller_info'
export const PRIZE_CONFIG_DB_NAME = 'b_prize_config' export const PRIZE_CONFIG_DB_NAME = 'b_prize_config'
export const ACTIVITY_CONFIG_DB_NAME = 'b_activity_config' export const ACTIVITY_CONFIG_DB_NAME = 'b_activity_config'
/** @format */ /** @format */
import { CommonUserService, CommonBaseService } from '../../service/common' import { CommonUserService, CommonBaseService } from '../../service/common'
import { getSellerSession, getUserOrderlist } from '../../utils' import { getSellerSession, getUserOrderlist,getTask } from '../../utils'
import { formatVipCbUrl, getShopVip } from '../../utils/common/vip' import { formatVipCbUrl, getShopVip } from '../../utils/common/vip'
import { CODE_TYPES } from '../../errorCode' import { CODE_TYPES } from '../../errorCode'
import { resultsModel, TBAPIS } from '../../sdk' import { resultsModel, TBAPIS } from '../../sdk'
export type IInfo = 'session' | 'vipInfo' | 'taobaoOrderist' | 'credits' export type IInfo = 'session' | 'vipInfo' | 'taobaoOrderist' | 'credits' | 'task'
async function initBaseInfo(context: IContext<IParams>, baseInfos: ICheckControllerInfos, needInfos: IInfo[]) { async function initBaseInfo(context: IContext<IParams>, baseInfos: ICheckControllerInfos, needInfos: IInfo[]) {
const handler = context?.cloud?.dataspace?.context?.handler const handler = context?.cloud?.dataspace?.context?.handler
...@@ -54,6 +54,12 @@ async function initBaseInfo(context: IContext<IParams>, baseInfos: ICheckControl ...@@ -54,6 +54,12 @@ async function initBaseInfo(context: IContext<IParams>, baseInfos: ICheckControl
baseInfos.credits = credits baseInfos.credits = credits
} }
if (needInfos.includes('task') && !baseInfos.credits) {
const task = await getTask(context)
if (!task._id) return CODE_TYPES.ERROR_UPDATE_RETRY
baseInfos.task = task
}
return baseInfos return baseInfos
} }
......
/**
* 任务相关方法
*
* @format
*/
import UserService from './user.service'
import { getToday, getUserOrderlist, generateVipUrl, formatVipCbUrl, setNewFollowUserData } from '../../utils'
import { TASK_RATE_TYPE, TASK_STATUS } from '../../constants'
import { getTodayCompleteTask, getTotalCompleteTask, setTaskStatus, updateTaskInfo } from '../../utils/common/task'
import { CODE_TYPES } from '../../errorCode'
export interface ITaskInfo {
taskType?: string // 任务类型
url?: string // 链接地址
rewards: number // 任务奖励
itemIds?: string // 商品配置ids 商品相关任务返回
todayCompleteTimes?: number // 今日完成次数
completeTimes?: number // 总共完成次数
taskRateType: number // 任务频率
times?: number // 任务每日限制次数
waitReceive?: number // 待领取次数
title: string // 任务文案
status: number // 任务状态 1未完成 2 待领取 3 已完成
}
export default class TaskService extends UserService {
constructor(context: IContext<IParams>) {
super(context)
}
// 根据活动tasks字段渲染任务
async initTaskList(userInfo: IUserInfo, activityInfo: IActivityInfo) {
return {
list: Object.keys(activityInfo?.tasks || {}).map((task: ITaskType) => {
return this.initTask(task, activityInfo?.tasks?.[task]?.title, userInfo, activityInfo)
})
}
}
/**
* 根据任务类型初始化 b端 tasks字段必须有相应的配置
*
* @param {ITaskType} taskType 任务类型
* @param {string} title 任务标题
* @param {IActivityInfo} activityInfo 任务配置
* @param {IUserInfo} userInfo 用户信息
* @return {ITaskInfo} {ITaskInfo} 任务信息
* @memberof TaskService
*/
initTask(taskType: ITaskType, title: string, userInfo: IUserInfo, activityInfo: IActivityInfo): ITaskInfo {
if (!activityInfo?.tasks?.[taskType]) {
console.error(`活动缺少${taskType}任务配置项`)
}
const { value, itemIds = '', taskRateType, times = 1, link = '' } = activityInfo?.tasks?.[taskType] || {}
const { remainTimes } = userInfo
const { todayCompleteTimes } = getTodayCompleteTask(taskType, userInfo)
const { completeTimes } = getTotalCompleteTask(taskType, userInfo)
return {
taskType,
title,
itemIds,
rewards: value,
taskRateType,
times,
url: taskType === 'member' ? generateVipUrl(formatVipCbUrl(this.context)) : link,
todayCompleteTimes,
completeTimes,
status: setTaskStatus(userInfo, taskType, taskRateType, times),
waitReceive: remainTimes?.[taskType]
}
}
/**
*
* 领取任务奖励
*
* @param {ITaskType} taskType
* @param {string} awardsTargetKey
* @param {IUserInfo} userInfo
* @return {number} rewards
* @memberof TaskService
*/
async receiveTaskRewards(
taskType: ITaskType,
awardsTargetKey: string,
userInfo: IUserInfo
): Promise<{
rewards: number
}> {
const waitReceiveTimes = userInfo?.remainTimes?.[taskType]
await this.updateUser(userInfo._id, {
$set: {
[`remainTimes.${taskType}`]: 0
},
$inc: {
[awardsTargetKey]: waitReceiveTimes
}
})
return {
rewards: waitReceiveTimes
}
}
/**
*根据任务类型更新任务待领取次数和任务记录
*
* @param {string} taskType
* @param {number} activityInfo
* @param {IActivityInfo} userInfo
* @returns {boolean}
* @memberof TaskService
*/
async completeTask(
taskType: ITaskType,
activityInfo: IActivityInfo,
userInfo: IUserInfo,
task: ITask = {},
customRecord: Object = {}
) {
const today = getToday()
const rewards = activityInfo?.tasks?.[taskType]?.value || 0
const { tasks } = activityInfo
const {
[taskType]: { taskRateType }
} = tasks
const {
openId,
data: { itemId }
} = this.context
const record = {
itemId,
openId: taskType === 'invites' ? openId : undefined,
createTime: Date.now(),
...customRecord
}
if (taskRateType === TASK_RATE_TYPE.EVERYDAY) {
//如果是每日任务,则更新每日任务表
const result = await updateTaskInfo(this.context, task._id, taskType, record)
if (result === 1) {
const updateResult = await this.updateUser(userInfo._id, {
$inc: {
[`remainTimes.${taskType}`]: +rewards
}
})
return updateResult === 1 ? { ok: 1 } : CODE_TYPES.ERROR_DO_TASK
}
return CODE_TYPES.ERROR_DO_TASK
} else {
//永久任务就更新永久任务表
let updateQuery: IUpdateQuery = {
$inc: {
[`remainTimes.${taskType}`]: +rewards
},
$push: {
[`taskInfo.${today}.${taskType}`]: { ...record, createTime: Date.now() }
}
}
if (taskType === 'follow') {
updateQuery.$set = {
follow: setNewFollowUserData(userInfo.follow)
}
}
const result = await this.updateUser(userInfo._id, updateQuery)
return result === 1 ? { ok: 1 } : CODE_TYPES.ERROR_DO_TASK
}
}
async getItemListWithCollectStatus(list: { list: ITaoBaoItems[] }, userInfo: IUserInfo) {
const { taskInfo } = getTotalCompleteTask('collectGoods', userInfo)
return {
list: list.list.map(v => {
return {
...v,
// 完成列表是否含有itemId
collected: taskInfo.some(completeItem => completeItem.itemId === v.itemId)
}
})
}
}
}
...@@ -8,6 +8,8 @@ import CommonTaskService from './task.service' ...@@ -8,6 +8,8 @@ import CommonTaskService from './task.service'
import CommonStatService from './stat.service' import CommonStatService from './stat.service'
import CommonGameService from './game.service' import CommonGameService from './game.service'
import CommonSignService from './sign.service' import CommonSignService from './sign.service'
import CommonGrowTaskService from './growTask.service'
const common = { const common = {
CommonAccessService, CommonAccessService,
...@@ -17,7 +19,8 @@ const common = { ...@@ -17,7 +19,8 @@ const common = {
CommonTaskService, CommonTaskService,
CommonStatService, CommonStatService,
CommonGameService, CommonGameService,
CommonSignService CommonSignService,
CommonGrowTaskService
} }
export default common export default common
...@@ -30,5 +33,6 @@ export { ...@@ -30,5 +33,6 @@ export {
CommonTaskService, CommonTaskService,
CommonStatService, CommonStatService,
CommonGameService, CommonGameService,
CommonSignService CommonSignService,
CommonGrowTaskService
} }
...@@ -30,6 +30,7 @@ interface IControllerInfos { ...@@ -30,6 +30,7 @@ interface IControllerInfos {
session?: string session?: string
credits?: number credits?: number
taobaoOrderist?: ITaoBaoOrders[] taobaoOrderist?: ITaoBaoOrders[]
task: ITask
} }
interface ICheckControllerInfos extends IControllerInfos { interface ICheckControllerInfos extends IControllerInfos {
...@@ -51,6 +52,15 @@ interface IVipInfo { ...@@ -51,6 +52,15 @@ interface IVipInfo {
url?: string url?: string
} }
interface ITask {
_id?:string
openId?: string
activityId?:string
taskInfo?: any
createDay?: string
createTime?: number
}
type IFunction = (...args: any[]) => any type IFunction = (...args: any[]) => any
type ValueOf<T> = T extends any[] ? T[number] : T[keyof T] type ValueOf<T> = T extends any[] ? T[number] : T[keyof T]
/** @format */
import { resultsModel } from '../../../sdk'
import { CODE_TYPES } from '../../../errorCode'
import { getTotalCompleteTask, getTodayCompleteGrowTask } from '../task'
import { TASK_RATE_TYPE } from '../../../constants'
export default async function checkTaskLimit(
context: IContext<{ activityId: string; taskType: ITaskType; itemId?: string }>,
{ userInfo, activityInfo, task }: IControllerInfos
) {
const { taskType, itemId } = context.data
const { itemIds } = activityInfo?.tasks?.[taskType] || {}
if (!taskType) {
console.error('使用checkTaskLimit, 云函数必须传入taskType参数')
return
}
if (!activityInfo?.tasks?.[taskType]) {
return resultsModel.error(CODE_TYPES.SYSTEM_ERROR, `该活动缺少${taskType}配置项`)
}
if (itemId && !itemIds) {
return resultsModel.error(CODE_TYPES.SYSTEM_ERROR, `该任务缺少商品配置项`)
}
// B端配置商品ids不匹配正确
if (itemId && itemIds) {
const itemIdArray: number[] = Array.isArray(itemIds) ? itemIds : itemIds.split(',').map(v => +v)
if (!itemIdArray.includes(+itemId)) return resultsModel.error(CODE_TYPES.PARAMS_ERROR, '商品id不匹配')
}
const { taskRateType, times = 1 } = activityInfo?.tasks?.[taskType]
// 永久任务已经完成
const { completeTimes } = getTotalCompleteTask(taskType, userInfo)
const hasComplete = completeTimes >= times && taskRateType === TASK_RATE_TYPE.FOREVER
if (hasComplete) return resultsModel.error(CODE_TYPES.ERROR_TASK_LIMIT, `该任务已完成,去看看其它任务吧~`)
// 每日任务今天已经完成
const { todayCompleteTimes, todayTaskInfo } = getTodayCompleteGrowTask(taskType, task)
const hasCompleteToday = times && todayCompleteTimes === times && taskRateType === TASK_RATE_TYPE.EVERYDAY
if (hasCompleteToday) {
return resultsModel.error(CODE_TYPES.ERROR_TASK_LIMIT, `该任务今天已完成,明天再来吧~`)
}
// 今日任务该商品已经完成
const itemHasCompleteToday =
todayTaskInfo.find(v => v.itemId && v.itemId === itemId) && taskRateType === TASK_RATE_TYPE.EVERYDAY
if (itemHasCompleteToday) {
return resultsModel.error(CODE_TYPES.PARAMS_ERROR, '今日该商品任务已完成')
}
}
...@@ -13,6 +13,7 @@ import checkUserInfo, { checkGameTimes } from './checkUserInfo' ...@@ -13,6 +13,7 @@ import checkUserInfo, { checkGameTimes } from './checkUserInfo'
import checkJoinId from './checkJoinId' import checkJoinId from './checkJoinId'
import checkOpenPrizeStatus from './checkOpenPrizeStatus' import checkOpenPrizeStatus from './checkOpenPrizeStatus'
import checkSign from './checkSign' import checkSign from './checkSign'
import checkGrowTaskLimit from './checkGrowTaskLimit'
const check = { const check = {
checkActivityTime, checkActivityTime,
...@@ -31,7 +32,8 @@ const check = { ...@@ -31,7 +32,8 @@ const check = {
checkJoinId, checkJoinId,
checkGameTimes, checkGameTimes,
checkOpenPrizeStatus, checkOpenPrizeStatus,
checkSign checkSign,
checkGrowTaskLimit
} }
export default check export default check
...@@ -53,5 +55,6 @@ export { ...@@ -53,5 +55,6 @@ export {
checkJoinId, checkJoinId,
checkGameTimes, checkGameTimes,
checkOpenPrizeStatus, checkOpenPrizeStatus,
checkSign checkSign,
checkGrowTaskLimit
} }
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
import { getToday } from './getToday' import { getToday } from './getToday'
import { TASK_STATUS, TASK_RATE_TYPE } from '../../constants' import { TASK_STATUS, TASK_RATE_TYPE } from '../../constants'
import { TASK_DB_NAME } from '../../db'
import { BaseDao } from '../../sdk'
import Task from '../../controller/task.controller'
/** /**
* 根据任务类型获取任务完成情况 * 根据任务类型获取任务完成情况
...@@ -97,3 +100,97 @@ export function setTaskStatus( ...@@ -97,3 +100,97 @@ export function setTaskStatus(
break break
} }
} }
/**
* 获取用户任务参与信息
* @param context
* @param openId
* @param activityId
*/
export const getTask = async (context: IContext<IParams>, openId: string = '', activityId?: string): Promise<ITask> => {
activityId = activityId ? activityId : context.data.activityId
openId = openId ? openId : context.openId
const taskInfodao: IBaseDao = new BaseDao(context, TASK_DB_NAME)
const createDay = getToday()
//查询每日任务完成记录
const result: ITask = await taskInfodao.findOne({
openId,
activityId,
createDay
})
//如果还没有今天的任务数据,则新增一条用户任务数据
if (!result) {
const insertObj = {
openId,
activityId,
taskInfo: {},
createTime: Date.now(),
createDay: createDay
}
const insertResult = await taskInfodao.insertOne(insertObj)
return { ...insertObj, _id: insertResult }
}
return result
}
/**
* 获取养成类每日任务信息
* @param taskType
* @param task
*/
export function getTodayCompleteGrowTask(
taskType: string,
task: ITask
): {
todayCompleteTimes: number
todayTaskInfo: ITaskDetail[]
} {
const today = getToday()
return {
todayCompleteTimes: task?.taskInfo?.[today]?.[taskType]?.length || 0,
todayTaskInfo: task?.taskInfo?.[today]?.[taskType] || []
}
}
/**
* 更新每日任务,适用于购买商品每日任务
* @param context
* @param activityId
*/
export async function updateTaskInfo(context: IContext<IParams>, _id: string, taskType: string, value) {
let taskInfodao: IBaseDao = new BaseDao(context, TASK_DB_NAME)
const today = getToday()
return await taskInfodao.update(
{
_id
},
{
$push: {
[`taskInfo.${today}.${taskType}`]: value
}
}
)
}
/**
* 增加每日任务每日任务,适用于普通每日任务
* @param context
* @param activityId
*/
export async function updateGoodsTaskInfo(context: IContext<IParams>, _id: string, taskType: string, value) {
let taskInfodao: IBaseDao = new BaseDao(context, TASK_DB_NAME)
const today = getToday()
return await taskInfodao.update(
{
_id
},
{
$set: {
[`taskInfo.${today}.${taskType}`]: value
}
}
)
}
...@@ -4,6 +4,10 @@ import updateSignTask from './updateSignTask' ...@@ -4,6 +4,10 @@ import updateSignTask from './updateSignTask'
import updateOrderGoods from './updateOrderGoods' import updateOrderGoods from './updateOrderGoods'
import updateFirstLoginToday from './updateFirstLoginToday' import updateFirstLoginToday from './updateFirstLoginToday'
import updateUserInfo, { reduceGameTimes, updateHelpRecord } from './updateUserInfo' import updateUserInfo, { reduceGameTimes, updateHelpRecord } from './updateUserInfo'
import updateSignGrowTask from './updateSignGrowTask'
import updateGrowOrderGoods from './updateGrowOrderGoods'
const update = { const update = {
updateVip, updateVip,
...@@ -12,7 +16,9 @@ const update = { ...@@ -12,7 +16,9 @@ const update = {
updateFirstLoginToday, updateFirstLoginToday,
updateUserInfo, updateUserInfo,
reduceGameTimes, reduceGameTimes,
updateHelpRecord updateHelpRecord,
updateSignGrowTask,
updateGrowOrderGoods
} }
export default update export default update
...@@ -24,5 +30,7 @@ export { ...@@ -24,5 +30,7 @@ export {
updateFirstLoginToday, updateFirstLoginToday,
updateUserInfo, updateUserInfo,
reduceGameTimes, reduceGameTimes,
updateHelpRecord updateHelpRecord,
updateSignGrowTask,
updateGrowOrderGoods
} }
/** @format */
import { getToday } from '../getToday'
import { getTotalCompleteTask, getTodayCompleteGrowTask, updateGoodsTaskInfo } from '../task'
import { TASK_RATE_TYPE } from '../../../constants'
import { getUserOrderlist } from '../getUserOrderlist'
import { resultsModel } from '../../../sdk'
import { CODE_TYPES } from '../../../errorCode'
export default async function updateOrderGoods(
context: IContext<IParams>,
{ userInfo, activityInfo, task }: IControllerInfos
): Promise<IPreUpdateQuery> {
const taskType = 'orderGoods'
const { value, itemIds, taskRateType, times } = activityInfo?.tasks?.[taskType] || {}
const { completeTimes, taskInfo } = getTotalCompleteTask(taskType, userInfo)
const { todayCompleteTimes } = getTodayCompleteGrowTask(taskType, task)
if (!activityInfo?.tasks?.[taskType]) return {}
// 永久任务且已完成
if (taskRateType === TASK_RATE_TYPE.FOREVER && completeTimes >= times) {
return {}
}
// 每日限制完成且完成次数达到限制
if (taskRateType === TASK_RATE_TYPE.EVERYDAY && todayCompleteTimes >= times) {
return {}
}
return taskRateType === TASK_RATE_TYPE.EVERYDAY ?
await getEverydayGoods(context, taskType, itemIds, task, times, value, userInfo)
: await getOrderGoods(taskType, itemIds, times, value, userInfo, taskInfo)
}
/**
* 更新永久订单信息
* @param taskType
* @param itemIds
* @param times
* @param value
* @param userInfo
*/
async function getOrderGoods(taskType: string, itemIds: string, times: number, value: number, userInfo: IUserInfo, taskInfo: ITaskDetail[]): Promise<IPreUpdateQuery> {
const today = getToday()
const orderResult = await getUserOrderlist(
this.context,
//@ts-ignore
userInfo.createTime || activityInfo?.startTime || Date.now(),
Date.now()
)
const itemIdsArr = itemIds.split(',').map(v => +v)
let projection = {
$inc: {
[`remainTimes.${taskType}`]: 0
},
$set: {}
}
let targetOrders = userInfo?.taskInfo?.[today]?.[taskType] || []
orderResult.forEach(v => {
// @ts-ignore
// 商品订单包含目标商品 且orderId为新订单
if (itemIdsArr.includes(v.itemId) && !taskInfo.some(order => order.orderId === v.orderId)) {
if (targetOrders.length >= times) {
return
}
projection.$inc[`remainTimes.${taskType}`] += +value
targetOrders.push({
itemId: v.itemId,
orderId: v.orderId,
payTime: v.payTime,
tId: v.tId,
createTime: Date.now()
})
}
if (targetOrders?.length) {
projection.$set[`taskInfo.${today}.${taskType}`] = targetOrders
}
})
return projection
}
/**
* 更新每日订单信息,仅适用于下单,如果是每日确认收货,需要单独处理
* @param context
* @param taskType
* @param itemIds
* @param task
* @param times
* @param value
* @param userInfo
*/
async function getEverydayGoods(context: IContext<IParams>, taskType: string, itemIds: string, task: ITask, times: number, value: number, userInfo: IUserInfo): Promise<IPreUpdateQuery> {
const today = getToday()
const TodayTime = new Date(today).getTime()
let startTime = TodayTime > userInfo.createTime ? TodayTime : userInfo.createTime
const orderResult = await getUserOrderlist(
context,
//@ts-ignore
startTime,
Date.now()
)
const itemIdsArr = itemIds.split(',').map(v => +v)
let projection = {
$inc: {
[`remainTimes.${taskType}`]: 0
},
$set: {}
}
let targetOrders = task?.taskInfo?.[today]?.[taskType] || []
let targetLength = targetOrders.length
orderResult.forEach(v => {
// @ts-ignore
// 商品订单包含目标商品 且orderId为新订单
if (itemIdsArr.includes(v.itemId) && !task?.taskInfo.some(order => order.orderId === v.orderId)) {
if (targetOrders.length >= times) {
return
}
projection.$inc[`remainTimes.${taskType}`] += +value
targetOrders.push({
itemId: v.itemId,
orderId: v.orderId,
payTime: v.payTime,
tId: v.tId,
createTime: Date.now()
})
}
})
if (targetOrders?.length && targetLength !== targetOrders?.length) {
const result = await updateGoodsTaskInfo(context, today, taskType, targetOrders)
if (result !== 1) {
// @ts-ignore
return resultsModel.error(CODE_TYPES.SYSTEM_ERROR, '订单任务更新失败')
}
}
return projection
}
/** @format */
import { getToday } from '../getToday'
import { getTodayCompleteGrowTask, updateTaskInfo } from '../task'
export default async function updateSignTask(
context: IContext<IParams>,
{ activityInfo, task }: IControllerInfos
): Promise<IPreUpdateQuery | undefined> {
if (!activityInfo?.tasks?.sign) return
// 获取签到奖励
const { value } = activityInfo?.tasks?.sign || {}
const { todayCompleteTimes } = getTodayCompleteGrowTask('sign', task)
if (todayCompleteTimes === 0) {
const result = await updateTaskInfo(context, task._id, 'sign', { createTime: Date.now() })
return result === 1 ? {
$inc: {
'remainTimes.sign': value || 0
},
} : {}
}
return {}
}
...@@ -27,3 +27,5 @@ export * from './common/userUpdate' ...@@ -27,3 +27,5 @@ export * from './common/userUpdate'
export * from './common/format' export * from './common/format'
export * from './common/doHandler' export * from './common/doHandler'
export * from './common/task'
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