index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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') {
  42. this.getTabBar().setData({
  43. mask: false
  44. })
  45. }
  46. },
  47. async getVipInfo() {
  48. let vipTime = await getVipInfo()
  49. console.log(vipTime);
  50. this.setData({
  51. vipTime
  52. })
  53. },
  54. creatShare() {
  55. let type = this.data.type
  56. let context = wx.createSelectorQuery();
  57. context
  58. .select('#vip')
  59. .fields({
  60. node: true,
  61. size: true
  62. }).exec((res) => {
  63. const canvas = res[0].node;
  64. const ctx = canvas.getContext('2d');
  65. const dpr = wx.getSystemInfoSync().pixelRatio;
  66. canvas.width = res[0].width * dpr;
  67. canvas.height = res[0].height * dpr;
  68. ctx.scale(dpr, dpr);
  69. ctx.font = '18px PingFang';
  70. let pic = canvas.createImage();
  71. 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'
  72. pic.onload = () => {
  73. ctx.drawImage(pic, 0, 0, 375, 201);
  74. if (type == 'svip') {
  75. ctx.fillStyle = "#D7E6FF";
  76. ctx.fillText('终身使用', 16, 184)
  77. } else {
  78. ctx.fillStyle = "#FFE6D2";
  79. ctx.fillText('有效期至:' + formatDate(this.data.vipTime, 5), 16, 184)
  80. }
  81. setTimeout(() => {
  82. wx.canvasToTempFilePath({
  83. canvas: canvas,
  84. success(res) {
  85. wx.saveImageToPhotosAlbum({
  86. filePath: res.tempFilePath,
  87. success(res) {
  88. wx.showToast({
  89. title: '保存成功!',
  90. icon: "none",
  91. duration: 3000
  92. })
  93. }
  94. })
  95. },
  96. fail(res) {
  97. console.log('fail', res);
  98. }
  99. }, this)
  100. }, 500)
  101. }
  102. })
  103. },
  104. }
  105. })