create.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <view class="uni-app">
  3. <view class="status-bar" />
  4. <view class="main-container">
  5. <wk-nav-bar :title="navTitle">
  6. <!-- #ifndef MP-WEIXIN -->
  7. <button class="button white-btn" @click="handleSave">
  8. 保存
  9. </button>
  10. <!-- #endif -->
  11. </wk-nav-bar>
  12. <view class="container">
  13. <view class="scroll-content">
  14. <wk-form
  15. ref="form"
  16. :fields="fieldArr"
  17. :batch-id="batchId"
  18. @change="handleValueChange" />
  19. <product-choose-add
  20. v-if="productField"
  21. ref="product"
  22. :default-value="productField.value"
  23. @change="handleChangeProduct" />
  24. <wk-audit-add
  25. v-if="showAudit"
  26. ref="wkAuditAdd" />
  27. </view>
  28. </view>
  29. <!-- #ifdef MP-WEIXIN -->
  30. <view class="footer-btn-group">
  31. <button
  32. v-if="productField"
  33. class="button choose-product-btn"
  34. @click="handleAddProduct">
  35. 添加产品
  36. </button>
  37. <button class="button" @click="handleSave">
  38. 保存
  39. </button>
  40. </view>
  41. <!-- #endif -->
  42. </view>
  43. </view>
  44. </template>
  45. <script>
  46. import {
  47. QueryFieldList,
  48. AddContract,
  49. UpdateContract
  50. } from 'API/crm/contract'
  51. import {
  52. QueryBusiness as QueryBusinessByCustomerId,
  53. QueryContacts as QueryContactsByCustomerId
  54. } from 'API/crm/customer'
  55. import {
  56. QueryProduct as BusinessProduct
  57. } from 'API/crm/business'
  58. import ProductChooseAdd from '../components/baseAdd/productChooseAdd.vue'
  59. import CreateMixins from '../mixins/create.js'
  60. import { deepCopy } from '@/utils/lib.js'
  61. export default {
  62. name: 'CreateContract',
  63. components: {
  64. ProductChooseAdd
  65. },
  66. mixins: [CreateMixins],
  67. data() {
  68. return {
  69. moduleType: 'contract',
  70. productField: null,
  71. defaultProductList: {}
  72. }
  73. },
  74. onLoad() {
  75. this.getFieldList()
  76. },
  77. methods: {
  78. getFieldList() {
  79. const params = { type: 1 }
  80. if (this.id) params.id = this.id
  81. QueryFieldList(params)
  82. .then(res => {
  83. let findIndex = res.findIndex(o => o.formType === 'product')
  84. if (findIndex !== -1) {
  85. this.productField = res.splice(findIndex, 1)[0]
  86. }
  87. res.forEach(field => {
  88. // 自动编号为非必填
  89. if (field.autoGeneNumber === 1) {
  90. field.isNull = 0
  91. }
  92. if (!this.$isEmpty(field.authLevel)) {
  93. // 判断字段权限,如果没有权限则直接禁止修改
  94. field.disabled = this.getDisabledStatusByAuth(field)
  95. if (field.disabled) {
  96. if (field.sys_config) {
  97. field.sys_config.disabledMsg = ''
  98. }
  99. }
  100. }
  101. field.value = this.mixinsFormatFieldValue(field)
  102. if (field.fieldName === 'businessId') {
  103. // 客户下商机
  104. field.disabled = true
  105. field.sys_config = {
  106. request: QueryBusinessByCustomerId,
  107. otherParams: {},
  108. disabledMsg: '请先选择客户'
  109. }
  110. } else if (field.fieldName === 'contactsId') {
  111. // 客户下联系人
  112. field.disabled = true
  113. field.sys_config = {
  114. request: QueryContactsByCustomerId,
  115. otherParams: {},
  116. disabledMsg: '请先选择客户'
  117. }
  118. }
  119. })
  120. this.fieldArr = res
  121. this.setForm()
  122. this.getAuditList()
  123. })
  124. .catch(() => {
  125. })
  126. },
  127. /**
  128. * 去选择产品
  129. */
  130. handleAddProduct() {
  131. this.$nextTick(() => {
  132. if (this.$refs.product) {
  133. this.$refs.product.handleChoose()
  134. }
  135. })
  136. },
  137. /**
  138. * 获取商机下产品
  139. * @param {Object} id
  140. */
  141. getProductList(id) {
  142. if (!id) return
  143. BusinessProduct({
  144. businessId: id,
  145. pageType: 0,
  146. })
  147. .then(response => {
  148. response.product = response.list
  149. delete response.list
  150. this.$set(this.productField, 'value', response)
  151. })
  152. .catch(() => {})
  153. },
  154. /**
  155. * 确认选择的产品
  156. * @param {Object} data
  157. */
  158. handleChangeProduct(data) {
  159. console.log('change product: ', data)
  160. if (this.$refs.form) {
  161. this.$refs.form.setFormVal('money', data.totalMoney)
  162. }
  163. },
  164. /**
  165. * 表单值发生改变
  166. * @param {Object} data
  167. */
  168. handleValueChange(data) {
  169. console.log('value change: ', data)
  170. this.changeAuditFlow(data.field, data.value)
  171. let findIndex = -1
  172. const bridge = getApp().globalData.formBridge
  173. const bridgeDefault = bridge.default || {}
  174. if (data.field.formType === 'customer') {
  175. // 如果修改的是客户
  176. const arr = ['businessId', 'contactsId']
  177. arr.forEach(key => {
  178. findIndex = this.fieldArr.findIndex(o => o.fieldName === key)
  179. if (findIndex !== -1 && !bridgeDefault.hasOwnProperty(key)) {
  180. const fieldItem = deepCopy(this.fieldArr[findIndex])
  181. if (data.value.length > 0) {
  182. fieldItem.sys_config.otherParams = {
  183. customerId: data.value[0].customerId,
  184. pageType: 0
  185. }
  186. fieldItem.disabled = false
  187. } else {
  188. fieldItem.sys_config.otherParams = {}
  189. fieldItem.disabled = true
  190. fieldItem.value = []
  191. }
  192. this.$refs.form.updateFields(findIndex, fieldItem)
  193. }
  194. })
  195. } else if (data.field.formType === 'business') {
  196. // 如果修改的是商机
  197. if (data.value.length === 0) {
  198. this.$set(this.productField, 'value', {})
  199. } else {
  200. this.getProductList(data.value[0].businessId || data.value[0].id)
  201. const money = data.value[0].money
  202. if (this.$isEmpty(money)) return
  203. findIndex = this.fieldArr.findIndex(o => o.fieldName === 'money')
  204. if (findIndex !== -1) {
  205. const fieldItem = deepCopy(this.fieldArr[findIndex])
  206. fieldItem.value = money
  207. this.$refs.form.updateFields(findIndex, fieldItem)
  208. }
  209. }
  210. }
  211. },
  212. /**
  213. * 保存
  214. */
  215. handleSave() {
  216. this.loading = true
  217. this.$refs.form.getForm().then(async form => {
  218. let startTime = form.entity.startTime
  219. let endTime = form.entity.endTime
  220. if (startTime && endTime && startTime > endTime) {
  221. this.$toast('合同的开始时间不能大于结束时间')
  222. this.loading = false
  223. return
  224. }
  225. if (this.productField) {
  226. const productData = this.$refs.product.getForm()
  227. form.product = productData.product
  228. form.entity = {
  229. ...form.entity,
  230. discountRate: productData.discountRate,
  231. totalPrice: productData.totalPrice
  232. }
  233. }
  234. this.baseGetAuditSaveData(form)
  235. this.baseFormatSaveData(form)
  236. console.log('save: ', form)
  237. // this.loading = false
  238. // return
  239. if (!form.examineFlowData) {
  240. this.$toast('请完善审批信息')
  241. this.loading = false
  242. return
  243. }
  244. const request = this.id ? UpdateContract : AddContract
  245. request(form).then(() => {
  246. this.$toast(this.id ? '修改成功' : '添加成功')
  247. this.$refreshAndToPrev(this)
  248. }).catch(() => {
  249. this.loading = false
  250. })
  251. }).catch(() => {
  252. this.loading = false
  253. })
  254. }
  255. }
  256. }
  257. </script>
  258. <style scoped lang="scss">
  259. @import '../style/create.scss';
  260. </style>