hooks.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import {useState, useEffect} from 'react';
  2. import {
  3. showModal,
  4. chooseMedia,
  5. showLoading,
  6. hideLoading,
  7. showToast,
  8. } from '@tarojs/taro';
  9. import {useMutation, useQuery} from '@tanstack/react-query';
  10. import {confirmDeliver, getInfo, uploadImg} from '@apis';
  11. import {useBoolean} from 'ahooks';
  12. import {useNavigate} from '@hooks';
  13. export function useUpload() {
  14. const [files, setFiles] = useState([]);
  15. const {mutate} = useMutation({
  16. mutationFn: uploadImg,
  17. onMutate() {
  18. showLoading({title: '正在提交图片', mask: true});
  19. },
  20. onSettled() {
  21. hideLoading({noConflict: true});
  22. },
  23. onSuccess(data) {
  24. data.code === '200' && setFiles(prev => [...prev, data.data.data]);
  25. },
  26. });
  27. function onAdd() {
  28. chooseMedia({
  29. count: 1,
  30. mediaType: ['image'],
  31. sourceType: ['album', 'camera'],
  32. success(res) {
  33. const {tempFiles} = res;
  34. mutate(tempFiles[0].tempFilePath);
  35. },
  36. });
  37. }
  38. function onRemove(idx) {
  39. return function () {
  40. showModal({
  41. title: '删除图片',
  42. content: `你确定要删除第${idx + 1}张图片吗?`,
  43. success({confirm}) {
  44. confirm &&
  45. setFiles(function (prev) {
  46. const next = [...prev];
  47. next.splice(idx, 1);
  48. return next;
  49. });
  50. },
  51. });
  52. };
  53. }
  54. return [files, {onAdd, onRemove, setFiles}];
  55. }
  56. export function useAnomaly() {
  57. const [anomaly, {set: setAnomaly}] = useBoolean();
  58. const [note, setNote] = useState('');
  59. useEffect(
  60. function () {
  61. !anomaly && setNote('');
  62. },
  63. [anomaly],
  64. );
  65. return [
  66. {anomaly, note},
  67. {setAnomaly, setNote},
  68. ];
  69. }
  70. export function useSubmit({customerNo, truckNo, anomaly, note, imgs}) {
  71. const {pop} = useNavigate();
  72. const {mutate, isLoading} = useMutation({
  73. mutationFn: confirmDeliver,
  74. onSuccess({code}) {
  75. if (code === '200') {
  76. pop();
  77. showToast({title: '收货成功', icon: 'success', mask: true});
  78. }
  79. },
  80. });
  81. function onSubmit() {
  82. if (!customerNo || !truckNo) {
  83. return showToast({title: '请先扫码', icon: 'error'});
  84. }
  85. if (anomaly && !note) {
  86. return showToast({title: '请输入异常内容', icon: 'error'});
  87. }
  88. if (!imgs.length) {
  89. return showToast({title: '请上传图片', icon: 'error'});
  90. }
  91. const imgUrls = imgs.join(',');
  92. showModal({
  93. title: '确认收货',
  94. content: `你确认要对${truckNo}${customerNo}进行确认收货吗?`,
  95. success({confirm}) {
  96. confirm &&
  97. mutate({
  98. customer: customerNo,
  99. truckNo,
  100. anomaly: anomaly ? '1' : '0',
  101. note,
  102. imgs: imgUrls,
  103. });
  104. },
  105. });
  106. }
  107. return [isLoading, onSubmit];
  108. }
  109. export function useInfo(customerNo, truckNo, {setFiles, setAnomaly, setNote}) {
  110. const no = customerNo + truckNo;
  111. const {data} = useQuery({
  112. queryKey: [getInfo.name, no],
  113. enabled: Boolean(no),
  114. async queryFn() {
  115. showLoading({title: '正在获取发货单信息', mask: true});
  116. const data = await getInfo(truckNo, customerNo);
  117. hideLoading({noConflict: true});
  118. if (data.code === '200') {
  119. if (data.data.list.length) {
  120. return data.data.list[0];
  121. }
  122. return null;
  123. }
  124. return null;
  125. },
  126. });
  127. useEffect(
  128. function () {
  129. if (!data) {
  130. setFiles([]);
  131. setNote('');
  132. setAnomaly(false);
  133. return;
  134. }
  135. setFiles(data.imgs ? data.imgs.split(',') : []);
  136. setAnomaly(data.anomaly === '1');
  137. setNote(data?.note ?? '');
  138. },
  139. [data, setAnomaly, setFiles, setNote],
  140. );
  141. }