index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <template>
  2. <el-dialog :model-value="visible" @update:model-value="$emit('update:visible', $event)" :title="dialogTitle"
  3. width="700px" destroy-on-close append-to-body class="add-customer-dialog">
  4. <div class="dialog-body">
  5. <!-- 头像区 -->
  6. <div class="avatar-section">
  7. <el-upload action="#" :show-file-list="false" :auto-upload="false" :on-change="handleUploadFile">
  8. <el-avatar :size="88"
  9. :src="avatarDisplayUrl || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'"
  10. class="avatar-preview" />
  11. <div class="avatar-tip">点击修改头像</div>
  12. </el-upload>
  13. </div>
  14. <el-form :model="form" label-width="90px" label-position="top" class="customer-form">
  15. <!-- 基本资料 -->
  16. <div class="section">
  17. <div class="section-title">基本资料</div>
  18. <el-row :gutter="24">
  19. <el-col :span="24">
  20. <el-form-item label="所属站点" required>
  21. <el-cascader v-model="formAreaValue" :options="areaTreeOptions"
  22. :props="{ value: 'value', label: 'label' }" placeholder="请选择站点" style="width: 100%" clearable
  23. @change="handleFormAreaChange" />
  24. </el-form-item>
  25. </el-col>
  26. <el-col :span="8">
  27. <el-form-item label="姓名" required><el-input v-model="form.name" placeholder="请输入姓名" /></el-form-item>
  28. </el-col>
  29. <el-col :span="8">
  30. <el-form-item label="电话" required><el-input v-model="form.phone" placeholder="请输入电话" /></el-form-item>
  31. </el-col>
  32. <el-col :span="8">
  33. <el-form-item label="性别">
  34. <el-select v-model="form.gender" placeholder="请选择">
  35. <el-option v-for="dict in sys_user_sex" :key="dict.value" :label="dict.label"
  36. :value="parseInt(dict.value)" />
  37. </el-select>
  38. </el-form-item>
  39. </el-col>
  40. </el-row>
  41. </div>
  42. <!-- 居住信息 -->
  43. <div class="section">
  44. <div class="section-title">居住信息</div>
  45. <el-row :gutter="24">
  46. <el-col :span="12">
  47. <el-form-item label="所在地区">
  48. <RegionCascader v-model="regionCascaderValue" />
  49. </el-form-item>
  50. </el-col>
  51. <el-col :span="12">
  52. <el-form-item label="详细住址" required>
  53. <el-input v-model="form.address" placeholder="请输入街道/门牌号" />
  54. </el-form-item>
  55. </el-col>
  56. <el-col :span="8">
  57. <el-form-item label="房屋类型">
  58. <el-select v-model="form.houseType" placeholder="请选择">
  59. <el-option v-for="dict in sys_house_type" :key="dict.value" :label="dict.label" :value="dict.value" />
  60. </el-select>
  61. </el-form-item>
  62. </el-col>
  63. <el-col :span="8">
  64. <el-form-item label="入门方式" required>
  65. <el-select v-model="form.entryMethod" placeholder="请选择">
  66. <el-option v-for="dict in sys_entry_method" :key="dict.value" :label="dict.label"
  67. :value="dict.value" />
  68. </el-select>
  69. </el-form-item>
  70. </el-col>
  71. <el-col :span="8" v-if="form.entryMethod === 'password'">
  72. <el-form-item label="开门密码" required>
  73. <el-input v-model="form.entryPassword" placeholder="请输入密码" />
  74. </el-form-item>
  75. </el-col>
  76. <el-col :span="8" v-if="form.entryMethod === 'key'">
  77. <el-form-item label="钥匙位置" required>
  78. <el-input v-model="form.keyLocation" placeholder="如:地毯下" />
  79. </el-form-item>
  80. </el-col>
  81. </el-row>
  82. </div>
  83. <!-- 其他 -->
  84. <div class="section">
  85. <div class="section-title">其他</div>
  86. <el-row :gutter="24">
  87. <el-col :span="12">
  88. <el-form-item label="用户标签">
  89. <el-select v-model="selectedTagIds" multiple placeholder="选择标签" collapse-tags collapse-tags-tooltip>
  90. <el-option v-for="tag in allUserTags" :key="tag.id" :label="tag.name" :value="tag.id">
  91. <el-tag :type="tag.colorType || 'info'" effect="light" size="small">{{ tag.name }}</el-tag>
  92. </el-option>
  93. </el-select>
  94. </el-form-item>
  95. </el-col>
  96. <el-col :span="12">
  97. <el-form-item label="备注说明">
  98. <el-input type="textarea" v-model="form.remark" :rows="2" placeholder="备注信息" />
  99. </el-form-item>
  100. </el-col>
  101. </el-row>
  102. </div>
  103. </el-form>
  104. </div>
  105. <template #footer>
  106. <div class="dialog-footer">
  107. <el-button @click="$emit('update:visible', false)" size="large">取消</el-button>
  108. <el-button type="primary" :loading="submitLoading" @click="saveUser" size="large">保存</el-button>
  109. </div>
  110. </template>
  111. </el-dialog>
  112. </template>
  113. <script setup>
  114. import { ref, reactive, computed, watch, getCurrentInstance, toRefs, onMounted } from 'vue'
  115. import { ElMessage } from 'element-plus'
  116. import { globalHeaders } from '@/utils/request'
  117. import { addCustomer, updateCustomer, getCustomer } from '@/api/archieves/customer'
  118. import { listAllTag } from '@/api/archieves/tag'
  119. import { listAreaStation } from '@/api/system/areaStation'
  120. import RegionCascader from '@/components/RegionCascader/index.vue'
  121. import { useRegionData } from '@/hooks/useRegionData'
  122. import { useUserStore } from '@/store/modules/user'
  123. const props = defineProps({
  124. visible: { type: Boolean, default: false },
  125. editData: { type: Object, default: null },
  126. orderMode: { type: Boolean, default: false },
  127. tenantId: { type: [String, Number], default: undefined },
  128. stationId: { type: [String, Number], default: undefined }
  129. })
  130. const emit = defineEmits(['update:visible', 'success'])
  131. const { proxy } = getCurrentInstance()
  132. const { sys_user_sex, sys_house_type, sys_entry_method } = toRefs(
  133. proxy?.useDict('sys_user_sex', 'sys_house_type', 'sys_entry_method')
  134. )
  135. const userStore = useUserStore()
  136. const { loadRegionData } = useRegionData()
  137. const submitLoading = ref(false)
  138. const avatarDisplayUrl = ref('')
  139. const selectedTagIds = ref([])
  140. const allUserTags = ref([])
  141. const allNodes = ref([])
  142. const formAreaValue = ref([])
  143. const regionCascaderValue = ref([])
  144. const baseUrl = import.meta.env.VITE_APP_BASE_API
  145. const uploadUrl = baseUrl + '/resource/oss/upload'
  146. const form = reactive({
  147. id: undefined, name: '', phone: '', avatar: undefined, gender: undefined,
  148. areaId: undefined, stationId: undefined, regionCode: '', address: '',
  149. houseType: '', entryMethod: '', entryPassword: '', keyLocation: '',
  150. tenantId: undefined, remark: '', tagIds: []
  151. })
  152. const areaTreeOptions = computed(() => {
  153. const buildTree = (data, parentId) => {
  154. return data
  155. .filter(item => String(item.parentId) === String(parentId))
  156. .map(item => {
  157. const children = buildTree(data, item.id)
  158. const node = { value: item.id, label: item.name }
  159. if (children.length > 0) {
  160. node.children = children
  161. }
  162. return node
  163. })
  164. }
  165. return buildTree(allNodes.value, 0)
  166. })
  167. const dialogTitle = computed(() => props.editData ? '编辑用户' : '新增用户')
  168. const loadTags = () => {
  169. listAllTag({ category: 'customer', status: 0 }).then((res) => {
  170. allUserTags.value = res.data || []
  171. }).catch(() => { })
  172. }
  173. const loadAreaStation = () => {
  174. listAreaStation().then((res) => {
  175. allNodes.value = res.data || []
  176. })
  177. }
  178. const handleFormAreaChange = (value) => {
  179. if (value && value.length > 0) {
  180. const lastId = value[value.length - 1]
  181. const node = allNodes.value.find(n => String(n.id) === String(lastId))
  182. if (node) {
  183. if (String(node.type) === '2') {
  184. form.stationId = lastId
  185. form.areaId = node.parentId
  186. } else {
  187. form.areaId = lastId
  188. form.stationId = undefined
  189. }
  190. }
  191. } else {
  192. form.areaId = undefined
  193. form.stationId = undefined
  194. }
  195. }
  196. const handleUploadFile = async (file) => {
  197. const fd = new FormData()
  198. fd.append('file', file.raw)
  199. try {
  200. const headers = globalHeaders()
  201. const res = await fetch(uploadUrl, {
  202. method: 'POST',
  203. headers: { 'Authorization': headers.Authorization, 'clientid': headers.clientid },
  204. body: fd
  205. })
  206. const result = await res.json()
  207. if (result.code === 200) {
  208. form.avatar = result.data.ossId
  209. avatarDisplayUrl.value = result.data.url
  210. } else {
  211. ElMessage.error(result.msg || '头像上传失败')
  212. }
  213. } catch (e) {
  214. ElMessage.error('头像上传失败')
  215. }
  216. }
  217. const saveUser = () => {
  218. if (!form.name) return ElMessage.warning('请输入姓名')
  219. if (!form.phone) return ElMessage.warning('请输入电话')
  220. if (!form.stationId) return ElMessage.warning('请选择所属站点')
  221. if (!form.address) return ElMessage.warning('请输入详细住址')
  222. if (!form.entryMethod) return ElMessage.warning('请选择入门方式')
  223. if (form.entryMethod === 'password' && !form.entryPassword) return ElMessage.warning('请输入开门密码')
  224. if (form.entryMethod === 'key' && !form.keyLocation) return ElMessage.warning('请输入钥匙存放位置')
  225. submitLoading.value = true
  226. form.tagIds = selectedTagIds.value
  227. if (regionCascaderValue.value && regionCascaderValue.value.length > 0) {
  228. form.regionCode = regionCascaderValue.value.join('/')
  229. } else {
  230. form.regionCode = ''
  231. }
  232. if (props.orderMode) {
  233. form.tenantId = props.tenantId
  234. form.stationId = props.stationId
  235. } else {
  236. form.tenantId = userStore.tenantId
  237. }
  238. const api = form.id ? updateCustomer(form) : addCustomer(form)
  239. api.then(() => {
  240. ElMessage.success('保存成功')
  241. emit('update:visible', false)
  242. emit('success')
  243. }).finally(() => {
  244. submitLoading.value = false
  245. })
  246. }
  247. const initForm = () => {
  248. selectedTagIds.value = []
  249. Object.assign(form, {
  250. id: undefined, name: '', phone: '', avatar: undefined, gender: undefined,
  251. areaId: undefined, stationId: undefined, regionCode: '', address: '',
  252. houseType: '', entryMethod: '', entryPassword: '', keyLocation: '',
  253. tenantId: props.orderMode ? props.tenantId : userStore.tenantId,
  254. remark: '', tagIds: []
  255. })
  256. avatarDisplayUrl.value = ''
  257. formAreaValue.value = []
  258. regionCascaderValue.value = []
  259. }
  260. const loadEditData = (data) => {
  261. Object.assign(form, {
  262. id: data.id, name: data.name, phone: data.phone, avatar: data.avatar,
  263. gender: data.gender, areaId: data.areaId, stationId: data.stationId,
  264. regionCode: data.regionCode || '', address: data.address,
  265. houseType: data.houseType, entryMethod: data.entryMethod,
  266. entryPassword: data.entryPassword, keyLocation: data.keyLocation,
  267. tenantId: props.orderMode ? props.tenantId : data.tenantId || userStore.tenantId,
  268. remark: data.remark || '', tagIds: []
  269. })
  270. avatarDisplayUrl.value = data.avatarUrl || ''
  271. regionCascaderValue.value = data.regionCode ? data.regionCode.split('/') : []
  272. selectedTagIds.value = data.tags ? data.tags.map(t => t.id) : []
  273. const targetId = data.stationId || data.areaId
  274. if (targetId) {
  275. const findPath = (nodes, targetId, path = []) => {
  276. for (const node of nodes) {
  277. const currentPath = [...path, node.id]
  278. if (String(node.id) === String(targetId)) return currentPath
  279. const children = allNodes.value.filter(n => String(n.parentId) === String(node.id))
  280. if (children.length > 0) {
  281. const result = findPath(children, targetId, currentPath)
  282. if (result) return result
  283. }
  284. }
  285. return null
  286. }
  287. const roots = allNodes.value.filter(n => String(n.parentId) === '0')
  288. formAreaValue.value = findPath(roots, targetId) || []
  289. } else {
  290. formAreaValue.value = []
  291. }
  292. }
  293. watch(() => props.visible, (val) => {
  294. if (val) {
  295. if (props.editData) {
  296. loadEditData(props.editData)
  297. } else {
  298. initForm()
  299. }
  300. }
  301. })
  302. onMounted(() => {
  303. loadTags()
  304. loadAreaStation()
  305. loadRegionData()
  306. })
  307. </script>
  308. <style scoped>
  309. .add-customer-dialog :deep(.el-dialog__body) {
  310. padding: 0 24px 0;
  311. max-height: 62vh;
  312. overflow-y: auto;
  313. }
  314. .dialog-body {
  315. padding-top: 8px;
  316. }
  317. .avatar-section {
  318. display: flex;
  319. justify-content: center;
  320. margin-bottom: 20px;
  321. }
  322. .avatar-preview {
  323. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  324. border: 3px solid #fff;
  325. outline: 1px solid #e8e8e8;
  326. cursor: pointer;
  327. }
  328. .avatar-tip {
  329. margin-top: 8px;
  330. font-size: 12px;
  331. color: #409eff;
  332. text-align: center;
  333. }
  334. .customer-form {
  335. padding-bottom: 10px;
  336. }
  337. .section {
  338. margin-bottom: 8px;
  339. padding: 16px 20px 4px;
  340. background: #fafbfc;
  341. border-radius: 8px;
  342. border: 1px solid #f0f0f0;
  343. }
  344. .section-title {
  345. font-size: 14px;
  346. font-weight: 600;
  347. color: #303133;
  348. margin-bottom: 14px;
  349. padding-left: 10px;
  350. border-left: 3px solid #409eff;
  351. }
  352. .dialog-footer {
  353. display: flex;
  354. justify-content: flex-end;
  355. gap: 12px;
  356. padding-top: 8px;
  357. }
  358. </style>