index.jsx 4.7 KB

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