order-confirm-popup.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <template>
  2. <view class="confirm-popup-root" v-if="visible" @click.stop.prevent>
  3. <view class="popup-mask" @click="handleClose"></view>
  4. <view class="popup-body">
  5. <view class="popup-header">
  6. <text class="header-title">订单确认</text>
  7. <view class="header-actions">
  8. <text class="preview-btn" @click="previewFullScreen">全屏查看</text>
  9. <text class="close-btn" @click="handleClose">×</text>
  10. </view>
  11. </view>
  12. <view class="client-info" v-if="clientName">
  13. <text class="client-label">客户:</text>
  14. <text class="client-value">{{ clientName }}</text>
  15. </view>
  16. <view class="canvas-wrapper">
  17. <canvas canvas-id="orderConfirmPopupCanvas" class="confirm-canvas"
  18. :style="{ width: canvasW + 'px', height: canvasH + 'px' }" @click="previewFullScreen">
  19. </canvas>
  20. </view>
  21. <view class="summary-row">
  22. <text>共 <text class="num-highlight">{{ models.length }}</text> 个型号,合计 <text class="num-highlight">{{
  23. totalCount }}</text> 支</text>
  24. </view>
  25. <view class="popup-footer">
  26. <button class="cancel-btn" @click="handleClose">取消</button>
  27. <button class="confirm-btn" :disabled="submitting" @click="handleConfirm">
  28. {{ submitting ? '提交中...' : '确认下单' }}
  29. </button>
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. /**
  36. * 订单确认弹窗 - canvas绘制表格
  37. * @Author: Trae
  38. */
  39. import { getImagesByNums } from '../../../api/drawing/drawingFile.js';
  40. export default {
  41. props: {
  42. visible: { type: Boolean, default: false },
  43. models: { type: Array, default: () => [] },
  44. clientName: { type: String, default: '' },
  45. submitting: { type: Boolean, default: false }
  46. },
  47. data() {
  48. return {
  49. canvasW: 345,
  50. canvasH: 300,
  51. canvasImagePath: '',
  52. imagePathMap: {}
  53. }
  54. },
  55. computed: {
  56. totalCount() {
  57. return this.models.reduce((sum, m) => sum + (parseInt(m.count) || 0), 0);
  58. }
  59. },
  60. watch: {
  61. visible(val) {
  62. if (val) {
  63. this.$nextTick(() => {
  64. this.loadImagesAndDraw();
  65. });
  66. }
  67. }
  68. },
  69. methods: {
  70. getCellWidths() {
  71. return [32, 56, 56, 42, 42, 42, 36, 56, 56, 46];
  72. },
  73. getHeaders() {
  74. return ['序号', '型材型号', '型材名称', '长度', '米重', '壁厚', '支数', '表面名称', '包装方式', '简图'];
  75. },
  76. getColumnX(colIndex) {
  77. const widths = this.getCellWidths();
  78. let x = 0;
  79. for (let i = 0; i < colIndex; i++) { x += widths[i]; }
  80. return x;
  81. },
  82. async loadImagesAndDraw() {
  83. const models = this.models;
  84. const nums = [...new Set(models.map(m => m.modelNum).filter(Boolean))];
  85. this.imagePathMap = {};
  86. if (nums.length > 0) {
  87. try {
  88. const res = await getImagesByNums(nums.join(','));
  89. const imageMap = res.data || {};
  90. await this.convertBase64ToTempPaths(imageMap);
  91. } catch (e) {
  92. console.error('加载简图失败:', e);
  93. }
  94. }
  95. setTimeout(() => { this.drawTable(); }, 150);
  96. },
  97. convertBase64ToTempPaths(imageMap) {
  98. const fs = uni.getFileSystemManager();
  99. const tasks = Object.entries(imageMap).map(([fnum, base64], i) => {
  100. return new Promise((resolve) => {
  101. const tempPath = `${wx.env.USER_DATA_PATH}/drawing_thumb_${i}_${Date.now()}.jpg`;
  102. fs.writeFile({
  103. filePath: tempPath,
  104. data: base64,
  105. encoding: 'base64',
  106. success: () => {
  107. this.imagePathMap[fnum] = tempPath;
  108. resolve();
  109. },
  110. fail: (err) => {
  111. console.error('写入简图文件失败:', fnum, err);
  112. resolve();
  113. }
  114. });
  115. });
  116. });
  117. return Promise.all(tasks);
  118. },
  119. drawTable() {
  120. const models = this.models;
  121. if (!models || models.length === 0) return;
  122. const colWidths = this.getCellWidths();
  123. const rowHeight = 32;
  124. const headerHeight = 36;
  125. const totalWidth = colWidths.reduce((a, b) => a + b, 0);
  126. const totalHeight = headerHeight + models.length * rowHeight + 4;
  127. this.canvasW = totalWidth;
  128. this.canvasH = totalHeight;
  129. const ctx = uni.createCanvasContext('orderConfirmPopupCanvas', this);
  130. ctx.setFillStyle('#ffffff');
  131. ctx.fillRect(0, 0, totalWidth, totalHeight);
  132. this.drawHeader(ctx, colWidths, headerHeight);
  133. this.drawRows(ctx, colWidths, models, headerHeight, rowHeight);
  134. ctx.draw(false, () => {
  135. setTimeout(() => {
  136. this.exportCanvasImage(totalWidth, totalHeight);
  137. }, 300);
  138. });
  139. },
  140. drawHeader(ctx, colWidths, headerHeight) {
  141. let x = 0;
  142. ctx.setLineWidth(1);
  143. ctx.setStrokeStyle('#666666');
  144. const headers = this.getHeaders();
  145. headers.forEach((header, i) => {
  146. ctx.setFillStyle('#f5f5f5');
  147. ctx.fillRect(x, 0, colWidths[i], headerHeight);
  148. ctx.setFillStyle('#222222');
  149. ctx.setFontSize(12);
  150. ctx.setTextAlign('center');
  151. ctx.setTextBaseline('middle');
  152. const cx = x + colWidths[i] / 2;
  153. ctx.fillText(header, cx, headerHeight / 2);
  154. ctx.beginPath();
  155. ctx.moveTo(x, 0);
  156. ctx.lineTo(x, headerHeight);
  157. ctx.stroke();
  158. x += colWidths[i];
  159. });
  160. ctx.beginPath();
  161. ctx.moveTo(x, 0);
  162. ctx.lineTo(x, headerHeight);
  163. ctx.stroke();
  164. ctx.setLineWidth(1.5);
  165. ctx.beginPath();
  166. ctx.moveTo(0, 0);
  167. ctx.lineTo(x, 0);
  168. ctx.stroke();
  169. ctx.beginPath();
  170. ctx.moveTo(0, headerHeight);
  171. ctx.lineTo(x, headerHeight);
  172. ctx.stroke();
  173. },
  174. drawRows(ctx, colWidths, models, headerHeight, rowHeight) {
  175. const totalW = colWidths.reduce((a, b) => a + b, 0);
  176. models.forEach((item, rowIndex) => {
  177. const y = headerHeight + rowIndex * rowHeight;
  178. ctx.setFillStyle(rowIndex % 2 === 0 ? '#ffffff' : '#fafafa');
  179. ctx.fillRect(0, y, totalW, rowHeight);
  180. ctx.setFillStyle('#333333');
  181. ctx.setFontSize(10);
  182. ctx.setTextAlign('center');
  183. ctx.setTextBaseline('middle');
  184. const val0 = '' + (rowIndex + 1);
  185. const val1 = this.truncateText(ctx, item.modelNum != null ? '' + item.modelNum : '', colWidths[1] - 6);
  186. const val2 = this.truncateText(ctx, item.modelName != null ? '' + item.modelName : '', colWidths[2] - 6);
  187. const val3 = this.toDecimalStr(item.length, 3);
  188. const val4 = this.toDecimalStr(item.meterWeight, 3);
  189. const val5 = this.toDecimalStr(item.wallThickness, 3);
  190. const val6 = item.count != null ? '' + item.count : '-';
  191. const val7 = this.truncateText(ctx, '' + (item.surfaceName || ''), colWidths[7] - 6);
  192. const val8 = this.truncateText(ctx, '' + (item.packName || ''), colWidths[8] - 6);
  193. const valueTexts = [val0, val1, val2, val3, val4, val5, val6, val7, val8];
  194. valueTexts.forEach((val, colIndex) => {
  195. const cx = this.getColumnX(colIndex) + colWidths[colIndex] / 2;
  196. ctx.fillText('' + val, cx, y + rowHeight / 2);
  197. });
  198. const imgPath = this.imagePathMap[item.modelNum];
  199. if (imgPath) {
  200. const imgColX = this.getColumnX(9);
  201. const imgSize = rowHeight - 8;
  202. const imgX = imgColX + (colWidths[9] - imgSize) / 2;
  203. const imgY = y + 4;
  204. ctx.drawImage(imgPath, imgX, imgY, imgSize, imgSize);
  205. }
  206. this.drawRowLines(ctx, colWidths, y, rowHeight);
  207. });
  208. ctx.setStrokeStyle('#666666');
  209. ctx.setLineWidth(1.5);
  210. ctx.beginPath();
  211. ctx.moveTo(0, headerHeight + models.length * rowHeight);
  212. ctx.lineTo(totalW, headerHeight + models.length * rowHeight);
  213. ctx.stroke();
  214. },
  215. drawRowLines(ctx, colWidths, y, rowHeight) {
  216. ctx.setStrokeStyle('#dddddd');
  217. ctx.setLineWidth(0.5);
  218. let x = 0;
  219. colWidths.forEach(w => {
  220. ctx.beginPath();
  221. ctx.moveTo(x, y);
  222. ctx.lineTo(x, y + rowHeight);
  223. ctx.stroke();
  224. x += w;
  225. });
  226. },
  227. toDecimalStr(val, digits) {
  228. if (val === null || val === undefined || val === '') return '-';
  229. const num = parseFloat(String(val));
  230. if (isNaN(num)) return '-';
  231. return num.toFixed(digits);
  232. },
  233. truncateText(ctx, text, maxWidth) {
  234. if (!text) return '';
  235. const metrics = ctx.measureText ? ctx.measureText(text) : null;
  236. if (!metrics || metrics.width <= maxWidth) return text;
  237. let truncated = text;
  238. while (truncated.length > 0) {
  239. truncated = truncated.slice(0, -1);
  240. const m = ctx.measureText(truncated + '…');
  241. if (m.width <= maxWidth) return truncated + '…';
  242. }
  243. return '';
  244. },
  245. exportCanvasImage(canvasW, canvasH) {
  246. uni.canvasToTempFilePath({
  247. canvasId: 'orderConfirmPopupCanvas',
  248. destWidth: canvasW * 2,
  249. destHeight: canvasH * 2,
  250. success: (res) => {
  251. this.canvasImagePath = res.tempFilePath;
  252. },
  253. fail: () => {
  254. console.error('canvas导出失败');
  255. }
  256. }, this);
  257. },
  258. previewFullScreen() {
  259. if (!this.canvasImagePath) {
  260. setTimeout(() => {
  261. if (this.canvasImagePath) {
  262. this.doPreviewImage();
  263. } else {
  264. uni.showToast({ title: '图片生成中,请稍后', icon: 'none' });
  265. }
  266. }, 500);
  267. return;
  268. }
  269. this.doPreviewImage();
  270. },
  271. doPreviewImage() {
  272. uni.previewImage({
  273. urls: [this.canvasImagePath],
  274. current: 0
  275. });
  276. },
  277. handleConfirm() {
  278. this.$emit('confirm');
  279. },
  280. handleClose() {
  281. this.$emit('close');
  282. }
  283. }
  284. }
  285. </script>
  286. <style scoped>
  287. .confirm-popup-root {
  288. position: fixed;
  289. top: 0;
  290. left: 0;
  291. width: 100vw;
  292. height: 100vh;
  293. z-index: 9999;
  294. display: flex;
  295. align-items: center;
  296. justify-content: center;
  297. }
  298. .popup-mask {
  299. position: absolute;
  300. top: 0;
  301. left: 0;
  302. width: 100%;
  303. height: 100%;
  304. background: rgba(0, 0, 0, 0.5);
  305. }
  306. .popup-body {
  307. position: relative;
  308. width: 680rpx;
  309. max-height: 90vh;
  310. background: #fff;
  311. border-radius: 24rpx;
  312. display: flex;
  313. flex-direction: column;
  314. overflow: hidden;
  315. }
  316. .popup-header {
  317. display: flex;
  318. justify-content: space-between;
  319. align-items: center;
  320. padding: 30rpx 30rpx 20rpx;
  321. border-bottom: 1rpx solid #f0f0f0;
  322. }
  323. .header-title {
  324. font-size: 34rpx;
  325. font-weight: bold;
  326. color: #1a1a1a;
  327. }
  328. .header-actions {
  329. display: flex;
  330. align-items: center;
  331. gap: 24rpx;
  332. }
  333. .preview-btn {
  334. font-size: 26rpx;
  335. color: #C1001C;
  336. }
  337. .close-btn {
  338. font-size: 40rpx;
  339. color: #999;
  340. line-height: 1;
  341. }
  342. .client-info {
  343. padding: 16rpx 30rpx;
  344. background: #fdf6f7;
  345. }
  346. .client-label {
  347. font-size: 26rpx;
  348. color: #666;
  349. }
  350. .client-value {
  351. font-size: 26rpx;
  352. color: #C1001C;
  353. font-weight: bold;
  354. }
  355. .canvas-wrapper {
  356. width: 690rpx;
  357. max-height: 55vh;
  358. overflow: auto;
  359. background: #fff;
  360. flex-shrink: 1;
  361. }
  362. .confirm-canvas {
  363. display: block;
  364. margin: 0 auto;
  365. }
  366. .summary-row {
  367. padding: 20rpx 30rpx;
  368. border-top: 1rpx solid #f0f0f0;
  369. text-align: center;
  370. font-size: 26rpx;
  371. color: #666;
  372. }
  373. .num-highlight {
  374. color: #C1001C;
  375. font-weight: bold;
  376. font-size: 28rpx;
  377. }
  378. .popup-footer {
  379. display: flex;
  380. gap: 20rpx;
  381. padding: 20rpx 30rpx 30rpx;
  382. border-top: 1rpx solid #f0f0f0;
  383. }
  384. .cancel-btn {
  385. flex: 1;
  386. height: 80rpx;
  387. background: #f5f5f5;
  388. color: #666;
  389. border-radius: 40rpx;
  390. display: flex;
  391. align-items: center;
  392. justify-content: center;
  393. font-size: 28rpx;
  394. border: none;
  395. }
  396. .confirm-btn {
  397. flex: 2;
  398. height: 80rpx;
  399. background: #C1001C;
  400. color: #fff;
  401. border-radius: 40rpx;
  402. display: flex;
  403. align-items: center;
  404. justify-content: center;
  405. font-size: 28rpx;
  406. font-weight: bold;
  407. border: none;
  408. }
  409. .confirm-btn[disabled] {
  410. background: #edb3bb;
  411. color: rgba(255, 255, 255, 0.6);
  412. }
  413. </style>