sureCombine.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <template>
  2. <div class="container">
  3. <div v-show="isPhotoOrsize == '图片'">
  4. <div style="padding: 20px 0;">合并签收以下快件:</div>
  5. <van-form @submit="onSubmit">
  6. <van-field name="checkboxGroup">
  7. <template #input>
  8. <van-checkbox-group v-model="checkboxGroup">
  9. <van-checkbox disabled style="margin-bottom: 10px;" :name="item.id" shape="square"
  10. v-for="item in goodsList" :key="item.id"> {{ item.expressNo }} & {{ item.expressType
  11. }} & {{ item.expressQuantity }}件 &{{ item.isPayOnDelivery == '否' ? '非到付' : '到付' }}
  12. </van-checkbox>
  13. </van-checkbox-group>
  14. </template>
  15. </van-field>
  16. <van-field readonly clickable name="shelfInformation" label="上传照片或签字板" lable-width="1000px" :value="currentSelect"
  17. placeholder="请选择照片或签字板" @click="isShelf = true" :rules="[{ required: true }]" />
  18. <van-popup v-model="isShelf" position="bottom">
  19. <van-picker title="标题" show-toolbar :columns="shelfs" @confirm="onConfirm" @cancel="onCancel" />
  20. </van-popup>
  21. <div v-if="currentSelect == '签字板'" style="display: flex;justify-content: flex-start;">
  22. <span style="margin-left: 0.4rem; margin-right: 1rem;">签字版图片:</span>
  23. <van-image width="2rem" height="2rem" fit="contain" :src="canvasPhotoIMG"/>
  24. </div>
  25. <div v-if="currentSelect == '图片'">
  26. <van-field name="uploader" label="上传照片" :rules="[{ required: true, message: '请上传图片' }]">
  27. <template #input>
  28. <van-uploader multiple :deletable="true"
  29. @delete="onDelete" v-model="uploader" :after-read="afterRead" :before-read="beforeRead" :max-count="1" />
  30. </template>
  31. </van-field>
  32. </div>
  33. <div>
  34. <van-button style="height: 40px; width: 150px; border-radius: 8px; margin-right: 20px;" type="info"
  35. native-type="submit">确定签收</van-button>
  36. <van-button style="height: 40px; width: 150px; border-radius: 8px;" type="default"
  37. @click="back">返回上一级</van-button>
  38. </div>
  39. <!-- <van-image class="batch-middel-text-style" width="100" height="100" :src="aaa" /> -->
  40. </van-form>
  41. </div>
  42. <div v-show="isPhotoOrsize == '签字板'">
  43. <canvas ref="canvas"
  44. :width="width"
  45. :height="height"
  46. @mousedown="startDrawing"
  47. @mouseup="stopDrawing"
  48. @mousemove="draw"
  49. @mouseout="stopDrawing"
  50. @touchstart="startDrawing"
  51. @touchend="stopDrawing"
  52. @touchmove="drawTouch" style="border: 1px solid;" ></canvas>
  53. <van-button type="default" native-type="button" @click="canvasClose">取消</van-button>
  54. <van-button type="default" native-type="button" @click="clearCanvas">重写</van-button>
  55. <van-button type="default" native-type="button" @click="saveSignature" >确定</van-button>
  56. </div>
  57. </div>
  58. </template>
  59. <script>
  60. import { appDelivery, appUploadTheSignature } from '@/api/index'
  61. import axios from 'axios';
  62. export default {
  63. name: 'sureCombine',
  64. props: ['isCombine', 'goodsList'],
  65. data() {
  66. return {
  67. checkbox: false,
  68. checkboxGroup: [],
  69. uploader: [],//图片
  70. isShelf:false,
  71. isPhotoOrsize:'图片',//控制展示
  72. currentSelect:'',
  73. shelfs:['图片', '签字板'],
  74. isDrawing: false,
  75. hasContent: false,
  76. context: null,
  77. lastX: 0,
  78. lastY: 0,
  79. width: window.innerWidth - 24,
  80. height: window.innerHeight - 100,
  81. photoIMG:'',//储存展示图片
  82. canvasPhotoIMG:'',//展示图片
  83. hasUploadedImage: false, // 标记是否已上传图片
  84. };
  85. },
  86. mounted() {
  87. document.addEventListener('touchmove', this.disabledMove, { passive: false })
  88. this.initCanvas();
  89. // 在组件挂载后,设置默认选中的值
  90. this.checkboxGroup = this.goodsList.map(item => item.id);
  91. },
  92. beforeDestroy(){
  93. document.removeEventListener('touchmove', this.disabledMove)
  94. },
  95. methods: {
  96. disabledMove(e){
  97. e.preventDefault();
  98. },
  99. initCanvas() {
  100. const canvas = this.$refs.canvas;
  101. this.context = canvas.getContext('2d');
  102. this.context.strokeStyle = '#000000';
  103. this.context.lineWidth = 2;
  104. },
  105. startDrawing(event) {
  106. this.isDrawing = true;
  107. const rect = this.$refs.canvas.getBoundingClientRect();
  108. this.lastX = event.clientX - rect.left;
  109. this.lastY = event.clientY - rect.top;
  110. // this.lastX = event.offsetX || event.touches[0].clientX;
  111. // this.lastY = event.offsetY || event.touches[0].clientY;
  112. },
  113. stopDrawing() {
  114. this.isDrawing = false;
  115. this.lastX = null;
  116. this.lastY = null;
  117. },
  118. draw(event) {
  119. if (!this.isDrawing || !this.lastX || !this.lastY) return;
  120. const rect = this.$refs.canvas.getBoundingClientRect();
  121. const x = event.clientX - rect.left;
  122. const y = event.clientY - rect.top;
  123. this.drawLine(this.lastX, this.lastY, x, y);
  124. this.lastX = x;
  125. this.lastY = y;
  126. },
  127. drawTouch(event) {
  128. this.hasContent = true;
  129. if (!this.isDrawing) return;
  130. const rect = this.$refs.canvas.getBoundingClientRect();
  131. const touch = event.touches[0];
  132. const x = touch.clientX - rect.left;
  133. const y = touch.clientY - rect.top;
  134. this.drawLine(this.lastX, this.lastY, x, y);
  135. this.lastX = x;
  136. this.lastY = y;
  137. },
  138. drawLine(x1, y1, x2, y2) {
  139. this.context.beginPath();
  140. this.context.moveTo(x1, y1);
  141. this.context.lineTo(x2, y2);
  142. this.context.stroke();
  143. },
  144. canvasClose() {
  145. this.isPhotoOrsize = '图片'
  146. },
  147. clearCanvas() {
  148. this.context.clearRect(0, 0, this.width, this.height);
  149. this.hasContent = false;
  150. },
  151. saveSignature() {
  152. if(this.hasContent == false) {
  153. return this.$toast('请签名')
  154. }
  155. this.canvasPhotoIMG = this.$refs.canvas.toDataURL('image/png');
  156. const canvas = this.$refs.canvas;
  157. canvas.toBlob(async (blob) => {
  158. const file = new File([blob], 'my_image.png', { type: 'image/png' });
  159. console.log(file,'file')
  160. const formData = new FormData();
  161. formData.append('file', file);
  162. const res = await appUploadTheSignature(formData)
  163. this.getFileUrl = res.data
  164. console.log(this.getFileUrl,'=addresse')
  165. });
  166. this.currentSelect = "签字板"
  167. this.isPhotoOrsize = '图片'
  168. this.context.clearRect(0, 0, this.width, this.height);
  169. this.hasContent = false;
  170. },
  171. onConfirm(value, index) {
  172. if(value == '签字板') {
  173. this.show =true
  174. }
  175. if(value == '图片') {
  176. this.uploader = []
  177. }
  178. this.currentSelect = value
  179. this.isPhotoOrsize = value
  180. this.isShelf = false
  181. },
  182. onCancel() {
  183. this.isShelf = false
  184. },
  185. beforeRead(file) {
  186. // 在这里可以添加对文件的校验逻辑
  187. // 如果返回 true, 则继续读取文件
  188. return true;
  189. },
  190. // 文件读取完成后的钩子
  191. async afterRead(file) {
  192. console.log(file)
  193. this.photoIMG = file.content
  194. this.hasUploadedImage = true;
  195. console.log(file.content,'--',this.photoIMG)
  196. const formData = new FormData();
  197. formData.append('file', file.file); // 这里的 'file' 是后端接收文件的字段名
  198. const res = await appUploadTheSignature(formData)
  199. this.getFileUrl = res.data
  200. },
  201. async onSubmit(values) {
  202. let data = []
  203. this.goodsList.forEach((item) => {
  204. if (values.checkboxGroup.find((itemA) => itemA === item.id)) {
  205. data.push(item.expressNo)
  206. }
  207. })
  208. console.log(this.getFileUrl,'传的对不对,是哪一个')
  209. const params = {
  210. signEnclosureImg: this.getFileUrl,//签署图片
  211. deliveryId: data.join(','),//快递单号逗号隔开
  212. deliveryPeopleId: localStorage.getItem('courierId'),// 派件员id
  213. }
  214. const res = await appDelivery({ ...params }, { emulateJSON: true, loading: true, message: '签收中' })
  215. if (res.code == 0) {
  216. this.$toast('签收成功')
  217. this.$router.push('/')
  218. } else {
  219. this.$toast('签收失败')
  220. }
  221. },
  222. back() {
  223. this.$emit('isCombine', true)
  224. },
  225. // phoneSubmit() {
  226. // if (this.hasUploadedImage) {
  227. // this.isPhotoOrsize = '图片'
  228. // this.currentSelect = '图片'
  229. // this.canvasPhotoIMG = this.photoIMG
  230. // console.log(this.photoIMG,'--123图片')
  231. // } else {
  232. // this.$toast('请先上传图片');
  233. // }
  234. // },
  235. // photoClose() {
  236. // this.isPhotoOrsize = '图片'
  237. // },
  238. // // 删除图片时的处理
  239. // onDelete(file) {
  240. // // 标记为未上传图片
  241. // this.hasUploadedImage = false;
  242. // console.log('已删除图片', file);
  243. // },
  244. }
  245. };
  246. </script>
  247. <style lang="less" scoped>
  248. .ckickChange {
  249. padding: 20px 20px;
  250. }
  251. * {
  252. font-size: 16px;
  253. }
  254. .van-field__label {
  255. width: 4rem;
  256. }
  257. .container {
  258. padding: 2px 10px 0 10px;
  259. background-color: #fff;
  260. }
  261. </style>