index.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <app-view class="goods-details">
  3. <template #header>
  4. <app-navbar title="采购详情" />
  5. </template>
  6. <Form ref="formRef" class="goods-details__form" @submit="onSubmit">
  7. <CellGroup>
  8. <app-select v-model="formData.THJDeliveryMode" name="THJDeliveryMode" label="交割方式"
  9. :rules="formRules.THJDeliveryMode" :options="details.deliverymodes"
  10. :optionProps="{ label: 'enumdicname', value: 'enumitemname' }" />
  11. <Field label="交割月份" name="PresaleApplyID" :rules="formRules.PresaleApplyID">
  12. <template #input>
  13. <div class="form-month">
  14. <app-select placeholder="开始月份" :options="deliveryMonths" :is-link="false"
  15. @confirm="onMonthChange" />
  16. <app-select placeholder="结束日期" :options="deliveryDays"
  17. :optionProps="{ label: 'enddate', value: 'presaleapplyid' }" :is-link="false"
  18. @confirm="onDayChange" />
  19. </div>
  20. </template>
  21. </Field>
  22. <app-select v-model="formData.DepositID" name="DepositID" label="支付方式" :rules="formRules.DepositID"
  23. :options="presaleApplyDeposits" v-if="selectedDate?.presaleapplyid" />
  24. <Field v-model="formData.Qty" name="Qty" type="digit" label="采购数量" :rules="formRules.Qty">
  25. <template #input>
  26. <div class="form-qty">
  27. <div class="form-qty__input">
  28. <input type="number" v-model="formData.Qty" placeholder="必填" />
  29. <span>≤{{ selectedDate?.remainqty ?? 0
  30. }}{{ getGoodsUnitName(details.goodsinfo?.unitid) }}</span>
  31. </div>
  32. <div class="form-qty__label">
  33. <span>定金{{ deposit }}元</span>
  34. </div>
  35. </div>
  36. </template>
  37. </Field>
  38. <Field label="收货信息" :rules="formRules.addressInfo" @click="openComponent('address')" is-link
  39. v-if="showAddressInfo">
  40. <template #input>
  41. <div class="form-address">
  42. <template v-if="validatorAddressInfo">
  43. <span>联系人:{{ formData.ContactName }}</span>
  44. <span>联系方式:{{ formData.ContactInfo }}</span>
  45. <span v-if="formData.THJDeliveryMode === 3">目的地:{{ formData.DesAddress }}</span>
  46. <span v-if="formData.ReceiptInfo">发票信息:{{ formData.ReceiptInfo }}</span>
  47. </template>
  48. <template v-else>
  49. <span class="form-address__placeholder">必填</span>
  50. </template>
  51. </div>
  52. </template>
  53. </Field>
  54. </CellGroup>
  55. </Form>
  56. <div class="goods-details__content">
  57. <div class="">
  58. <Button type="primary" @click="formRef?.submit" round block>采购下单</Button>
  59. </div>
  60. <div class="" v-if="selectedDate">
  61. <span>参考价(元/{{ getGoodsUnitName(details.goodsinfo?.unitid) }})</span>
  62. <span>{{ selectedDate.unitprice }}</span>
  63. </div>
  64. <Divider>历史价格走势</Divider>
  65. <div class="">
  66. 走势图
  67. </div>
  68. <Divider>商品详情</Divider>
  69. <div class="">
  70. <template v-for="(url, index) in goodsImages" :key="index">
  71. <img :src="url" alt="" />
  72. </template>
  73. </div>
  74. </div>
  75. <component ref="componentRef" :is="componentMap.get(componentId)" v-bind="{ formData }" @updated="updateForm"
  76. @closed="closeComponent" v-if="componentId" />
  77. </app-view>
  78. </template>
  79. <script lang="ts" setup>
  80. import { shallowRef, computed, defineAsyncComponent } from 'vue'
  81. import { CellGroup, Button, Field, Form, FormInstance, Toast, FieldRule, Divider } from 'vant'
  82. import { fullloading } from '@/utils/vant'
  83. import { getImageUrl } from '@/filters'
  84. import { getGoodsUnitName } from '@/constants/unit'
  85. import { useComponent } from '@/hooks/component'
  86. import { useNavigation } from '@/hooks/navigation'
  87. import { useWrstandardDetails } from '@/business/goods'
  88. import { usePurchaseOrderDesting } from '@/business/trade'
  89. import AppSelect from '@mobile/components/base/select/index.vue'
  90. import Long from 'long'
  91. const componentMap = new Map<string, unknown>([
  92. ['address', defineAsyncComponent(() => import('./components/address/index.vue'))],
  93. ])
  94. const { componentRef, componentId, openComponent, closeComponent, closeComponentEach } = useComponent()
  95. const { getQueryStringToNumber, beforeRouteLeave } = useNavigation()
  96. const wrstandardid = getQueryStringToNumber('wrstandardid')
  97. const formRef = shallowRef<FormInstance>()
  98. const { details, getWrstandardDetails } = useWrstandardDetails(wrstandardid)
  99. const { formData, formSubmit } = usePurchaseOrderDesting()
  100. // 交割日期列表
  101. const deliveryDays = shallowRef<Model.THJWrstandardDetailRsp['deliverymonth']>([])
  102. // 当前选中的交割日期
  103. const selectedDate = shallowRef<Model.THJWrstandardDetailRsp['deliverymonth'][number]>()
  104. // 商品图片列表
  105. const goodsImages = computed(() => {
  106. const pictureurls = details.value.goodsinfo?.pictureurls ?? ''
  107. return pictureurls.split(',').map((path) => getImageUrl(path))
  108. })
  109. // 交割月份列表
  110. const deliveryMonths = computed(() => {
  111. const months = details.value.deliverymonth ?? []
  112. const monthMap = new Map<string, { label: string, value: string; }>()
  113. months.forEach(({ endmonth }) => {
  114. if (!monthMap.has(endmonth)) {
  115. monthMap.set(endmonth, {
  116. label: endmonth,
  117. value: endmonth,
  118. })
  119. }
  120. })
  121. return [...monthMap.values()]
  122. })
  123. // 预售申请列表
  124. const presaleApplyDeposits = computed(() => {
  125. const deposits = details.value.presaleapplydeposits ?? []
  126. const presaleApplyId = selectedDate.value?.presaleapplyid
  127. return deposits.filter((e) => e.presaleapplyid === presaleApplyId).map(({ depositid, depositrate }) => ({
  128. label: `${depositrate * 100}%`,
  129. value: depositid
  130. }))
  131. })
  132. // 计算定价
  133. const deposit = computed(() => {
  134. const { Qty = 0, DepositID } = formData.value
  135. const { presaleapplydeposits } = details.value
  136. const { unitprice = 0 } = selectedDate.value ?? {}
  137. const apply = presaleapplydeposits?.find((e) => e.depositid === DepositID) // 选中的支付方式
  138. if (apply) {
  139. return unitprice * apply.depositrate * Qty
  140. }
  141. return 0
  142. })
  143. // 是否显示收货信息
  144. const showAddressInfo = computed(() => {
  145. const { THJDeliveryMode } = formData.value
  146. return THJDeliveryMode === 2 || THJDeliveryMode === 3
  147. })
  148. // 验证收货信息
  149. const validatorAddressInfo = computed(() => {
  150. const { THJDeliveryMode, ContactName, ContactInfo, DesAddress } = formData.value
  151. switch (THJDeliveryMode) {
  152. case 2: {
  153. if (ContactName && ContactInfo) return true
  154. return false
  155. }
  156. case 3: {
  157. if (ContactName && ContactInfo && DesAddress) return true
  158. return false
  159. }
  160. }
  161. return true
  162. })
  163. // 表单验证规则
  164. const formRules: { [key in keyof Proto.SpotPresaleDestingOrderReq | 'addressInfo']?: FieldRule[] } = {
  165. THJDeliveryMode: [{
  166. message: '请选择交割方式',
  167. validator: () => {
  168. return !!formData.value.THJDeliveryMode
  169. }
  170. }],
  171. PresaleApplyID: [
  172. {
  173. message: '请选择交割日期',
  174. validator: () => {
  175. return !!formData.value.PresaleApplyID
  176. }
  177. }
  178. ],
  179. DepositID: [{
  180. message: '请选择支付方式',
  181. validator: () => {
  182. return !!formData.value.DepositID
  183. }
  184. }],
  185. Qty: [{
  186. message: '请输入采购数量',
  187. validator: (val) => {
  188. if (val) {
  189. const qty = Number(val)
  190. const { remainqty = 0 } = selectedDate.value ?? {}
  191. if (qty > 0 && qty <= remainqty) {
  192. return true
  193. }
  194. return '采购数量不正确'
  195. }
  196. return false
  197. }
  198. }],
  199. addressInfo: [{
  200. message: '请输入收货信息',
  201. validator: () => {
  202. return validatorAddressInfo.value
  203. }
  204. }],
  205. }
  206. // 切换交割月份
  207. const onMonthChange = (value: string) => {
  208. const months = details.value.deliverymonth ?? []
  209. deliveryDays.value = months.filter((e) => e.endmonth === value) // 重新过滤交割日期列表
  210. selectedDate.value = undefined // 清除选中的交割日期
  211. formData.value.PresaleApplyID = undefined
  212. formRef.value?.validate('PresaleApplyID')
  213. }
  214. // 切换交割日期
  215. const onDayChange = (value: string) => {
  216. const months = details.value.deliverymonth ?? []
  217. selectedDate.value = months.find((e) => e.presaleapplyid === value)
  218. formData.value.PresaleApplyID = Long.fromString(value)
  219. formData.value.DepositID = undefined
  220. formRef.value?.validate('PresaleApplyID')
  221. }
  222. // 更新表单数据
  223. const updateForm = (formValue: Proto.SpotPresaleDestingOrderReq) => {
  224. formData.value = formValue
  225. }
  226. const onSubmit = () => {
  227. fullloading(() => {
  228. formSubmit().then(() => {
  229. Toast.success('下单成功')
  230. }).catch((err) => {
  231. Toast.fail(err)
  232. })
  233. })
  234. }
  235. getWrstandardDetails()
  236. beforeRouteLeave(() => closeComponentEach())
  237. </script>
  238. <style lang="less">
  239. @import './index.less';
  240. </style>