test-toast.nvue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <template>
  2. <view class="page">
  3. <view class="header">
  4. <text class="title">Toast 组件测试</text>
  5. </view>
  6. <!-- 基础功能测试 -->
  7. <view class="section">
  8. <text class="section-title">基础功能测试</text>
  9. <view class="btn" @click="testSuccess">
  10. <text class="btn-text">测试 Success</text>
  11. </view>
  12. <view class="btn" @click="testError">
  13. <text class="btn-text">测试 Error</text>
  14. </view>
  15. <view class="btn" @click="testLoading">
  16. <text class="btn-text">测试 Loading (3秒后关闭)</text>
  17. </view>
  18. <view class="btn" @click="testText">
  19. <text class="btn-text">测试 纯文字</text>
  20. </view>
  21. </view>
  22. <!-- 全局事件测试(模拟HTTP模块调用) -->
  23. <view class="section">
  24. <text class="section-title">全局事件测试 (模拟HTTP调用)</text>
  25. <view class="btn btn-orange" @click="testGlobalSuccess">
  26. <text class="btn-text">uni.$emit Success</text>
  27. </view>
  28. <view class="btn btn-orange" @click="testGlobalError">
  29. <text class="btn-text">uni.$emit Error</text>
  30. </view>
  31. <view class="btn btn-orange" @click="testGlobalLoading">
  32. <text class="btn-text">uni.$emit Loading</text>
  33. </view>
  34. </view>
  35. <!-- HTTP 请求集成测试 -->
  36. <view class="section">
  37. <text class="section-title">HTTP 请求测试</text>
  38. <view class="btn btn-blue" @click="testHttpSuccess">
  39. <text class="btn-text">模拟成功请求 (带loading)</text>
  40. </view>
  41. <view class="btn btn-blue" @click="testHttpError">
  42. <text class="btn-text">模拟失败请求 (显示错误)</text>
  43. </view>
  44. <view class="btn btn-blue" @click="testHttpNoLoading">
  45. <text class="btn-text">模拟请求 (不显示loading)</text>
  46. </view>
  47. <view class="btn btn-blue" @click="testHttpConcurrent">
  48. <text class="btn-text">模拟并发请求 (loading计数)</text>
  49. </view>
  50. </view>
  51. <!-- toast 工具方法测试 -->
  52. <view class="section">
  53. <text class="section-title">toast.js 工具方法测试</text>
  54. <view class="btn btn-green" @click="testToastUtil">
  55. <text class="btn-text">import toast 调用</text>
  56. </view>
  57. </view>
  58. <!-- 结果展示 -->
  59. <view class="section" v-if="logs.length > 0">
  60. <text class="section-title">测试日志</text>
  61. <view v-for="(log, index) in logs" :key="index" class="log-item">
  62. <text class="log-text">{{ log }}</text>
  63. </view>
  64. </view>
  65. <ay-toast ref="ayToast" />
  66. </view>
  67. </template>
  68. <script>
  69. import toast from '@/utils/toast.js'
  70. export default {
  71. data() {
  72. return {
  73. logs: []
  74. }
  75. },
  76. methods: {
  77. addLog(msg) {
  78. const time = new Date().toLocaleTimeString()
  79. this.logs.unshift(`[${time}] ${msg}`)
  80. // 最多保留20条
  81. if (this.logs.length > 20) {
  82. this.logs = this.logs.slice(0, 20)
  83. }
  84. },
  85. // ===== 基础功能测试(通过 $refs 直接调用组件方法) =====
  86. testSuccess() {
  87. this.addLog('调用 this.$refs.ayToast.success')
  88. this.$refs.ayToast.success('操作成功!')
  89. },
  90. testError() {
  91. this.addLog('调用 this.$refs.ayToast.error')
  92. this.$refs.ayToast.error('操作失败!')
  93. },
  94. testLoading() {
  95. this.addLog('调用 this.$refs.ayToast.loading, 3秒后hide')
  96. this.$refs.ayToast.loading('加载中...')
  97. setTimeout(() => {
  98. this.$refs.ayToast.hide()
  99. this.addLog('loading 已关闭')
  100. }, 3000)
  101. },
  102. testText() {
  103. this.addLog('调用 this.$refs.ayToast.show (text)')
  104. this.$refs.ayToast.show({ type: 'text', title: '这是一条纯文字提示', duration: 2000 })
  105. },
  106. // ===== 全局事件测试 =====
  107. testGlobalSuccess() {
  108. this.addLog('uni.$emit ayToast:show (success)')
  109. uni.$emit('ayToast:show', {
  110. type: 'success',
  111. title: '全局事件-成功',
  112. duration: 2000
  113. })
  114. },
  115. testGlobalError() {
  116. this.addLog('uni.$emit ayToast:show (error)')
  117. uni.$emit('ayToast:show', {
  118. type: 'error',
  119. title: '全局事件-失败',
  120. duration: 2500
  121. })
  122. },
  123. testGlobalLoading() {
  124. this.addLog('uni.$emit ayToast:show (loading), 3秒后hide')
  125. uni.$emit('ayToast:show', {
  126. type: 'loading',
  127. title: '全局加载中...',
  128. duration: 0,
  129. mask: true
  130. })
  131. setTimeout(() => {
  132. uni.$emit('ayToast:hide')
  133. this.addLog('全局loading 已关闭')
  134. }, 3000)
  135. },
  136. // ===== HTTP 请求集成测试 =====
  137. testHttpSuccess() {
  138. this.addLog('发起模拟成功请求...')
  139. this._mockRequest(1500, true).then(res => {
  140. this.addLog(`请求成功: ${JSON.stringify(res)}`)
  141. }).catch(err => {
  142. this.addLog(`请求失败: ${err.message}`)
  143. })
  144. },
  145. testHttpError() {
  146. this.addLog('发起模拟失败请求...')
  147. this._mockRequest(1000, false).then(res => {
  148. this.addLog(`请求成功: ${JSON.stringify(res)}`)
  149. }).catch(err => {
  150. this.addLog(`请求失败: ${err.message}`)
  151. })
  152. },
  153. testHttpNoLoading() {
  154. this.addLog('发起请求(无loading)...')
  155. this._mockRequest(1000, true, false).then(res => {
  156. this.addLog(`请求成功(无loading): ${JSON.stringify(res)}`)
  157. }).catch(err => {
  158. this.addLog(`请求失败: ${err.message}`)
  159. })
  160. },
  161. testHttpConcurrent() {
  162. this.addLog('发起3个并发请求...')
  163. const p1 = this._mockRequest(1000, true)
  164. const p2 = this._mockRequest(2000, true)
  165. const p3 = this._mockRequest(3000, true)
  166. Promise.all([p1, p2, p3]).then(() => {
  167. this.addLog('3个并发请求全部完成')
  168. })
  169. },
  170. // ===== toast.js 工具方法测试 =====
  171. testToastUtil() {
  172. this.addLog('调用 toast.success (import方式)')
  173. toast.success('通过 import toast 调用')
  174. },
  175. // ===== 模拟HTTP请求 =====
  176. _mockRequest(delay = 1500, success = true, showLoading = true) {
  177. if (showLoading) {
  178. toast.loading('请求中...')
  179. }
  180. return new Promise((resolve, reject) => {
  181. setTimeout(() => {
  182. if (showLoading) {
  183. toast.hide()
  184. }
  185. if (success) {
  186. toast.success('请求成功')
  187. resolve({ code: 200, data: { id: 1, name: '测试数据' } })
  188. } else {
  189. toast.error('网络错误,请重试')
  190. reject(new Error('模拟网络错误'))
  191. }
  192. }, delay)
  193. })
  194. }
  195. }
  196. }
  197. </script>
  198. <style scoped>
  199. .page {
  200. padding-top: 40rpx;
  201. padding-left: 30rpx;
  202. padding-right: 30rpx;
  203. padding-bottom: 60rpx;
  204. /* #ifndef APP-NVUE */
  205. min-height: 100vh;
  206. /* #endif */
  207. background-color: #f5f5f5;
  208. }
  209. .header {
  210. padding-top: 40rpx;
  211. padding-bottom: 40rpx;
  212. align-items: center;
  213. /* #ifndef APP-NVUE */
  214. display: flex;
  215. justify-content: center;
  216. /* #endif */
  217. }
  218. .title {
  219. font-size: 36rpx;
  220. font-weight: bold;
  221. color: #333333;
  222. }
  223. .section {
  224. margin-bottom: 40rpx;
  225. background-color: #ffffff;
  226. border-radius: 16rpx;
  227. padding-top: 24rpx;
  228. padding-bottom: 24rpx;
  229. padding-left: 24rpx;
  230. padding-right: 24rpx;
  231. }
  232. .section-title {
  233. font-size: 28rpx;
  234. font-weight: bold;
  235. color: #666666;
  236. margin-bottom: 20rpx;
  237. }
  238. .btn {
  239. background-color: #389588;
  240. border-radius: 12rpx;
  241. padding-top: 20rpx;
  242. padding-bottom: 20rpx;
  243. margin-top: 16rpx;
  244. align-items: center;
  245. /* #ifndef APP-NVUE */
  246. display: flex;
  247. justify-content: center;
  248. /* #endif */
  249. }
  250. .btn-orange {
  251. background-color: #e6a23c;
  252. }
  253. .btn-blue {
  254. background-color: #409eff;
  255. }
  256. .btn-green {
  257. background-color: #67c23a;
  258. }
  259. .btn-text {
  260. color: #ffffff;
  261. font-size: 28rpx;
  262. }
  263. .log-item {
  264. padding-top: 8rpx;
  265. padding-bottom: 8rpx;
  266. border-bottom-width: 1rpx;
  267. border-bottom-style: solid;
  268. border-bottom-color: #eeeeee;
  269. }
  270. .log-text {
  271. font-size: 22rpx;
  272. color: #999999;
  273. }
  274. </style>