index.vue 15 KB

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