index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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">
  52. <text class="form-label require">详细住址</text>
  53. <input class="form-input" v-model="form.address" placeholder="请输入街道/门牌号" />
  54. </view>
  55. <view class="form-item" @click="showHouseTypeSelect = true">
  56. <text class="form-label">房屋类型</text>
  57. <view class="picker-value" :class="{'placeholder': !form.houseType}">
  58. {{ getHouseTypeLabel }}
  59. </view>
  60. <view class="right-arrow"></view>
  61. </view>
  62. <view class="form-item" @click="showEntryMethodSelect = true">
  63. <text class="form-label require">入门方式</text>
  64. <view class="picker-value" :class="{'placeholder': !form.entryMethod}">
  65. {{ getEntryMethodLabel }}
  66. </view>
  67. <view class="right-arrow"></view>
  68. </view>
  69. <view class="form-item" v-if="form.entryMethod === 'password'">
  70. <text class="form-label require">开门密码</text>
  71. <input class="form-input" v-model="form.entryPassword" placeholder="请输入密码" />
  72. </view>
  73. <view class="form-item" v-if="form.entryMethod === 'key'">
  74. <text class="form-label require">钥匙位置</text>
  75. <input class="form-input" v-model="form.keyLocation" placeholder="如:地毯下" />
  76. </view>
  77. </view>
  78. <!-- 其他 -->
  79. <view class="section-title">其他</view>
  80. <view class="form-card">
  81. <view class="form-item vertical">
  82. <text class="form-label">备注说明</text>
  83. <textarea class="form-textarea" v-model="form.remark" placeholder="请输入备注" />
  84. </view>
  85. </view>
  86. <!-- 底部固定操作栏 -->
  87. <view class="footer-bar">
  88. <button class="save-btn" :loading="saving" @click="onSave">保存修改</button>
  89. </view>
  90. </block>
  91. <!-- 站点选择弹窗 @Author: Antigravity -->
  92. <view class="center-modal-mask" v-if="showStationModal" @click="showStationModal = false" @touchmove.stop.prevent>
  93. <view class="center-modal-content station-modal" @click.stop>
  94. <view class="modal-header">
  95. <text class="modal-title">所属站点修改</text>
  96. <view class="close-btn" @click="showStationModal = false"></view>
  97. </view>
  98. <view class="step-indicator">
  99. <view class="step-item" :class="{ 'active': currentStep === 0 }" @click="currentStep = 0">{{ selectedCityName || '城市' }}</view>
  100. <view class="step-divider">/</view>
  101. <view class="step-item" :class="{ 'active': currentStep === 1 }" @click="selectedCityId ? currentStep = 1 : null">{{ selectedAreaName || '区域' }}</view>
  102. <view class="step-divider">/</view>
  103. <view class="step-item" :class="{ 'active': currentStep === 2 }" @click="selectedAreaId ? currentStep = 2 : null">{{ selectedStationName || '站点' }}</view>
  104. </view>
  105. <scroll-view scroll-y class="modal-list-scroll">
  106. <view class="list-item" v-for="item in currentList" :key="item.id" @click="onStepSelect(item)">
  107. <text class="item-text">{{ item.name }}</text>
  108. <view class="checkmark" v-if="isStepChecked(item)"></view>
  109. </view>
  110. <view class="empty-tip" v-if="currentList.length === 0">暂无数据</view>
  111. </scroll-view>
  112. </view>
  113. </view>
  114. <center-select v-model="showGenderSelect" title="性别修改" :options="genderOptions" :value="form.gender" @select="(item) => form.gender = item.value" />
  115. <center-select v-model="showHouseTypeSelect" title="房屋类型" :options="houseTypeOptions" :value="form.houseType" @select="(item) => form.houseType = item.value" />
  116. <center-select v-model="showEntryMethodSelect" title="入门方式" :options="entryMethodOptions" :value="form.entryMethod" @select="onEntryMethodChangeDetail" />
  117. </view>
  118. </template>
  119. <script setup>
  120. /**
  121. * @Author: Antigravity
  122. */
  123. import { ref, reactive, computed } from 'vue'
  124. import { onLoad } from '@dcloudio/uni-app'
  125. import navBar from '@/components/nav-bar/index.vue'
  126. import centerSelect from '@/components/center-select/index.vue'
  127. import { getCustomer, updateCustomer } from '@/api/archieves/customer'
  128. import { listAreaStation } from '@/api/system/areaStation'
  129. import { uploadFile } from '@/api/system/oss'
  130. import customerEnums from '@/json/customer.json'
  131. const { houseTypeOptions, entryMethodOptions } = customerEnums
  132. const genderOptions = [{ label: '男', value: 0 }, { label: '女', value: 1 }]
  133. const loading = ref(true)
  134. const saving = ref(false)
  135. const allStationNodes = ref([])
  136. const avatarDisplayUrl = ref('')
  137. const showGenderSelect = ref(false)
  138. const showHouseTypeSelect = ref(false)
  139. const showEntryMethodSelect = ref(false)
  140. const showStationModal = ref(false)
  141. const currentStep = ref(0)
  142. const selectedCityId = ref(null)
  143. const selectedCityName = ref('')
  144. const selectedAreaId = ref(null)
  145. const selectedAreaName = ref('')
  146. const selectedStationId = ref(null)
  147. const selectedStationName = ref('')
  148. const form = reactive({
  149. id: undefined, name: '', phone: '', gender: undefined, areaId: undefined, stationId: undefined,
  150. address: '', houseType: '', entryMethod: '', entryPassword: '', keyLocation: '', remark: '', avatar: undefined
  151. })
  152. onLoad(async (options) => {
  153. try {
  154. loading.value = true
  155. const stationRes = await listAreaStation()
  156. allStationNodes.value = Array.isArray(stationRes) ? stationRes : (stationRes?.data || [])
  157. if (options.id) {
  158. const res = await getCustomer(options.id)
  159. if (res) {
  160. const data = res.data || res
  161. Object.assign(form, data)
  162. avatarDisplayUrl.value = data.avatarUrl || ''
  163. resolveStationNames()
  164. }
  165. }
  166. } catch(err) { console.error(err) } finally { loading.value = false }
  167. })
  168. // 根据 ID 反查名称
  169. const resolveStationNames = () => {
  170. if (!form.stationId) return
  171. const nodes = allStationNodes.value
  172. const station = nodes.find(n => String(n.id) === String(form.stationId))
  173. if (station) {
  174. selectedStationId.value = station.id
  175. selectedStationName.value = station.name
  176. const area = nodes.find(n => String(n.id) === String(station.parentId))
  177. if (area) {
  178. selectedAreaId.value = area.id
  179. selectedAreaName.value = area.name
  180. const city = nodes.find(n => String(n.id) === String(area.parentId))
  181. if (city) {
  182. selectedCityId.value = city.id
  183. selectedCityName.value = city.name
  184. }
  185. }
  186. }
  187. }
  188. const openStationModal = () => {
  189. currentStep.value = 0
  190. showStationModal.value = true
  191. }
  192. const currentList = computed(() => {
  193. if (currentStep.value === 0) return allStationNodes.value.filter(n => String(n.parentId) === '0' || !n.parentId)
  194. if (currentStep.value === 1) return allStationNodes.value.filter(n => String(n.parentId) === String(selectedCityId.value))
  195. return allStationNodes.value.filter(n => String(n.parentId) === String(selectedAreaId.value))
  196. })
  197. const onStepSelect = (item) => {
  198. if (currentStep.value === 0) {
  199. selectedCityId.value = item.id; selectedCityName.value = item.name
  200. selectedAreaId.value = null; selectedAreaName.value = ''
  201. selectedStationId.value = null; selectedStationName.value = ''
  202. currentStep.value = 1
  203. } else if (currentStep.value === 1) {
  204. selectedAreaId.value = item.id; selectedAreaName.value = item.name
  205. selectedStationId.value = null; selectedStationName.value = ''
  206. currentStep.value = 2
  207. } else {
  208. selectedStationId.value = item.id; selectedStationName.value = item.name
  209. form.stationId = item.id; form.areaId = selectedAreaId.value
  210. showStationModal.value = false
  211. }
  212. }
  213. const isStepChecked = (item) => {
  214. if (currentStep.value === 0) return selectedCityId.value === item.id
  215. if (currentStep.value === 1) return selectedAreaId.value === item.id
  216. return selectedStationId.value === item.id
  217. }
  218. const getStationLabel = computed(() => form.stationId ? `${selectedCityName.value} - ${selectedAreaName.value} - ${selectedStationName.value}` : '请选择')
  219. const getGenderLabel = computed(() => genderOptions.find(o => o.value === form.gender)?.label || '请选择')
  220. const getHouseTypeLabel = computed(() => houseTypeOptions.find(o => o.value === form.houseType)?.label || '请选择')
  221. const getEntryMethodLabel = computed(() => entryMethodOptions.find(o => o.value === form.entryMethod)?.label || '请选择')
  222. const onEntryMethodChangeDetail = (item) => {
  223. form.entryMethod = item.value
  224. form.entryPassword = ''; form.keyLocation = ''
  225. }
  226. // 选择并上传头像
  227. const chooseAvatar = () => {
  228. uni.chooseImage({
  229. count: 1,
  230. sizeType: ['compressed'],
  231. success: async (res) => {
  232. try {
  233. uni.showLoading({ title: '上传中...' })
  234. const uploadRes = await uploadFile(res.tempFilePaths[0])
  235. form.avatar = uploadRes.ossId
  236. avatarDisplayUrl.value = uploadRes.url
  237. uni.hideLoading()
  238. uni.showToast({ title: '修改成功', icon: 'success' })
  239. } catch (err) {
  240. uni.hideLoading()
  241. console.error('头像上传失败', err)
  242. }
  243. }
  244. })
  245. }
  246. const onSave = async () => {
  247. if (!form.name) return uni.showToast({ title: '请输入姓名', icon: 'none' })
  248. if (!form.phone) return uni.showToast({ title: '请输入手机号', icon: 'none' })
  249. if (!form.stationId) return uni.showToast({ title: '请选择所属站点', icon: 'none' })
  250. if (!form.address) return uni.showToast({ title: '请输入详细住址', icon: 'none' })
  251. if (!form.entryMethod) return uni.showToast({ title: '请选择入门方式', icon: 'none' })
  252. saving.value = true
  253. try {
  254. await updateCustomer(form)
  255. uni.showToast({ title: '保存成功', icon: 'success' })
  256. setTimeout(() => uni.navigateBack(), 1000)
  257. } catch(err) { } finally { saving.value = false }
  258. }
  259. </script>
  260. <style lang="scss" scoped>
  261. .user-edit-page { min-height: 100vh; background: #f7f8fa; padding-bottom: calc(140rpx + env(safe-area-inset-bottom)); }
  262. .loading-state { padding-top: 15vh; display: flex; flex-direction: column; align-items: center; justify-content: center; }
  263. .spinner { width: 44rpx; height: 44rpx; border: 4rpx solid #f3f3f3; border-top: 4rpx solid #ff9500; border-radius: 50%; animation: spin 1s linear infinite; }
  264. .loading-txt { font-size: 24rpx; color: #999; margin-top: 20rpx; }
  265. @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
  266. .avatar-section { display: flex; justify-content: center; padding: 40rpx 0 20rpx; }
  267. .avatar-wrap { display: flex; flex-direction: column; align-items: center; gap: 12rpx; }
  268. .avatar-img { width: 144rpx; height: 144rpx; border-radius: 50%; border: 4rpx solid #fff; box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08); }
  269. .avatar-placeholder { width: 144rpx; height: 144rpx; border-radius: 50%; background: #f5f5f5; border: 2rpx dashed #EEEEEE; display: flex; align-items: center; justify-content: center; }
  270. .avatar-icon {
  271. width: 60rpx; height: 60rpx; position: relative;
  272. .head { width: 30rpx; height: 30rpx; border-radius: 50%; background: #ddd; position: absolute; top: 0; left: 50%; transform: translateX(-50%); }
  273. .body { width: 50rpx; height: 26rpx; border-radius: 20rpx 20rpx 0 0; background: #ddd; position: absolute; bottom: 0; left: 50%; transform: translateX(-50%); }
  274. }
  275. .avatar-tip { font-size: 22rpx; color: #999; }
  276. .section-title { font-size: 30rpx; font-weight: bold; color: #333; padding: 32rpx 32rpx 16rpx; }
  277. .form-card { background: #fff; border-radius: 24rpx; padding: 8rpx 32rpx; margin: 0 24rpx 24rpx; }
  278. .form-item { display: flex; align-items: center; padding: 28rpx 0; border-bottom: 2rpx solid #EEEEEE; position: relative; }
  279. .form-item:last-child { border-bottom: none; }
  280. .form-item.vertical { flex-direction: column; align-items: stretch; }
  281. .form-textarea { width: 100%; height: 160rpx; margin-top: 16rpx; background: #f9f9f9; padding: 20rpx; box-sizing: border-box; border-radius: 12rpx; font-size: 28rpx; }
  282. .form-label { width: 220rpx; font-size: 28rpx; color: #333; flex-shrink: 0; }
  283. .form-label.require::before { content: '*'; color: #f56c6c; margin-right: 4rpx; }
  284. .form-input { flex: 1; font-size: 28rpx; color: #333; text-align: right; }
  285. .picker-value { flex: 1; font-size: 28rpx; color: #333; text-align: right; margin-right: 16rpx; }
  286. .picker-value.placeholder { color: #ccc; }
  287. .right-arrow { width: 14rpx; height: 14rpx; border-right: 3rpx solid #ccc; border-top: 3rpx solid #ccc; transform: rotate(45deg); }
  288. .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; }
  289. .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; }
  290. .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); }
  291. .center-modal-content { width: 620rpx; background: #fff; border-radius: 32rpx; display: flex; flex-direction: column; overflow: hidden; animation: popIn 0.3s ease-out; }
  292. @keyframes popIn { from { transform: scale(0.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }
  293. .modal-header { padding: 32rpx; border-bottom: 2rpx solid #f2f2f2; position: relative; text-align: center; }
  294. .modal-title { font-size: 32rpx; font-weight: bold; color: #333; }
  295. .close-btn {
  296. position: absolute; right: 30rpx; top: 32rpx; width: 40rpx; height: 40rpx;
  297. &::before, &::after { content: ''; position: absolute; top: 18rpx; left: 5rpx; width: 30rpx; height: 4rpx; background: #999; transform: rotate(45deg); border-radius: 4rpx; }
  298. &::after { transform: rotate(-45deg); }
  299. }
  300. .step-indicator { display: flex; align-items: center; justify-content: center; padding: 24rpx; background: #fdfdfd; border-bottom: 2rpx solid #f9f9f9; gap: 8rpx; }
  301. .step-item { font-size: 24rpx; color: #999; max-width: 150rpx; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  302. .step-item.active { color: #ff9500; font-weight: bold; }
  303. .modal-list-scroll { flex: 1; max-height: 55vh; padding: 0 32rpx; }
  304. .list-item { display: flex; align-items: center; justify-content: space-between; padding: 32rpx 0; border-bottom: 2rpx solid #f9f9f9; }
  305. .checkmark { width: 12rpx; height: 24rpx; border-right: 4rpx solid #ff9500; border-bottom: 4rpx solid #ff9500; transform: rotate(45deg); }
  306. .empty-tip { padding: 80rpx 0; text-align: center; color: #ccc; }
  307. </style>