保存客户与美甲师表单改造检查点
This commit is contained in:
@@ -9,6 +9,7 @@ Page({
|
||||
creating: false,
|
||||
createForm: {
|
||||
nickname: '',
|
||||
wechatId: '',
|
||||
phone: ''
|
||||
}
|
||||
},
|
||||
@@ -72,6 +73,7 @@ Page({
|
||||
this.setData({
|
||||
createForm: {
|
||||
nickname: '',
|
||||
wechatId: '',
|
||||
phone: ''
|
||||
}
|
||||
})
|
||||
@@ -80,18 +82,19 @@ Page({
|
||||
async submitCustomer() {
|
||||
const form = this.data.createForm
|
||||
const nickname = String(form.nickname || '').trim()
|
||||
const wechatId = String(form.wechatId || '').trim()
|
||||
const phone = String(form.phone || '').trim()
|
||||
if (!nickname || !phone) {
|
||||
wx.showToast({ title: '请填写客户姓名和手机号', icon: 'none' })
|
||||
if (!nickname) {
|
||||
wx.showToast({ title: '请填写客户姓名', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (!/^1[3-9]\d{9}$/.test(phone)) {
|
||||
if (phone && !/^1[3-9]\d{9}$/.test(phone)) {
|
||||
wx.showToast({ title: '请输入正确手机号', icon: 'none' })
|
||||
return
|
||||
}
|
||||
this.setData({ creating: true })
|
||||
try {
|
||||
await customersApi.create({ nickname, phone })
|
||||
await customersApi.create({ nickname, wechatId: wechatId || undefined, phone: phone || undefined })
|
||||
wx.showToast({ title: '客户已新增', icon: 'success' })
|
||||
this.resetCreateForm()
|
||||
this.setData({ createModalVisible: false })
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
const assert = require('assert')
|
||||
const customersApi = require('../../api/customers')
|
||||
|
||||
let definition
|
||||
global.Page = value => { definition = value }
|
||||
global.wx = {
|
||||
showToast: () => {},
|
||||
navigateTo: () => {}
|
||||
}
|
||||
require('./index')
|
||||
|
||||
function createPage() {
|
||||
return {
|
||||
...definition,
|
||||
data: JSON.parse(JSON.stringify(definition.data)),
|
||||
setData(values) {
|
||||
Object.entries(values).forEach(([key, value]) => {
|
||||
const parts = key.split('.')
|
||||
let target = this.data
|
||||
for (let index = 0; index < parts.length - 1; index += 1) target = target[parts[index]]
|
||||
target[parts[parts.length - 1]] = value
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function run() {
|
||||
const page = createPage()
|
||||
let submitted
|
||||
let toast
|
||||
global.wx.showToast = options => { toast = options.title }
|
||||
customersApi.create = async value => { submitted = value }
|
||||
customersApi.list = async () => ({ list: [] })
|
||||
|
||||
page.setData({ 'createForm.nickname': '小周', 'createForm.wechatId': 'wx_zhou' })
|
||||
await page.submitCustomer()
|
||||
assert.deepStrictEqual(submitted, { nickname: '小周', wechatId: 'wx_zhou', phone: undefined })
|
||||
|
||||
submitted = undefined
|
||||
page.setData({ 'createForm.nickname': '', 'createForm.phone': '' })
|
||||
await page.submitCustomer()
|
||||
assert.strictEqual(submitted, undefined)
|
||||
assert.strictEqual(toast, '请填写客户姓名')
|
||||
}
|
||||
|
||||
run().catch(error => {
|
||||
console.error(error)
|
||||
process.exitCode = 1
|
||||
})
|
||||
@@ -34,8 +34,12 @@
|
||||
<input value="{{createForm.nickname}}" data-field="nickname" bindinput="onCreateInput" placeholder="请输入客户姓名" />
|
||||
</view>
|
||||
<view class="modal-field">
|
||||
<text class="required-label">手机号</text>
|
||||
<input value="{{createForm.phone}}" data-field="phone" bindinput="onCreateInput" type="number" maxlength="11" placeholder="请输入手机号" />
|
||||
<text>微信号</text>
|
||||
<input value="{{createForm.wechatId}}" data-field="wechatId" bindinput="onCreateInput" placeholder="选填" />
|
||||
</view>
|
||||
<view class="modal-field">
|
||||
<text>手机号</text>
|
||||
<input value="{{createForm.phone}}" data-field="phone" bindinput="onCreateInput" type="number" maxlength="11" placeholder="选填" />
|
||||
</view>
|
||||
</view>
|
||||
<button class="primary-btn modal-submit" loading="{{creating}}" bindtap="submitCustomer">保存客户</button>
|
||||
|
||||
Reference in New Issue
Block a user