ht.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <template>
  2. <view class="content">
  3. <image v-if="!showcanvas" :src="img" mode="widthFix" style="width: 750rpx;"></image>
  4. <view @click="savePic" class="btn">保存</view>
  5. <view class="" style="height: 100rpx;">
  6. </view>
  7. <canvas canvas-id="firstCanvas" id="firstCanvas" class="htDetail"
  8. :style="{height: (cHeight + 'px'),width:(cWidth + 'px')}"></canvas>
  9. </view>
  10. </template>
  11. <script>
  12. import {
  13. getYyDetail
  14. } from '@/api/model.js'
  15. export default {
  16. data() {
  17. return {
  18. content: {},
  19. showcanvas: true,
  20. cWidth: '',
  21. cHeight: '',
  22. imgs: [],
  23. img: '',
  24. htDetail: {},
  25. id: 0,
  26. hid: 0,
  27. contents: {}
  28. }
  29. },
  30. onLoad(opt) {
  31. if(opt.id) {
  32. this.id = opt.id
  33. this.getYyDetail()
  34. this.htDetail = uni.getStorageSync('htDetail') || {}
  35. }
  36. if(opt.hid) {
  37. this.hid = opt.hid
  38. this.setDetail()
  39. }
  40. },
  41. onShow() {
  42. // this.SystemInfo = await this.getSystemInfo();
  43. },
  44. onReachBottom() {
  45. },
  46. onReady() {
  47. },
  48. methods: {
  49. savePic() {
  50. uni.getSetting({
  51. //获取用户的当前设置
  52. success: res => {
  53. if (res.authSetting['scope.writePhotosAlbum']) {
  54. //验证用户是否授权可以访问相册
  55. uni.saveImageToPhotosAlbum({
  56. filePath: this.img,
  57. success: function(res2) {
  58. uni.hideLoading();
  59. uni.showToast({
  60. title: '保存成功,请从相册选择再分享',
  61. icon: 'none',
  62. duration: 2000
  63. });
  64. },
  65. fail: function(err) {
  66. uni.hideLoading();
  67. uni.showToast({
  68. title: '保存失败',
  69. icon: 'none',
  70. duration: 2000
  71. });
  72. }
  73. });
  74. } else {
  75. uni.authorize({
  76. //如果没有授权,向用户发起请求
  77. scope: 'scope.writePhotosAlbum',
  78. success: () => {
  79. this.saveImageToPhotosAlbum();
  80. },
  81. fail: () => {
  82. uni.showToast({
  83. title: '请打开保存相册权限,再点击保存相册分享',
  84. icon: 'none',
  85. duration: 2000
  86. });
  87. setTimeout(() => {
  88. uni.openSetting({
  89. //调起客户端小程序设置界面,让用户开启访问相册
  90. success: res2 => {
  91. // console.log(res2.authSetting)
  92. }
  93. });
  94. }, 2000);
  95. }
  96. });
  97. }
  98. }
  99. });
  100. },
  101. getSystemInfo() {
  102. return new Promise((req, rej) => {
  103. console.log('zheli');
  104. // wx.getWindowInfo({
  105. // success: function(res) {
  106. // console.log(res,'wind');
  107. // req(res)
  108. // }
  109. // });
  110. let win = wx.getWindowInfo()
  111. console.log(win);
  112. req(win)
  113. })
  114. },
  115. getImageInfo(image) {
  116. return new Promise((req, rej) => {
  117. uni.getImageInfo({
  118. src: image,
  119. success: function(res) {
  120. req(res)
  121. },
  122. });
  123. })
  124. },
  125. async downloadAndProcessFile(url) {
  126. return new Promise((resolve, reject) => {
  127. uni.downloadFile({
  128. url: url,
  129. success: (res) => {
  130. if (res.statusCode === 200) {
  131. console.log(res.tempFilePath, 'res.tempFilePath');
  132. resolve(res.tempFilePath);
  133. } else {
  134. reject(new Error('File download failed'));
  135. }
  136. },
  137. fail: (error) => {
  138. console.log(error, 'tupianerro',url);
  139. reject(error);
  140. }
  141. });
  142. });
  143. },
  144. // 绘制
  145. async showCanvas() {
  146. let that = this
  147. try {
  148. let SystemInfo = await this.getSystemInfo();
  149. var context = uni.createCanvasContext('firstCanvas')
  150. let cheight = 0;
  151. let heightList = [0]
  152. const pjWidht = that.imgs.reduce((sum, item) => sum + item.width, 0) / that.imgs.length;
  153. for (let i = 0; i < that.imgs.length;) {
  154. console.log(that.imgs[i].width, 'that.imgs[i].width');
  155. let mheight = that.imgs[i].height * (that.imgs[i].width / pjWidht);
  156. console.log(that.imgs[i].height, 'that.imgs[i].height');
  157. that.imgs[i].height = mheight
  158. cheight += mheight;
  159. heightList.push(cheight)
  160. i++
  161. }
  162. console.log(heightList, 'heightList');
  163. //设置画布宽高
  164. that.cHeight = cheight
  165. that.cWidth = pjWidht
  166. context.fillRect(0, 0, that.cWidth, that.cHeigh);
  167. context.setFillStyle('#000000');
  168. let height = 0
  169. // 绘制底图
  170. for (let i = 0; i < that.imgs.length;) {
  171. console.log('that.imgs[i].src',that.imgs[i].src);
  172. const avatarImagePath = await that.downloadAndProcessFile(that.imgs[i].src);
  173. console.log(avatarImagePath, 'avatarImagePath');
  174. context.drawImage(avatarImagePath, 0, height, pjWidht, that.imgs[i].height)
  175. height += that.imgs[i].height
  176. i++
  177. }
  178. // 绘制甲方数据
  179. console.log(that.htDetail.content.jsonAr,'that.htDetail.content.jsonAr');
  180. await that.setFont(that.htDetail.content.jsonAr, context)
  181. // 计算图像变形率
  182. let bl = that.cWidth / 750
  183. console.log(bl);
  184. // 绘制甲方数据
  185. // 绘制乙方数据
  186. console.log(that.htDetail.content.jsonBr,'that.htDetail.content.jsonBr');
  187. await that.setFont(that.htDetail.content.jsonBr, context)
  188. context.draw(false, e => {
  189. console.log('wancheng');
  190. setTimeout(() => {
  191. console.log(that.cWidth, that.cHeight);
  192. uni.canvasToTempFilePath({
  193. x: 0,
  194. y: 0,
  195. width: that.cWidth,
  196. height: that.cHeight,
  197. destWidth: that.cWidth,
  198. destHeight: that.cHeight,
  199. canvasId: 'firstCanvas',
  200. success: function(res) {
  201. console.log(333, res);
  202. // 在H5平台下,tempFilePath 为 base64
  203. that.showcanvas = false;
  204. that.img = res.tempFilePath;
  205. }
  206. });
  207. }, 500)
  208. });
  209. } catch (error) {
  210. console.log(error, 'error');
  211. //TODO handle the exception
  212. }
  213. },
  214. async setFont(array, context) {
  215. let that = this
  216. // 计算图像变形率
  217. let bl = that.cWidth / 750
  218. for (let j = 0; j < array.length;) {
  219. let item = array[j]
  220. let x = item.left * bl;
  221. // 文字大小
  222. let fontSize = item.fontSize * bl
  223. // 处理文字基准线问题
  224. let y = item.top * bl + fontSize;
  225. if (item.value) {
  226. if (item.type == 'text' || item.type == 'number') {
  227. context.setFontSize(fontSize);
  228. context.fillText(item.value, x, y);
  229. }
  230. if (item.type == 'date') {
  231. let arr = item.value.split('-')
  232. let val = ''
  233. if (item.dateType) {
  234. if (item.dateType == 'xxxx年xx月xx日') {
  235. val = arr[0] + '年' + arr[1] + '月' + arr[2] + '日'
  236. } else if (item.dateType == 'xxxx-xx-xx') {
  237. val = item.value
  238. }
  239. } else {
  240. val = arr[0] + ' ' + arr[1] + ' ' + arr[2] + ' '
  241. }
  242. context.setFontSize(fontSize);
  243. context.fillText(val, x, y);
  244. console.log(x, y, '时间');
  245. }
  246. // 处理地址换行
  247. if (item.type == 'address') {
  248. that.setAddress(x, y, item.value, that.cWidth - 30, context, fontSize)
  249. }
  250. if (item.type == 'langtext') {
  251. that.setAddress(x, y, item.value, x + item.width * bl, context, fontSize)
  252. }
  253. console.log(item.code);
  254. if (item.type == 'img') {
  255. if(item.value && item.value != '1') {
  256. let path = await that.downloadAndProcessFile(item.value)
  257. context.drawImage(path, item.left * 1 * bl, item.top * 1 * bl, item.width * bl,
  258. item.height * bl)
  259. }else if(that.contents[item.code]) {
  260. console.log('zhe');
  261. let path = await that.downloadAndProcessFile(that.contents[item.code])
  262. context.drawImage(path, item.left * 1 * bl, item.top * 1 * bl, item.width * bl,
  263. item.height * bl)
  264. }
  265. }
  266. }else {
  267. if (item.type == 'date' && item.code == 'to_check_time' && that.contents[item.code]) {
  268. let date = new Date(that.contents[item.code] * 1000)
  269. const year = date.getFullYear();
  270. const month = String(date.getMonth() + 1).padStart(2, '0');
  271. const day = String(date.getDate()).padStart(2, '0');
  272. let val = `${year} ${month} ${day}`;
  273. context.setFontSize(fontSize);
  274. context.fillText(val, x, y);
  275. console.log(x, y, '时间');
  276. }
  277. if (item.type == 'img') {
  278. if(that.contents[item.code]) {
  279. console.log('zhe');
  280. let path = await that.downloadAndProcessFile(that.contents[item.code])
  281. context.drawImage(path, item.left * 1 * bl, item.top * 1 * bl, item.width * bl,
  282. item.height * bl)
  283. }
  284. }
  285. }
  286. j++
  287. }
  288. },
  289. setAddress(x, y, text, maxWidth, context, fontSize) {
  290. let that = this
  291. // let text = item.value
  292. let words = text.split('');
  293. let line = '';
  294. // let maxWidth = that.cWidth - 30
  295. for (let n = 0; n < words.length; n++) {
  296. let testLine = line + words[n] + ' ';
  297. let metrics = context.measureText(testLine);
  298. let testWidth = metrics.width + x;
  299. if (testWidth > maxWidth && n > 0) {
  300. console.log(line);
  301. context.fillText(line, x, y);
  302. line = words[n] + ' ';
  303. y += fontSize * 1.5; // 移动到下一行
  304. } else {
  305. line = testLine;
  306. }
  307. }
  308. context.fillText(line, x, y);
  309. },
  310. getHtDetail() {
  311. },
  312. setDetail() {
  313. let that = this
  314. that.htDetail = uni.getStorageSync('htDetails') || {}
  315. let lens = 0
  316. let width = 0;
  317. let height = 0;
  318. console.log();
  319. let len = that.htDetail.imgs.length
  320. that.htDetail.imgs.forEach(async (item, index) => {
  321. let data = await uni.getImageInfo({
  322. src: item,
  323. success(ress) {
  324. that.$set(that.imgs, index, {
  325. src: item,
  326. width: ress.width,
  327. height: ress.height
  328. })
  329. lens++;
  330. if (lens == len) {
  331. that.cHeight = height;
  332. console.log(that.cHeight, 'heigh');
  333. that.showCanvas()
  334. }
  335. }
  336. })
  337. })
  338. },
  339. getYyDetail() {
  340. let that = this
  341. getYyDetail({
  342. id: this.id
  343. }).then(res => {
  344. console.log(res,'resssssss');
  345. let width = 0;
  346. let height = 0;
  347. let len = res.data.imgs.length
  348. that.content = JSON.parse(res.data.content)
  349. console.log(that.content,'that.content--------------------------------------------------');
  350. that.contents = res.data
  351. let lens = 0
  352. res.data.imgs.forEach(async (item, index) => {
  353. let data = await uni.getImageInfo({
  354. src: item,
  355. success(ress) {
  356. that.$set(that.imgs, index, {
  357. src: item,
  358. width: ress.width,
  359. height: ress.height
  360. })
  361. lens++;
  362. if (lens == len) {
  363. that.cHeight = height;
  364. console.log(that.cHeight, 'heigh');
  365. that.showCanvas()
  366. }
  367. }
  368. })
  369. })
  370. })
  371. }
  372. }
  373. }
  374. </script>
  375. <style lang="scss" scoped>
  376. .htDetail {
  377. width: 750rpx;
  378. position: fixed;
  379. top: -999999px;
  380. z-index: 0;
  381. }
  382. .btn {
  383. position: fixed;
  384. bottom: 0;
  385. width: 750rpx;
  386. height: 100rpx;
  387. text-align: center;
  388. line-height: 100rpx;
  389. color: #fff;
  390. background-color: $base-color;
  391. }
  392. </style>