| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- <template>
- <view class="page-add-relation">
- <view class="form-card">
- <!-- Relation Type -->
- <view class="form-group">
- <text class="form-label">关系类型 *</text>
- <view class="type-grid">
- <view
- v-for="opt in relationTypeOptions"
- :key="opt.value"
- class="type-chip"
- :class="{ active: form.relationType === opt.value }"
- @click="form.relationType = opt.value"
- >
- <text class="chip-text">{{ opt.label }}</text>
- </view>
- </view>
- </view>
- <!-- Name -->
- <view class="form-group">
- <text class="form-label">姓名</text>
- <input
- v-model="form.name"
- class="form-input"
- placeholder="请输入姓名"
- placeholder-class="input-placeholder"
- maxlength="20"
- />
- </view>
- <!-- Birth Date -->
- <view class="form-group">
- <text class="form-label">生日 *</text>
- <picker mode="date" :value="form.birthDate" @change="onBirthDateChange">
- <view class="form-input picker-trigger">
- <text :class="{ 'text-placeholder': !form.birthDate }">
- {{ form.birthDate || '请选择出生日期' }}
- </text>
- </view>
- </picker>
- </view>
- <!-- Gender -->
- <view class="form-group">
- <text class="form-label">性别</text>
- <view class="gender-row">
- <view
- v-for="opt in genderOptions"
- :key="opt.value"
- class="gender-option"
- :class="{ active: form.gender === opt.value }"
- @click="form.gender = opt.value"
- >
- <text class="gender-text">{{ opt.label }}</text>
- </view>
- </view>
- </view>
- <!-- Note -->
- <view class="form-group">
- <text class="form-label">备注</text>
- <input
- v-model="form.note"
- class="form-input"
- placeholder="选填,如:工作中的合作伙伴"
- placeholder-class="input-placeholder"
- maxlength="50"
- />
- </view>
- </view>
- <!-- Save Button -->
- <view class="save-bar">
- <view class="btn-save" :class="{ saving: saving }" @click="handleSave">
- <text class="btn-save-text">{{ saving ? '保存中...' : '保存' }}</text>
- </view>
- </view>
- </view>
- </template>
- <script setup>
- import { ref, reactive } from 'vue'
- import { onLoad } from '@dcloudio/uni-app'
- import requestInstance from '@/utils/api'
- const relationApi = {
- create: (data) => requestInstance.post('/relation/create', data),
- update: (data) => requestInstance.post('/relation/update', data),
- get: (id) => requestInstance.post('/relation/get', { id })
- }
- const relationTypeOptions = [
- { value: 'spouse', label: '夫妻' },
- { value: 'parent_child', label: '亲子' },
- { value: 'colleague', label: '同事' },
- { value: 'friend', label: '朋友' },
- { value: 'superior', label: '上下级' }
- ]
- const genderOptions = [
- { value: 0, label: '保密' },
- { value: 1, label: '男' },
- { value: 2, label: '女' }
- ]
- const form = reactive({
- relationType: '',
- name: '',
- birthDate: '',
- gender: 0,
- note: ''
- })
- const isEdit = ref(false)
- const editId = ref(null)
- const saving = ref(false)
- onLoad((query) => {
- if (query && query.id) {
- isEdit.value = true
- editId.value = query.id
- uni.setNavigationBarTitle({ title: '编辑关系人' })
- fetchRelation(query.id)
- }
- })
- async function fetchRelation(id) {
- try {
- const data = await relationApi.get(id)
- if (data) {
- form.relationType = data.relationType || ''
- form.name = data.name || ''
- form.birthDate = data.birthDate || ''
- form.gender = data.gender ?? 0
- form.note = data.note || ''
- }
- } catch (e) {
- uni.showToast({ title: '加载关系人信息失败', icon: 'none' })
- }
- }
- function onBirthDateChange(e) {
- form.birthDate = e.detail.value
- }
- function validate() {
- if (!form.relationType) {
- uni.showToast({ title: '请选择关系类型', icon: 'none' })
- return false
- }
- if (!form.birthDate) {
- uni.showToast({ title: '请选择出生日期', icon: 'none' })
- return false
- }
- return true
- }
- async function handleSave() {
- if (!validate()) return
- if (saving.value) return
- saving.value = true
- try {
- if (isEdit.value) {
- await relationApi.update({ id: editId.value, ...form })
- uni.showToast({ title: '更新成功', icon: 'success' })
- } else {
- await relationApi.create(form)
- uni.showToast({ title: '添加成功', icon: 'success' })
- }
- setTimeout(() => {
- uni.navigateBack()
- }, 1200)
- } catch (e) {
- // error toast already handled by requestInstance
- } finally {
- saving.value = false
- }
- }
- </script>
- <style scoped lang="scss">
- @import "@/styles/theme.scss";
- .page-add-relation {
- min-height: 100vh;
- background: var(--color-bg);
- padding: 16px;
- }
- /* Form card */
- .form-card {
- background: var(--color-bg-card);
- border-radius: 16px;
- padding: 24px 20px;
- box-shadow: var(--shadow-card);
- }
- .form-group {
- margin-bottom: 24px;
- }
- .form-group:last-child {
- margin-bottom: 0;
- }
- .form-label {
- display: block;
- font-size: 14px;
- font-weight: 600;
- color: var(--color-text-secondary);
- margin-bottom: 10px;
- }
- /* Input fields */
- .form-input {
- width: 100%;
- height: 44px;
- background: var(--color-bg-card);
- border: 1px solid var(--color-border);
- border-radius: 10px;
- padding: 0 14px;
- font-size: 15px;
- color: var(--color-text-primary);
- box-sizing: border-box;
- }
- .form-input:focus {
- border-color: var(--color-brand);
- box-shadow: 0 0 0 2px rgba(30, 58, 95, 0.1);
- }
- .input-placeholder {
- color: var(--color-text-secondary);
- font-size: 15px;
- }
- /* Picker */
- .picker-trigger {
- display: flex;
- align-items: center;
- }
- .text-placeholder {
- color: var(--color-text-secondary);
- font-size: 15px;
- }
- /* Relation type chips */
- .type-grid {
- display: flex;
- flex-wrap: wrap;
- gap: 10px;
- }
- .type-chip {
- padding: 10px 20px;
- border: 1px solid var(--color-border);
- border-radius: 10px;
- background: var(--color-bg-card);
- text-align: center;
- flex: 0 0 auto;
- min-width: 80px;
- }
- .type-chip:active {
- transform: scale(0.97);
- }
- .type-chip.active {
- border-color: var(--color-brand);
- background: var(--color-brand);
- }
- .type-chip.active .chip-text {
- color: var(--color-bg-card);
- }
- .chip-text {
- font-size: 14px;
- font-weight: 500;
- color: var(--color-text-secondary);
- }
- /* Gender row */
- .gender-row {
- display: flex;
- gap: 12px;
- }
- .gender-option {
- flex: 1;
- padding: 10px 0;
- border: 1px solid var(--color-border);
- border-radius: 10px;
- text-align: center;
- background: var(--color-bg-card);
- }
- .gender-option:active {
- transform: scale(0.97);
- }
- .gender-option.active {
- border-color: var(--color-brand);
- background: var(--color-brand);
- }
- .gender-option.active .gender-text {
- color: var(--color-bg-card);
- }
- .gender-text {
- font-size: 14px;
- font-weight: 500;
- color: var(--color-text-secondary);
- }
- /* Save button */
- .save-bar {
- margin-top: 24px;
- padding-bottom: 16px;
- }
- .btn-save {
- width: 100%;
- height: 48px;
- background: var(--color-brand);
- border-radius: 12px;
- display: flex;
- align-items: center;
- justify-content: center;
- }
- .btn-save:active {
- transform: scale(0.97);
- opacity: 0.9;
- }
- .btn-save.saving {
- opacity: 0.7;
- pointer-events: none;
- }
- .btn-save-text {
- color: var(--color-bg-card);
- font-size: 16px;
- font-weight: 600;
- }
- </style>
|