auth-test.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <template>
  2. <view class="auth-test">
  3. <view class="header">
  4. <text class="title">权限测试页面</text>
  5. <text class="subtitle">测试4个核心动作的登录拦截</text>
  6. </view>
  7. <view class="test-section">
  8. <view class="section-title">测试4个核心动作</view>
  9. <view class="test-item" @click="testAddToCart">
  10. <text class="test-icon">🛒</text>
  11. <text class="test-text">测试:加入购物车</text>
  12. <text class="test-desc">商品详情页-加入购物车按钮</text>
  13. </view>
  14. <view class="test-item" @click="testBuyNow">
  15. <text class="test-icon">💰</text>
  16. <text class="test-text">测试:立即购买</text>
  17. <text class="test-desc">商品详情页-立即购买按钮</text>
  18. </view>
  19. <view class="test-item" @click="testMyPage">
  20. <text class="test-icon">👤</text>
  21. <text class="test-text">测试:我的页面</text>
  22. <text class="test-desc">点击"我的"tab</text>
  23. </view>
  24. <view class="test-item" @click="testLegou">
  25. <text class="test-icon">🛍️</text>
  26. <text class="test-text">测试:乐购页面</text>
  27. <text class="test-desc">点击"乐购"tab</text>
  28. </view>
  29. </view>
  30. <view class="status-section">
  31. <view class="section-title">当前状态</view>
  32. <view class="status-item">
  33. <text class="status-label">登录状态:</text>
  34. <text class="status-value" :class="{ 'logged-in': isLoggedIn, 'guest': !isLoggedIn }">
  35. {{ isLoggedIn ? '已登录' : '游客模式' }}
  36. </text>
  37. </view>
  38. <view class="status-item" v-if="isLoggedIn">
  39. <text class="status-label">用户信息:</text>
  40. <text class="status-value">{{ userInfo.phone || '未知' }}</text>
  41. </view>
  42. </view>
  43. <view class="action-section">
  44. <view class="section-title">操作</view>
  45. <button class="action-btn logout-btn" @click="handleLogout" v-if="isLoggedIn">
  46. 退出登录
  47. </button>
  48. <button class="action-btn login-btn" @click="handleLogin" v-else>
  49. 去登录
  50. </button>
  51. </view>
  52. </view>
  53. </template>
  54. <script>
  55. import { isLoggedIn, getUserInfo, requireAuth, PROTECTED_ACTIONS, clearUserData, navigateToLogin } from '@/utils/auth.js'
  56. export default {
  57. name: 'AuthTest',
  58. data() {
  59. return {
  60. isLoggedIn: false,
  61. userInfo: {}
  62. }
  63. },
  64. onLoad() {
  65. console.log('权限测试页面加载')
  66. },
  67. onShow() {
  68. this.checkLoginStatus()
  69. },
  70. methods: {
  71. checkLoginStatus() {
  72. this.isLoggedIn = isLoggedIn()
  73. if (this.isLoggedIn) {
  74. this.userInfo = getUserInfo() || {}
  75. }
  76. },
  77. // 测试加入购物车
  78. testAddToCart() {
  79. requireAuth(() => {
  80. uni.showToast({
  81. title: '✅ 已登录,可以加入购物车',
  82. icon: 'success'
  83. })
  84. }, {
  85. action: PROTECTED_ACTIONS.ADD_TO_CART,
  86. returnUrl: '/pages/test/auth-test'
  87. })
  88. },
  89. // 测试立即购买
  90. testBuyNow() {
  91. requireAuth(() => {
  92. uni.showToast({
  93. title: '✅ 已登录,可以立即购买',
  94. icon: 'success'
  95. })
  96. }, {
  97. action: PROTECTED_ACTIONS.BUY_NOW,
  98. returnUrl: '/pages/test/auth-test'
  99. })
  100. },
  101. // 测试我的页面
  102. testMyPage() {
  103. requireAuth(() => {
  104. uni.showToast({
  105. title: '✅ 已登录,可以访问我的页面',
  106. icon: 'success'
  107. })
  108. }, {
  109. action: PROTECTED_ACTIONS.VIEW_MY_PAGE,
  110. returnUrl: '/pages/test/auth-test'
  111. })
  112. },
  113. // 测试乐购页面
  114. testLegou() {
  115. requireAuth(() => {
  116. uni.showToast({
  117. title: '✅ 已登录,可以访问乐购页面',
  118. icon: 'success'
  119. })
  120. }, {
  121. action: PROTECTED_ACTIONS.VIEW_LEGOU,
  122. returnUrl: '/pages/test/auth-test'
  123. })
  124. },
  125. // 退出登录
  126. handleLogout() {
  127. uni.showModal({
  128. title: '提示',
  129. content: '确定要退出登录吗?',
  130. success: (res) => {
  131. if (res.confirm) {
  132. clearUserData()
  133. this.checkLoginStatus()
  134. uni.showToast({
  135. title: '已退出登录',
  136. icon: 'success'
  137. })
  138. }
  139. }
  140. })
  141. },
  142. // 去登录
  143. handleLogin() {
  144. navigateToLogin('/pages/test/auth-test')
  145. }
  146. }
  147. }
  148. </script>
  149. <style lang="scss" scoped>
  150. .auth-test {
  151. min-height: 100vh;
  152. background: #f5f5f5;
  153. padding: 20rpx;
  154. }
  155. .header {
  156. text-align: center;
  157. padding: 40rpx 0;
  158. .title {
  159. font-size: 36rpx;
  160. font-weight: bold;
  161. color: #333;
  162. display: block;
  163. margin-bottom: 10rpx;
  164. }
  165. .subtitle {
  166. font-size: 26rpx;
  167. color: #666;
  168. }
  169. }
  170. .test-section, .status-section, .action-section {
  171. background: #ffffff;
  172. border-radius: 12rpx;
  173. padding: 30rpx;
  174. margin-bottom: 20rpx;
  175. .section-title {
  176. font-size: 32rpx;
  177. font-weight: bold;
  178. color: #333;
  179. margin-bottom: 20rpx;
  180. border-bottom: 1rpx solid #f0f0f0;
  181. padding-bottom: 10rpx;
  182. }
  183. }
  184. .test-item {
  185. display: flex;
  186. align-items: center;
  187. padding: 20rpx 0;
  188. border-bottom: 1rpx solid #f5f5f5;
  189. &:last-child {
  190. border-bottom: none;
  191. }
  192. .test-icon {
  193. font-size: 40rpx;
  194. margin-right: 20rpx;
  195. }
  196. .test-text {
  197. font-size: 28rpx;
  198. color: #333;
  199. font-weight: bold;
  200. margin-right: 20rpx;
  201. }
  202. .test-desc {
  203. font-size: 24rpx;
  204. color: #999;
  205. flex: 1;
  206. }
  207. }
  208. .status-item {
  209. display: flex;
  210. align-items: center;
  211. padding: 15rpx 0;
  212. .status-label {
  213. font-size: 28rpx;
  214. color: #666;
  215. width: 160rpx;
  216. }
  217. .status-value {
  218. font-size: 28rpx;
  219. font-weight: bold;
  220. &.logged-in {
  221. color: #28a745;
  222. }
  223. &.guest {
  224. color: #dc3545;
  225. }
  226. }
  227. }
  228. .action-btn {
  229. width: 100%;
  230. height: 80rpx;
  231. border: none;
  232. border-radius: 40rpx;
  233. font-size: 28rpx;
  234. font-weight: bold;
  235. &.logout-btn {
  236. background: #dc3545;
  237. color: #ffffff;
  238. }
  239. &.login-btn {
  240. background: #28a745;
  241. color: #ffffff;
  242. }
  243. }
  244. </style>