ay-toast.nvue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <template>
  2. <view class="ay-toast-root" v-if="visible">
  3. <!-- 遮罩层 -->
  4. <view class="ay-toast-mask" v-if="mask" @click="onMaskClick"></view>
  5. <!-- Toast主体 -->
  6. <view class="ay-toast-container">
  7. <view class="ay-toast-box" ref="toastBox">
  8. <!-- 图标区域 -->
  9. <view class="ay-toast-icon-wrap" v-if="type !== 'text'">
  10. <!-- 成功图标 -->
  11. <view class="ay-toast-icon-success" v-if="type === 'success'">
  12. <view class="success-bg"></view>
  13. <view class="success-line1"></view>
  14. <view class="success-line2"></view>
  15. </view>
  16. <!-- 失败图标 -->
  17. <view class="ay-toast-icon-error" v-if="type === 'error'">
  18. <view class="error-bg"></view>
  19. <view class="error-line1"></view>
  20. <view class="error-line2"></view>
  21. </view>
  22. <!-- 加载图标 -->
  23. <view class="ay-toast-icon-loading" v-if="type === 'loading'">
  24. <view class="loading-ring" ref="loadingRef"></view>
  25. </view>
  26. </view>
  27. <!-- 文字区域 -->
  28. <text class="ay-toast-text" :class="textColorClass" v-if="title">{{ title }}</text>
  29. </view>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. // #ifdef APP-NVUE
  35. const animation = weex.requireModule('animation')
  36. // #endif
  37. export default {
  38. name: 'ayToast',
  39. data() {
  40. return {
  41. visible: false,
  42. type: 'text', // success | error | loading | text
  43. title: '',
  44. duration: 2000,
  45. mask: false,
  46. timer: null,
  47. loadingAnimTimer: null
  48. }
  49. },
  50. computed: {
  51. textColorClass() {
  52. if (this.type === 'success') return 'text-success'
  53. if (this.type === 'error') return 'text-error'
  54. return 'text-default'
  55. }
  56. },
  57. created() {
  58. uni.$on('ayToast:show', this.show)
  59. uni.$on('ayToast:hide', this.hide)
  60. },
  61. beforeUnmount() {
  62. this._cleanup()
  63. },
  64. beforeDestroy() {
  65. this._cleanup()
  66. },
  67. methods: {
  68. _cleanup() {
  69. uni.$off('ayToast:show', this.show)
  70. uni.$off('ayToast:hide', this.hide)
  71. this.clearTimer()
  72. this._stopLoadingAnim()
  73. },
  74. show(options) {
  75. this.clearTimer()
  76. this._stopLoadingAnim()
  77. if (typeof options === 'string') {
  78. options = { title: options, type: 'text' }
  79. }
  80. const {
  81. type = 'text',
  82. title = '',
  83. duration,
  84. mask
  85. } = options
  86. this.type = type
  87. this.title = title
  88. this.mask = mask !== undefined ? mask : (type === 'loading')
  89. if (duration !== undefined) {
  90. this.duration = duration
  91. } else {
  92. this.duration = type === 'loading' ? 0 : 2000
  93. }
  94. this.visible = true
  95. // 入场动画 + loading旋转
  96. this.$nextTick(() => {
  97. this._playEnterAnim()
  98. if (type === 'loading') {
  99. this._startLoadingAnim()
  100. }
  101. })
  102. if (this.duration > 0) {
  103. this.timer = setTimeout(() => {
  104. this.hide()
  105. }, this.duration)
  106. }
  107. },
  108. success(title, options = {}) {
  109. this.show({ type: 'success', title, ...options })
  110. },
  111. error(title, options = {}) {
  112. this.show({ type: 'error', title, ...options })
  113. },
  114. loading(title = '加载中...', options = {}) {
  115. this.show({ type: 'loading', title, duration: 0, mask: true, ...options })
  116. },
  117. hide() {
  118. this.clearTimer()
  119. this._stopLoadingAnim()
  120. this.visible = false
  121. },
  122. clearTimer() {
  123. if (this.timer) {
  124. clearTimeout(this.timer)
  125. this.timer = null
  126. }
  127. },
  128. // 入场缩放动画
  129. _playEnterAnim() {
  130. // #ifdef APP-NVUE
  131. const el = this.$refs.toastBox
  132. if (!el) return
  133. animation.transition(el, {
  134. styles: { transform: 'scale(0.8)', opacity: 0 },
  135. duration: 0
  136. }, () => {
  137. animation.transition(el, {
  138. styles: { transform: 'scale(1)', opacity: 1 },
  139. duration: 200,
  140. timingFunction: 'ease-out'
  141. })
  142. })
  143. // #endif
  144. },
  145. // loading 旋转动画
  146. _startLoadingAnim() {
  147. // #ifdef APP-NVUE
  148. const el = this.$refs.loadingRef
  149. if (!el) return
  150. const doRotate = () => {
  151. if (!this.visible || this.type !== 'loading') return
  152. animation.transition(el, {
  153. styles: { transform: 'rotate(360deg)' },
  154. duration: 900,
  155. timingFunction: 'linear'
  156. }, () => {
  157. if (!this.visible || this.type !== 'loading') return
  158. animation.transition(el, {
  159. styles: { transform: 'rotate(0deg)' },
  160. duration: 0
  161. }, () => {
  162. doRotate()
  163. })
  164. })
  165. }
  166. doRotate()
  167. // #endif
  168. },
  169. _stopLoadingAnim() {
  170. if (this.loadingAnimTimer) {
  171. clearTimeout(this.loadingAnimTimer)
  172. this.loadingAnimTimer = null
  173. }
  174. },
  175. onMaskClick() {
  176. // loading状态下防止误操作
  177. }
  178. }
  179. }
  180. </script>
  181. <style scoped>
  182. .ay-toast-root {
  183. position: fixed;
  184. top: 0;
  185. left: 0;
  186. right: 0;
  187. bottom: 0;
  188. z-index: 99999;
  189. justify-content: center;
  190. align-items: center;
  191. /* #ifndef APP-NVUE */
  192. display: flex;
  193. width: 100%;
  194. height: 100%;
  195. /* #endif */
  196. }
  197. .ay-toast-mask {
  198. position: absolute;
  199. top: 0;
  200. left: 0;
  201. right: 0;
  202. bottom: 0;
  203. background-color: rgba(0, 0, 0, 0.15);
  204. /* #ifndef APP-NVUE */
  205. width: 100%;
  206. height: 100%;
  207. /* #endif */
  208. }
  209. .ay-toast-container {
  210. justify-content: center;
  211. align-items: center;
  212. /* #ifndef APP-NVUE */
  213. display: flex;
  214. flex-direction: column;
  215. /* #endif */
  216. }
  217. .ay-toast-box {
  218. background-color: #FFFFFF;
  219. border-radius: 20rpx;
  220. padding-top: 36rpx;
  221. padding-bottom: 32rpx;
  222. padding-left: 44rpx;
  223. padding-right: 44rpx;
  224. min-width: 220rpx;
  225. max-width: 360rpx;
  226. justify-content: center;
  227. align-items: center;
  228. /* #ifdef APP-NVUE */
  229. border-width: 1px;
  230. border-style: solid;
  231. border-color: #f0f0f0;
  232. /* #endif */
  233. /* #ifndef APP-NVUE */
  234. display: flex;
  235. flex-direction: column;
  236. box-shadow: 0 16rpx 48rpx rgba(56, 149, 136, 0.12);
  237. /* #endif */
  238. }
  239. /* 图标容器 */
  240. .ay-toast-icon-wrap {
  241. width: 76rpx;
  242. height: 76rpx;
  243. margin-bottom: 20rpx;
  244. justify-content: center;
  245. align-items: center;
  246. /* #ifndef APP-NVUE */
  247. display: flex;
  248. position: relative;
  249. /* #endif */
  250. }
  251. /* ===== 成功图标 ===== */
  252. .ay-toast-icon-success {
  253. width: 76rpx;
  254. height: 76rpx;
  255. justify-content: center;
  256. align-items: center;
  257. /* #ifndef APP-NVUE */
  258. display: flex;
  259. position: relative;
  260. /* #endif */
  261. }
  262. .success-bg {
  263. position: absolute;
  264. width: 76rpx;
  265. height: 76rpx;
  266. border-radius: 38rpx;
  267. background-color: #1a9e78;
  268. /* #ifndef APP-NVUE */
  269. top: 0;
  270. left: 0;
  271. /* #endif */
  272. }
  273. .success-line1 {
  274. position: absolute;
  275. width: 18rpx;
  276. height: 4rpx;
  277. background-color: #ffffff;
  278. border-radius: 2rpx;
  279. top: 42rpx;
  280. left: 20rpx;
  281. transform: rotate(45deg);
  282. }
  283. .success-line2 {
  284. position: absolute;
  285. width: 32rpx;
  286. height: 4rpx;
  287. background-color: #ffffff;
  288. border-radius: 2rpx;
  289. top: 37rpx;
  290. left: 28rpx;
  291. transform: rotate(-45deg);
  292. }
  293. /* ===== 错误图标 ===== */
  294. .ay-toast-icon-error {
  295. width: 76rpx;
  296. height: 76rpx;
  297. justify-content: center;
  298. align-items: center;
  299. /* #ifndef APP-NVUE */
  300. display: flex;
  301. position: relative;
  302. /* #endif */
  303. }
  304. .error-bg {
  305. position: absolute;
  306. width: 76rpx;
  307. height: 76rpx;
  308. border-radius: 38rpx;
  309. background-color: #F65252;
  310. /* #ifndef APP-NVUE */
  311. top: 0;
  312. left: 0;
  313. /* #endif */
  314. }
  315. .error-line1 {
  316. position: absolute;
  317. width: 32rpx;
  318. height: 4rpx;
  319. background-color: #ffffff;
  320. border-radius: 2rpx;
  321. transform: rotate(45deg);
  322. /* #ifndef APP-NVUE */
  323. top: 50%;
  324. left: 50%;
  325. margin-top: -2.5rpx;
  326. margin-left: -20rpx;
  327. /* #endif */
  328. }
  329. .error-line2 {
  330. position: absolute;
  331. width: 32rpx;
  332. height: 4rpx;
  333. background-color: #ffffff;
  334. border-radius: 2rpx;
  335. transform: rotate(-45deg);
  336. /* #ifndef APP-NVUE */
  337. top: 50%;
  338. left: 50%;
  339. margin-top: -2.5rpx;
  340. margin-left: -20rpx;
  341. /* #endif */
  342. }
  343. /* ===== 加载图标 ===== */
  344. .ay-toast-icon-loading {
  345. width: 76rpx;
  346. height: 76rpx;
  347. justify-content: center;
  348. align-items: center;
  349. /* #ifndef APP-NVUE */
  350. display: flex;
  351. position: relative;
  352. /* #endif */
  353. }
  354. .loading-ring {
  355. width: 56rpx;
  356. height: 56rpx;
  357. border-radius: 28rpx;
  358. border-width: 4rpx;
  359. border-style: solid;
  360. border-color: #e8f5f2;
  361. /* #ifndef APP-NVUE */
  362. border-top-color: #1a9e78;
  363. box-sizing: border-box;
  364. animation: loading-spin 0.9s linear infinite;
  365. /* #endif */
  366. /* #ifdef APP-NVUE */
  367. border-top-color: #1a9e78;
  368. /* #endif */
  369. }
  370. /* 文字 */
  371. .ay-toast-text {
  372. font-size: 28rpx;
  373. text-align: center;
  374. /* #ifdef APP-NVUE */
  375. lines: 2;
  376. /* #endif */
  377. /* #ifndef APP-NVUE */
  378. overflow: hidden;
  379. text-overflow: ellipsis;
  380. display: -webkit-box;
  381. -webkit-line-clamp: 2;
  382. -webkit-box-orient: vertical;
  383. word-break: break-all;
  384. /* #endif */
  385. max-width: 320rpx;
  386. }
  387. .text-default {
  388. color: #545F71;
  389. }
  390. .text-success {
  391. color: #1a9e78;
  392. }
  393. .text-error {
  394. color: #F65252;
  395. }
  396. /* #ifndef APP-NVUE */
  397. @keyframes loading-spin {
  398. from {
  399. transform: rotate(0deg);
  400. }
  401. to {
  402. transform: rotate(360deg);
  403. }
  404. }
  405. /* #endif */
  406. </style>