login.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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://scantest.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. console.log('登录失败!' + res.msg);
  61. }
  62. })
  63. } else {
  64. console.log('登录失败!' + res.errMsg);
  65. uni.showToast({ title: res.msg || '登录失败', icon: 'none' });
  66. }
  67. },
  68. fail(err) {
  69. uni.showToast({ title: err.msg || '登录失败', icon: 'none' });
  70. console.log('登录失败', err);
  71. }
  72. });
  73. },
  74. verify() {
  75. verifyScanCode(this.scanCode).then(res => {
  76. console.log(res);
  77. if (res.code === 0) {
  78. uni.setStorageSync("scanCode", this.scanCode);
  79. if (res.data.url !== null && res.data.url !== undefined && res.data.url !== '') {
  80. uni.redirectTo({
  81. url: '/pages/' + this.path + '/index/authCode'
  82. })
  83. return;
  84. }
  85. if (this.status) {
  86. uni.redirectTo({
  87. url: '/pages/' + this.path + '/index/claim'
  88. })
  89. return;
  90. }
  91. getScanData(this.scanCode).then(res => {
  92. let url;
  93. if (res.code === 0) {
  94. url = '/pages/' + this.path + '/index/' + res.data.url;
  95. uni.setStorageSync('scanDetail', res.data);
  96. } else {
  97. url = '/pages/' + this.path + '/error?msg=' + res.msg;
  98. }
  99. uni.redirectTo({
  100. url: url // 目标页面路径
  101. })
  102. })
  103. } else {
  104. let url = '/pages/' + this.path + '/error?msg=' + res.msg;
  105. uni.redirectTo({
  106. url: url // 目标页面路径
  107. })
  108. }
  109. })
  110. }
  111. }
  112. }
  113. </script>