index.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <template>
  2. <view class="user-edit-page">
  3. <nav-bar title="编辑用户" bgColor="#fff" color="#000"></nav-bar>
  4. <view v-if="loading" class="loading-state">
  5. <view class="spinner"></view>
  6. <text class="loading-txt">同步资料中...</text>
  7. </view>
  8. <block v-else>
  9. <!-- 头像上传 -->
  10. <view class="avatar-section">
  11. <view class="avatar-wrap" @click="chooseAvatar">
  12. <image v-if="avatarDisplayUrl" :src="avatarDisplayUrl" class="avatar-img" mode="aspectFill"></image>
  13. <view v-else class="avatar-placeholder">
  14. <view class="avatar-icon">
  15. <view class="head"></view>
  16. <view class="body"></view>
  17. </view>
  18. </view>
  19. <text class="avatar-tip">点击修改头像</text>
  20. </view>
  21. </view>
  22. <!-- 基本资料 -->
  23. <view class="section-title">基本资料</view>
  24. <view class="form-card">
  25. <view class="form-item">
  26. <text class="form-label require">姓名</text>
  27. <input class="form-input" v-model="form.name" placeholder="请输入姓名" />
  28. </view>
  29. <view class="form-item">
  30. <text class="form-label require">手机号</text>
  31. <input class="form-input" v-model="form.phone" type="number" placeholder="请输入手机号" />
  32. </view>
  33. <view class="form-item" @click="showGenderSelect = true">
  34. <text class="form-label">性别</text>
  35. <view class="picker-value" :class="{'placeholder': form.gender === undefined}">
  36. {{ getGenderLabel }}
  37. </view>
  38. <view class="right-arrow"></view>
  39. </view>
  40. </view>
  41. <!-- 居住信息 -->
  42. <view class="section-title">居住信息</view>
  43. <view class="form-card">
  44. <view class="form-item" @click="openStationModal">
  45. <text class="form-label require">所属站点</text>
  46. <view class="picker-value" :class="{'placeholder': form.stationId === undefined}">
  47. {{ getStationLabel }}
  48. </view>
  49. <view class="right-arrow"></view>
  50. </view>
  51. <view class="form-item" @click="openRegionModal">
  52. <text class="form-label">所在地区</text>
  53. <view class="picker-value" :class="{'placeholder': !regionLabel}">
  54. {{ regionLabel || '请选择省/市/区' }}
  55. </view>
  56. <view class="right-arrow"></view>
  57. </view>
  58. <view class="form-item">
  59. <text class="form-label require">详细住址</text>
  60. <input class="form-input" v-model="form.address" placeholder="请输入街道/门牌号" />
  61. </view>
  62. <view class="form-item" @click="showHouseTypeSelect = true">
  63. <text class="form-label">房屋类型</text>
  64. <view class="picker-value" :class="{'placeholder': !form.houseType}">
  65. {{ getHouseTypeLabel }}
  66. </view>
  67. <view class="right-arrow"></view>
  68. </view>
  69. <view class="form-item" @click="showEntryMethodSelect = true">
  70. <text class="form-label require">入门方式</text>
  71. <view class="picker-value" :class="{'placeholder': !form.entryMethod}">
  72. {{ getEntryMethodLabel }}
  73. </view>
  74. <view class="right-arrow"></view>
  75. </view>
  76. <view class="form-item" v-if="form.entryMethod === 'password'">
  77. <text class="form-label require">开门密码</text>
  78. <input class="form-input" v-model="form.entryPassword" placeholder="请输入密码" />
  79. </view>
  80. <view class="form-item" v-if="form.entryMethod === 'key'">
  81. <text class="form-label require">钥匙位置</text>
  82. <input class="form-input" v-model="form.keyLocation" placeholder="如:地毯下" />
  83. </view>
  84. </view>
  85. <!-- 其他 -->
  86. <view class="section-title">其他</view>
  87. <view class="form-card">
  88. <view class="form-item vertical">
  89. <text class="form-label">备注说明</text>
  90. <textarea class="form-textarea" v-model="form.remark" placeholder="请输入备注" />
  91. </view>
  92. </view>
  93. <!-- 底部固定操作栏 -->
  94. <view class="footer-bar">
  95. <button class="save-btn" :loading="saving" @click="onSave">保存修改</button>
  96. </view>
  97. </block>
  98. <!-- 站点选择弹窗 @Author: Antigravity -->
  99. <view class="center-modal-mask" v-if="showStationModal" @click="showStationModal = false" @touchmove.stop.prevent>
  100. <view class="center-modal-content station-modal" @click.stop>
  101. <view class="modal-header">
  102. <text class="modal-title">所属站点修改</text>
  103. <view class="close-btn" @click="showStationModal = false"></view>
  104. </view>
  105. <view class="step-indicator">
  106. <view class="step-item" :class="{ 'active': currentStep === 0 }" @click="currentStep = 0">{{ selectedCityName || '城市' }}</view>
  107. <view class="step-divider">/</view>
  108. <view class="step-item" :class="{ 'active': currentStep === 1 }" @click="selectedCityId ? currentStep = 1 : null">{{ selectedAreaName || '区域' }}</view>
  109. <view class="step-divider">/</view>
  110. <view class="step-item" :class="{ 'active': currentStep === 2 }" @click="selectedAreaId ? currentStep = 2 : null">{{ selectedStationName || '站点' }}</view>
  111. </view>
  112. <scroll-view scroll-y class="modal-list-scroll">
  113. <view class="list-item" v-for="item in currentList" :key="item.id" @click="onStepSelect(item)">
  114. <text class="item-text">{{ item.name }}</text>
  115. <view class="checkmark" v-if="isStepChecked(item)"></view>
  116. </view>
  117. <view class="empty-tip" v-if="currentList.length === 0">暂无数据</view>
  118. </scroll-view>
  119. </view>
  120. </view>
  121. <!-- 省市区三级联动居中弹窗 -->
  122. <view class="center-modal-mask" v-if="showRegionModal" @click="showRegionModal = false" @touchmove.stop.prevent>
  123. <view class="center-modal-content region-modal" @click.stop>
  124. <view class="modal-header">
  125. <text class="modal-title">选择地区</text>
  126. <view class="close-btn" @click="showRegionModal = false"></view>
  127. </view>
  128. <view class="cascade-indicator">
  129. <text v-for="(node, idx) in regionPath" :key="idx" class="path-node" @click="backToRegionLevel(idx)">{{ node.name }}</text>
  130. <text class="path-node active" v-if="regionPath.length < 3">请选择</text>
  131. </view>
  132. <scroll-view scroll-y class="modal-list-scroll">
  133. <view
  134. class="list-item"
  135. v-for="item in currentRegionList"
  136. :key="item.code"
  137. @click="onRegionStepSelect(item)"
  138. >
  139. <text class="item-text">{{ item.name }}</text>
  140. <view class="checkmark" v-if="isRegionSelected(item)"></view>
  141. </view>
  142. <view class="empty-tip" v-if="currentRegionList.length === 0">暂无数据</view>
  143. </scroll-view>
  144. </view>
  145. </view>
  146. <center-select v-model="showGenderSelect" title="性别修改" :options="genderOptions" :value="form.gender" @select="(item) => form.gender = item.value" />
  147. <center-select v-model="showHouseTypeSelect" title="房屋类型" :options="houseTypeOptions" :value="form.houseType" @select="(item) => form.houseType = item.value" />
  148. <center-select v-model="showEntryMethodSelect" title="入门方式" :options="entryMethodOptions" :value="form.entryMethod" @select="onEntryMethodChangeDetail" />
  149. </view>
  150. </template>
  151. <script setup>
  152. /**
  153. * @Author: Antigravity
  154. */
  155. import { ref, reactive, computed } from 'vue'
  156. import { onLoad } from '@dcloudio/uni-app'
  157. import navBar from '@/components/nav-bar/index.vue'
  158. import centerSelect from '@/components/center-select/index.vue'
  159. import { getCustomer, updateCustomer } from '@/api/archieves/customer'
  160. import { listAreaStation } from '@/api/system/areaStation'
  161. import { listRegionTree } from '@/api/system/region'
  162. import { uploadFile } from '@/api/system/oss'
  163. import customerEnums from '@/json/customer.json'
  164. const { houseTypeOptions, entryMethodOptions } = customerEnums
  165. const genderOptions = [{ label: '男', value: 0 }, { label: '女', value: 1 }]
  166. const loading = ref(true)
  167. const saving = ref(false)
  168. const allStationNodes = ref([])
  169. const avatarDisplayUrl = ref('')
  170. const regionTree = ref([])
  171. const showGenderSelect = ref(false)
  172. const showHouseTypeSelect = ref(false)
  173. const showEntryMethodSelect = ref(false)
  174. const showStationModal = ref(false)
  175. const showRegionModal = ref(false)
  176. const currentStep = ref(0)
  177. const selectedCityId = ref(null)
  178. const selectedCityName = ref('')
  179. const selectedAreaId = ref(null)
  180. const selectedAreaName = ref('')
  181. const selectedStationId = ref(null)
  182. const selectedStationName = ref('')
  183. const form = reactive({
  184. id: undefined, name: '', phone: '', gender: undefined, areaId: undefined, stationId: undefined,
  185. address: '', houseType: '', entryMethod: '', entryPassword: '', keyLocation: '', remark: '', avatar: undefined,
  186. regionCode: ''
  187. })
  188. onLoad(async (options) => {
  189. try {
  190. loading.value = true
  191. const stationRes = await listAreaStation()
  192. allStationNodes.value = Array.isArray(stationRes) ? stationRes : (stationRes?.data || [])
  193. const regionRes = await listRegionTree()
  194. regionTree.value = Array.isArray(regionRes) ? regionRes : []
  195. if (options.id) {
  196. const res = await getCustomer(options.id)
  197. if (res) {
  198. const data = res.data || res
  199. Object.assign(form, data)
  200. avatarDisplayUrl.value = data.avatarUrl || ''
  201. resolveStationNames()
  202. resolveRegionByCode()
  203. }
  204. }
  205. } catch(err) { console.error(err) } finally { loading.value = false }
  206. })
  207. // 根据 ID 反查名称
  208. const resolveStationNames = () => {
  209. if (!form.stationId) return
  210. const nodes = allStationNodes.value
  211. const station = nodes.find(n => String(n.id) === String(form.stationId))
  212. if (station) {
  213. selectedStationId.value = station.id
  214. selectedStationName.value = station.name
  215. const area = nodes.find(n => String(n.id) === String(station.parentId))
  216. if (area) {
  217. selectedAreaId.value = area.id
  218. selectedAreaName.value = area.name
  219. const city = nodes.find(n => String(n.id) === String(area.parentId))
  220. if (city) {
  221. selectedCityId.value = city.id
  222. selectedCityName.value = city.name
  223. }
  224. }
  225. }
  226. }
  227. // ========== 省市区三级联动 ==========
  228. const regionPath = ref([])
  229. const regionLabel = ref('')
  230. const currentRegionList = computed(() => {
  231. let list = regionTree.value
  232. for (let node of regionPath.value) {
  233. const found = list.find(l => l.code === node.code)
  234. if (found && found.children) list = found.children
  235. else list = []
  236. }
  237. return list
  238. })
  239. const openRegionModal = () => {
  240. regionPath.value = []
  241. showRegionModal.value = true
  242. }
  243. const backToRegionLevel = (idx) => { regionPath.value = regionPath.value.slice(0, idx) }
  244. const onRegionStepSelect = (item) => {
  245. regionPath.value.push({ code: item.code, name: item.name })
  246. if (!item.children || item.children.length === 0 || regionPath.value.length >= 3) {
  247. const fullLabel = regionPath.value.map(p => p.name).join(' / ')
  248. form.regionCode = regionPath.value.map(p => p.code).join('/')
  249. regionLabel.value = fullLabel
  250. showRegionModal.value = false
  251. }
  252. }
  253. const isRegionSelected = (item) => {
  254. const level = regionPath.value.length
  255. return regionPath.value[level]?.code === item.code
  256. }
  257. // 根据 regionCode 回显已选地区
  258. const resolveRegionByCode = () => {
  259. if (!form.regionCode) return
  260. const codes = form.regionCode.split('/')
  261. const names = []
  262. let list = regionTree.value
  263. for (let code of codes) {
  264. const found = list.find(l => String(l.code) === String(code))
  265. if (found) {
  266. names.push(found.name)
  267. regionPath.value.push({ code: found.code, name: found.name })
  268. list = found.children || []
  269. } else break
  270. }
  271. if (names.length > 0) regionLabel.value = names.join(' / ')
  272. }
  273. const openStationModal = () => {
  274. currentStep.value = 0
  275. showStationModal.value = true
  276. }
  277. const currentList = computed(() => {
  278. if (currentStep.value === 0) return allStationNodes.value.filter(n => String(n.parentId) === '0' || !n.parentId)
  279. if (currentStep.value === 1) return allStationNodes.value.filter(n => String(n.parentId) === String(selectedCityId.value))
  280. return allStationNodes.value.filter(n => String(n.parentId) === String(selectedAreaId.value))
  281. })
  282. const onStepSelect = (item) => {
  283. if (currentStep.value === 0) {
  284. selectedCityId.value = item.id; selectedCityName.value = item.name
  285. selectedAreaId.value = null; selectedAreaName.value = ''
  286. selectedStationId.value = null; selectedStationName.value = ''
  287. currentStep.value = 1
  288. } else if (currentStep.value === 1) {
  289. selectedAreaId.value = item.id; selectedAreaName.value = item.name
  290. selectedStationId.value = null; selectedStationName.value = ''
  291. currentStep.value = 2
  292. } else {
  293. selectedStationId.value = item.id; selectedStationName.value = item.name
  294. form.stationId = item.id; form.areaId = selectedAreaId.value
  295. showStationModal.value = false
  296. }
  297. }
  298. const isStepChecked = (item) => {
  299. if (currentStep.value === 0) return selectedCityId.value === item.id
  300. if (currentStep.value === 1) return selectedAreaId.value === item.id
  301. return selectedStationId.value === item.id
  302. }
  303. const getStationLabel = computed(() => form.stationId ? `${selectedCityName.value} - ${selectedAreaName.value} - ${selectedStationName.value}` : '请选择')
  304. const getGenderLabel = computed(() => genderOptions.find(o => o.value === form.gender)?.label || '请选择')
  305. const getHouseTypeLabel = computed(() => houseTypeOptions.find(o => o.value === form.houseType)?.label || '请选择')
  306. const getEntryMethodLabel = computed(() => entryMethodOptions.find(o => o.value === form.entryMethod)?.label || '请选择')
  307. const onEntryMethodChangeDetail = (item) => {
  308. form.entryMethod = item.value
  309. form.entryPassword = ''; form.keyLocation = ''
  310. }
  311. // 选择并上传头像
  312. const chooseAvatar = () => {
  313. uni.chooseImage({
  314. count: 1,
  315. sizeType: ['compressed'],
  316. success: async (res) => {
  317. try {
  318. uni.showLoading({ title: '上传中...' })
  319. const uploadRes = await uploadFile(res.tempFilePaths[0])
  320. form.avatar = uploadRes.ossId
  321. avatarDisplayUrl.value = uploadRes.url
  322. uni.hideLoading()
  323. uni.showToast({ title: '修改成功', icon: 'success' })
  324. } catch (err) {
  325. uni.hideLoading()
  326. console.error('头像上传失败', err)
  327. }
  328. }
  329. })
  330. }
  331. const onSave = async () => {
  332. if (!form.name) return uni.showToast({ title: '请输入姓名', icon: 'none' })
  333. if (!form.phone) return uni.showToast({ title: '请输入手机号', icon: 'none' })
  334. if (!form.stationId) return uni.showToast({ title: '请选择所属站点', icon: 'none' })
  335. if (!form.address) return uni.showToast({ title: '请输入详细住址', icon: 'none' })
  336. if (!form.entryMethod) return uni.showToast({ title: '请选择入门方式', icon: 'none' })
  337. saving.value = true
  338. try {
  339. await updateCustomer(form)
  340. uni.showToast({ title: '保存成功', icon: 'success' })
  341. setTimeout(() => uni.navigateBack(), 1000)
  342. } catch(err) { } finally { saving.value = false }
  343. }
  344. </script>
  345. <style lang="scss" scoped>
  346. .user-edit-page { min-height: 100vh; background: #f7f8fa; padding-bottom: calc(140rpx + env(safe-area-inset-bottom)); }
  347. .loading-state { padding-top: 15vh; display: flex; flex-direction: column; align-items: center; justify-content: center; }
  348. .spinner { width: 44rpx; height: 44rpx; border: 4rpx solid #f3f3f3; border-top: 4rpx solid #ff9500; border-radius: 50%; animation: spin 1s linear infinite; }
  349. .loading-txt { font-size: 24rpx; color: #999; margin-top: 20rpx; }
  350. @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
  351. .avatar-section { display: flex; justify-content: center; padding: 40rpx 0 20rpx; }
  352. .avatar-wrap { display: flex; flex-direction: column; align-items: center; gap: 12rpx; }
  353. .avatar-img { width: 144rpx; height: 144rpx; border-radius: 50%; border: 4rpx solid #fff; box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08); }
  354. .avatar-placeholder { width: 144rpx; height: 144rpx; border-radius: 50%; background: #f5f5f5; border: 2rpx dashed #EEEEEE; display: flex; align-items: center; justify-content: center; }
  355. .avatar-icon {
  356. width: 60rpx; height: 60rpx; position: relative;
  357. .head { width: 30rpx; height: 30rpx; border-radius: 50%; background: #ddd; position: absolute; top: 0; left: 50%; transform: translateX(-50%); }
  358. .body { width: 50rpx; height: 26rpx; border-radius: 20rpx 20rpx 0 0; background: #ddd; position: absolute; bottom: 0; left: 50%; transform: translateX(-50%); }
  359. }
  360. .avatar-tip { font-size: 22rpx; color: #999; }
  361. .section-title { font-size: 30rpx; font-weight: bold; color: #333; padding: 32rpx 32rpx 16rpx; }
  362. .form-card { background: #fff; border-radius: 24rpx; padding: 8rpx 32rpx; margin: 0 24rpx 24rpx; }
  363. .form-item { display: flex; align-items: center; padding: 28rpx 0; border-bottom: 2rpx solid #EEEEEE; position: relative; }
  364. .form-item:last-child { border-bottom: none; }
  365. .form-item.vertical { flex-direction: column; align-items: stretch; }
  366. .form-textarea { width: 100%; height: 160rpx; margin-top: 16rpx; background: #f9f9f9; padding: 20rpx; box-sizing: border-box; border-radius: 12rpx; font-size: 28rpx; }
  367. .form-label { width: 220rpx; font-size: 28rpx; color: #333; flex-shrink: 0; }
  368. .form-label.require::before { content: '*'; color: #f56c6c; margin-right: 4rpx; }
  369. .form-input { flex: 1; font-size: 28rpx; color: #333; text-align: right; }
  370. .picker-value { flex: 1; font-size: 28rpx; color: #333; text-align: right; margin-right: 16rpx; }
  371. .picker-value.placeholder { color: #ccc; }
  372. .right-arrow { width: 14rpx; height: 14rpx; border-right: 3rpx solid #ccc; border-top: 3rpx solid #ccc; transform: rotate(45deg); }
  373. .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; }
  374. .save-btn { width: 100%; height: 88rpx; background: linear-gradient(90deg, #ffd53f, #ff9500); color: #fff; border: none; border-radius: 44rpx; font-size: 32rpx; font-weight: bold; line-height: 88rpx; }
  375. .center-modal-mask { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.6); z-index: 10000; display: flex; align-items: center; justify-content: center; backdrop-filter: blur(4rpx); }
  376. .center-modal-content { width: 620rpx; background: #fff; border-radius: 32rpx; display: flex; flex-direction: column; overflow: hidden; animation: popIn 0.3s ease-out; }
  377. @keyframes popIn { from { transform: scale(0.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }
  378. .modal-header { padding: 32rpx; border-bottom: 2rpx solid #f2f2f2; position: relative; text-align: center; }
  379. .modal-title { font-size: 32rpx; font-weight: bold; color: #333; }
  380. .close-btn {
  381. position: absolute; right: 30rpx; top: 32rpx; width: 40rpx; height: 40rpx;
  382. &::before, &::after { content: ''; position: absolute; top: 18rpx; left: 5rpx; width: 30rpx; height: 4rpx; background: #999; transform: rotate(45deg); border-radius: 4rpx; }
  383. &::after { transform: rotate(-45deg); }
  384. }
  385. .step-indicator { display: flex; align-items: center; justify-content: center; padding: 24rpx; background: #fdfdfd; border-bottom: 2rpx solid #f9f9f9; gap: 8rpx; }
  386. .step-item { font-size: 24rpx; color: #999; max-width: 150rpx; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  387. .step-item.active { color: #ff9500; font-weight: bold; }
  388. .modal-list-scroll { flex: 1; max-height: 55vh; padding: 0 32rpx; }
  389. .list-item { display: flex; align-items: center; justify-content: space-between; padding: 32rpx 0; border-bottom: 2rpx solid #f9f9f9; }
  390. .checkmark { width: 12rpx; height: 24rpx; border-right: 4rpx solid #ff9500; border-bottom: 4rpx solid #ff9500; transform: rotate(45deg); }
  391. .empty-tip { padding: 80rpx 0; text-align: center; color: #ccc; }
  392. /* 省市区级联样式 */
  393. .region-modal {
  394. max-height: 75vh;
  395. }
  396. .cascade-indicator {
  397. display: flex;
  398. align-items: center;
  399. padding: 20rpx 32rpx;
  400. background: #fdfdfd;
  401. border-bottom: 2rpx solid #f9f9f9;
  402. gap: 8rpx;
  403. flex-wrap: wrap;
  404. }
  405. .cascade-indicator .path-node {
  406. font-size: 24rpx;
  407. color: #999;
  408. padding: 4rpx 12rpx;
  409. border-radius: 8rpx;
  410. background: #f5f5f5;
  411. }
  412. .cascade-indicator .path-node.active {
  413. color: #ff9500;
  414. font-weight: bold;
  415. background: #fff5e6;
  416. }
  417. </style>