login.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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: 'a77af70c72ae74e0',
  22. path: '',
  23. }
  24. },
  25. onLoad(options) {
  26. uni.removeStorageSync("scanCode");
  27. uni.removeStorageSync("Authorization-status");
  28. // 获取并解码 q 参数
  29. let qlink = decodeURIComponent(options.q || '');
  30. if (qlink) {
  31. const decodedUrl = decodeURIComponent(qlink); // 先解码
  32. // 1. 去掉域名部分,提取路径
  33. const scanCode = decodedUrl.replace('https://scantest.dnzc.vip/', ''); // 得到 "a7e4a3f2947004f6"
  34. this.scanCode = scanCode;
  35. }
  36. this.login(); // 调用登录方法
  37. },
  38. methods: {
  39. login() {
  40. let that = this;
  41. wx.login({
  42. success(res) {
  43. if (res.code) {
  44. // 例如发送到服务器进行登录态验证等操作
  45. login(that.scanCode, res.code).then(res => {
  46. console.log('登录成功', res);
  47. if (res.code === 0) {
  48. if (res.data.isWxAuth == 1) {
  49. uni.setStorageSync('Authorization-status', true);
  50. }
  51. setToken(res.data.sessionId)
  52. that.path = res.data.page_path;
  53. that.verify();
  54. } else {
  55. console.log('登录失败!' + res.msg);
  56. }
  57. })
  58. } else {
  59. console.log('登录失败!' + res.errMsg);
  60. }
  61. },
  62. fail(err) {
  63. console.log('登录失败', err);
  64. }
  65. });
  66. },
  67. verify() {
  68. if(this.scanCode == '' ||this.scanCode == undefined){
  69. uni.redirectTo({
  70. url: '/pages/cjx/my/my'
  71. })
  72. return;
  73. }
  74. verifyScanCode(this.scanCode).then(res => {
  75. console.log(res);
  76. if (res.code === 0) {
  77. uni.setStorageSync("scanCode", this.scanCode);
  78. if (res.data.url !== null && res.data.url !== undefined && res.data.url !== '') {
  79. uni.redirectTo({
  80. url: '/pages/' + this.path + '/index/authCode'
  81. })
  82. return;
  83. }
  84. getScanData(this.scanCode).then(res => {
  85. if (res.code === 0) {
  86. let url = '/pages/' + this.path + '/index/' + res.data.url;
  87. uni.setStorageSync('scanDetail', res.data);
  88. uni.redirectTo({
  89. url: url
  90. })
  91. } else {
  92. }
  93. })
  94. } else {
  95. let url = '/pages/' + this.path + '/error?msg=' + res.msg;
  96. uni.redirectTo({
  97. url: url // 目标页面路径
  98. })
  99. }
  100. })
  101. }
  102. }
  103. }
  104. </script>