index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <view class="user-add-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 { addCustomer } from '@/api/archieves/customer'
  84. import { getInfo } from '@/api/system/user'
  85. import { listAreaStation } from '@/api/system/areaStation'
  86. import customerEnums from '@/json/customer.json'
  87. const { houseTypeOptions, entryMethodOptions } = customerEnums
  88. const genderOptions = [{ label: '男', value: 0 }, { label: '女', value: 1 }]
  89. const saving = ref(false)
  90. const stationOptions = ref([[], [], []])
  91. const stationIndex = ref([0, 0, 0])
  92. const allStationNodes = ref([])
  93. const form = reactive({
  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 () => {
  107. try {
  108. const stationRes = await listAreaStation()
  109. allStationNodes.value = Array.isArray(stationRes) ? stationRes : (stationRes?.data || [])
  110. initStationPicker()
  111. } catch (err) {
  112. console.error('获取站点失败', err)
  113. }
  114. })
  115. // 初始化三级联动
  116. const initStationPicker = () => {
  117. const nodes = allStationNodes.value
  118. // 第一级:城市 (parentId == 0)
  119. const cities = nodes.filter(n => String(n.parentId) === '0')
  120. if (cities.length === 0) return
  121. // 第二级:默认第一个城市的区域
  122. const areas = nodes.filter(n => String(n.parentId) === String(cities[0].id))
  123. // 第三级:默认第一个区域的站点
  124. const stations = areas.length > 0 ? nodes.filter(n => String(n.parentId) === String(areas[0].id)) : []
  125. stationOptions.value = [cities, areas, stations]
  126. stationIndex.value = [0, 0, 0]
  127. }
  128. // 监听滚动变动
  129. const onStationColumnChange = (e) => {
  130. const column = e.detail.column
  131. const value = e.detail.value
  132. stationIndex.value[column] = value
  133. const nodes = allStationNodes.value
  134. if (column === 0) {
  135. // 切城市:更新区域和站点
  136. const selectedCity = stationOptions.value[0][value]
  137. if (selectedCity) {
  138. const newAreas = nodes.filter(n => String(n.parentId) === String(selectedCity.id))
  139. stationOptions.value[1] = newAreas
  140. stationOptions.value[2] = newAreas.length > 0 ? nodes.filter(n => String(n.parentId) === String(newAreas[0].id)) : []
  141. } else {
  142. stationOptions.value[1] = []
  143. stationOptions.value[2] = []
  144. }
  145. stationIndex.value.splice(1, 1, 0)
  146. stationIndex.value.splice(2, 1, 0)
  147. } else if (column === 1) {
  148. // 切区域:更新站点
  149. const selectedArea = stationOptions.value[1][value]
  150. if (selectedArea) {
  151. const newStations = nodes.filter(n => String(n.parentId) === String(selectedArea.id))
  152. stationOptions.value[2] = newStations
  153. } else {
  154. stationOptions.value[2] = []
  155. }
  156. stationIndex.value.splice(2, 1, 0)
  157. }
  158. }
  159. // 确认选择
  160. const onStationChange = (e) => {
  161. stationIndex.value = e.detail.value
  162. const stations = stationOptions.value[2]
  163. const selectedStation = stations[stationIndex.value[2]]
  164. if (selectedStation && String(selectedStation.type) === '2') {
  165. form.stationId = selectedStation.id
  166. form.areaId = selectedStation.parentId
  167. } else {
  168. uni.showToast({ title: '请选择到具体的站点层级', icon: 'none' })
  169. }
  170. }
  171. const getGenderLabel = computed(() => genderOptions.find(o => o.value === form.gender)?.label || '请选择')
  172. const getHouseTypeLabel = computed(() => houseTypeOptions.find(o => o.value === form.houseType)?.label || '请选择')
  173. const getEntryMethodLabel = computed(() => entryMethodOptions.find(o => o.value === form.entryMethod)?.label || '请选择')
  174. const getStationLabel = computed(() => {
  175. if (!form.stationId) return '请选择'
  176. const city = stationOptions.value[0][stationIndex.value[0]]?.name || ''
  177. const area = stationOptions.value[1][stationIndex.value[1]]?.name || ''
  178. const station = stationOptions.value[2][stationIndex.value[2]]?.name || ''
  179. return `${city} - ${area} - ${station}`
  180. })
  181. const onGenderChange = (e) => { form.gender = genderOptions[e.detail.value].value }
  182. const onHouseTypeChange = (e) => { form.houseType = houseTypeOptions[e.detail.value].value }
  183. const onEntryMethodChange = (e) => {
  184. form.entryMethod = entryMethodOptions[e.detail.value].value
  185. form.entryPassword = ''
  186. form.keyLocation = ''
  187. }
  188. const onSave = async () => {
  189. if (!form.name) return uni.showToast({ title: '请输入姓名', icon: 'none' })
  190. if (!form.phone) return uni.showToast({ title: '请输入手机号', icon: 'none' })
  191. if (!form.stationId) return uni.showToast({ title: '请选择所属站点', icon: 'none' })
  192. if (!form.address) return uni.showToast({ title: '请输入详细住址', icon: 'none' })
  193. if (!form.entryMethod) return uni.showToast({ title: '请选择入门方式', icon: 'none' })
  194. if (form.entryMethod === 'password' && !form.entryPassword) return uni.showToast({ title: '请输入开门密码', icon: 'none' })
  195. if (form.entryMethod === 'key' && !form.keyLocation) return uni.showToast({ title: '请输入钥匙位置', icon: 'none' })
  196. saving.value = true
  197. try {
  198. const submitData = { ...form }
  199. let tenantId = uni.getStorageSync('tenantId')
  200. // 如果本地没有获取到,重新调用接口获取一遍
  201. if (!tenantId) {
  202. const userRes = await getInfo()
  203. if (userRes && userRes.user && userRes.user.tenantId) {
  204. tenantId = userRes.user.tenantId
  205. uni.setStorageSync('tenantId', tenantId) // 顺便存回本地
  206. }
  207. }
  208. if (tenantId) submitData.tenantId = tenantId
  209. await addCustomer(submitData)
  210. uni.showToast({ title: '新增成功', icon: 'success' })
  211. setTimeout(() => uni.navigateBack(), 1000)
  212. } catch(err) {
  213. // 500 错误由 request 拦截器统一弹出 res.msg,无需二次封装
  214. } finally {
  215. saving.value = false
  216. }
  217. }
  218. </script>
  219. <style lang="scss" scoped>
  220. .user-add-page { min-height: 100vh; background: #f7f8fa; padding-bottom: calc(140rpx + env(safe-area-inset-bottom)); }
  221. .section-title {
  222. font-size: 28rpx; font-weight: bold; color: #666;
  223. padding: 24rpx 32rpx 12rpx;
  224. }
  225. .form-card { background: #fff; border-radius: 24rpx; padding: 8rpx 32rpx; margin: 0 24rpx 24rpx; }
  226. .form-item { display: flex; align-items: center; padding: 28rpx 0; border-bottom: 1rpx solid #f5f5f5; }
  227. .form-item:last-child { border-bottom: none; }
  228. .form-item.vertical { flex-direction: column; align-items: flex-start; }
  229. .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; }
  230. .form-label { width: 200rpx; font-size: 28rpx; color: #333; flex-shrink: 0; }
  231. .form-label.require::before { content: '*'; color: #f56c6c; margin-right: 4rpx; }
  232. .form-input { flex: 1; font-size: 28rpx; color: #333; text-align: right; }
  233. .picker-value { flex: 1; font-size: 28rpx; color: #333; text-align: right; min-height: 40rpx; }
  234. .picker-value.placeholder { color: #ccc; }
  235. .footer-bar {
  236. position: fixed;
  237. bottom: 0;
  238. left: 0;
  239. right: 0;
  240. background: #fff;
  241. padding: 20rpx 32rpx calc(20rpx + env(safe-area-inset-bottom));
  242. box-shadow: 0 -4rpx 16rpx rgba(0, 0, 0, 0.05);
  243. z-index: 100;
  244. }
  245. .save-btn {
  246. width: 100%;
  247. height: 88rpx;
  248. background: linear-gradient(90deg, #ffd53f, #ff9500);
  249. color: #333; border: none; border-radius: 44rpx;
  250. font-size: 32rpx; font-weight: bold; line-height: 88rpx;
  251. }
  252. </style>