purchase-form.vue 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. <!-- 采购流程专用表单组件 -->
  2. <template>
  3. <view class="purchase-form-component">
  4. <!-- 基本信息 -->
  5. <uni-card>
  6. <uni-section titleFontSize="1.3rem" title="基本信息" type="line"></uni-section>
  7. <uni-forms ref="baseFormRef" :modelValue="baseForm" :rules="baseFormRules" label-position="left" :label-width="100" :border="true">
  8. <uni-forms-item name="contractPurchaseNumber" label="采购单号">
  9. <uni-easyinput v-model="baseForm.contractPurchaseNumber" disabled placeholder="自动生成"></uni-easyinput>
  10. </uni-forms-item>
  11. <uni-forms-item name="contractPurchaseName" label="采购单名称" required>
  12. <uni-easyinput v-model="baseForm.contractPurchaseName"
  13. :disabled="!isInitiateOrFieldEditable('contract_purchase_name')"
  14. :placeholder="'请输入采购单名称'"
  15. :clearable="false"></uni-easyinput>
  16. </uni-forms-item>
  17. <uni-forms-item name="applyDate" label="申请日期">
  18. <uni-easyinput v-model="baseForm.applyDate" type="date" disabled />
  19. </uni-forms-item>
  20. <uni-forms-item name="department" label="申请部门">
  21. <uni-easyinput v-model="baseForm.department" disabled></uni-easyinput>
  22. </uni-forms-item>
  23. <!-- 部门意见 -->
  24. <uni-forms-item label="部门意见" name="departmentalOpinion">
  25. <view class="element_value_container">
  26. <view v-if="isApprovalFieldEditable(departmentalOpinionElem)" class="element_value">
  27. <!-- 审批签字板 -->
  28. <view v-if="departmentalOpinionElem && departmentalOpinionElem.defaultValue == ''">
  29. <uni-row>
  30. <uni-col :span="24">
  31. <button type="primary" @click="handleApprovalSignature('departmental_opinion')">手动签名</button>
  32. </uni-col>
  33. <uni-col :span="24">
  34. <button style="margin-top: 5px;" type="primary" @click="handleAutoSeal('departmental_opinion')">一键签名</button>
  35. </uni-col>
  36. </uni-row>
  37. </view>
  38. <view v-else-if="departmentalOpinionElem && departmentalOpinionElem.defaultValue" class="signature_img">
  39. <img style="width: 100%;" mode="widthFix" @click="handleApprovalSignature('departmental_opinion')"
  40. :src="config.baseUrlPre + departmentalOpinionElem.sealImgPath"
  41. :alt="departmentalOpinionElem.elementName + '签名'" />
  42. </view>
  43. </view>
  44. <view v-else-if="departmentalOpinionElem && typeof departmentalOpinionElem.sealImgPath === 'string' && departmentalOpinionElem.sealImgPath.startsWith('/shares')" class="signature_img">
  45. <img style="width: 100%;" mode="widthFix"
  46. :src="config.baseUrlPre + departmentalOpinionElem.sealImgPath" />
  47. </view>
  48. </view>
  49. </uni-forms-item>
  50. <!-- 分管副总意见 -->
  51. <uni-forms-item label="分管副总" name="dgmOpinion">
  52. <view class="element_value_container">
  53. <view v-if="isApprovalFieldEditable(deputyGeneralManagerOpinionElem)" class="element_value">
  54. <!-- 审批签字板 -->
  55. <view v-if="deputyGeneralManagerOpinionElem && deputyGeneralManagerOpinionElem.defaultValue == ''">
  56. <uni-row>
  57. <uni-col :span="24">
  58. <button type="primary" @click="handleApprovalSignature('deputy_general_manager_opinion')">手动签名</button>
  59. </uni-col>
  60. <uni-col :span="24">
  61. <button style="margin-top: 5px;" type="primary" @click="handleAutoSeal('deputy_general_manager_opinion')">一键签名</button>
  62. </uni-col>
  63. </uni-row>
  64. </view>
  65. <view v-else-if="deputyGeneralManagerOpinionElem && deputyGeneralManagerOpinionElem.defaultValue" class="signature_img">
  66. <img style="width: 100%;" mode="widthFix" @click="handleApprovalSignature('deputy_general_manager_opinion')"
  67. :src="config.baseUrlPre + deputyGeneralManagerOpinionElem.sealImgPath"
  68. :alt="deputyGeneralManagerOpinionElem.elementName + '签名'" />
  69. </view>
  70. </view>
  71. <view v-else-if="deputyGeneralManagerOpinionElem && typeof deputyGeneralManagerOpinionElem.sealImgPath === 'string' && deputyGeneralManagerOpinionElem.sealImgPath.startsWith('/shares')" class="signature_img">
  72. <img style="width: 100%;" mode="widthFix"
  73. :src="config.baseUrlPre + deputyGeneralManagerOpinionElem.sealImgPath" />
  74. </view>
  75. </view>
  76. </uni-forms-item>
  77. <!-- 审核副总意见 -->
  78. <uni-forms-item label="分管副总" name="auditDgmOpinion">
  79. <view class="element_value_container">
  80. <view v-if="isApprovalFieldEditable(auditDeputyGeneralManagerOpinionElem)" class="element_value">
  81. <!-- 审批签字板 -->
  82. <view v-if="auditDeputyGeneralManagerOpinionElem && auditDeputyGeneralManagerOpinionElem.defaultValue == ''">
  83. <uni-row>
  84. <uni-col :span="24">
  85. <button type="primary" @click="handleApprovalSignature('audit_deputy_general_manager_opinion')">手动签名</button>
  86. </uni-col>
  87. <uni-col :span="24">
  88. <button style="margin-top: 5px;" type="primary" @click="handleAutoSeal('audit_deputy_general_manager_opinion')">一键签名</button>
  89. </uni-col>
  90. </uni-row>
  91. </view>
  92. <view v-else-if="auditDeputyGeneralManagerOpinionElem && auditDeputyGeneralManagerOpinionElem.defaultValue" class="signature_img">
  93. <img style="width: 100%;" mode="widthFix" @click="handleApprovalSignature('audit_deputy_general_manager_opinion')"
  94. :src="config.baseUrlPre + auditDeputyGeneralManagerOpinionElem.sealImgPath"
  95. :alt="auditDeputyGeneralManagerOpinionElem.elementName + '签名'" />
  96. </view>
  97. </view>
  98. <view v-else-if="auditDeputyGeneralManagerOpinionElem && typeof auditDeputyGeneralManagerOpinionElem.sealImgPath === 'string' && auditDeputyGeneralManagerOpinionElem.sealImgPath.startsWith('/shares')" class="signature_img">
  99. <img style="width: 100%;" mode="widthFix"
  100. :src="config.baseUrlPre + auditDeputyGeneralManagerOpinionElem.sealImgPath" />
  101. </view>
  102. </view>
  103. </uni-forms-item>
  104. <!-- 总经理意见 -->
  105. <uni-forms-item label="总经理" name="gmOpinion">
  106. <view class="element_value_container">
  107. <view v-if="isApprovalFieldEditable(generalManagerOpinionElem)" class="element_value">
  108. <!-- 审批签字板 -->
  109. <view v-if="generalManagerOpinionElem && generalManagerOpinionElem.defaultValue == ''">
  110. <uni-row>
  111. <uni-col :span="24">
  112. <button type="primary" @click="handleApprovalSignature('general_manager_opinion')">手动签名</button>
  113. </uni-col>
  114. <uni-col :span="24">
  115. <button style="margin-top: 5px;" type="primary" @click="handleAutoSeal('general_manager_opinion')">一键签名</button>
  116. </uni-col>
  117. </uni-row>
  118. </view>
  119. <view v-else-if="generalManagerOpinionElem && generalManagerOpinionElem.defaultValue" class="signature_img">
  120. <img style="width: 100%;" mode="widthFix" @click="handleApprovalSignature('general_manager_opinion')"
  121. :src="config.baseUrlPre + generalManagerOpinionElem.sealImgPath"
  122. :alt="generalManagerOpinionElem.elementName + '签名'" />
  123. </view>
  124. </view>
  125. <view v-else-if="generalManagerOpinionElem && typeof generalManagerOpinionElem.sealImgPath === 'string' && generalManagerOpinionElem.sealImgPath.startsWith('/shares')" class="signature_img">
  126. <img style="width: 100%;" mode="widthFix"
  127. :src="config.baseUrlPre + generalManagerOpinionElem.sealImgPath" />
  128. </view>
  129. </view>
  130. </uni-forms-item>
  131. </uni-forms>
  132. </uni-card>
  133. <!-- 物料明细 -->
  134. <uni-card>
  135. <uni-section titleFontSize="1.3rem" title="物料明细" type="line"></uni-section>
  136. <!-- 添加物料按钮:发起环节或字段可编辑时显示 -->
  137. <view v-if="isSeModel" class="material-actions">
  138. <button type="primary" size="mini" @click="openMaterialSelector" plain>添加物料</button>
  139. </view>
  140. <!-- 物料列表 - 卡片式展示 -->
  141. <view v-if="materialList.length > 0" class="material-list">
  142. <view v-for="(item, index) in materialList" :key="'material-' + item.materialCode + '-' + index" class="material-card">
  143. <view class="material-header" @click="toggleExpand(index)">
  144. <view class="material-main-info">
  145. <text class="material-name">{{ item.materialName }}</text>
  146. <text class="material-code">{{ item.materialCode }}</text>
  147. </view>
  148. <view class="material-expand">
  149. <uni-icons :type="item.expanded ? 'up' : 'down'" size="16" color="#999"></uni-icons>
  150. </view>
  151. </view>
  152. <!-- 展开的详细信息 -->
  153. <view v-show="item.expanded" class="material-detail">
  154. <view class="detail-row">
  155. <text class="detail-label">规格型号:</text>
  156. <text class="detail-value">{{ item.materialModel }}</text>
  157. <text class="detail-label" style="margin-left: 15px;">单位:</text>
  158. <text class="detail-value">{{ item.measureName }}</text>
  159. </view>
  160. <view class="detail-row">
  161. <text class="detail-label">申请人:</text>
  162. <text class="detail-value">{{ item.applyUser || '-' }}</text>
  163. <text class="detail-label">用途:</text>
  164. <text class="detail-value">{{ item.remark || '-' }}</text>
  165. </view>
  166. <view class="detail-row delete-row">
  167. <text class="detail-label">数量:</text>
  168. <uni-easyinput
  169. v-model="item.qty"
  170. type="digit"
  171. v-if="isSeModel"
  172. placeholder="请输入数量"
  173. style="width: 100px; display: inline-block; margin-right: 15px;"
  174. />
  175. <text v-if="!isSeModel" class="detail-value">{{ item.qty }}</text>
  176. <button v-if="isSeModel" type="warn" size="mini" @click="removeMaterial(index)">删除</button>
  177. </view>
  178. </view>
  179. </view>
  180. </view>
  181. <view v-else-if="materialList.length === 0" class="empty-materials">
  182. <text>暂无物料</text>
  183. </view>
  184. </uni-card>
  185. <!-- 选择器弹出层 -->
  186. <uni-popup ref="selectorPopup" type="center">
  187. <view class="selector-popup">
  188. <view class="popup-header">
  189. <text class="popup-title">选择物料</text>
  190. <uni-icons type="closeempty" size="20" @click="closePopup"></uni-icons>
  191. </view>
  192. <!-- 搜索框 -->
  193. <view class="search-bar">
  194. <uni-easyinput
  195. v-model="searchKeyword"
  196. placeholder="输入物料名称搜索"
  197. clearable
  198. @confirm="handleSearch"
  199. />
  200. <button type="primary" size="mini" @click="handleSearch">搜索</button>
  201. </view>
  202. <scroll-view
  203. scroll-y
  204. class="popup-content"
  205. refresher-enabled
  206. :refresher-triggered="isRefreshing"
  207. @refresh="onRefresh"
  208. @scrolltolower="loadMore"
  209. >
  210. <view v-for="(item, index) in selectorList" :key="index"
  211. class="selector-item"
  212. @click="selectItem(item)">
  213. <view class="selector-item-content">
  214. <view class="item-info">
  215. <text class="item-name">{{ item.materialName || item.itemName }}</text>
  216. <text class="item-code">{{ item.materialCode || item.itemCode }}</text>
  217. <text class="item-spec">{{ item.materialModel || item.specification || '' }}</text>
  218. <text class="item-unit">{{ item.measureName || item.unit || '' }}</text>
  219. </view>
  220. <text v-if="isSelected(item)" class="selected-tag">已选择</text>
  221. </view>
  222. </view>
  223. <!-- 加载状态 -->
  224. <view v-if="isLoading && selectorList.length > 0" class="loading-text">
  225. <uni-load-more status="loading" />
  226. </view>
  227. <!-- 没有更多数据 -->
  228. <view v-else-if="!hasMore && selectorList.length > 0" class="no-more-text">
  229. <text>没有更多了</text>
  230. </view>
  231. <!-- 空数据提示 -->
  232. <view v-if="selectorList.length === 0 && !isLoading" class="empty-data">
  233. <text>{{ searchKeyword ? '暂无相关数据' : '暂无数据' }}</text>
  234. </view>
  235. </scroll-view>
  236. </view>
  237. </uni-popup>
  238. <!-- 签名板弹出层 -->
  239. <uni-popup ref="signaturePopup" @maskClick="closeSignature">
  240. <view class="signature_container" :class="{ 'signature_container_landscape': isLandscape }">
  241. <view class="signature_content">
  242. <l-signature ref="signatureRef" v-if="signaturePopupShow" :landscape="isLandscape" :penSize="8"
  243. :minLineWidth="4" :maxLineWidth="12" :openSmooth="true" :preferToDataURL="true"
  244. backgroundColor="#ffffff" penColor="black"></l-signature>
  245. </view>
  246. <view class="signature_button_container">
  247. <uni-row :gutter="10">
  248. <uni-col :span="6">
  249. <button type="warn" @click="onclickSignatureButton('undo')">撤销</button>
  250. </uni-col>
  251. <uni-col :span="6">
  252. <button type="warn" @click="onclickSignatureButton('clear')">清空</button>
  253. </uni-col>
  254. <uni-col :span="6">
  255. <button type="primary" @click="onclickSignatureButton('save')">保存</button>
  256. </uni-col>
  257. <uni-col :span="6">
  258. <button @click="onclickSignatureButton('landscape')">全屏</button>
  259. </uni-col>
  260. </uni-row>
  261. </view>
  262. </view>
  263. </uni-popup>
  264. </view>
  265. </template>
  266. <script setup lang="ts">
  267. import { ref, watch, computed, nextTick } from 'vue'
  268. import { getMaterialList } from '@/api/purchase.js'
  269. import { useUserStore } from '@/store/user.js'
  270. import $modal from '@/plugins/modal.js'
  271. import config from '@/config.js'
  272. import { uploadSignatureBoardImg, getSeal } from '@/api/process.js'
  273. const userStore = useUserStore()
  274. const props = defineProps({
  275. formData: {
  276. type: Object,
  277. default: () => ({})
  278. },
  279. formElements: {
  280. type: Array,
  281. default: () => []
  282. },
  283. repeatingForm: {
  284. type: Object,
  285. default: () => ({ elementItem: [], elements: [] })
  286. },
  287. // 是否为发起环节(seModel == '1')
  288. isInitiate: {
  289. type: Boolean,
  290. default: false
  291. },
  292. // 当前环节可编辑的字段列表
  293. editableFields: {
  294. type: Array,
  295. default: () => []
  296. },
  297. // 当前环节的审批意见配置
  298. currentTacheOpinion: {
  299. type: Object,
  300. default: null
  301. }
  302. })
  303. const emits = defineEmits(['update', 'signature-change'])
  304. // 表单数据
  305. const baseForm = ref<any>({
  306. contractPurchaseNumber: '',
  307. contractPurchaseName: '',
  308. applyDate: '',
  309. department: ''
  310. })
  311. const baseFormRules = ref({
  312. contractPurchaseName: [
  313. {
  314. required: true,
  315. errorMessage: '请输入采购单名称'
  316. }
  317. ]
  318. })
  319. const materialList = ref<any[]>([])
  320. const selectorList = ref<any[]>([])
  321. const searchKeyword = ref('')
  322. const isLoading = ref(false)
  323. const hasMore = ref(true)
  324. const currentPage = ref(1)
  325. const pageSize = ref(20)
  326. const isRefreshing = ref(false)
  327. const selectorPopup = ref(null)
  328. const signaturePopup = ref(null)
  329. const signatureRef = ref(null)
  330. const signatureImg = ref('')
  331. const currentApprovalText = ref('')
  332. const approvals = ref<any[]>([]) // 历史审批意见列表
  333. const signaturePopupShow = ref(false)
  334. const isLandscape = ref(false)
  335. // 获取指定的审批意见元素
  336. const getApprovalElement = (fieldName: string) => {
  337. if (!props.formElements || !Array.isArray(props.formElements)) {
  338. return null
  339. }
  340. // 从 formElements 中找到对应的字段
  341. const elem = props.formElements.find(e => e.tableField === fieldName || e.elementName.includes(fieldName.replace(/_/g, '')))
  342. return elem || null
  343. }
  344. // 获取部门意见元素(计算属性,避免重复调用)
  345. const departmentalOpinionElem = computed(() => getApprovalElement('departmental_opinion'))
  346. // 获取副总经理意见元素
  347. const deputyGeneralManagerOpinionElem = computed(() => getApprovalElement('deputy_general_manager_opinion'))
  348. // 获取审计副总经理意见元素
  349. const auditDeputyGeneralManagerOpinionElem = computed(() => getApprovalElement('audit_deputy_general_manager_opinion'))
  350. // 获取总经理意见元素
  351. const generalManagerOpinionElem = computed(() => getApprovalElement('general_manager_opinion'))
  352. // 是否发起环节
  353. const isSeModel = computed(() => props.isInitiate)
  354. // 计算各部门意见是否可编辑
  355. const isApprovalFieldEditable = (elem: any) => {
  356. if (!elem) return false
  357. return props.editableFields.includes(elem.tableField)
  358. }
  359. // 处理审批意见签名
  360. let lastFormInsId = ''
  361. // 计算字段是否可编辑 (只需要该字段在 table_fields 中即可)
  362. const getFieldEditable = (fieldName: string) => {
  363. // 如果没有配置 editableFields,则都不可编辑
  364. if (!props.editableFields || props.editableFields.length === 0) {
  365. return false
  366. }
  367. // 检查该字段是否在可编辑字段列表中 (table_fields 包含的字段才可以编辑)
  368. return props.editableFields.includes(fieldName)
  369. }
  370. // 计算是否为发起环节或字段可编辑 (满足任一条件即可)
  371. const isInitiateOrFieldEditable = (fieldName: string) => {
  372. return props.isInitiate || getFieldEditable(fieldName)
  373. }
  374. // 监听表单数据变化
  375. watch(() => props.formData, (newVal) => {
  376. if (!newVal || Object.keys(newVal).length === 0) {
  377. return
  378. }
  379. // 使用 formInsId 判断是否是新的数据(formInsId 肯定存在)
  380. const currentFormInsId = newVal.lFormInsId || newVal.universalid || ''
  381. // 只有当 formInsId 变化时,才认为是新的数据需要加载
  382. if (currentFormInsId && currentFormInsId !== lastFormInsId) {
  383. lastFormInsId = currentFormInsId
  384. // 填充基本信息
  385. baseForm.value = {
  386. contractPurchaseNumber: newVal.contractPurchaseNumber || '',
  387. contractPurchaseName: newVal.contractPurchaseName || '',
  388. applyDate: newVal.applyDate || '',
  389. department: newVal.department || ''
  390. }
  391. // 从 details 或 detailList 中获取物料列表 (后端返回的是 details)
  392. const rawList = newVal.details || newVal.detailList || []
  393. // 加载物料数据 (即使为空也加载,因为可能是新增场景)
  394. if (Array.isArray(rawList)) {
  395. materialList.value = rawList.map((item: any) => ({
  396. materialCode: item.materialCode || item.itemCode,
  397. materialName: item.materialName || item.itemName,
  398. materialModel: item.materialModel || item.specification,
  399. measureName: item.measureName || item.unit,
  400. itemTypeName: item.itemTypeName || '',
  401. qty: item.qty || 0,
  402. applyUser: item.applyUser || '', // 申请人
  403. remark: item.remark || '', // 用途
  404. applyId: item.applyId || null, // 申请ID(用于数据完整性)
  405. applyDeptName: item.applyDeptName || '', // 申请部门名称(用于数据完整性)
  406. expanded: true // 默认展开
  407. }))
  408. } else {
  409. materialList.value = []
  410. }
  411. }
  412. }, { immediate: true, deep: true })
  413. // 切换展开/收起状态
  414. function toggleExpand(index: number) {
  415. if (materialList.value[index]) {
  416. materialList.value[index].expanded = !materialList.value[index].expanded
  417. }
  418. }
  419. // 打开物料选择器
  420. async function openMaterialSelector() {
  421. currentPage.value = 1
  422. hasMore.value = true
  423. selectorList.value = []
  424. searchKeyword.value = ''
  425. await loadMaterialList(1)
  426. selectorPopup.value.open()
  427. }
  428. // 关闭弹出层
  429. function closePopup() {
  430. selectorPopup.value.close()
  431. }
  432. // 加载物料列表
  433. function loadMaterialList(page: number = 1) {
  434. isLoading.value = true
  435. getMaterialList(userStore.user.useId, page, pageSize.value, searchKeyword.value)
  436. .then(({ returnParams }) => {
  437. const list = returnParams.list || []
  438. const total = returnParams.total || 0
  439. if (page === 1) {
  440. selectorList.value = list
  441. } else {
  442. selectorList.value = [...selectorList.value, ...list]
  443. }
  444. hasMore.value = selectorList.value.length < total
  445. currentPage.value = page
  446. })
  447. .catch(err => {
  448. console.error('加载物料失败:', err)
  449. $modal.msgError('加载物料失败')
  450. })
  451. .finally(() => {
  452. isLoading.value = false
  453. isRefreshing.value = false
  454. })
  455. }
  456. // 搜索
  457. function handleSearch() {
  458. loadMaterialList(1)
  459. }
  460. // 刷新
  461. function onRefresh() {
  462. isRefreshing.value = true
  463. loadMaterialList(1)
  464. }
  465. // 加载更多
  466. function loadMore() {
  467. if (!isLoading.value && hasMore.value) {
  468. loadMaterialList(currentPage.value + 1)
  469. }
  470. }
  471. // 选择物料
  472. async function selectItem(item: any) {
  473. // 检查是否已选择
  474. const exists = materialList.value.find(m =>
  475. m.materialCode === item.materialCode ||
  476. m.materialCode === item.itemCode
  477. )
  478. if (!exists) {
  479. // 使用完整的字段映射(参考 start.vue)
  480. const newItem = {
  481. materialCode: item.materialCode || item.itemCode,
  482. materialName: item.materialName || item.itemName,
  483. materialModel: item.materialModel || item.specification,
  484. measureName: item.measureName || item.unit,
  485. itemTypeName: item.itemTypeName || '',
  486. qty: 0,
  487. applyUser: '', // 申请人(新增时为空)
  488. remark: '', // 用途(新增时为空)
  489. applyId: null, // 申请ID(用于数据完整性)
  490. applyDeptName: '', // 申请部门名称(用于数据完整性)
  491. expanded: true
  492. }
  493. materialList.value = [...materialList.value, newItem]
  494. await nextTick()
  495. emitUpdate()
  496. }
  497. closePopup()
  498. }
  499. // 判断是否已选择
  500. function isSelected(item: any) {
  501. return materialList.value.some(m => m.materialCode === item.materialCode)
  502. }
  503. // 删除物料
  504. async function removeMaterial(index: number) {
  505. uni.showModal({
  506. title: '',
  507. content: '确认删除该物料?',
  508. success: async (res) => {
  509. if (res.confirm) {
  510. // 使用新数组替换旧数组,确保触发响应式更新
  511. materialList.value = materialList.value.filter((_, i) => i !== index)
  512. await nextTick()
  513. emitUpdate()
  514. }
  515. }
  516. })
  517. }
  518. // 发送更新事件
  519. function emitUpdate() {
  520. emits('update', {
  521. ...baseForm.value,
  522. detailList: materialList.value
  523. })
  524. }
  525. // 处理审批意见签名
  526. const currentApprovalFieldName = ref('')
  527. // 存储每个签名字段的印章信息
  528. const approvalSealInfo = ref<Record<string, any>>({})
  529. function handleApprovalSignature(fieldName: string) {
  530. currentApprovalFieldName.value = fieldName
  531. signaturePopupShow.value = false
  532. nextTick(() => {
  533. signaturePopupShow.value = true
  534. })
  535. signaturePopup.value.open()
  536. }
  537. // 处理一键签名
  538. function handleAutoSeal(fieldName: string) {
  539. const elem = getApprovalElement(fieldName)
  540. if (elem) {
  541. getSeal(userStore.user.useId).then(({ returnParams }) => {
  542. const elem = getApprovalElement(fieldName)
  543. elem.defaultValue = returnParams.sealFileId.universalid
  544. elem.sealImgPath = returnParams.sealFileId.path
  545. // 保存印章信息(用于后端处理)
  546. // 一键签名时,sealFileId.universalid 就是印章模板 ID
  547. approvalSealInfo.value[fieldName] = {
  548. sealInsId: returnParams.sealFileId.universalid, // 签名实例 ID(这里与印章模板 ID 相同)
  549. sealFileId: returnParams.sealFileId.universalid, // 印章模板 ID(用于 imgval)
  550. left: 0, // 默认左边距为 0
  551. top: 0 // 默认上边距为 0
  552. }
  553. }).catch(err => {
  554. $modal.msgError('获取签名失败:' + err)
  555. })
  556. }
  557. }
  558. // 初始化签字板
  559. function initSignature() {
  560. signaturePopupShow.value = false
  561. setTimeout(() => {
  562. signaturePopupShow.value = true
  563. }, 100)
  564. }
  565. // 点击签字板按钮
  566. function onclickSignatureButton(event: string) {
  567. switch (event) {
  568. case 'undo':
  569. signatureRef.value.undo()
  570. break
  571. case 'clear':
  572. signatureRef.value.clear()
  573. break
  574. case 'save':
  575. signatureRef.value.canvasToTempFilePath({
  576. success: (res: any) => {
  577. if (res.isEmpty) {
  578. $modal.msgError('签名不能为空!')
  579. return
  580. }
  581. // 判断上传文件是否是 base64
  582. if (res.tempFilePath.substring(0, 'data:image/png;base64,'.length) == 'data:image/png;base64,') {
  583. const _fileData = res.tempFilePath
  584. uploadSignatureBoardImg(userStore.user.useId, _fileData, getApprovalElement(currentApprovalFieldName.value).tableField)
  585. .then(({ returnParams }) => {
  586. const elem = getApprovalElement(currentApprovalFieldName.value)
  587. elem.defaultValue = returnParams.sealInsID
  588. elem.sealImgPath = returnParams.path
  589. // 保存印章信息(用于后端处理)
  590. // sealInsID 是手写签名的实例 ID
  591. approvalSealInfo.value[currentApprovalFieldName.value] = {
  592. sealInsId: returnParams.sealInsID, // 签名实例 ID
  593. sealFileId: returnParams.sealInsID, // 手写签名图片 ID
  594. left: 0, // 默认左边距为 0
  595. top: 0 // 默认上边距为 0
  596. }
  597. currentApprovalFieldName.value = ''
  598. signaturePopupShow.value = false
  599. signaturePopup.value.close()
  600. })
  601. } else {
  602. // 转 base64
  603. uni.getFileSystemManager().readFile({
  604. filePath: res.tempFilePath,
  605. encoding: 'base64',
  606. success: (fileData: any) => {
  607. const _fileData = 'data:image/png;base64,' + fileData.data
  608. uploadSignatureBoardImg(userStore.user.useId, _fileData, getApprovalElement(currentApprovalFieldName.value).tableField)
  609. .then(({ returnParams }) => {
  610. const elem = getApprovalElement(currentApprovalFieldName.value)
  611. elem.defaultValue = returnParams.sealInsID
  612. elem.sealImgPath = returnParams.path
  613. // 保存印章信息(用于后端处理)
  614. // sealInsID 是手写签名的实例 ID
  615. approvalSealInfo.value[currentApprovalFieldName.value] = {
  616. sealInsId: returnParams.sealInsID, // 签名实例 ID
  617. sealFileId: returnParams.sealInsID, // 手写签名图片 ID
  618. left: 0, // 默认左边距为 0
  619. top: 0 // 默认上边距为 0
  620. }
  621. currentApprovalFieldName.value = ''
  622. signaturePopupShow.value = false
  623. signaturePopup.value.close()
  624. })
  625. }
  626. })
  627. }
  628. }
  629. })
  630. break
  631. case 'landscape':
  632. isLandscape.value = !isLandscape.value
  633. initSignature()
  634. break
  635. }
  636. }
  637. // 清空签名
  638. function clearSignature() {
  639. onclickSignatureButton('clear')
  640. }
  641. // 保存签名
  642. function saveSignature() {
  643. onclickSignatureButton('save')
  644. }
  645. // 关闭签名
  646. function closeSignature() {
  647. signaturePopupShow.value = false
  648. signaturePopup.value.close()
  649. }
  650. // 暴露验证方法给父组件
  651. defineExpose({
  652. validate: async () => {
  653. // 采购表单验证
  654. if (!baseForm.value.contractPurchaseName) {
  655. return Promise.reject(new Error('请输入采购单名称'))
  656. }
  657. // 验证采购单名称长度
  658. if (baseForm.value.contractPurchaseName && baseForm.value.contractPurchaseName.length > 100) {
  659. return Promise.reject(new Error('采购单名称长度不能超过 100 个字符'))
  660. }
  661. if (!materialList.value || materialList.value.length === 0) {
  662. return Promise.reject(new Error('请至少添加一个物料'))
  663. }
  664. // 验证物料数量(发起环节或字段可编辑时)
  665. if (isSeModel.value && materialList.value.length > 0) {
  666. for (let i = 0; i < materialList.value.length; i++) {
  667. const item = materialList.value[i]
  668. // 验证数量不能为空且必须大于 0
  669. if (!item.qty || Number(item.qty) <= 0) {
  670. return Promise.reject(new Error(`请填写第${i + 1}个物料的数量,且必须大于 0`))
  671. }
  672. }
  673. }
  674. // 验证审批意见字段(如果在 table_fields 中)
  675. const approvalFields = [
  676. { fieldName: 'departmental_opinion', msg: '部门意见' },
  677. { fieldName: 'deputy_general_manager_opinion', msg: '副总经理意见' },
  678. { fieldName: 'audit_deputy_general_manager_opinion', msg: '审核副总意见' },
  679. { fieldName: 'general_manager_opinion', msg: '总经理意见' }
  680. ]
  681. for (const field of approvalFields) {
  682. const elem = getApprovalElement(field.fieldName)
  683. if (elem) {
  684. // 检查该字段是否在当前环节的可编辑字段列表中
  685. // editableFields 包含了当前环节可编辑的所有字段(即 table_fields)
  686. if (props.editableFields && props.editableFields.includes(field.fieldName)) {
  687. if (!elem.defaultValue || elem.defaultValue === '') {
  688. return Promise.reject(new Error(field.msg + '不能为空!'))
  689. }
  690. }
  691. }
  692. }
  693. return Promise.resolve()
  694. },
  695. // 获取表单数据(返回 formElements 格式)
  696. getFormElements: () => {
  697. const formElements: any[] = []
  698. // 添加基本信息字段
  699. Object.keys(baseForm.value).forEach(key => {
  700. const value = baseForm.value[key]
  701. if (value !== undefined && value !== null) {
  702. formElements.push({
  703. name: key,
  704. value: String(value),
  705. type: '0' // 普通文本类型
  706. })
  707. }
  708. })
  709. // 添加审批意见字段
  710. const approvalFields = ['departmental_opinion', 'deputy_general_manager_opinion', 'audit_deputy_general_manager_opinion', 'general_manager_opinion']
  711. approvalFields.forEach(fieldName => {
  712. const elem = getApprovalElement(fieldName)
  713. if (elem) {
  714. formElements.push({
  715. name: fieldName,
  716. value: elem.defaultValue || '',
  717. type: elem.type || '0'
  718. })
  719. // 添加签名图片的 imgval 值(用于后端处理)
  720. // 格式:sealFileId_left_top(必须使用印章模板 ID,而不是签名实例 ID)
  721. if (approvalSealInfo.value[fieldName]) {
  722. const sealInfo = approvalSealInfo.value[fieldName]
  723. formElements.push({
  724. name: fieldName + '_imgval',
  725. value: `${sealInfo.sealFileId}_${sealInfo.left}_${sealInfo.top}`,
  726. type: '0'
  727. })
  728. } else if (elem.sealImgPath && elem.defaultValue) {
  729. // 兼容旧数据:如果有 sealImgPath 和 defaultValue,提取 sealId 构建 imgval
  730. // sealImgPath 格式:/shares/document/seal/593268258724500.png
  731. const sealIdMatch = elem.sealImgPath.match(/\/seal\/(\d+)\.png$/)
  732. if (sealIdMatch) {
  733. const sealId = sealIdMatch[1]
  734. formElements.push({
  735. name: fieldName + '_imgval',
  736. value: `${sealId}_0_0`,
  737. type: '0'
  738. })
  739. }
  740. }
  741. }
  742. })
  743. // 添加物料明细(JSON 字符串格式)
  744. if (materialList.value && materialList.value.length > 0) {
  745. formElements.push({
  746. name: 'detailList',
  747. value: JSON.stringify(materialList.value),
  748. type: '0'
  749. })
  750. }
  751. return formElements
  752. },
  753. // 获取表单数据(兼容旧版本,返回键值对格式)
  754. getFormData: () => {
  755. const formData: any = {
  756. ...baseForm.value,
  757. detailList: materialList.value || []
  758. }
  759. // 添加审批意见字段数据
  760. const approvalFields = ['departmental_opinion', 'deputy_general_manager_opinion', 'audit_deputy_general_manager_opinion', 'general_manager_opinion']
  761. approvalFields.forEach(fieldName => {
  762. const elem = getApprovalElement(fieldName)
  763. if (elem) {
  764. formData[fieldName] = elem.defaultValue || ''
  765. // 添加签名图片的 imgval 值(用于后端处理)
  766. // 格式:sealFileId_left_top(必须使用印章模板 ID,而不是签名实例 ID)
  767. if (approvalSealInfo.value[fieldName]) {
  768. const sealInfo = approvalSealInfo.value[fieldName]
  769. formData[fieldName + '_imgval'] = `${sealInfo.sealFileId}_${sealInfo.left}_${sealInfo.top}`
  770. } else if (elem.sealImgPath && elem.defaultValue) {
  771. // 兼容旧数据:如果有 sealImgPath 和 defaultValue,提取 sealId 构建 imgval
  772. // sealImgPath 格式:/shares/document/seal/593268258724500.png
  773. const sealIdMatch = elem.sealImgPath.match(/\/seal\/(\d+)\.png$/)
  774. if (sealIdMatch) {
  775. const sealId = sealIdMatch[1]
  776. formData[fieldName + '_imgval'] = `${sealId}_0_0`
  777. }
  778. }
  779. }
  780. })
  781. return formData
  782. }
  783. })
  784. </script>
  785. <style lang="scss" scoped>
  786. /*.purchase-form-component {*/
  787. // 基本信息中的禁用字段样式优化
  788. ::v-deep .uni-forms {
  789. .uni-forms-item__content {
  790. .uni-easyinput__content-input {
  791. font-size: calc(14px + 1.2*(1rem - 16px)) !important;
  792. font-weight: 500;
  793. color: #333;
  794. }
  795. .uni-date {
  796. .uni-icons {
  797. font-size: calc(22px + 1.2*(1rem - 16px)) !important;
  798. font-weight: 500;
  799. }
  800. .uni-date__x-input {
  801. height: auto;
  802. font-size: calc(14px + 1.2*(1rem - 16px)) !important;
  803. font-weight: 500;
  804. color: #333;
  805. }
  806. }
  807. }
  808. }
  809. // 审批意见字段样式(与 index.vue 保持一致)
  810. .element_value_container {
  811. .signature_img {
  812. width: 180px;
  813. }
  814. }
  815. // 签名弹窗样式(与 index.vue 保持一致)
  816. .signature_container {
  817. background-color: #f5f5f5;
  818. height: 40vh;
  819. width: 90vw;
  820. .signature_content {
  821. height: 80%;
  822. width: 100%;
  823. }
  824. .signature_button_container {
  825. height: 20%;
  826. width: 100%;
  827. button {
  828. height: 100%;
  829. }
  830. }
  831. }
  832. .signature_container_landscape {
  833. height: 100vh;
  834. width: 100vw;
  835. .signature_content {
  836. height: 85%;
  837. width: 100%;
  838. }
  839. .signature_button_container {
  840. margin-top: 10%;
  841. height: 15%;
  842. width: 100%;
  843. button {
  844. height: 100%;
  845. width: 100%;
  846. transform: rotate(90deg);
  847. }
  848. }
  849. }
  850. .material-actions {
  851. display: flex;
  852. gap: 10px;
  853. margin-bottom: 15px;
  854. button {
  855. flex: 1;
  856. }
  857. }
  858. // 物料列表 - 卡片式展示
  859. .material-list {
  860. display: flex;
  861. flex-direction: column;
  862. gap: 10px;
  863. }
  864. .material-card {
  865. border: 1px solid #e5e5e5;
  866. border-radius: 6px;
  867. overflow: hidden;
  868. background-color: #fff;
  869. margin-bottom: 8px;
  870. .material-header {
  871. display: flex;
  872. justify-content: space-between;
  873. align-items: center;
  874. padding: 10px 12px;
  875. background-color: #f8f9fa;
  876. cursor: pointer;
  877. .material-main-info {
  878. display: flex;
  879. align-items: center;
  880. gap: 8px;
  881. flex: 1;
  882. .material-name {
  883. font-size: 14px;
  884. font-weight: bold;
  885. color: #333;
  886. }
  887. .material-code {
  888. font-size: 12px;
  889. color: #999;
  890. white-space: nowrap;
  891. }
  892. }
  893. .material-expand {
  894. display: flex;
  895. align-items: center;
  896. margin-left: 10px;
  897. }
  898. }
  899. .material-detail {
  900. padding: 10px 12px;
  901. border-top: 1px solid #e5e5e5;
  902. .detail-row {
  903. display: flex;
  904. align-items: center;
  905. flex-wrap: wrap;
  906. gap: 8px;
  907. .detail-label {
  908. font-size: 13px;
  909. color: #666;
  910. flex-shrink: 0;
  911. }
  912. .detail-value {
  913. flex: 0 0 auto;
  914. font-size: 13px;
  915. color: #333;
  916. }
  917. }
  918. .delete-row {
  919. margin-top: 10px;
  920. text-align: center;
  921. padding-top: 8px;
  922. border-top: 1px dashed #e5e5e5;
  923. }
  924. }
  925. }
  926. .empty-materials {
  927. text-align: center;
  928. padding: 30px;
  929. color: #909399;
  930. font-size: 14px;
  931. }
  932. // 弹窗样式
  933. .selector-popup {
  934. width: 90%;
  935. max-width: 500px;
  936. max-height: 70vh;
  937. background-color: #fff;
  938. border-radius: 12px;
  939. overflow: hidden;
  940. box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  941. /* 确保弹窗居中 */
  942. position: relative;
  943. margin: 0 auto;
  944. .popup-header {
  945. display: flex;
  946. justify-content: space-between;
  947. align-items: center;
  948. padding: 15px;
  949. border-bottom: 1px solid #e5e5e5;
  950. .popup-title {
  951. font-size: 16px;
  952. font-weight: bold;
  953. }
  954. }
  955. .search-bar {
  956. display: flex;
  957. gap: 10px;
  958. padding: 10px 15px;
  959. align-items: center;
  960. }
  961. .popup-content {
  962. max-height: 50vh;
  963. .selector-item {
  964. padding: 10px 12px;
  965. border-bottom: 1px solid #f0f0f0;
  966. &:active {
  967. background-color: #f5f5f5;
  968. }
  969. .selector-item-content {
  970. display: flex;
  971. justify-content: space-between;
  972. align-items: center;
  973. gap: 8px;
  974. .item-info {
  975. flex: 1;
  976. display: flex;
  977. flex-wrap: wrap;
  978. align-items: center;
  979. gap: 4px;
  980. .item-name {
  981. font-size: 15px;
  982. font-weight: bold;
  983. color: #333;
  984. flex-basis: 100%;
  985. margin-bottom: 4px;
  986. }
  987. .item-code,
  988. .item-spec,
  989. .item-unit {
  990. font-size: 13px;
  991. color: #666;
  992. white-space: nowrap;
  993. font-weight: 500;
  994. }
  995. .item-code::after,
  996. .item-spec::after,
  997. .item-unit::after {
  998. content: ' | ';
  999. margin: 0 4px;
  1000. color: #ddd;
  1001. }
  1002. .item-unit:last-child::after {
  1003. content: '';
  1004. }
  1005. }
  1006. .selected-tag {
  1007. color: #409eff;
  1008. font-size: 12px;
  1009. flex-shrink: 0;
  1010. white-space: nowrap;
  1011. }
  1012. }
  1013. }
  1014. .loading-text {
  1015. text-align: center;
  1016. padding: 12px;
  1017. color: #909399;
  1018. font-size: 13px;
  1019. }
  1020. .no-more-text {
  1021. text-align: center;
  1022. padding: 12px;
  1023. color: #909399;
  1024. font-size: 13px;
  1025. }
  1026. .empty-data {
  1027. text-align: center;
  1028. padding: 30px;
  1029. color: #909399;
  1030. }
  1031. }
  1032. }
  1033. /*}*/
  1034. </style>