login.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <view class="">
  3. <u-loading-page iconSize="160" :loading="true" loading-text="loading..."
  4. image="/static/images/common/loading.png"></u-loading-page>
  5. </view>
  6. </template>
  7. <script>
  8. import {
  9. setToken
  10. } from '@/utils/auth'
  11. import {
  12. login,
  13. verifyScanCode
  14. } from '@/api/login'
  15. import {
  16. getScanData
  17. } from '@/api/scan'
  18. export default {
  19. data() {
  20. return {
  21. scanCode: '',
  22. path: '',
  23. status: false
  24. }
  25. },
  26. onLoad(options) {
  27. uni.removeStorageSync("scanCode");
  28. uni.removeStorageSync("Authorization-status");
  29. // 获取并解码 q 参数
  30. let qlink = decodeURIComponent(options.q || '');
  31. if (qlink) {
  32. const decodedUrl = decodeURIComponent(qlink); // 先解码
  33. // 1. 去掉域名部分,提取路径
  34. const scanCode = decodedUrl.replace('https://e.dnzc.vip/', ''); // 得到 "a7e4a3f2947004f6"
  35. this.scanCode = scanCode;
  36. }
  37. this.login(); // 调用登录方法
  38. },
  39. methods: {
  40. login() {
  41. let that = this;
  42. if (this.scanCode == '' || this.scanCode == undefined) {
  43. this.status = true;
  44. }
  45. wx.login({
  46. success(res) {
  47. if (res.code) {
  48. // 例如发送到服务器进行登录态验证等操作
  49. login(that.scanCode, res.code).then(res => {
  50. console.log('登录成功', res);
  51. if (res.code === 0) {
  52. if (res.data.isWxAuth == 1) {
  53. uni.setStorageSync('Authorization-status', true);
  54. }
  55. that.scanCode = res.data.qrCode;
  56. setToken(res.data.sessionId)
  57. that.path = res.data.page_path;
  58. that.verify();
  59. } else {
  60. if (res.code === 98) {
  61. uni.showToast({
  62. title: '请重新扫描二维码进入页面',
  63. icon: 'none'
  64. });
  65. } else {
  66. uni.showToast({
  67. title: res.msg,
  68. icon: 'none'
  69. });
  70. console.log('登录失败!' + res.msg);
  71. }
  72. }
  73. })
  74. } else {
  75. console.log('登录失败!' + res.errMsg);
  76. uni.showToast({
  77. title: res.msg || '登录失败',
  78. icon: 'none'
  79. });
  80. }
  81. },
  82. fail(err) {
  83. uni.showToast({
  84. title: err.msg || '登录失败',
  85. icon: 'none'
  86. });
  87. console.log('登录失败', err);
  88. }
  89. });
  90. },
  91. verify() {
  92. verifyScanCode(this.scanCode).then(res => {
  93. console.log(res);
  94. if (res.code === 0) {
  95. uni.setStorageSync("scanCode", this.scanCode);
  96. if (res.data.url !== null && res.data.url !== undefined && res.data.url !== '') {
  97. uni.redirectTo({
  98. url: '/pages/' + this.path + '/index/authCode'
  99. })
  100. return;
  101. }
  102. getScanData(this.scanCode).then(res => {
  103. let url;
  104. if (res.code === 0) {
  105. url = '/pages/' + this.path + '/index/' + res.data.url;
  106. uni.setStorageSync('scanDetail', res.data);
  107. } else {
  108. url = '/pages/' + this.path + '/error?msg=' + res.msg;
  109. }
  110. uni.redirectTo({
  111. url: url // 目标页面路径
  112. })
  113. })
  114. } else {
  115. let url = '/pages/' + this.path + '/error?msg=' + res.msg;
  116. uni.redirectTo({
  117. url: url // 目标页面路径
  118. })
  119. }
  120. })
  121. }
  122. }
  123. }
  124. </script>