index.vue 14 KB

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