index.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // pages/order_receipt/index.js
  2. import { getUserInfo } from '../../api/user.js';
  3. import { getOrderDetail } from '../../api/order.js';
  4. const app = getApp()
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. parameter: {
  11. 'navbar': '1',
  12. 'return': '1',
  13. 'title': '订单回执',
  14. },
  15. show: true,
  16. // orderId:'wx159384742516922741',
  17. orderId: ' ',
  18. cartInfo: [],//购物车产品
  19. orderInfo: { system_store: {} },//订单详情
  20. windowW: '',
  21. windowH: '',
  22. order_id: '',
  23. img: ''
  24. },
  25. /**
  26. * 登录回调
  27. *
  28. */
  29. onLoadFun: function () {
  30. this.getOrderInfo();
  31. this.setData({
  32. show: true
  33. })
  34. },
  35. /**
  36. * 获取订单详细信息
  37. *
  38. */
  39. getOrderInfo: function () {
  40. var that = this;
  41. wx.showLoading({ title: "正在加载中" });
  42. getOrderDetail(that.data.orderId).then(res => {
  43. var e = res.data;
  44. that.CreateCanvas(e)
  45. // console.log(productimg)
  46. wx.hideLoading();
  47. that.setData({
  48. orderInfo: res.data,
  49. order_id: res.data.order_id,
  50. cartInfo: res.data.cartInfo,
  51. evaluate: _type == 3 ? 3 : 0,
  52. system_store: res.data.system_store
  53. });
  54. }).catch(err => {
  55. });
  56. },
  57. /**
  58. * 生命周期函数--监听页面加载
  59. */
  60. onLoad: function (options) {
  61. var that = this
  62. var orderId =options.orderId;
  63. that.setData({
  64. orderId:orderId
  65. })
  66. // 获取设备宽高,以备海报全屏显示
  67. wx.getSystemInfo({
  68. success: function (res) {
  69. that.setData({
  70. windowW: res.windowWidth,
  71. windowH: res.windowHeight
  72. })
  73. },
  74. })
  75. // 海报背景图线上地址
  76. var url = '/images/bg.png'
  77. // 头部背景图片
  78. var urll = '/images/receipt_bgcolor.png'
  79. var orderimg = '/images/icon_receipt.png'
  80. var adressimg = '/images/address_icon_location.png'
  81. that.setData({
  82. bgpic: url
  83. })
  84. // that.getBG(urll).then(function (locationData) {
  85. that.setData({
  86. propic: urll
  87. })
  88. that.setData({
  89. orderimg: orderimg
  90. })
  91. that.setData({
  92. adressimg: adressimg
  93. })
  94. // })
  95. },
  96. CreateCanvas: function (e) {
  97. var that = this;
  98. var productimg = e.cartInfo[0].productInfo.image;
  99. console.log(productimg);
  100. that.getBG(productimg).then(function (locationData) {
  101. that.setData({
  102. product: locationData
  103. })
  104. that.drawCanvas(e)
  105. })
  106. },
  107. // 绘制canvas
  108. drawCanvas(e) {
  109. var that = this
  110. var windowW = that.data.windowW
  111. var windowH = that.data.windowH
  112. var text = e.cartInfo[0].productInfo.store_name
  113. if (e.cartInfo[0].productInfo.attrInfo == undefined) {
  114. var texta = '';
  115. console.log("1")
  116. } else {
  117. var texta = e.cartInfo[0].productInfo.attrInfo.suk;
  118. console.log("2")
  119. }
  120. // 使用 wx.createContext 获取绘图上下文 context
  121. var context = wx.createCanvasContext('firstCanvas')
  122. // 海报背景图
  123. context.drawImage(that.data.bgpic, (windowW - 380) / 2, (windowH - 560) / 2, 380, 580)
  124. // 商品图片
  125. context.drawImage(that.data.propic, (windowW - 380) / 2, (windowH - 560) / 2, 380, 125)
  126. //支付成功icon
  127. context.drawImage(that.data.orderimg, (windowW - 340) / 2, (windowH - 530) / 2, 36, 36)
  128. //地址icon
  129. context.drawImage(that.data.adressimg, (windowW - 348) / 2, (windowH - 250) / 2, 15, 20)
  130. // context.drawImage(that.data.product, (windowW - 348) / 2, (windowH - 250) / 2, 15, 20)
  131. context.drawImage(that.data.product, (windowW - 348) / 2, (windowH - 45) / 2, 85, 85)
  132. // 商品文字描述
  133. context.setFontSize(20)
  134. context.setFillStyle("#fff")
  135. context.fillText('下单成功', (windowW - 240) / 2, (windowH - 480) / 2)
  136. context.setFontSize(16)
  137. context.setFillStyle("#fff")
  138. context.fillText('订单号', (windowW - 340) / 2, (windowH - 405) / 2)
  139. context.setFontSize(16)
  140. context.setFillStyle("#fff")
  141. context.fillText(e.order_id, (windowW - 180) / 2, (windowH - 405) / 2)
  142. context.setFontSize(16)
  143. context.setFillStyle("#fff")
  144. context.fillText('下单时间', (windowW - 340) / 2, (windowH - 345) / 2)
  145. context.setFontSize(16)
  146. context.setFillStyle("#fff")
  147. context.fillText(e._add_time, (windowW - 180) / 2, (windowH - 345) / 2)
  148. context.font = 'bold 1px sans-serif'
  149. context.setFontSize(16)
  150. context.setFillStyle("#333333")
  151. context.fillText(e.real_name, (windowW - 270) / 2, (windowH - 245) / 2)
  152. context.font = 'normal 1px sans-serif'
  153. context.setFontSize(14)
  154. context.setFillStyle("#333333")
  155. context.fillText(e.user_phone, (windowW - 170) / 2, (windowH - 245) / 2)
  156. context.setFontSize(14)
  157. context.setFillStyle("#CCCCCC")
  158. context.fillText(e.user_address, (windowW - 270) / 2, (windowH - 188) / 2)
  159. context.setFontSize(10)
  160. context.setFillStyle("#727C8E")
  161. // context.fillText('¥99.99', (windowW + 50) / 2, (windowH + 55) / 2)
  162. context.setLineWidth(0.1)
  163. context.moveTo((windowW - 350) / 2, (windowH - 145) / 2); //设置线条的起始路径坐标
  164. context.lineTo((windowW + 350) / 2, (windowH - 145) / 2); //设置线条的终点路径坐标
  165. context.stroke(); //对当前路径进行描边
  166. context.font = 'bold 1px sans-serif'
  167. context.setFontSize(16)
  168. context.setFillStyle("#333333")
  169. context.fillText('商品', (windowW - 345) / 2, (windowH - 80) / 2)
  170. // 商品名字,名字很长调用方法将文字折行,传参 文字内容text,画布context
  171. var row = that.newLine(text, context)
  172. var a = 24//定义行高25
  173. for (var i = 0; i < row.length; i++) {
  174. context.font = 'normal 1px sans-serif'
  175. context.setFontSize(16)
  176. context.setFillStyle("#333333")
  177. context.fillText(row[i], (windowW - 150) / 2, (windowH - 0) / 2 + a * i, 320)
  178. }
  179. context.setFontSize(13)
  180. context.setFillStyle("#999999")
  181. context.fillText(texta, (windowW - 150) / 2, (windowH + 100) / 2)
  182. context.setFontSize(10)
  183. context.setFillStyle("#727C8E")
  184. // context.fillText('¥99.99', (windowW + 50) / 2, (windowH + 55) / 2)
  185. context.setLineWidth(0.1)
  186. context.moveTo((windowW - 350) / 2, (windowH + 170) / 2); //设置线条的起始路径坐标
  187. context.lineTo((windowW + 350) / 2, (windowH + 170) / 2); //设置线条的终点路径坐标
  188. context.stroke(); //对当前路径进行描边
  189. context.setFontSize(15)
  190. context.setFillStyle("#B1B1B1")
  191. context.fillText('商品数量', (windowW - 350) / 2, (windowH + 240) / 2)
  192. context.setFontSize(15)
  193. context.setFillStyle("#1D1D1D")
  194. context.fillText("x" + e.cartInfo[0].cart_num, (windowW + 290) / 2, (windowH + 240) / 2)
  195. context.setFontSize(15)
  196. context.setFillStyle("#B1B1B1")
  197. context.fillText('商品总额', (windowW - 350) / 2, (windowH + 320) / 2)
  198. context.setFontSize(15)
  199. context.setFillStyle("#1D1D1D")
  200. context.fillText("¥" + e.cartInfo[0].now_price, (windowW + 200) / 2, (windowH + 320) / 2)
  201. context.setFontSize(15)
  202. context.setFillStyle("#B1B1B1")
  203. context.fillText('实际付款', (windowW - 350) / 2, (windowH + 390) / 2)
  204. context.setFontSize(18)
  205. context.setFillStyle("#D0021B")
  206. context.fillText("¥" + e.pay_price, (windowW + 180) / 2, (windowH +390) / 2)
  207. context.draw()
  208. },
  209. // 点击保存按钮,同时将画布转化为图片
  210. daochu: function () {
  211. var that = this;
  212. wx.canvasToTempFilePath({
  213. x: 0,
  214. y: 0,
  215. canvasId: 'firstCanvas',
  216. fileType: 'jpg',
  217. quality: 1,
  218. success: function (res) {
  219. that.setData({
  220. shareImage: res.tempFilePath
  221. })
  222. setTimeout(function () {
  223. wx.showModal({
  224. title: '提示',
  225. content: '将生成的海报保存到手机相册,可以发送给微信好友或分享到朋友圈',
  226. success(res) {
  227. if (res.confirm) {
  228. that.eventSave()
  229. } else if (res.cancel) {
  230. console.log('用户点击取消')
  231. }
  232. }
  233. })
  234. }, 1000)
  235. }
  236. })
  237. },
  238. // 将商品分享图片保存到本地
  239. eventSave() {
  240. wx.saveImageToPhotosAlbum({
  241. filePath: this.data.shareImage,
  242. success(res) {
  243. wx.showToast({
  244. title: '保存图片成功',
  245. icon: 'success',
  246. duration: 2000
  247. })
  248. }
  249. })
  250. },
  251. //将线上图片地址下载到本地,此函数进行了封装,只有在需要转换url的时候调用即可
  252. getBG(url) {
  253. console.log(url + '111')
  254. // Promise函数给我很大的帮助,让我能return出回调函数中的值
  255. return new Promise(function (resolve) {
  256. wx.downloadFile({
  257. url: url,
  258. success: function (res) {
  259. url = res.tempFilePath
  260. resolve(url);
  261. console.log(url)
  262. }
  263. })
  264. })
  265. },
  266. // canvas多文字换行
  267. newLine(txt, context) {
  268. var txtArr = txt.split('')
  269. var temp = ''
  270. var row = []
  271. for (var i = 0; i < txtArr.length; i++) {
  272. if (context.measureText(temp).width < 210) {
  273. temp += txtArr[i]
  274. } else {
  275. i--
  276. row.push(temp)
  277. temp = ''
  278. }
  279. }
  280. row.push(temp)
  281. //如果数组长度大于3 则截取前三个
  282. if (row.length > 2) {
  283. var rowCut = row.slice(0, 2);
  284. var rowPart = rowCut[1];
  285. var test = "";
  286. var empty = [];
  287. for (var a = 0; a < rowPart.length; a++) {
  288. if (context.measureText(test).width < 0) {
  289. test += rowPart[a];
  290. } else {
  291. break;
  292. }
  293. }
  294. empty.push(test);
  295. var group = empty[0] //这里只显示三行,超出的用...表示
  296. rowCut.splice(2, 1, group);
  297. row = rowCut;
  298. }
  299. return row
  300. },
  301. /**
  302. * 生命周期函数--监听页面初次渲染完成
  303. */
  304. onReady: function () {
  305. var that = this
  306. },
  307. /**
  308. * 生命周期函数--监听页面显示
  309. */
  310. onShow: function () {
  311. },
  312. /**
  313. * 生命周期函数--监听页面隐藏
  314. */
  315. onHide: function () {
  316. },
  317. /**
  318. * 生命周期函数--监听页面卸载
  319. */
  320. onUnload: function () {
  321. }
  322. })