index.jsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { Input, Button, Spin, message } from 'antd'
  2. import React, { useState, useEffect, useRef } from 'react'
  3. import styles from '@/pages/Customer/Company/components/ConnectCompany/index.less'
  4. import { SearchOutlined } from '@ant-design/icons'
  5. import { useDebounceFn } from 'ahooks'
  6. import { useDispatch } from 'dva'
  7. import { apiAddClient, apiChangeClient, queryClient } from '@/services/customer'
  8. import consts from '@/basic/consts'
  9. import { formatValues } from '@/components/LazyCascader'
  10. import AddClient from '../AddClient'
  11. import CustomModal from '@/components/Modal/src/components/CustomModal'
  12. const ConnectClient = ({ dataId, onSelect, preUrl = 'client', showFooter = true }) => {
  13. const dispatch = useDispatch()
  14. const scrollRef = useRef()
  15. const [state, setState] = useState({
  16. laoding: true,
  17. client: [],
  18. searchVal: '',
  19. noMore: false,
  20. current: 1,
  21. pageSize: 10,
  22. total: 0
  23. })
  24. const initData = async params => {
  25. const { data: { client, total } = { client: [], total: 0 }, code = -1 } = await queryClient({
  26. ...params,
  27. waiver: true
  28. })
  29. if (code === consts.RET_CODE.SUCCESS) {
  30. if (params?.current === 1) {
  31. scrollRef.current?.scrollTo({ top: 0 })
  32. // 请求的是第一页或者搜索框
  33. setState({ ...state, client, total, laoding: false, searchVal: params?.search })
  34. } else {
  35. setState({
  36. ...state,
  37. client: state.client.concat(client),
  38. total,
  39. searchVal: params?.search,
  40. laoding: false,
  41. current: params?.current || 1
  42. })
  43. }
  44. }
  45. }
  46. useEffect(() => {
  47. initData({ current: 1, pageSize: 10 })
  48. }, [])
  49. const onChange = e => {
  50. const { value = '' } = e.target || e.currentTarget
  51. initData({ search: value, current: 1, pageSize: state.pageSize })
  52. }
  53. const { run: handleInputChange } = useDebounceFn(onChange)
  54. const closeModal = () => {
  55. dispatch({
  56. type: 'single/changeModal'
  57. })
  58. }
  59. const addConfirm = async values => {
  60. const payload = formatValues(values)
  61. const { code = -1, msg = '新增成功' } = await apiAddClient(payload)
  62. if (code === consts.RET_CODE.SUCCESS) {
  63. if (Object.hasOwnProperty.call(values, `${preUrl}Id`)) {
  64. message.success('新增成功并且绑定成功')
  65. onSelect(values.name)
  66. closeModal()
  67. } else {
  68. message.success(msg)
  69. initData()
  70. }
  71. }
  72. }
  73. // 商机/软件锁关联客户
  74. const connectClient = async clientId => {
  75. const { code = -1 } = await apiChangeClient(preUrl, { id: dataId, clientId })
  76. if (code === consts.RET_CODE.SUCCESS) {
  77. onSelect()
  78. closeModal()
  79. }
  80. }
  81. const handleListScroll = () => {
  82. // 未滚动到底部
  83. if (
  84. scrollRef.current.scrollHeight - scrollRef.current.clientHeight <=
  85. scrollRef.current.scrollTop
  86. ) {
  87. // 已到底部
  88. if (state.current * state.pageSize > state.total) {
  89. setState({ ...state, noMore: true })
  90. return
  91. }
  92. state.current * state.pageSize < state.total &&
  93. initData({ current: state.current + 1, pageSize: state.pageSize, search: state.searchVal })
  94. }
  95. }
  96. return (
  97. <CustomModal
  98. title={
  99. <Input
  100. prefix={<SearchOutlined />}
  101. className={styles.inputSearch}
  102. placeholder="输入客户名称搜索"
  103. onChange={handleInputChange}
  104. />
  105. }>
  106. <div
  107. className={styles.modalContent}
  108. ref={scrollRef}
  109. onScroll={handleListScroll}
  110. style={{ paddingBottom: 0 }}>
  111. <Spin spinning={state.laoding}>
  112. {state.client.map(item => (
  113. <div key={item.id} className={styles.card}>
  114. <span className="w-1/6">{item.clientName}</span>
  115. <span className="w-1/6">{item.position}</span>
  116. <span className="w-1/6">{item.telephone}</span>
  117. <span className="w-1/2 flex justify-between">
  118. <span>{item.companyName}</span>
  119. <Button type="primary" size="small" onClick={() => connectClient(item.id)}>
  120. 选择ta
  121. </Button>
  122. </span>
  123. </div>
  124. ))}
  125. </Spin>
  126. {state.noMore && <div className="text-center text-gray-400 my-4">已到底部</div>}
  127. </div>
  128. {showFooter ? (
  129. <div className={styles.modalFooter}>
  130. <AddClient
  131. onConfirm={addConfirm}
  132. dataId={dataId}
  133. dataType={preUrl}
  134. buttonProps={{ ghost: true }}
  135. btnTitle={'添加并关联到新客户'}
  136. />
  137. </div>
  138. ) : null}
  139. </CustomModal>
  140. )
  141. }
  142. export default ConnectClient