homePage.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template>
  2. <view>
  3. </view>
  4. </template>
  5. <script setup>
  6. // 添加路由拦截
  7. import '@/interceptor/routeInterceptor.js'
  8. import { encryptByEnAESLogin } from '@/utils/index.js'
  9. import { onLoad } from '@dcloudio/uni-app'
  10. import { defaultColor } from '@/static/js/theme.js'
  11. import { api_wechatLoginEncrypt, api_loginEncrypt } from "@/http/api.js"
  12. import { useWechatAuth } from '@/share/useWechatAuth.js'
  13. import { useLoginSuccess } from '@/share/useLoginSuccess.js'
  14. import { useSetTitle } from '@/share/useSetTitle.js'
  15. useSetTitle()
  16. const { wechatAuth } = useWechatAuth()
  17. const { loginSuccess } = useLoginSuccess()
  18. /**
  19. * 微信登录
  20. */
  21. function wechatLoginEncrypt() {
  22. const url = window.location.href.split("?");
  23. if (!url[1]) {
  24. wechatAuth();
  25. } else {
  26. const code = {
  27. code: url[1],
  28. type: 'user',
  29. };
  30. uni.showLoading({
  31. title: "登录中",
  32. mask: true,
  33. });
  34. api_wechatLoginEncrypt(code).then(res => {
  35. uni.hideLoading();
  36. if (res.state == 200) {
  37. loginSuccess(res.user);
  38. } else if (res.state == 501) {
  39. uni.showModal({
  40. title: '提示',
  41. content: '您的账号被删除,请联系管理员',
  42. showCancel: false,
  43. confirmColor: defaultColor,
  44. confirmText: '取消',
  45. });
  46. } else if (res.state == 403) {
  47. uni.reLaunch({
  48. url: `/pages/initBind/initBind?wechat=${res.wechat}`
  49. })
  50. } else {
  51. uni.showToast({
  52. icon: 'none',
  53. title: res.remarks || '请求数据失败!'
  54. });
  55. }
  56. });
  57. }
  58. }
  59. /**
  60. * 账号密码登录
  61. */
  62. function loginEncrypt(username, password) {
  63. uni.showLoading({
  64. title: "登录中",
  65. mask: true,
  66. });
  67. let postData = {
  68. k: encryptByEnAESLogin(JSON.stringify({username, password}))
  69. };
  70. api_loginEncrypt(postData).then(res => {
  71. uni.hideLoading();
  72. if (res.state == 200) {
  73. loginSuccess(res.data);
  74. } else {
  75. uni.showToast({
  76. icon: 'none',
  77. title: res.remarks || '请求数据失败!'
  78. });
  79. }
  80. });
  81. }
  82. onLoad((option) => {
  83. uni.clearStorageSync();
  84. // 获取当前页面的实例
  85. const pages = getCurrentPages();
  86. const currentPage = pages[pages.length - 1];
  87. // 获取URL参数
  88. const options = currentPage.options;
  89. if (process.env.NODE_ENV === 'development' && options.username && options.password) {
  90. // http://localhost:8081/user/#/?username=liaomingming&password=Dstech@123
  91. // 本地开发调试用
  92. loginEncrypt(options.username, options.password)
  93. } else {
  94. wechatLoginEncrypt();
  95. }
  96. })
  97. </script>