v-toast.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <view
  3. class="v-toast"
  4. :class="[isShow ? 'u-show' : '', 'u-position-' + tmpConfig.position]"
  5. :style="{
  6. zIndex: uZIndex
  7. }"
  8. >
  9. <view class="u-icon-wrap">
  10. <!-- <u-icon v-if="tmpConfig.icon" class="u-icon" :name="iconName" :size="30" :color="tmpConfig.type"></u-icon> -->
  11. <u-icon class="u-icon" v-if="tmpConfig.icon" custom-prefix="custom-icon" :name="iconName" size="36" :color="iconColor"></u-icon>
  12. </view>
  13. <text class="u-text">{{ tmpConfig.title }}</text>
  14. </view>
  15. </template>
  16. <script>
  17. /**
  18. * toast 消息提示
  19. * @description 此组件表现形式类似uni的uni.showToastAPI,但也有不同的地方。
  20. * @tutorial https://www.uviewui.com/components/toast.html
  21. * @property {String} z-index toast展示时的z-index值
  22. * @event {Function} show 显示toast,如需一进入页面就显示toast,请在onReady生命周期调用
  23. * @example <v-toast ref="vToast" />
  24. */
  25. export default {
  26. name: 'v-toast',
  27. props: {
  28. // z-index值
  29. zIndex: {
  30. type: [Number, String],
  31. default: ''
  32. }
  33. },
  34. data() {
  35. return {
  36. isShow: false,
  37. timer: null, // 定时器
  38. config: {
  39. params: {}, // URL跳转的参数,对象
  40. title: '', // 显示文本
  41. type: 'success', // 主题类型,primary,success,error,warning,black
  42. duration: 2000, // 显示的时间,毫秒
  43. isTab: false, // 是否跳转tab页面
  44. url: '', // toast消失后是否跳转页面,有则跳转,优先级高于back参数
  45. icon: true, // 显示的图标
  46. position: 'center', // toast出现的位置
  47. success: null, // 执行完后的回调函数
  48. back: false // 结束toast是否自动返回上一页
  49. },
  50. tmpConfig: {} // 将用户配置和内置配置合并后的临时配置变量
  51. }
  52. },
  53. computed: {
  54. iconName() {
  55. // 只有不为none,并且type为error|warning|success的时候,才显示图标
  56. if (['error', 'warning', 'success'].indexOf(this.tmpConfig.type) >= 0 && this.tmpConfig.icon) {
  57. // let icon = this.$u.type2icon(this.tmpConfig.type);
  58. // return icon;
  59. switch (this.tmpConfig.type) {
  60. case 'success':
  61. return 'correct'
  62. break
  63. case 'warning':
  64. return 'tips'
  65. break
  66. case 'error':
  67. return 'error'
  68. }
  69. }
  70. },
  71. iconColor() {
  72. switch (this.tmpConfig.type) {
  73. case 'success':
  74. return '#85C861'
  75. break
  76. case 'warning':
  77. return '#FFD674'
  78. break
  79. case 'error':
  80. return '#FF7070'
  81. }
  82. },
  83. uZIndex() {
  84. // 显示toast时候,如果用户有传递z-index值,有限使用
  85. return this.isShow ? (this.zIndex ? this.zIndex : this.$u.zIndex.toast) : '-1'
  86. }
  87. },
  88. methods: {
  89. // 显示toast组件,由父组件通过this.$refs.xxx.show(options)形式调用
  90. show(options) {
  91. // 不将结果合并到this.config变量,避免多次条用v-toast,前后的配置造成混论
  92. this.tmpConfig = this.$u.deepMerge(this.config, options)
  93. if (this.timer) {
  94. // 清除定时器
  95. clearTimeout(this.timer)
  96. this.timer = null
  97. }
  98. this.isShow = true
  99. this.timer = setTimeout(() => {
  100. // 倒计时结束,清除定时器,隐藏toast组件
  101. this.isShow = false
  102. clearTimeout(this.timer)
  103. this.timer = null
  104. // 判断是否存在success方法,如果存在就执行
  105. typeof this.tmpConfig.success === 'function' && this.tmpConfig.success()
  106. this.timeEnd()
  107. }, this.tmpConfig.duration)
  108. },
  109. // 隐藏toast组件,由父组件通过this.$refs.xxx.hide()形式调用
  110. hide() {
  111. this.isShow = false
  112. if (this.timer) {
  113. // 清除定时器
  114. clearTimeout(this.timer)
  115. this.timer = null
  116. }
  117. },
  118. // 倒计时结束之后,进行的一些操作
  119. timeEnd() {
  120. // 如果带有url值,根据isTab为true或者false进行跳转
  121. if (this.tmpConfig.url) {
  122. // 如果url没有"/"开头,添加上,因为uni的路由跳转需要"/"开头
  123. if (this.tmpConfig.url[0] != '/') this.tmpConfig.url = '/' + this.tmpConfig.url
  124. // 判断是否有传递显式的参数
  125. if (Object.keys(this.tmpConfig.params).length) {
  126. // 判断用户传递的url中,是否带有参数
  127. // 使用正则匹配,主要依据是判断是否有"/","?","="等,如“/page/index/index?name=mary"
  128. // 如果有params参数,转换后无需带上"?"
  129. let query = ''
  130. if (/.*\/.*\?.*=.*/.test(this.tmpConfig.url)) {
  131. // object对象转为get类型的参数
  132. query = this.$u.queryParams(this.tmpConfig.params, false)
  133. this.tmpConfig.url = this.tmpConfig.url + '&' + query
  134. } else {
  135. query = this.$u.queryParams(this.tmpConfig.params)
  136. this.tmpConfig.url += query
  137. }
  138. }
  139. // 如果是跳转tab页面,就使用uni.switchTab
  140. if (this.tmpConfig.isTab) {
  141. uni.switchTab({
  142. url: this.tmpConfig.url
  143. })
  144. } else {
  145. uni.navigateTo({
  146. url: this.tmpConfig.url
  147. })
  148. }
  149. } else if (this.tmpConfig.back) {
  150. // 回退到上一页
  151. this.$u.route({
  152. type: 'back'
  153. })
  154. }
  155. }
  156. }
  157. }
  158. </script>
  159. <style lang="scss" scoped>
  160. .v-toast {
  161. position: fixed;
  162. display: flex;
  163. align-items: center;
  164. justify-content: center;
  165. padding: 0 40rpx;
  166. // z-index: -1;
  167. transition: opacity 0.2s;
  168. text-align: center;
  169. border-radius: 16rpx;
  170. // background: #585858;
  171. background-color: rgba($color: #000000, $alpha: 0.7);
  172. height: 80rpx;
  173. font-size: 28rpx;
  174. opacity: 0;
  175. }
  176. .animation {
  177. animation: move 0.2s ease;
  178. }
  179. @keyframes move {
  180. 0% {
  181. transform: translateX(-50%) translateY(20%);
  182. }
  183. // 80% {
  184. // transform: translateX(-50%) translateY(-80%);
  185. // }
  186. 100% {
  187. transform: translateX(-50%) translateY(-50%);
  188. }
  189. }
  190. .v-toast.u-show {
  191. opacity: 1;
  192. }
  193. .u-text {
  194. color: #fff;
  195. word-break: keep-all;
  196. white-space: nowrap;
  197. line-height: normal;
  198. }
  199. .u-icon {
  200. margin-right: 10rpx;
  201. display: flex;
  202. align-items: center;
  203. line-height: normal;
  204. }
  205. .u-position-center {
  206. left: 50%;
  207. top: 50%;
  208. transform: translateX(-50%) translateY(-50%);
  209. position: fixed;
  210. }
  211. .u-position-top {
  212. left: 50%;
  213. top: 20%;
  214. transform: translateX(-50%) translateY(-50%);
  215. }
  216. .u-position-bottom {
  217. left: 50%;
  218. bottom: 20%;
  219. transform: translateX(-50%) translateY(-50%);
  220. }
  221. .u-type-primary {
  222. // color: $u-type-primary;
  223. // background-color: $u-type-primary-light;
  224. // border: 1px solid rgb(215, 234, 254);
  225. }
  226. .u-type-success {
  227. color: $u-type-success;
  228. background-color: $u-type-success-light;
  229. border: 1px solid #bef5c8;
  230. }
  231. .u-type-error {
  232. color: $u-type-error;
  233. background-color: $u-type-error-light;
  234. border: 1px solid #fde2e2;
  235. }
  236. .u-type-warning {
  237. // color: $u-type-warning;
  238. // background-color: $u-type-warning-light;
  239. // border: 1px solid #faecd8;
  240. color: #fff;
  241. font-size: 28rpx;
  242. background-color: rgba($color: #000000, $alpha: 0.5);
  243. }
  244. .u-type-info {
  245. color: $u-type-info;
  246. background-color: $u-type-info-light;
  247. border: 1px solid #ebeef5;
  248. }
  249. .u-type-default {
  250. color: #fff;
  251. background-color: #585858;
  252. }
  253. </style>