优化:统一小程序业务引用选择交互
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
const employeesApi = require('../../api/employees')
|
||||
const { chooseImageFilePath, uploadObjectFile } = require('../../utils/upload')
|
||||
|
||||
const DEFAULT_EMPLOYEE_PASSWORD = 'Fingertip@123'
|
||||
|
||||
Page({
|
||||
data: {
|
||||
pickerMode: false,
|
||||
form: {
|
||||
nickname: '',
|
||||
phone: '',
|
||||
account: '',
|
||||
password: DEFAULT_EMPLOYEE_PASSWORD,
|
||||
wechatId: '',
|
||||
avatar: '',
|
||||
avatarUrl: ''
|
||||
},
|
||||
avatarUploading: false,
|
||||
creating: false
|
||||
},
|
||||
|
||||
onLoad(options) {
|
||||
this.setData({ pickerMode: options && options.picker === '1' })
|
||||
},
|
||||
|
||||
onInput(event) {
|
||||
const field = event.currentTarget.dataset.field
|
||||
const value = event.detail.value
|
||||
const updates = { [`form.${field}`]: value }
|
||||
if (field === 'phone' && !this.data.form.account) updates['form.account'] = value
|
||||
this.setData(updates)
|
||||
},
|
||||
|
||||
async chooseAvatar() {
|
||||
if (this.data.avatarUploading || this.data.creating) return
|
||||
try {
|
||||
const filePath = await chooseImageFilePath()
|
||||
if (!filePath) return
|
||||
this.setData({ avatarUploading: true })
|
||||
const uploadResult = await uploadObjectFile(filePath)
|
||||
const avatarUrl = uploadResult.fileUrl || uploadResult.primaryUrl || filePath
|
||||
this.setData({
|
||||
'form.avatar': avatarUrl,
|
||||
'form.avatarUrl': avatarUrl
|
||||
})
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '头像上传失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ avatarUploading: false })
|
||||
}
|
||||
},
|
||||
|
||||
removeAvatar() {
|
||||
this.setData({ 'form.avatar': '', 'form.avatarUrl': '' })
|
||||
},
|
||||
|
||||
async submit() {
|
||||
const form = this.data.form
|
||||
const nickname = String(form.nickname || '').trim()
|
||||
const phone = String(form.phone || '').trim()
|
||||
const account = String(form.account || '').trim()
|
||||
const password = String(form.password || '').trim()
|
||||
const wechatId = String(form.wechatId || '').trim()
|
||||
if (!nickname || !phone || !account || !password || !wechatId) {
|
||||
wx.showToast({ title: '请补全员工信息', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (!/^1[3-9]\d{9}$/.test(phone)) {
|
||||
wx.showToast({ title: '请输入正确手机号', icon: 'none' })
|
||||
return
|
||||
}
|
||||
this.setData({ creating: true })
|
||||
try {
|
||||
const result = await employeesApi.createUser({
|
||||
nickname,
|
||||
phone,
|
||||
contactPhone: phone,
|
||||
account,
|
||||
password,
|
||||
wechatId,
|
||||
avatar: form.avatar || undefined,
|
||||
type: 3
|
||||
})
|
||||
const id = result && typeof result === 'object' ? (result.id || result.userId) : result
|
||||
const employee = { id, name: nickname, title: nickname, avatar: form.avatar || '' }
|
||||
const eventChannel = this.getOpenerEventChannel && this.getOpenerEventChannel()
|
||||
if (eventChannel && eventChannel.emit) eventChannel.emit('employeeCreated', employee)
|
||||
wx.showToast({ title: '员工已新增', icon: 'success' })
|
||||
setTimeout(() => wx.navigateBack(), 450)
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '新增失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ creating: false })
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"navigationBarTitleText": "新增员工"
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
const assert = require('assert')
|
||||
const employeesApi = require('../../api/employees')
|
||||
|
||||
let navigatedBack = false
|
||||
global.wx = {
|
||||
getAccountInfoSync: () => ({ miniProgram: { envVersion: 'develop' } }),
|
||||
getDeviceInfo: () => ({ platform: 'devtools' }),
|
||||
showToast: () => {},
|
||||
navigateBack: () => { navigatedBack = true }
|
||||
}
|
||||
global.setTimeout = callback => { callback(); return 1 }
|
||||
|
||||
let definition
|
||||
global.Page = value => { definition = value }
|
||||
require('./index')
|
||||
|
||||
let emitted
|
||||
const page = {
|
||||
...definition,
|
||||
data: JSON.parse(JSON.stringify(definition.data)),
|
||||
getOpenerEventChannel: () => ({ emit: (name, value) => { emitted = { name, value } } }),
|
||||
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() {
|
||||
page.onInput({ currentTarget: { dataset: { field: 'phone' } }, detail: { value: '13800138000' } })
|
||||
assert.strictEqual(page.data.form.account, '13800138000')
|
||||
Object.assign(page.data.form, {
|
||||
nickname: '小周',
|
||||
phone: '13800138000',
|
||||
account: '13800138000',
|
||||
password: 'Fingertip@123',
|
||||
wechatId: 'wx_zhou'
|
||||
})
|
||||
employeesApi.createUser = async () => 88
|
||||
await page.submit()
|
||||
assert.strictEqual(emitted.name, 'employeeCreated')
|
||||
assert.deepStrictEqual(emitted.value, { id: 88, name: '小周', title: '小周', avatar: '' })
|
||||
assert.strictEqual(navigatedBack, true)
|
||||
}
|
||||
|
||||
run().catch(error => {
|
||||
console.error(error)
|
||||
process.exitCode = 1
|
||||
})
|
||||
@@ -0,0 +1,44 @@
|
||||
<view class="page no-tab employee-create-page">
|
||||
<view class="create-intro">
|
||||
<text class="create-title">录入员工账号</text>
|
||||
<text class="create-subtitle">{{pickerMode ? '保存后自动选择该员工' : '保存后即可用于预约指定和员工管理'}}</text>
|
||||
</view>
|
||||
|
||||
<view class="card avatar-card" bindtap="chooseAvatar">
|
||||
<image wx:if="{{form.avatarUrl}}" class="avatar-preview" src="{{form.avatarUrl}}" mode="aspectFill"></image>
|
||||
<image wx:else class="avatar-preview" src="/assets/images/default-user-avatar.png" mode="aspectFit"></image>
|
||||
<view class="avatar-copy">
|
||||
<text class="avatar-title">员工头像</text>
|
||||
<text class="avatar-tip">点击选择图片,可稍后补充</text>
|
||||
</view>
|
||||
<text class="avatar-action">{{avatarUploading ? '上传中' : '更换'}}</text>
|
||||
</view>
|
||||
<button wx:if="{{form.avatarUrl}}" class="remove-avatar" catchtap="removeAvatar">移除当前头像</button>
|
||||
|
||||
<view class="card form-card create-form">
|
||||
<view class="field">
|
||||
<text class="field-label required">姓名</text>
|
||||
<input value="{{form.nickname}}" data-field="nickname" bindinput="onInput" placeholder="请输入员工姓名" />
|
||||
</view>
|
||||
<view class="field">
|
||||
<text class="field-label required">手机号</text>
|
||||
<input value="{{form.phone}}" data-field="phone" bindinput="onInput" type="number" maxlength="11" placeholder="请输入手机号" />
|
||||
</view>
|
||||
<view class="field">
|
||||
<text class="field-label required">登录账号</text>
|
||||
<input value="{{form.account}}" data-field="account" bindinput="onInput" placeholder="默认跟随手机号" />
|
||||
</view>
|
||||
<view class="field">
|
||||
<text class="field-label required">初始密码</text>
|
||||
<input value="{{form.password}}" data-field="password" bindinput="onInput" password placeholder="请输入初始密码" />
|
||||
</view>
|
||||
<view class="field">
|
||||
<text class="field-label required">微信号</text>
|
||||
<input value="{{form.wechatId}}" data-field="wechatId" bindinput="onInput" placeholder="请输入微信号" />
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="fixed-bottom">
|
||||
<button class="primary-btn" loading="{{creating}}" disabled="{{avatarUploading || creating}}" bindtap="submit">保存员工</button>
|
||||
</view>
|
||||
</view>
|
||||
@@ -0,0 +1,81 @@
|
||||
.employee-create-page {
|
||||
padding-top: 28rpx;
|
||||
}
|
||||
|
||||
.create-intro {
|
||||
margin: 0 6rpx 24rpx;
|
||||
}
|
||||
|
||||
.create-title,
|
||||
.create-subtitle,
|
||||
.avatar-title,
|
||||
.avatar-tip {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.create-title {
|
||||
color: #29241f;
|
||||
font-size: 38rpx;
|
||||
font-weight: 900;
|
||||
}
|
||||
|
||||
.create-subtitle {
|
||||
margin-top: 10rpx;
|
||||
color: #8b837b;
|
||||
font-size: 23rpx;
|
||||
}
|
||||
|
||||
.avatar-card {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 24rpx;
|
||||
}
|
||||
|
||||
.avatar-preview {
|
||||
width: 96rpx;
|
||||
height: 96rpx;
|
||||
flex: 0 0 auto;
|
||||
border-radius: 50%;
|
||||
background: #eee9e2;
|
||||
}
|
||||
|
||||
.avatar-copy {
|
||||
min-width: 0;
|
||||
flex: 1;
|
||||
margin-left: 20rpx;
|
||||
}
|
||||
|
||||
.avatar-title {
|
||||
color: #302a25;
|
||||
font-size: 28rpx;
|
||||
font-weight: 900;
|
||||
}
|
||||
|
||||
.avatar-tip {
|
||||
margin-top: 8rpx;
|
||||
color: #948c83;
|
||||
font-size: 22rpx;
|
||||
}
|
||||
|
||||
.avatar-action {
|
||||
color: #2f6f62;
|
||||
font-size: 24rpx;
|
||||
font-weight: 800;
|
||||
}
|
||||
|
||||
.remove-avatar {
|
||||
width: auto;
|
||||
min-height: 58rpx;
|
||||
margin: 8rpx 8rpx 10rpx auto;
|
||||
padding: 0 12rpx;
|
||||
color: #a8554e;
|
||||
font-size: 22rpx;
|
||||
}
|
||||
|
||||
.create-form {
|
||||
margin-top: 22rpx;
|
||||
}
|
||||
|
||||
.create-form .field-label {
|
||||
width: 176rpx;
|
||||
}
|
||||
Reference in New Issue
Block a user