index.vue 19 KB

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