login.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. }
  38. this.login(); // 调用登录方法
  39. },
  40. methods: {
  41. login() {
  42. let that = this;
  43. if (this.scanCode == '' || this.scanCode == undefined) {
  44. this.status = true;
  45. }
  46. wx.login({
  47. success(resLogin) {
  48. if (resLogin.code) {
  49. // 例如发送到服务器进行登录态验证等操作
  50. login(that.scanCode, resLogin.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. if ((that.scanCode === '' || that.scanCode === undefined) && res.data.roles.length > 1){
  57. that.getIsNoLogin(res.data.uid);
  58. return;
  59. }
  60. that.scanCode = res.data.qrCode;
  61. setToken(res.data.sessionId);
  62. that.path = res.data.page_path;
  63. that.verify();
  64. } else {
  65. if (res.code === 98) {
  66. that.getIsNoLogin();
  67. return;
  68. } else {
  69. uni.showToast({
  70. title: res.msg,
  71. icon: 'none'
  72. });
  73. console.log('登录失败!' + res.msg);
  74. }
  75. }
  76. })
  77. } else {
  78. console.log('登录失败!' + res.errMsg);
  79. uni.showToast({
  80. title: res.msg || '登录失败',
  81. icon: 'none'
  82. });
  83. }
  84. },
  85. fail(err) {
  86. uni.showToast({
  87. title: err.msg || '登录失败',
  88. icon: 'none'
  89. });
  90. console.log('登录失败', err);
  91. }
  92. });
  93. },
  94. verify() {
  95. verifyScanCode(this.scanCode).then(res => {
  96. console.log(res);
  97. if (res.code === 0) {
  98. uni.setStorageSync("scanCode", this.scanCode);
  99. if (res.data.url !== null && res.data.url !== undefined && res.data.url !== '') {
  100. uni.redirectTo({
  101. url: '/pages/' + this.path + '/index/authCode'
  102. })
  103. return;
  104. }
  105. getScanData(this.scanCode).then(res => {
  106. let url;
  107. if (res.code === 0) {
  108. url = '/pages/' + this.path + '/index/' + res.data.url;
  109. uni.setStorageSync('scanDetail', res.data);
  110. } else {
  111. url = '/pages/' + this.path + '/error?msg=' + res.msg;
  112. }
  113. uni.redirectTo({
  114. url: url // 目标页面路径
  115. })
  116. })
  117. } else {
  118. let url = '/pages/' + this.path + '/error?msg=' + res.msg;
  119. uni.redirectTo({
  120. url: url // 目标页面路径
  121. })
  122. }
  123. })
  124. },
  125. getIsNoLogin(uid) {
  126. wx.login({
  127. success(resLogin) {
  128. if (resLogin.code) {
  129. getIsNoLogin({jsCode:resLogin.code}).then(res => {
  130. if (res.code === 0){
  131. setAdminInfo(res.data);
  132. setAdminToken(res.data.sessionId)
  133. }
  134. // 跳转登陆页
  135. uni.redirectTo({
  136. url: '/pages/hexiao/login?uid=0'
  137. })
  138. })
  139. }
  140. }
  141. });
  142. }
  143. }
  144. }
  145. </script>