index.js 3.4 KB

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