Commit 6444ac45 authored by qinhaitao's avatar qinhaitao

refactor: 💡 统一字段

parent 43e53752
......@@ -28,11 +28,11 @@ Component({
<%- !taskKeys.includes(configList[i].key) && configList[i].key !== 'label' ? `${configList[i].key}: ${configList[i].defaultValue || "''"},` : '' %><% } %>
tasks: {
// 关注店铺
attentionStore: {
follow: {
value: '',
},
// 成为会员
beMembership: {
member: {
value: '',
},
// 签到
......@@ -45,7 +45,7 @@ Component({
times: '',
},
// 邀请好友
inviteFriends: {
invites: {
title: '', // 任务标题
taskRateType: 1, // 任务频率类型
times: 3, // 任务为每日限次次数值
......
......@@ -81,17 +81,17 @@
<form-item
class="edit-content-form-item"
label="成为会员"
validateState="{{formState['taskMap.beMembership'].status}}"
help="{{formState['taskMap.beMembership'].message}}"
validateState="{{formState['taskMap.member'].status}}"
help="{{formState['taskMap.member'].message}}"
asterisk="{{false}}">
<view class="form-flex">
<checkbox class="form-check" checked="{{taskMap.beMembership.checked}}" data-name="taskMap.beMembership.checked" onChange="onCheckChange"></checkbox>
<checkbox class="form-check" checked="{{taskMap.member.checked}}" data-name="taskMap.member.checked" onChange="onCheckChange"></checkbox>
<tb-config-input
textBefore="获得"
textAfter="体力值"
validateRange="{{[1, 99]}}"
value="{{taskMap.beMembership.value}}"
dataName="taskMap.beMembership.value"
value="{{taskMap.member.value}}"
dataName="taskMap.member.value"
onChange="onTaskInputChange",
placeholder="1-99"
/>
......@@ -101,16 +101,16 @@
<form-item
class="edit-content-form-item"
label="邀请好友"
validateState="{{formState['taskMap.inviteFriends'].status}}"
help="{{formState['taskMap.inviteFriends'].message}}"
validateState="{{formState['taskMap.invites'].status}}"
help="{{formState['taskMap.invites'].message}}"
asterisk="{{false}}">
<view class="form-flex">
<checkbox class="form-check" checked="{{taskMap.inviteFriends.checked}}" data-name="taskMap.inviteFriends.checked" onChange="onCheckChange"></checkbox>
<checkbox class="form-check" checked="{{taskMap.invites.checked}}" data-name="taskMap.invites.checked" onChange="onCheckChange"></checkbox>
<task-config
unit="体力值"
type="inviteFriends"
taskData="{{taskMap.inviteFriends}}"
dataName="taskMap.inviteFriends"
type="invites"
taskData="{{taskMap.invites}}"
dataName="taskMap.invites"
onUpdate="onTaskChange"
/>
</view>
......
import moment from 'moment'
import schema from 'async-validator';
import { descriptor, formatValidator } from './validate';
import {
getActivityDetail,
saveActivityInfo,
generateRule
} from '/api'
import {
addFloat
} from '/utils/helper'
import moment from "moment";
import schema from "async-validator";
import { descriptor, formatValidator } from "./validate";
import { getActivityDetail, saveActivityInfo, generateRule } from "/api";
import { addFloat } from "/utils/helper";
const THANKS_TYPE = 5;
const STOCK_LIMIT = 1;
const STOCK_NO_LIMIT = 2;
var validator = new schema(descriptor);
Component({
data: {
labelCol: { fixedSpan: 5 },
id: '',
title: '',
subtitle: '',
startTime: '',
endTime: '',
id: "",
title: "",
subtitle: "",
startTime: "",
endTime: "",
timeRange: [],
rule: '',
rule: "",
prizeInfoList: [
{
ename: "",
......@@ -37,7 +30,7 @@ Component({
name: "",
switchStock: STOCK_NO_LIMIT,
useStock: 0,
rank: "1"
rank: "1",
},
{
ename: "",
......@@ -49,7 +42,7 @@ Component({
name: "",
switchStock: STOCK_NO_LIMIT,
useStock: 0,
rank: "2"
rank: "2",
},
{
ename: "",
......@@ -61,20 +54,20 @@ Component({
name: "",
switchStock: STOCK_NO_LIMIT,
useStock: 0,
rank: "3"
}
rank: "3",
},
],
commandTitle: '',
commandImg: '',
logoImg: '',
beenInvitedText: '',
commandTitle: "",
commandImg: "",
logoImg: "",
beenInvitedText: "",
taskList: [
// {
// type: "beMembership",//会员
// type: "member",//会员
// value: 20,//完成任务获得值
// },
// {
// type: "attentionStore",
// type: "follow",
// value: 20,
// },
// {
......@@ -87,7 +80,7 @@ Component({
// times: 1,
// },
// {
// type: "inviteFriends",
// type: "invites",
// title: "标题",//任务标题
// taskRateType: 1,//任务频率类型
// times: 3,//任务为每日限次次数值
......@@ -128,12 +121,12 @@ Component({
],
taskMap: {
// attentionStore: {
// follow: {
// value: '',
// },
beMembership: {
value: '',
checked: false
member: {
value: "",
checked: false,
},
// sign: {
// value: ''
......@@ -142,21 +135,21 @@ Component({
// value: '',
// times: '',
// },
inviteFriends: {
title: '', // 任务标题
invites: {
title: "", // 任务标题
taskRateType: 1, // 任务频率类型
times: 3, // 任务为每日限次次数值
value: '', // 任务奖励
checked: false
value: "", // 任务奖励
checked: false,
},
browseGoods: {
title: "寻宝屋商品",
taskRateType: 1,
times: 3,
value: '',
link: '',
itemIds: '',
checked: true
value: "",
link: "",
itemIds: "",
checked: true,
},
// collectGoods: {
// title: "",
......@@ -168,32 +161,34 @@ Component({
jumpLink: {
title: "",
taskRateType: 3,
link: "",//跳转链接
link: "", //跳转链接
times: 3,
value: '',
checked: false
value: "",
checked: false,
},
},
image: '',
image: "",
isEnd: false,
isStart: false,
originalStartTime: '',
originalStartTime: "",
prizeDialogData: {},
formState: formatValidator(descriptor),
isPostParams: true
isPostParams: true,
},
props: {},
didMount() {
const { id } = this.$page.$router.params
const { id } = this.$page.$router.params;
if (id) {
this.getActivityInfo(id)
this.getActivityInfo(id);
}
},
methods: {
// 获取id活动信息
async getActivityInfo(activityId) {
try {
const { success, data, message } = await getActivityDetail({ activityId })
const { success, data, message } = await getActivityDetail({
activityId,
});
if (!success) {
this.showFailToast(message);
return;
......@@ -207,49 +202,51 @@ Component({
originalStartTime: +rest.startTime,
isStart: +rest.startTime < Date.now(),
isEnd: +rest.endTime < Date.now(),
prizeInfoList: prizeInfoList && prizeInfoList.length > 0 ? prizeInfoList : this.data.prizeInfoList,
prizeInfoList:
prizeInfoList && prizeInfoList.length > 0
? prizeInfoList
: this.data.prizeInfoList,
taskList: rest.taskList,
taskMap: this.formatTaskListToMap(rest.taskList)
})
taskMap: this.formatTaskListToMap(rest.taskList),
});
} catch (error) {
console.log(error, 'err')
console.log(error, "err");
}
},
onChange(e) {
const { value } = e.detail;
const { name } = e.target.dataset;
this.setData({
[name]: value
})
[name]: value,
});
},
onRuleChange(rule) {
this.setData({
rule
})
rule,
});
},
setDataByKey(name, val) {
const keys = name.split('.');
const keys = name.split(".");
if (keys.length === 1) {
this.setData({
[keys[0]]: val
})
[keys[0]]: val,
});
}
if (keys.length === 2) {
let targetKey = this.data[keys[0]];
targetKey[keys[1]] = val;
let newVal = Object.assign({}, targetKey)
let newVal = Object.assign({}, targetKey);
this.setData({
[keys[0]]: newVal
})
[keys[0]]: newVal,
});
}
if (keys.length === 3) {
let targetKey = this.data[keys[0]];
targetKey[keys[1]][keys[2]] = val;
let newVal = Object.assign({}, targetKey)
let newVal = Object.assign({}, targetKey);
this.setData({
[keys[0]]: newVal
})
[keys[0]]: newVal,
});
}
},
onTaskInputChange(e) {
......@@ -263,14 +260,14 @@ Component({
this.setDataByKey(name, value);
},
onTaskChange(data, key) {
console.log(data, key)
console.log(data, key);
this.setDataByKey(key, data);
},
onImageChange(image) {
console.log('image', image)
console.log("image", image);
this.setData({
image
})
image,
});
},
onTimeChange(timeRange, error) {
const { formState } = this.data;
......@@ -282,27 +279,26 @@ Component({
formState: {
...formState,
timeRange: {
status: error ? 'error' : 'success',
message: error || ''
}
}
})
status: error ? "error" : "success",
message: error || "",
},
},
});
},
onPrizeListChange(prizeInfoList) {
console.log('prizeInfoList', prizeInfoList);
console.log("prizeInfoList", prizeInfoList);
this.setData({
prizeInfoList
})
prizeInfoList,
});
},
validateForm(data, target) {
const _this = this;
return new Promise((resolve, reject) => {
validator.validate(data, (errors, fields) => {
console.log('errors', errors);
console.log('fields', fields);
console.log("errors", errors);
console.log("fields", fields);
const { formState } = _this.data;
_this.setFormTips(formState, errors, target)
_this.setFormTips(formState, errors, target);
if (errors) {
resolve(false);
} else {
......@@ -311,52 +307,53 @@ Component({
// validation passed
});
})
});
},
setFormTips(formState, errors, target) {
errors = errors || []
errors = errors || [];
let newValidator = {};
Object.keys(formState).forEach(key => {
let error = errors.filter(v => target ? v.field === target && v.field === key : v.field === key).length && errors.filter(v => v.field === key)[0];
Object.keys(formState).forEach((key) => {
let error =
errors.filter((v) =>
target ? v.field === target && v.field === key : v.field === key
).length && errors.filter((v) => v.field === key)[0];
newValidator[key] = {
status: error ? 'error' : 'success',
message: error ? error.message : ''
}
})
status: error ? "error" : "success",
message: error ? error.message : "",
};
});
console.log('newValidator', newValidator)
console.log("newValidator", newValidator);
this.setData({
formState: newValidator
})
formState: newValidator,
});
},
// 提交信息
async onSubmit() {
const isValidForm = await this.validateForm(this.data);
// console.log(this.formatActivityParams(this.data))
if (!isValidForm) return;
console.log('成功')
console.log("成功");
const params = this.formatActivityParams(this.data)
const params = this.formatActivityParams(this.data);
console.log(params)
console.log(params);
if (this.data.isPostParams) {
this.setData({
isPostParams: false
})
isPostParams: false,
});
const { success, message } = await saveActivityInfo(params);
if (success) {
this.backList();
this.setData({
isPostParams: true
})
isPostParams: true,
});
} else {
this.showFailToast(message);
}
......@@ -364,15 +361,15 @@ Component({
},
showFailToast(text) {
my.showToast({
type: 'fail',
content: text
})
type: "fail",
content: text,
});
},
// 格式化参数
formatActivityParams(params) {
const {
id = '',
activityId = '',
id = "",
activityId = "",
startTime,
endTime,
rule,
......@@ -387,7 +384,6 @@ Component({
beenInvitedText,
} = params;
return {
id,
activityId,
......@@ -401,131 +397,127 @@ Component({
taskList: this.formatTaskMapToList(taskMap),
commandTitle,
commandImg,
beenInvitedText
}
beenInvitedText,
};
},
// taskMap转化taskList
formatTaskMapToList(taskMap) {
let list = [];
Object.keys(taskMap).forEach(type => {
Object.keys(taskMap).forEach((type) => {
if (!taskMap[type].checked) return;
let task = {
...taskMap[type],
type
}
list.push(task)
type,
};
list.push(task);
});
return list;
},
// taskList转化taskMap
formatTaskListToMap(taskList = []) {
let taskMap = {};
taskList.forEach(task => {
taskMap[task['type']] = task;
taskList.forEach((task) => {
taskMap[task["type"]] = task;
});
return taskMap
return taskMap;
},
// 补足谢谢参与类型
formatPobalityPrizeList(prizeInfoList = []) {
let totalPercent = prizeInfoList.reduce((total, next) => {
return total = addFloat(total, +next.probability)
}, 0)
return (total = addFloat(total, +next.probability));
}, 0);
// 排名类型不含谢谢参与
if (prizeInfoList[0].rank) return prizeInfoList;
let prizeInfoListCopy = prizeInfoList.concat().map((v, index) => ({ ...v, level: index }));
let prizeInfoListCopy = prizeInfoList
.concat()
.map((v, index) => ({ ...v, level: index }));
if (totalPercent < 100) {
let thanksType = {
type: THANKS_TYPE,
name: '谢谢参与',
probability: 100 - totalPercent
name: "谢谢参与",
probability: 100 - totalPercent,
};
prizeInfoListCopy.push(thanksType);
}
prizeInfoListCopy.push(thanksType)
}
return prizeInfoListCopy
return prizeInfoListCopy;
},
cancelEdit() {
this.backList()
this.backList();
},
onInputChange(e) {
let { detail: { value },
currentTarget: { dataset }
} = e
let { name } = dataset
this.setData({ [name]: value })
let {
detail: { value },
currentTarget: { dataset },
} = e;
let { name } = dataset;
this.setData({ [name]: value });
},
onChangeByDataName(val, dataName) {
this.setDataByKey(dataName, val)
this.setDataByKey(dataName, val);
},
// 选择奖品弹窗
handlerShowPrize(evt) {
const {
prizeInfoList
} = this.data
let {
name
} = evt.target.dataset
if (name === 'edit') {
const { prizeInfoList } = this.data;
let { name } = evt.target.dataset;
if (name === "edit") {
this.setData({
showPrize: true,
prizeDialogData: evt.target.dataset.x,
prizeDialogEdit: true,
hasEditPrize: true
})
hasEditPrize: true,
});
} else {
if (prizeInfoList.length >= 20) {
this.showFailToast('最多创建20个奖励配置')
return false
this.showFailToast("最多创建20个奖励配置");
return false;
}
this.setData({
showPrize: true,
prizeDialogData: {},
prizeDialogEdit: false,
hasEditPrize: false
})
hasEditPrize: false,
});
}
},
//生成规则
generateRule() {
const {
title,
startTime,
endTime,
prizeInfoList
} = this.data;
const { title, startTime, endTime, prizeInfoList } = this.data;
if (title && startTime && endTime && prizeInfoList.length) {
let bool = true;
prizeInfoList.forEach(i => {
prizeInfoList.forEach((i) => {
if (!i.image || !i.name) {
bool = false
bool = false;
}
})
});
if (!bool) {
this.showFailToast('请检查奖品配置是否正确')
return
this.showFailToast("请检查奖品配置是否正确");
return;
}
console.log({
console.log(
{
title,
startTime: startTime,
endTime: endTime,
prizeInfoList
}, 'time');
prizeInfoList,
},
"time"
);
generateRule({
title,
startTime: new Date(startTime).getTime(),
endTime: new Date(endTime).getTime(),
prizeInfoList
}).then(res => {
prizeInfoList,
}).then((res) => {
if (res.success) {
this.setData({
rule: res.data,
hasEditChangePrize: false
})
hasEditChangePrize: false,
});
}
})
});
} else {
this.showFailToast('请填写完整信息')
this.showFailToast("请填写完整信息");
}
},
......@@ -533,29 +525,29 @@ Component({
switch (data) {
case "group":
this.setData({
showGroup: false
})
return
showGroup: false,
});
return;
case "prize":
this.setData({
showPrize: false,
prizeDialogData: {}
})
return
prizeDialogData: {},
});
return;
case "baby":
this.setData({
showBaby: false
})
return
showBaby: false,
});
return;
case "link":
this.setData({
showLink: false
})
return
showLink: false,
});
return;
}
},
backList() {
this.$page.$router.push("/activity/list");
}
},
})
\ No newline at end of file
},
});
import { TASK_RATE_TYPE } from '../const'
import flatten from 'lodash/flatten'
import { TASK_RATE_TYPE } from "../const";
import flatten from "lodash/flatten";
import { rankTableValidator } from "../../../utils/validate";
// 使用 async-validator 校验
......@@ -9,22 +9,22 @@ export const descriptor = {
required: true,
validator: (rule, value) => {
value = value.trim();
return !!value && value.length <= 12
return !!value && value.length <= 12;
},
message: "请输入正确的活动名称"
message: "请输入正确的活动名称",
},
subtitle: {
required: true,
validator: (rule, value) => {
value = value.trim();
return !!value && value.length <= 16
return !!value && value.length <= 16;
},
message: "请输入正确的活动副标题"
message: "请输入正确的活动副标题",
},
prizeInfoList: {
required: true,
validator: rankTableValidator
validator: rankTableValidator,
},
//活动的开始时间和结束时间校验判断
timeRange: {
......@@ -45,7 +45,7 @@ export const descriptor = {
},
logoImg: {
required: true,
message: '请配置图片'
message: "请配置图片",
},
commandTitle: {
required: true,
......@@ -53,7 +53,7 @@ export const descriptor = {
value = value.trim();
return !!value;
},
message: '请输入淘口令名称'
message: "请输入淘口令名称",
},
beenInvitedText: {
required: true,
......@@ -61,11 +61,11 @@ export const descriptor = {
value = value.trim();
return !!value;
},
message: '请输入被邀请者文案'
message: "请输入被邀请者文案",
},
commandImg: {
required: true,
message: '请输入淘口令图片'
message: "请输入淘口令图片",
},
rule: {
required: true,
......@@ -73,62 +73,59 @@ export const descriptor = {
value = value.trim();
return !!value;
},
message: '请输入活动规则'
message: "请输入活动规则",
},
taskMap: {
type: 'object',
type: "object",
required: true,
fields: {
beMembership: {
member: {
validator: (rule, val, cb, source) => {
if(!source.beMembership.checked) return true;
return !!val.value
if (!source.member.checked) return true;
return !!val.value;
},
message: '请输入成为会员任务的奖励'
message: "请输入成为会员任务的奖励",
},
inviteFriends: {
invites: {
validator: (rule, val, cb, source) => {
if(!source.inviteFriends.checked) return true;
return !!(val.title && val.value)
if (!source.invites.checked) return true;
return !!(val.title && val.value);
},
message: '请输入邀请任务'
message: "请输入邀请任务",
},
jumpLink: {
validator: (rule, val, cb, source) => {
if(!source.jumpLink.checked) return true;
if (!source.jumpLink.checked) return true;
return !!(val.title && val.value && val.link);
},
message: '请输入浏览指定页面任务'
message: "请输入浏览指定页面任务",
},
browseGoods: {
validator: (rule, val) => {
return !!(val.itemIds);
return !!val.itemIds;
},
message: "请输入寻宝屋商品任务",
},
},
},
message: '请输入寻宝屋商品任务'
}
}
}
};
export const formatValidator = (descriptor, validators) => {
validators = validators || {}
Object.keys(descriptor).forEach(key => {
if(descriptor[key].fields) {
Object.keys(descriptor[key].fields).forEach(fieldKey => {
validators = validators || {};
Object.keys(descriptor).forEach((key) => {
if (descriptor[key].fields) {
Object.keys(descriptor[key].fields).forEach((fieldKey) => {
validators[`${key}.${fieldKey}`] = {
status: 'success',
message: ''
}
})
status: "success",
message: "",
};
});
} else {
validators[key] = {
status: 'success',
message: ''
status: "success",
message: "",
};
}
}
});
return validators
}
\ No newline at end of file
return validators;
};
......@@ -29,9 +29,8 @@ const DRAW_STATUS = {
//库存类型
const STOCK_TYPE = {
LIMIT_STOCK: 1,
NO_LIMIT_STOCK: 2
}
NO_LIMIT_STOCK: 2,
};
// 日志类型: error,info
const LOGGER_TYPE = {
......@@ -42,8 +41,8 @@ const LOGGER_TYPE = {
const SHIP_STATUS = {
NO_ADDRESS: 1,
NO_SHIP: 2,
HAS_SHIP: 3
}
HAS_SHIP: 3,
};
const PRIZE_TYPE = {
EQUITY: 1,
......@@ -55,8 +54,8 @@ const PRIZE_TYPE = {
const PRIZE_DATA_TYPR = {
RANK_PRIZE: 1,
PRO_PRIZE: 2,
BUILD_PRIZE: 3
}
BUILD_PRIZE: 3,
};
// code类型
const CODE_TYPES = {
PARAMS_ERROR: {
......@@ -95,11 +94,11 @@ const TEMPLATE_INFO = {
};
const TASK_TYPE_CHINA = {
beMembership: "成为会员",
attentionStore: "关注店铺",
member: "成为会员",
follow: "关注店铺",
sign: "签到",
exchangeCredits: "兑换积分",
inviteFriends: "邀请好友",
invites: "邀请好友",
orderGoods: "下单商品",
browseGoods: "浏览商品",
jumpLink: "跳转链接",
......@@ -108,11 +107,11 @@ const TASK_TYPE_CHINA = {
const TASK_CHECK_TYPE = {
value: {
list: [
"beMembership",
"attentionStore",
"member",
"follow",
"sign",
"exchangeCredits",
"inviteFriends",
"invites",
"orderGoods",
"jumpLink",
"collectGoods",
......@@ -123,7 +122,7 @@ const TASK_CHECK_TYPE = {
times: {
list: [
"exchangeCredits",
"inviteFriends",
"invites",
"orderGoods",
"browseGoods",
"jumpLink",
......@@ -131,19 +130,13 @@ const TASK_CHECK_TYPE = {
],
},
title: {
list: [
"inviteFriends",
"orderGoods",
"browseGoods",
"jumpLink",
"collectGoods",
],
list: ["invites", "orderGoods", "browseGoods", "jumpLink", "collectGoods"],
},
itemIds: {
list: ["orderGoods", "browseGoods", "collectGoods"],
},
taskRateType: {
list: ["inviteFriends", "orderGoods", "browseGoods", "collectGoods"],
list: ["invites", "orderGoods", "browseGoods", "collectGoods"],
},
link: { list: ["jumpLink"] },
};
......@@ -171,5 +164,5 @@ module.exports = {
PRIZE_TYPE,
SHIP_STATUS,
STOCK_TYPE,
PRIZE_DATA_TYPR
PRIZE_DATA_TYPR,
};
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