login.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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,setAdminToken,setAdminInfo
  10. } from '@/utils/auth'
  11. import {
  12. login,
  13. verifyScanCode,
  14. getIsNoLogin
  15. } from '@/api/login'
  16. import {
  17. getScanData
  18. } from '@/api/scan'
  19. export default {
  20. data() {
  21. return {
  22. scanCode: '',
  23. path: '',
  24. status: false
  25. }
  26. },
  27. onLoad(options) {
  28. uni.removeStorageSync("scanCode");
  29. uni.removeStorageSync("Authorization-status");
  30. // 获取并解码 q 参数
  31. let qlink = decodeURIComponent(options.q || '');
  32. if (qlink) {
  33. const decodedUrl = decodeURIComponent(qlink); // 先解码
  34. // 1. 去掉域名部分,提取路径
  35. const scanCode = decodedUrl.replace('https://e.dnzc.vip/', ''); // 得到 "a7e4a3f2947004f6"
  36. this.scanCode = scanCode;
  37. if (decodedUrl.indexOf('https://d.dnzc.vip') !== -1){
  38. uni.navigateTo({
  39. url: '/pages/common?url=' + encodeURIComponent(decodedUrl)
  40. })
  41. return;
  42. }
  43. }
  44. this.login(); // 调用登录方法
  45. },
  46. methods: {
  47. login() {
  48. let that = this;
  49. if (this.scanCode == '' || this.scanCode == undefined) {
  50. this.status = true;
  51. }
  52. wx.login({
  53. success(resLogin) {
  54. if (resLogin.code) {
  55. // 例如发送到服务器进行登录态验证等操作
  56. login(that.scanCode, resLogin.code).then(res => {
  57. console.log('登录成功', res);
  58. if (res.code === 0) {
  59. if (res.data.isWxAuth == 1) {
  60. uni.setStorageSync('Authorization-status', true);
  61. }
  62. if ((that.scanCode === '' || that.scanCode === undefined) && res.data.roles.length > 1){
  63. that.getIsNoLogin(res.data.uid);
  64. return;
  65. }
  66. that.scanCode = res.data.qrCode;
  67. setToken(res.data.sessionId);
  68. that.path = res.data.page_path;
  69. that.verify();
  70. } else {
  71. if (res.code === 98) {
  72. that.getIsNoLogin();
  73. return;
  74. } else {
  75. uni.showToast({
  76. title: res.msg,
  77. icon: 'none'
  78. });
  79. console.log('登录失败!' + res.msg);
  80. }
  81. }
  82. })
  83. } else {
  84. console.log('登录失败!' + res.errMsg);
  85. uni.showToast({
  86. title: res.msg || '登录失败',
  87. icon: 'none'
  88. });
  89. }
  90. },
  91. fail(err) {
  92. uni.showToast({
  93. title: err.msg || '登录失败',
  94. icon: 'none'
  95. });
  96. console.log('登录失败', err);
  97. }
  98. });
  99. },
  100. verify() {
  101. verifyScanCode(this.scanCode).then(res => {
  102. console.log(res);
  103. if (res.code === 0) {
  104. uni.setStorageSync("scanCode", this.scanCode);
  105. if (res.data.url !== null && res.data.url !== undefined && res.data.url !== '') {
  106. let skipUrl = '/pages/' + this.path + '/index/authCode';
  107. uni.redirectTo({
  108. url: '/pages/skip?url='+skipUrl
  109. })
  110. return;
  111. }
  112. getScanData(this.scanCode).then(res => {
  113. let url;
  114. if (res.code === 0) {
  115. uni.setStorageSync('scanDetail', res.data);
  116. let skipUrl = '/pages/' + this.path + '/index/' + res.data.url;
  117. uni.redirectTo({
  118. url: '/pages/skip?url='+skipUrl
  119. })
  120. return;
  121. } else {
  122. url = '/pages/' + this.path + '/error?msg=' + res.msg;
  123. }
  124. uni.redirectTo({
  125. url: url // 目标页面路径
  126. })
  127. })
  128. } else {
  129. let url = '/pages/' + this.path + '/error?msg=' + res.msg;
  130. uni.redirectTo({
  131. url: url // 目标页面路径
  132. })
  133. }
  134. })
  135. },
  136. getIsNoLogin(uid) {
  137. uni.redirectTo({
  138. url: '/pages/hexiao/login?uid=0'
  139. })
  140. }
  141. }
  142. }
  143. </script>