index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import computeOffset from '../behaviors/computeOffset';
  2. import zIndex from '../behaviors/zIndex';
  3. import watchShow from '../behaviors/watchShow';
  4. Component({
  5. /**
  6. * 组件的属性列表
  7. */
  8. behaviors: [computeOffset, zIndex, watchShow],
  9. externalClasses: ['l-bg-class', 'l-icon-class', 'l-class', 'l-image-class', 'l-title-class '],
  10. properties: {
  11. // 显示与隐藏
  12. show: {
  13. type: Boolean,
  14. value: false
  15. },
  16. // 提示框的文本内容
  17. title: String,
  18. // icon
  19. icon: String,
  20. iconSize: String,
  21. iconColor: String,
  22. // image
  23. image: String,
  24. // 文字的显示方位
  25. placement: {
  26. type: String,
  27. value: 'bottom'
  28. },
  29. // 提示框显示的时长
  30. duration: {
  31. type: Number,
  32. value: 1500
  33. },
  34. // 提示框的层级
  35. zIndex: {
  36. type: Number,
  37. value: 777
  38. },
  39. // 设置提示框是否为垂直居中
  40. center: {
  41. type: Boolean,
  42. value: true
  43. },
  44. // 是否显示透明蒙层,防止触摸穿透
  45. mask: {
  46. type: Boolean,
  47. value: false
  48. },
  49. openApi: {
  50. type: Boolean,
  51. value: true,
  52. },
  53. offsetX: Number,
  54. offsetY: Number
  55. },
  56. /**
  57. * 组件的初始数据
  58. */
  59. data: {
  60. status: false,
  61. success: '',
  62. fail: '',
  63. complete: ''
  64. },
  65. // 解决 addListener undefined 的错误
  66. observers: {
  67. 'icon': function () {}
  68. },
  69. attached() {
  70. if (this.data.openApi) {
  71. this.initToast();
  72. }
  73. },
  74. pageLifetimes: {
  75. show() {
  76. if (this.data.openApi) {
  77. this.initToast();
  78. }
  79. this.offsetMargin();
  80. },
  81. },
  82. /**
  83. * 组件的方法列表
  84. */
  85. methods: {
  86. initToast() {
  87. wx.lin = wx.lin || {};
  88. wx.lin.showToast = (options = {}) => {
  89. const {
  90. title = '',
  91. icon = '',
  92. image = '',
  93. placement = 'bottom',
  94. duration = 1500,
  95. center = true,
  96. mask = false,
  97. success = null,
  98. complete = null,
  99. offsetX = 0,
  100. offsetY = 0,
  101. iconSize = '60',
  102. iconColor = ''
  103. } = options;
  104. this.setData({
  105. title,
  106. icon,
  107. image,
  108. placement,
  109. duration,
  110. center,
  111. mask,
  112. success,
  113. complete,
  114. offsetY,
  115. offsetX,
  116. iconSize,
  117. iconColor
  118. });
  119. this.changeStatus();
  120. return this;
  121. };
  122. wx.lin.hideToast = () => {
  123. this.setData({
  124. status: false
  125. });
  126. };
  127. },
  128. strlen(str) {
  129. var len = 0;
  130. for (var i = 0; i < str.length; i++) {
  131. var c = str.charCodeAt(i);
  132. if ((c >= '0x0001' && c <= '0x007e') || ('0xff60' <= c && c <= '0xff9f')) {
  133. len++;
  134. } else {
  135. len += 2;
  136. }
  137. }
  138. return len;
  139. },
  140. // 阻止滑动
  141. doNothingMove() {
  142. // do nothing……
  143. },
  144. // 点击事件
  145. onMaskTap() {
  146. let detail = true;
  147. let option = {
  148. bubbles: true,
  149. composed: true
  150. };
  151. if (this.data.locked !== true) {
  152. this.setData({
  153. fullScreen: 'hide',
  154. status: 'hide',
  155. });
  156. }
  157. this.triggerEvent('lintap', detail, option);
  158. }
  159. }
  160. });