apply.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <template>
  2. <view class="bx-page bx-page--form">
  3. <view class="topbar" :style="safeAreaStyle">
  4. <view class="topbar-left">
  5. <text class="topbar-back" @click="goBack">←</text>
  6. <text class="topbar-title">备用金申请</text>
  7. </view>
  8. <view class="topbar-right" />
  9. </view>
  10. <view class="page-padded">
  11. <view class="form-card">
  12. <view class="form-header">
  13. <text class="form-header-title">备用金信息</text>
  14. <text class="form-badge">{{ isEdit ? '编辑' : '新建' }}</text>
  15. </view>
  16. <view class="field">
  17. <text class="field-label">申请原因<text class="req">*</text></text>
  18. <textarea
  19. class="form-textarea"
  20. :class="{ error: fieldErr.reason }"
  21. :value="form.reason"
  22. placeholder="请输入申请原因"
  23. :maxlength="500"
  24. @input="onReasonInput"
  25. />
  26. </view>
  27. <view class="field">
  28. <text class="field-label">申请金额<text class="req">*</text></text>
  29. <view class="form-input-prefix">
  30. <input
  31. class="form-input"
  32. :class="{ error: fieldErr.amount }"
  33. type="digit"
  34. :value="form.amount"
  35. placeholder="请输入金额"
  36. @input="onAmountInput"
  37. />
  38. </view>
  39. </view>
  40. <view class="field">
  41. <text class="field-label">备注</text>
  42. <textarea
  43. class="form-textarea"
  44. :value="form.remark"
  45. placeholder="选填"
  46. :maxlength="500"
  47. @input="onRemarkInput"
  48. />
  49. </view>
  50. </view>
  51. <view class="form-card">
  52. <view class="form-header">
  53. <text class="form-header-title">附件</text>
  54. </view>
  55. <button
  56. class="btn btn-primary btn-sm"
  57. :disabled="uploading"
  58. @click="chooseAttachments"
  59. >
  60. {{ uploading ? '上传中…' : '上传图片' }}
  61. </button>
  62. <view v-if="files.length" class="att-grid">
  63. <view v-for="(file, idx) in files" :key="file.id" class="att-img">
  64. <image :src="file.previewUrl || file.localPath" mode="aspectFill" />
  65. <view v-if="!file._pending" class="del" @click.stop="removeFile(idx)">×</view>
  66. </view>
  67. </view>
  68. <view v-else class="detail-section-empty" style="margin-top: 8px">暂无附件(可选)</view>
  69. </view>
  70. </view>
  71. <view class="bottom-action-bar">
  72. <button class="btn btn-primary btn-block" :disabled="submitting" @click="onSubmit">
  73. {{ submitting ? '保存中…' : '保存' }}
  74. </button>
  75. </view>
  76. </view>
  77. </template>
  78. <script setup>
  79. import { ref, reactive, computed } from 'vue'
  80. import { onLoad } from '@dcloudio/uni-app'
  81. import { PAGE_PATHS } from '@/config/api'
  82. import { advanceApi, attachmentApi } from '@/api/bx'
  83. import { getSafeAreaStyle } from '@/utils/safeArea'
  84. const safeAreaStyle = getSafeAreaStyle()
  85. const MAX_ATTACHMENTS = 10
  86. const editId = ref('')
  87. const savedId = ref('')
  88. const submitting = ref(false)
  89. const uploading = ref(false)
  90. const files = ref([])
  91. const form = reactive({
  92. reason: '',
  93. amount: '',
  94. remark: ''
  95. })
  96. const fieldErr = reactive({ reason: false, amount: false })
  97. const isEdit = computed(() => !!editId.value || !!savedId.value)
  98. function onReasonInput(e) {
  99. form.reason = e.detail.value
  100. fieldErr.reason = false
  101. }
  102. function onAmountInput(e) {
  103. form.amount = e.detail.value
  104. fieldErr.amount = false
  105. }
  106. function onRemarkInput(e) {
  107. form.remark = e.detail.value
  108. }
  109. function goBack() {
  110. uni.navigateBack({ fail: () => uni.redirectTo({ url: PAGE_PATHS.bxAdvanceApply }) })
  111. }
  112. function validate() {
  113. fieldErr.reason = false
  114. fieldErr.amount = false
  115. if (!form.reason?.trim()) {
  116. fieldErr.reason = true
  117. uni.showToast({ title: '请输入申请原因', icon: 'none' })
  118. return false
  119. }
  120. const raw = String(form.amount ?? '').trim()
  121. if (!raw) {
  122. fieldErr.amount = true
  123. uni.showToast({ title: '请输入申请金额', icon: 'none' })
  124. return false
  125. }
  126. const amt = Number(raw)
  127. if (Number.isNaN(amt) || amt <= 0) {
  128. fieldErr.amount = true
  129. uni.showToast({ title: '请输入大于 0 的申请金额', icon: 'none' })
  130. return false
  131. }
  132. return true
  133. }
  134. async function chooseAttachments() {
  135. const remain = MAX_ATTACHMENTS - files.value.length
  136. if (remain <= 0) {
  137. uni.showToast({ title: `最多上传 ${MAX_ATTACHMENTS} 个附件`, icon: 'none' })
  138. return
  139. }
  140. uni.chooseImage({
  141. count: remain,
  142. sizeType: ['compressed'],
  143. sourceType: ['album', 'camera'],
  144. success: async (res) => {
  145. const paths = res.tempFilePaths || []
  146. for (const path of paths) {
  147. await uploadOne(path)
  148. }
  149. }
  150. })
  151. }
  152. async function uploadOne(filePath) {
  153. const pendingId = `pending_${Date.now()}_${Math.random().toString(36).slice(2, 7)}`
  154. files.value.push({
  155. id: pendingId,
  156. previewUrl: filePath,
  157. localPath: filePath,
  158. _pending: true,
  159. _existing: false
  160. })
  161. uploading.value = true
  162. try {
  163. const res = await attachmentApi.upload(filePath, '', 0)
  164. const data = res?.data || {}
  165. const id = String(data.id || '')
  166. let previewUrl = filePath
  167. if (id) {
  168. try {
  169. const pRes = await attachmentApi.preview(id)
  170. if (pRes?.data?.url) previewUrl = pRes.data.url
  171. } catch (e) {
  172. // keep local
  173. }
  174. }
  175. const idx = files.value.findIndex((f) => f.id === pendingId)
  176. if (idx >= 0) {
  177. files.value[idx] = {
  178. id: id || pendingId,
  179. previewUrl,
  180. localPath: filePath,
  181. _pending: false,
  182. _existing: false
  183. }
  184. }
  185. } catch (e) {
  186. files.value = files.value.filter((f) => f.id !== pendingId)
  187. } finally {
  188. uploading.value = files.value.some((f) => f._pending)
  189. }
  190. }
  191. function removeFile(idx) {
  192. const file = files.value[idx]
  193. if (!file) return
  194. if (file._existing && file.id && !String(file.id).startsWith('pending_')) {
  195. attachmentApi.delete(file.id).catch(() => {})
  196. }
  197. files.value.splice(idx, 1)
  198. }
  199. async function bindNewAttachments(bizId) {
  200. const newIds = files.value
  201. .filter((f) => f.id && !f._existing && !f._pending && !String(f.id).startsWith('pending_'))
  202. .map((f) => f.id)
  203. if (!newIds.length) return
  204. await attachmentApi.bindBiz(newIds, 'advance', bizId)
  205. files.value.forEach((f) => {
  206. if (newIds.includes(f.id)) f._existing = true
  207. })
  208. }
  209. async function loadEditData(id) {
  210. try {
  211. const res = await advanceApi.getById(id)
  212. const data = res?.data || {}
  213. form.reason = data.reason || ''
  214. form.amount = data.amount != null ? String(data.amount) : ''
  215. form.remark = data.remark != null ? String(data.remark) : ''
  216. try {
  217. const attRes = await attachmentApi.listByBiz('advance', id)
  218. const list = attRes?.data || []
  219. const mapped = []
  220. for (const a of list) {
  221. let previewUrl = ''
  222. if ((a.fileType || '').startsWith('image/')) {
  223. try {
  224. const pRes = await attachmentApi.preview(a.id)
  225. previewUrl = pRes?.data?.url || ''
  226. } catch (e) {
  227. // skip
  228. }
  229. }
  230. mapped.push({
  231. id: String(a.id),
  232. previewUrl,
  233. _pending: false,
  234. _existing: true
  235. })
  236. }
  237. files.value = mapped
  238. } catch (e) {
  239. files.value = []
  240. }
  241. } catch (e) {
  242. uni.showToast({ title: '加载数据失败', icon: 'none' })
  243. }
  244. }
  245. async function onSubmit() {
  246. if (!validate()) return
  247. if (uploading.value) {
  248. uni.showToast({ title: '附件上传中,请稍候', icon: 'none' })
  249. return
  250. }
  251. submitting.value = true
  252. const isEditingAction = !!editId.value || !!savedId.value
  253. try {
  254. const data = {
  255. reason: form.reason.trim(),
  256. amount: Number(form.amount),
  257. remark: form.remark
  258. }
  259. let id
  260. if (editId.value || savedId.value) {
  261. id = savedId.value || editId.value
  262. await advanceApi.update(id, data)
  263. await bindNewAttachments(id)
  264. } else {
  265. const res = await advanceApi.create(data)
  266. id = String(res?.data?.id ?? res?.id ?? '')
  267. if (!id) throw new Error('保存成功但未返回申请单ID')
  268. await bindNewAttachments(id)
  269. savedId.value = id
  270. editId.value = id
  271. }
  272. uni.showModal({
  273. title: isEditingAction ? '保存成功' : '创建成功',
  274. content: '备用金申请已保存为草稿',
  275. confirmText: '提交审批',
  276. confirmColor: '#00a874',
  277. cancelText: '继续编辑',
  278. success: async (modalRes) => {
  279. if (!modalRes.confirm) return
  280. try {
  281. await advanceApi.submit(id)
  282. uni.showToast({ title: '提交成功', icon: 'success' })
  283. setTimeout(() => goBack(), 500)
  284. } catch (e) {
  285. // toasted
  286. }
  287. }
  288. })
  289. } catch (e) {
  290. // toasted by bxRequest when BizError
  291. } finally {
  292. submitting.value = false
  293. }
  294. }
  295. onLoad((query) => {
  296. const id = query?.id ? String(query.id) : ''
  297. if (id) {
  298. editId.value = id
  299. loadEditData(id)
  300. }
  301. })
  302. </script>
  303. <style lang="scss">
  304. @import '@/styles/bx-theme.scss';
  305. </style>