login.vue 3.3 KB

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