index.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import ad from '../index/ad'
  2. const ratio = wx.getSystemInfoSync().pixelRatio
  3. ad({
  4. data: {
  5. value: 'https://github.com/wux-weapp/wux-weapp',
  6. fgColor: 'black',
  7. whiteSpace: 10,
  8. qrcodeStatus: 'expired',
  9. },
  10. onChange(e) {
  11. const value = e.detail.value
  12. const fgColor = this.randomColor()
  13. this.setData({
  14. value,
  15. fgColor,
  16. })
  17. },
  18. onSliderChange(e) {
  19. console.log('onSliderChange', e.detail)
  20. const whiteSpace = e.detail.value[0]
  21. if (this.data.whiteSpace !== whiteSpace) {
  22. this.setData({
  23. whiteSpace,
  24. })
  25. }
  26. },
  27. QRCodeLoad(e) {
  28. console.log('QRCodeLoad', e.detail)
  29. },
  30. QRCodeError(e) {
  31. console.log('QRCodeError', e.detail)
  32. },
  33. onRefresh() {
  34. console.log('onRefresh')
  35. setTimeout(() => {
  36. this.setData({
  37. qrcodeStatus: 'loading',
  38. })
  39. setTimeout(() => {
  40. if (Math.random() > .5) {
  41. this.setData({
  42. qrcodeStatus: 'activated',
  43. value: 'https://www.wuxui.com',
  44. })
  45. } else {
  46. this.setData({
  47. qrcodeStatus: 'expired',
  48. })
  49. }
  50. }, 1500)
  51. })
  52. },
  53. previewImage() {
  54. // 在自定义组件下,当前组件实例的 this,以操作组件内 <canvas> 组件
  55. const that = this.selectComponent('#qrcode')
  56. // 获取 canvas 组件实例
  57. const canvas = that.getCanvasNode()
  58. wx.canvasToTempFilePath({
  59. canvas,
  60. destWidth: 200 * ratio,
  61. destHeight: 200 * ratio,
  62. success: (res) => {
  63. wx.previewImage({
  64. urls: [res.tempFilePath],
  65. })
  66. },
  67. }, that)
  68. },
  69. downloadQRCode() {
  70. // 在自定义组件下,当前组件实例的 this,以操作组件内 <canvas> 组件
  71. const that = this.selectComponent('#qrcode')
  72. // 获取 canvas 组件实例
  73. const canvas = that.getCanvasNode()
  74. wx.canvasToTempFilePath({
  75. canvas,
  76. destWidth: 200 * ratio,
  77. destHeight: 200 * ratio,
  78. success: (res) => {
  79. wx.saveImageToPhotosAlbum({
  80. filePath: res.tempFilePath,
  81. })
  82. },
  83. }, that)
  84. },
  85. randomColor() {
  86. const colorStr = Math.floor(Math.random() * 0xFFFFFF).toString(16).toUpperCase()
  87. const length = colorStr.length
  88. const prefixStr = '000000'.substring(0, 6 - colorStr.length)
  89. return `#${prefixStr}${colorStr}`
  90. },
  91. })