ttk-app-core
Version:
@ttk/recat enterprise develop framework
69 lines (63 loc) • 2.51 kB
JavaScript
// import { getList, getThead, getDisplayDate } from './webapi'
import webapi from './webapi'
import { commit } from '@ttk/app-loader'
import * as validator from '@/utils/validator'
// 定义需要校验的字段和校验规则, 支持同一个字段多条规则和异步校验,详情可参考
// https://github.com/yiminghe/async-validator
const rules = {
phone: [{type: 'string', required: true, message: '请输入正确的手机号', pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/}],
captcha: [{type: 'string', required: true, message: '请输入图形验证码'}],
verifyCode: [{type: 'string', required: true, message: '请输入手机验证码'}],
password: {type: 'string', required: true, message: '请输入密码', dependValidator: (fields, value, cb) => {
if (!fields.confirm) {
cb()
} else if (fields.confirm != value) {
cb('两个密码不一致', 'confirm') // confirm字段显示错误信息,若本字段显示错误信息,则第二个参数'confirm'可不传
} else {
cb('', 'confirm') // 消除confirm可能的校验错误状态
}
}},
confirm: {type: 'string', required: true, message: '请确认密码', dependValidator: (fields, value, cb) => {
if (fields.password !== value) {
return cb('两个密码不一致')
} else {
return cb()
}
}},
remember: {validator: (rule, value, callback) => {
if (value === true) {
return callback()
} else {
callback('请阅读并同意用户注册协议')
}
}},
}
// 初始化表单
export function initForm(reduce, gf, param) {
return async (dispatch, getState) => {
let data = param
if (data.id) {
data = await webapi.attributeForm(param)
}
reduce('attributeForm', { type: 'update', data })
}
}
// 更新表单
export function updateFormObj(reduce, gf, fields) {
return async (dispatch, getState) => {
if (!fields)
fields = gf(['attributeForm'])
// 字段校验。返回一个长度为2的数组,第一个是boolean值,代表是否校验成功,第二个是错误消息状态
const [result, resultObj] = await validator.fieldValidator(fields, rules, gf(['attributeForm']))
// 更新文本域验证状态
reduce('validateState', { type: 'update', data: resultObj })
// 更新表单数据
reduce('attributeForm', { type: 'update', data: fields })
return result
}
}
export function registryAction(reduce, gf, data) {
return async (dispatch, getState) => {
return true
}
}