login.vue 4.1 KB

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