autoLogin.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <view class="wrap">
  3. <view class="tips">
  4. <image class="b-image" src="/static/imgs/loading_image.png" alt="" mode="aspectFit"></image>
  5. <view class="b-tit">尝试自动登录,请稍后....</view>
  6. </view>
  7. </view>
  8. </template>
  9. <script>
  10. import { wechatAutoLogin } from '@/service/api/user.js';
  11. import { smartLogin } from '@/service/request/main.js';
  12. import { encodeRedirectUrl, decodeRedirectUrl } from '../../common/util.js';
  13. export default {
  14. data() {
  15. return {};
  16. },
  17. onLoad(options) {
  18. console.log('onLoad autoLogin', options);
  19. // 微信小程序自动登录
  20. const returnPage = options.from ? decodeRedirectUrl(options.from) : 'pages/home/index';
  21. uni.login().then(([err, res]) => {
  22. console.log('uni.login', err, res);
  23. if (!err) {
  24. wechatAutoLogin(res.code).then(([err, res]) => {
  25. console.log('wechatAutoLogin', err, res);
  26. this.$store.commit('unlock', 'silentReload');
  27. if (!err) {
  28. console.warn('needLogin silentReload', 'success');
  29. this.$store.commit('setAccessToken', res.token);
  30. this.$store.commit('setActivityId', res.activity_id);
  31. // 返回之前页面
  32. uni.reLaunch({
  33. url: '/' + returnPage
  34. });
  35. } else {
  36. console.error('needLogin silentReload', 'fail');
  37. // uni.reLaunch({
  38. // url: '/answer_pages/user/login?from=' + encodeRedirectUrl(returnPage)
  39. // })
  40. smartLogin(returnPage);
  41. }
  42. });
  43. } else {
  44. console.error('needLogin silentReload', 'code fail');
  45. this.$store.commit('unlock', 'silentReload');
  46. // uni.reLaunch({
  47. // url: '/answer_pages/user/login?from=' + encodeRedirectUrl(returnPage)
  48. // })
  49. smartLogin(returnPage);
  50. }
  51. });
  52. },
  53. methods: {}
  54. };
  55. </script>
  56. <style lang="scss">
  57. page {
  58. padding-bottom: env(safe-area-inset-bottom);
  59. background: #ffffff;
  60. height: 100%;
  61. }
  62. .wrap {
  63. display: flex;
  64. align-items: center;
  65. justify-content: center;
  66. width: 100%;
  67. height: 100%;
  68. }
  69. .tips {
  70. display: flex;
  71. flex-direction: column;
  72. align-items: center;
  73. .b-image {
  74. width: 300upx;
  75. height: 240upx;
  76. }
  77. .b-tit {
  78. margin-top: 40upx;
  79. color: #666;
  80. font-size: 28upx;
  81. }
  82. }
  83. </style>