ContractForm.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <template>
  2. <Dialog v-model="dialogVisible" :title="dialogTitle" width="820">
  3. <el-form
  4. ref="formRef"
  5. v-loading="formLoading"
  6. :model="formData"
  7. :rules="formRules"
  8. label-width="110px"
  9. >
  10. <el-row :gutter="20">
  11. <el-col :span="12">
  12. <el-form-item label="合同编号" prop="no">
  13. <el-input v-model="formData.no" placeholder="请输入合同编号" />
  14. </el-form-item>
  15. </el-col>
  16. <el-col :span="12">
  17. <el-form-item label="合同名称" prop="name">
  18. <el-input v-model="formData.name" placeholder="请输入合同名称" />
  19. </el-form-item>
  20. </el-col>
  21. <el-col :span="12">
  22. <el-form-item label="客户名称" prop="customerId">
  23. <el-select v-model="formData.customerId">
  24. <el-option
  25. v-for="item in customerList"
  26. :key="item.id"
  27. :label="item.name"
  28. :value="item.id!"
  29. />
  30. </el-select>
  31. </el-form-item>
  32. </el-col>
  33. <el-col :span="12">
  34. <el-form-item label="商机名称" prop="businessId">
  35. <el-select v-model="formData.businessId">
  36. <el-option
  37. v-for="item in businessList"
  38. :key="item.id"
  39. :label="item.name"
  40. :value="item.id!"
  41. />
  42. </el-select>
  43. </el-form-item>
  44. </el-col>
  45. <el-col :span="12">
  46. <el-form-item label="下单日期" prop="orderDate">
  47. <el-date-picker
  48. v-model="formData.orderDate"
  49. placeholder="选择下单日期"
  50. type="date"
  51. value-format="x"
  52. />
  53. </el-form-item>
  54. </el-col>
  55. <el-col :span="12">
  56. <el-form-item label="合同金额" prop="price">
  57. <el-input v-model="formData.price" placeholder="请输入合同金额" />
  58. </el-form-item>
  59. </el-col>
  60. <el-col :span="12">
  61. <el-form-item label="开始时间" prop="startTime">
  62. <el-date-picker
  63. v-model="formData.startTime"
  64. placeholder="选择开始时间"
  65. type="date"
  66. value-format="x"
  67. />
  68. </el-form-item>
  69. </el-col>
  70. <el-col :span="12">
  71. <el-form-item label="结束时间" prop="endTime">
  72. <el-date-picker
  73. v-model="formData.endTime"
  74. placeholder="选择结束时间"
  75. type="date"
  76. value-format="x"
  77. />
  78. </el-form-item>
  79. </el-col>
  80. <el-col :span="12">
  81. <el-form-item label="公司签约人" prop="signUserId">
  82. <el-select v-model="formData.signUserId">
  83. <el-option
  84. v-for="item in userList"
  85. :key="item.id"
  86. :label="item.nickname"
  87. :value="item.id!"
  88. />
  89. </el-select>
  90. </el-form-item>
  91. </el-col>
  92. <el-col :span="12">
  93. <el-form-item label="客户签约人" prop="contactId">
  94. <el-select v-model="formData.contactId" :disabled="!formData.customerId">
  95. <el-option
  96. v-for="item in getContactOptions"
  97. :key="item.id"
  98. :label="item.name"
  99. :value="item.id!"
  100. />
  101. </el-select>
  102. </el-form-item>
  103. </el-col>
  104. <el-col :span="12">
  105. <el-form-item label="负责人" prop="ownerUserId">
  106. <el-select v-model="formData.ownerUserId">
  107. <el-option
  108. v-for="item in userList"
  109. :key="item.id"
  110. :label="item.nickname"
  111. :value="item.id!"
  112. />
  113. </el-select>
  114. </el-form-item>
  115. </el-col>
  116. <el-col :span="24">
  117. <el-form-item label="备注" prop="remark">
  118. <el-input
  119. v-model="formData.remark"
  120. :rows="3"
  121. placeholder="请输入备注"
  122. type="textarea"
  123. />
  124. </el-form-item>
  125. </el-col>
  126. <!-- TODO @puhui999:productItems 改成 products 会更好点;因为它不是 item 哈 -->
  127. <el-col :span="24">
  128. <el-form-item label="产品列表" prop="productList">
  129. <ProductList v-model="formData.productItems" />
  130. </el-form-item>
  131. </el-col>
  132. <el-col :span="12">
  133. <el-form-item label="整单折扣(%)" prop="discountPercent">
  134. <el-input-number
  135. v-model="formData.discountPercent"
  136. :min="0"
  137. :max="100"
  138. :precision="0"
  139. placeholder="请输入整单折扣"
  140. class="!w-100%"
  141. />
  142. </el-form-item>
  143. </el-col>
  144. <el-col :span="12">
  145. <el-form-item label="产品总金额(元)" prop="productPrice">
  146. {{ fenToYuan(formData.productPrice) }}
  147. </el-form-item>
  148. </el-col>
  149. </el-row>
  150. </el-form>
  151. <template #footer>
  152. <el-button :disabled="formLoading" type="primary" @click="submitForm">保存</el-button>
  153. <el-button @click="dialogVisible = false">取 消</el-button>
  154. </template>
  155. </Dialog>
  156. </template>
  157. <script lang="ts" setup>
  158. import * as CustomerApi from '@/api/crm/customer'
  159. import * as ContractApi from '@/api/crm/contract'
  160. import * as UserApi from '@/api/system/user'
  161. import * as ContactApi from '@/api/crm/contact'
  162. import * as BusinessApi from '@/api/crm/business'
  163. import ProductList from './components/ProductList.vue'
  164. import { fenToYuan } from '@/utils'
  165. const { t } = useI18n() // 国际化
  166. const message = useMessage() // 消息弹窗
  167. const dialogVisible = ref(false) // 弹窗的是否展示
  168. const dialogTitle = ref('') // 弹窗的标题
  169. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  170. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  171. const formData = ref<ContractApi.ContractVO>({} as ContractApi.ContractVO)
  172. const formRules = reactive({
  173. name: [{ required: true, message: '合同名称不能为空', trigger: 'blur' }],
  174. customerId: [{ required: true, message: '客户不能为空', trigger: 'blur' }],
  175. orderDate: [{ required: true, message: '下单日期不能为空', trigger: 'blur' }],
  176. ownerUserId: [{ required: true, message: '负责人不能为空', trigger: 'blur' }],
  177. no: [{ required: true, message: '合同编号不能为空', trigger: 'blur' }]
  178. })
  179. const formRef = ref() // 表单 Ref
  180. /** 监听合同产品变化,计算合同产品总价 */
  181. watch(
  182. () => formData.value.productItems,
  183. (val) => {
  184. if (!val || val.length === 0) {
  185. formData.value.productPrice = 0
  186. return
  187. }
  188. // 使用 reduce 函数进行累加
  189. formData.value.productPrice = val.reduce(
  190. (accumulator, currentValue) =>
  191. isNaN(accumulator + currentValue.totalPrice) ? 0 : accumulator + currentValue.totalPrice,
  192. 0
  193. )
  194. },
  195. { deep: true }
  196. )
  197. /** 打开弹窗 */
  198. const open = async (type: string, id?: number) => {
  199. dialogVisible.value = true
  200. dialogTitle.value = t('action.' + type)
  201. formType.value = type
  202. resetForm()
  203. // 修改时,设置数据
  204. if (id) {
  205. formLoading.value = true
  206. try {
  207. formData.value = await ContractApi.getContract(id)
  208. } finally {
  209. formLoading.value = false
  210. }
  211. }
  212. await getAllApi()
  213. }
  214. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  215. /** 提交表单 */
  216. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  217. const submitForm = async () => {
  218. // 校验表单
  219. if (!formRef) return
  220. const valid = await formRef.value.validate()
  221. if (!valid) return
  222. // 提交请求
  223. formLoading.value = true
  224. try {
  225. const data = unref(formData.value) as unknown as ContractApi.ContractVO
  226. if (formType.value === 'create') {
  227. await ContractApi.createContract(data)
  228. message.success(t('common.createSuccess'))
  229. } else {
  230. await ContractApi.updateContract(data)
  231. message.success(t('common.updateSuccess'))
  232. }
  233. dialogVisible.value = false
  234. // 发送操作成功的事件
  235. emit('success')
  236. } finally {
  237. formLoading.value = false
  238. }
  239. }
  240. /** 重置表单 */
  241. const resetForm = () => {
  242. formData.value = {} as ContractApi.ContractVO
  243. formRef.value?.resetFields()
  244. }
  245. /** 获取其它相关数据 */
  246. const getAllApi = async () => {
  247. await Promise.all([getCustomerList(), getUserList(), getContactListList(), getBusinessList()])
  248. }
  249. /** 获取客户 */
  250. const customerList = ref<CustomerApi.CustomerVO[]>([])
  251. const getCustomerList = async () => {
  252. customerList.value = await CustomerApi.getSimpleCustomerList()
  253. }
  254. /** 动态获取客户联系人 */
  255. const contactList = ref<ContactApi.ContactVO[]>([])
  256. const getContactOptions = computed(() =>
  257. contactList.value.filter((item) => item.customerId === formData.value.customerId)
  258. )
  259. const getContactListList = async () => {
  260. contactList.value = await ContactApi.getSimpleContactList()
  261. }
  262. /** 获取用户列表 */
  263. const userList = ref<UserApi.UserVO[]>([])
  264. const getUserList = async () => {
  265. userList.value = await UserApi.getSimpleUserList()
  266. }
  267. /** 获取商机 */
  268. const businessList = ref<BusinessApi.BusinessVO[]>([])
  269. const getBusinessList = async () => {
  270. businessList.value = await BusinessApi.getSimpleBusinessList()
  271. }
  272. </script>