index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import {
  2. getVipInfo,
  3. } from '~/api/user'
  4. import {
  5. formatDate
  6. } from '~/utils/util'
  7. Component({
  8. /**
  9. * 组件的属性列表
  10. */
  11. properties: {
  12. },
  13. /**
  14. * 组件的初始数据
  15. */
  16. data: {
  17. show: false,
  18. type: '',
  19. vipTime: ''
  20. },
  21. methods: {
  22. open({
  23. type
  24. }) {
  25. console.log(this.getTabBar());
  26. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  27. this.getTabBar().setData({
  28. mask: true
  29. })
  30. }
  31. this.getVipInfo()
  32. this.setData({
  33. type,
  34. show: true
  35. })
  36. },
  37. closeModal() {
  38. this.setData({
  39. show: false
  40. })
  41. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  42. this.getTabBar().setData({
  43. mask: false
  44. })
  45. }
  46. },
  47. async getVipInfo() {
  48. let vipTime = await getVipInfo()
  49. this.setData({
  50. vipTime
  51. })
  52. },
  53. creatShare() {
  54. let type = this.data.type
  55. let context = wx.createSelectorQuery();
  56. context
  57. .select('#vip')
  58. .fields({
  59. node: true,
  60. size: true
  61. }).exec((res) => {
  62. const canvas = res[0].node;
  63. const ctx = canvas.getContext('2d');
  64. const dpr = wx.getSystemInfoSync().pixelRatio;
  65. canvas.width = res[0].width * dpr;
  66. canvas.height = res[0].height * dpr;
  67. ctx.scale(dpr, dpr);
  68. ctx.font = '18px PingFang';
  69. let pic = canvas.createImage();
  70. pic.src = type == 'svip' ? 'http://reader-wx.ai160.com/images/reader/v3/learn/vip1.png' : 'http://reader-wx.ai160.com/images/reader/v3/learn/vip2.png'
  71. pic.onload = () => {
  72. ctx.drawImage(pic, 0, 0, 375, 201);
  73. if (type == 'svip') {
  74. ctx.fillStyle = "#D7E6FF";
  75. ctx.fillText('终身使用', 16, 184)
  76. } else {
  77. ctx.fillStyle = "#FFE6D2";
  78. ctx.fillText('有效期至:' + formatDate(this.data.vipTime, 5), 16, 184)
  79. }
  80. setTimeout(() => {
  81. wx.canvasToTempFilePath({
  82. canvas: canvas,
  83. success(res) {
  84. wx.saveImageToPhotosAlbum({
  85. filePath: res.tempFilePath,
  86. success(res) {
  87. wx.showToast({
  88. title: '保存成功!',
  89. icon: "none",
  90. duration: 3000
  91. })
  92. }
  93. })
  94. },
  95. fail(res) {
  96. console.log('fail', res);
  97. }
  98. }, this)
  99. }, 500)
  100. }
  101. })
  102. },
  103. }
  104. })