index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <view class="user-edit-page">
  3. <NavBar title="编辑用户" bgColor="#fff" color="#000"></NavBar>
  4. <!-- 基本资料 -->
  5. <view class="section-title">基本资料</view>
  6. <view class="form-card">
  7. <view class="form-item">
  8. <text class="form-label require">姓名</text>
  9. <input class="form-input" v-model="form.name" placeholder="请输入姓名" />
  10. </view>
  11. <view class="form-item">
  12. <text class="form-label require">手机号</text>
  13. <input class="form-input" v-model="form.phone" type="number" placeholder="请输入手机号" />
  14. </view>
  15. <view class="form-item">
  16. <text class="form-label">性别</text>
  17. <picker :range="genderOptions" range-key="label" @change="onGenderChange">
  18. <view class="picker-value" :class="{'placeholder': form.gender === undefined}">
  19. {{ getGenderLabel }}
  20. </view>
  21. </picker>
  22. </view>
  23. </view>
  24. <!-- 居住信息 -->
  25. <view class="section-title">居住信息</view>
  26. <view class="form-card">
  27. <view class="form-item">
  28. <text class="form-label require">所属站点</text>
  29. <picker mode="multiSelector" :range="stationOptions" range-key="name" @change="onStationChange" @columnchange="onStationColumnChange" :value="stationIndex">
  30. <view class="picker-value" :class="{'placeholder': form.stationId === undefined}">
  31. {{ getStationLabel }}
  32. </view>
  33. </picker>
  34. </view>
  35. <view class="form-item">
  36. <text class="form-label require">详细住址</text>
  37. <input class="form-input" v-model="form.address" placeholder="请输入街道/门牌号" />
  38. </view>
  39. <view class="form-item">
  40. <text class="form-label">房屋类型</text>
  41. <picker :range="houseTypeOptions" range-key="label" @change="onHouseTypeChange">
  42. <view class="picker-value" :class="{'placeholder': !form.houseType}">
  43. {{ getHouseTypeLabel }}
  44. </view>
  45. </picker>
  46. </view>
  47. <view class="form-item">
  48. <text class="form-label require">入门方式</text>
  49. <picker :range="entryMethodOptions" range-key="label" @change="onEntryMethodChange">
  50. <view class="picker-value" :class="{'placeholder': !form.entryMethod}">
  51. {{ getEntryMethodLabel }}
  52. </view>
  53. </picker>
  54. </view>
  55. <view class="form-item" v-if="form.entryMethod === 'password'">
  56. <text class="form-label require">开门密码</text>
  57. <input class="form-input" v-model="form.entryPassword" placeholder="请输入密码" />
  58. </view>
  59. <view class="form-item" v-if="form.entryMethod === 'key'">
  60. <text class="form-label require">钥匙位置</text>
  61. <input class="form-input" v-model="form.keyLocation" placeholder="如:地毯下" />
  62. </view>
  63. </view>
  64. <!-- 其他 -->
  65. <view class="section-title">其他</view>
  66. <view class="form-card">
  67. <view class="form-item vertical">
  68. <text class="form-label">备注说明</text>
  69. <textarea class="form-textarea" v-model="form.remark" placeholder="请输入备注" />
  70. </view>
  71. </view>
  72. <!-- 底部固定操作栏 -->
  73. <view class="footer-bar">
  74. <button class="save-btn" :loading="saving" @click="onSave">保存修改</button>
  75. </view>
  76. </view>
  77. </template>
  78. <script setup>
  79. // @Author: Antigravity
  80. import { ref, reactive, computed } from 'vue'
  81. import { onLoad } from '@dcloudio/uni-app'
  82. import NavBar from '@/components/nav-bar/index.vue'
  83. import { getCustomer, updateCustomer } from '@/api/archieves/customer'
  84. import { listAreaStation } from '@/api/system/areaStation'
  85. import customerEnums from '@/json/customer.json'
  86. const { houseTypeOptions, entryMethodOptions } = customerEnums
  87. const genderOptions = [{ label: '男', value: 0 }, { label: '女', value: 1 }]
  88. const saving = ref(false)
  89. const stationOptions = ref([[], [], []])
  90. const stationIndex = ref([0, 0, 0])
  91. const allStationNodes = ref([])
  92. const form = reactive({
  93. id: undefined,
  94. name: '',
  95. phone: '',
  96. gender: undefined,
  97. areaId: undefined,
  98. stationId: undefined,
  99. address: '',
  100. houseType: '',
  101. entryMethod: '',
  102. entryPassword: '',
  103. keyLocation: '',
  104. remark: ''
  105. })
  106. onLoad(async (options) => {
  107. try {
  108. uni.showLoading({ title: '加载中...' })
  109. // 1. 获取站点数据
  110. const stationRes = await listAreaStation()
  111. allStationNodes.value = Array.isArray(stationRes) ? stationRes : (stationRes?.data || [])
  112. // 2. 如果是编辑,获取用户详情
  113. if (options.id) {
  114. const res = await getCustomer(options.id)
  115. if (res) {
  116. Object.assign(form, res)
  117. }
  118. }
  119. // 3. 初始化选择器并尝试回定位
  120. initStationPicker()
  121. } catch(err) {
  122. console.error(err)
  123. } finally {
  124. uni.hideLoading()
  125. }
  126. })
  127. // 初始化三级联动
  128. const initStationPicker = () => {
  129. const nodes = allStationNodes.value
  130. if (nodes.length === 0) return
  131. const cities = nodes.filter(n => String(n.parentId) === '0')
  132. // 如果已有 stationId,尝试找索引
  133. let cIdx = 0, aIdx = 0, sIdx = 0
  134. if (form.stationId) {
  135. const targetStation = nodes.find(n => String(n.id) === String(form.stationId))
  136. if (targetStation) {
  137. const targetArea = nodes.find(n => String(n.id) === String(targetStation.parentId))
  138. if (targetArea) {
  139. cIdx = cities.findIndex(n => String(n.id) === String(targetArea.parentId))
  140. cIdx = cIdx === -1 ? 0 : cIdx
  141. const areas = nodes.filter(n => String(n.parentId) === String(cities[cIdx].id))
  142. aIdx = areas.findIndex(n => String(n.id) === String(targetArea.id))
  143. aIdx = aIdx === -1 ? 0 : aIdx
  144. const stations = nodes.filter(n => String(n.parentId) === String(areas[aIdx].id))
  145. sIdx = stations.findIndex(n => String(n.id) === String(targetStation.id))
  146. sIdx = sIdx === -1 ? 0 : sIdx
  147. stationOptions.value = [cities, areas, stations]
  148. stationIndex.value = [cIdx, aIdx, sIdx]
  149. return
  150. }
  151. }
  152. }
  153. // 默认初始化
  154. const areas = nodes.filter(n => String(n.parentId) === String(cities[0]?.id || ''))
  155. const stations = areas.length > 0 ? nodes.filter(n => String(n.parentId) === String(areas[0].id)) : []
  156. stationOptions.value = [cities, areas, stations]
  157. stationIndex.value = [0, 0, 0]
  158. }
  159. const onStationColumnChange = (e) => {
  160. const column = e.detail.column
  161. const value = e.detail.value
  162. stationIndex.value[column] = value
  163. const nodes = allStationNodes.value
  164. if (column === 0) {
  165. const selectedCity = stationOptions.value[0][value]
  166. const newAreas = selectedCity ? nodes.filter(n => String(n.parentId) === String(selectedCity.id)) : []
  167. stationOptions.value[1] = newAreas
  168. stationOptions.value[2] = newAreas.length > 0 ? nodes.filter(n => String(n.parentId) === String(newAreas[0].id)) : []
  169. stationIndex.value[1] = 0
  170. stationIndex.value[2] = 0
  171. } else if (column === 1) {
  172. const selectedArea = stationOptions.value[1][value]
  173. const newStations = selectedArea ? nodes.filter(n => String(n.parentId) === String(selectedArea.id)) : []
  174. stationOptions.value[2] = newStations
  175. stationIndex.value[2] = 0
  176. }
  177. }
  178. const onStationChange = (e) => {
  179. stationIndex.value = e.detail.value
  180. const stations = stationOptions.value[2]
  181. const selectedStation = stations[stationIndex.value[2]]
  182. if (selectedStation && String(selectedStation.type) === '2') {
  183. form.stationId = selectedStation.id
  184. form.areaId = selectedStation.parentId
  185. } else {
  186. uni.showToast({ title: '请选择到具体的站点层级', icon: 'none' })
  187. }
  188. }
  189. const getGenderLabel = computed(() => genderOptions.find(o => o.value === form.gender)?.label || '请选择')
  190. const getHouseTypeLabel = computed(() => houseTypeOptions.find(o => o.value === form.houseType)?.label || '请选择')
  191. const getEntryMethodLabel = computed(() => entryMethodOptions.find(o => o.value === form.entryMethod)?.label || '请选择')
  192. const getStationLabel = computed(() => {
  193. if (!form.stationId) return '请选择'
  194. const city = stationOptions.value[0][stationIndex.value[0]]?.name || ''
  195. const area = stationOptions.value[1][stationIndex.value[1]]?.name || ''
  196. const station = stationOptions.value[2][stationIndex.value[2]]?.name || ''
  197. return `${city} - ${area} - ${station}`
  198. })
  199. const onGenderChange = (e) => { form.gender = genderOptions[e.detail.value].value }
  200. const onHouseTypeChange = (e) => { form.houseType = houseTypeOptions[e.detail.value].value }
  201. const onEntryMethodChange = (e) => {
  202. form.entryMethod = entryMethodOptions[e.detail.value].value
  203. form.entryPassword = ''
  204. form.keyLocation = ''
  205. }
  206. const onSave = async () => {
  207. if (!form.name) return uni.showToast({ title: '请输入姓名', icon: 'none' })
  208. if (!form.phone) return uni.showToast({ title: '请输入手机号', icon: 'none' })
  209. if (!form.stationId) return uni.showToast({ title: '请选择所属站点', icon: 'none' })
  210. if (!form.address) return uni.showToast({ title: '请输入详细住址', icon: 'none' })
  211. if (!form.entryMethod) return uni.showToast({ title: '请选择入门方式', icon: 'none' })
  212. saving.value = true
  213. try {
  214. await updateCustomer(form)
  215. uni.showToast({ title: '保存成功', icon: 'success' })
  216. setTimeout(() => uni.navigateBack(), 1000)
  217. } catch(err) {
  218. } finally {
  219. saving.value = false
  220. }
  221. }
  222. </script>
  223. <style lang="scss" scoped>
  224. .user-edit-page { min-height: 100vh; background: #f7f8fa; padding-bottom: calc(140rpx + env(safe-area-inset-bottom)); }
  225. .section-title { font-size: 28rpx; font-weight: bold; color: #666; padding: 24rpx 32rpx 12rpx; }
  226. .form-card { background: #fff; border-radius: 24rpx; padding: 8rpx 32rpx; margin: 0 24rpx 24rpx; }
  227. .form-item { display: flex; align-items: center; padding: 28rpx 0; border-bottom: 1rpx solid #f5f5f5; }
  228. .form-item:last-child { border-bottom: none; }
  229. .form-item.vertical { flex-direction: column; align-items: flex-start; }
  230. .form-item.vertical .form-textarea { width: 100%; height: 160rpx; margin-top: 16rpx; background: #f9f9f9; padding: 16rpx; box-sizing: border-box; border-radius: 12rpx; font-size: 28rpx; }
  231. .form-label { width: 200rpx; font-size: 28rpx; color: #333; flex-shrink: 0; }
  232. .form-label.require::before { content: '*'; color: #f56c6c; margin-right: 4rpx; }
  233. .form-input { flex: 1; font-size: 28rpx; color: #333; text-align: right; }
  234. .picker-value { flex: 1; font-size: 28rpx; color: #333; text-align: right; min-height: 40rpx; }
  235. .picker-value.placeholder { color: #ccc; }
  236. .footer-bar { position: fixed; bottom: 0; left: 0; right: 0; background: #fff; padding: 20rpx 32rpx calc(20rpx + env(safe-area-inset-bottom)); box-shadow: 0 -4rpx 16rpx rgba(0, 0, 0, 0.05); z-index: 100; }
  237. .save-btn { width: 100%; height: 88rpx; background: linear-gradient(90deg, #ffd53f, #ff9500); color: #333; border: none; border-radius: 44rpx; font-size: 32rpx; font-weight: bold; line-height: 88rpx; }
  238. </style>