login.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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: 'a77ef7cc629d8400',
  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://scantest.dnzc.vip/', ''); // 得到 "a7e4a3f2947004f6"
  35. this.scanCode = scanCode;
  36. }
  37. this.login(); // 调用登录方法
  38. },
  39. methods: {
  40. login() {
  41. let that = this;
  42. console.log("111111111111")
  43. if (this.scanCode == '' || this.scanCode == undefined) {
  44. this.status = true;
  45. }
  46. wx.login({
  47. success(res) {
  48. if (res.code) {
  49. // 例如发送到服务器进行登录态验证等操作
  50. login(that.scanCode, res.code).then(res => {
  51. console.log('登录成功', res);
  52. if (res.code === 0) {
  53. if (res.data.isWxAuth == 1) {
  54. uni.setStorageSync('Authorization-status', true);
  55. }
  56. that.scanCode = res.data.qrCode;
  57. setToken(res.data.sessionId)
  58. that.path = res.data.page_path;
  59. that.verify();
  60. } else {
  61. console.log('登录失败!' + res.msg);
  62. }
  63. })
  64. } else {
  65. console.log('登录失败!' + res.errMsg);
  66. }
  67. },
  68. fail(err) {
  69. console.log('登录失败', err);
  70. }
  71. });
  72. },
  73. verify() {
  74. verifyScanCode(this.scanCode).then(res => {
  75. console.log(res);
  76. if (res.code === 0) {
  77. uni.setStorageSync("scanCode", this.scanCode);
  78. if (res.data.url !== null && res.data.url !== undefined && res.data.url !== '') {
  79. uni.redirectTo({
  80. url: '/pages/' + this.path + '/index/authCode'
  81. })
  82. return;
  83. }
  84. if (this.status) {
  85. uni.redirectTo({
  86. url: '/pages/' + this.path + '/my/my'
  87. })
  88. return;
  89. }
  90. getScanData(this.scanCode).then(res => {
  91. if (res.code === 0) {
  92. let url = '/pages/' + this.path + '/index/' + res.data.url;
  93. uni.setStorageSync('scanDetail', res.data);
  94. uni.redirectTo({
  95. url: url
  96. })
  97. } else {
  98. }
  99. })
  100. } else {
  101. let url = '/pages/' + this.path + '/error?msg=' + res.msg;
  102. uni.redirectTo({
  103. url: url // 目标页面路径
  104. })
  105. }
  106. })
  107. }
  108. }
  109. }
  110. </script>