index.js 3.5 KB

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