login.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <view class="login">
  3. <u-loading-page
  4. :loading="true"
  5. loading-text="登陆中..."
  6. :bg-color="$colors.bgColor"
  7. color="#ffffff"
  8. />
  9. </view>
  10. </template>
  11. <script>
  12. import Cache from '../utils/cache'
  13. export default {
  14. components: {},
  15. data() {
  16. return {
  17. path: '/pages/index/index',
  18. query: {}
  19. }
  20. },
  21. computed: {},
  22. methods: {
  23. login() {
  24. // this.$loading('登陆中...')
  25. this.$api.user.login().then(async res => {
  26. this.$hideLoading()
  27. const { token, user_info } = res.data
  28. await this.$store.dispatch('user/token', token)
  29. await this.$store.dispatch('user/info', user_info)
  30. // 绑定上级
  31. const parentId = Cache.get('parent_id')
  32. if (parentId && !user_info.info.parent_id) {
  33. this.$api.user.bind(parentId).then(res => {
  34. console.log('-->bind parent success')
  35. this.$store.dispatch('user/info', res.data)
  36. Cache.remove('parent_id')
  37. })
  38. }
  39. this.relaunch()
  40. }).catch(async() => {
  41. await this.$store.dispatch('user/clear')
  42. this.relaunch()
  43. })
  44. },
  45. relaunch() {
  46. console.log('-->reLaunch path', this.path)
  47. uni.reLaunch({
  48. url: this.path,
  49. success() {
  50. console.log('-->reLaunch success')
  51. },
  52. fail(err) {
  53. uni.reLaunch({
  54. url: '/pages/index/index'
  55. })
  56. console.log('-->reLaunch error', err)
  57. }
  58. })
  59. }
  60. },
  61. onLoad(options) {
  62. console.log('-->data', options)
  63. if (this.$api.user.isLogin()) {
  64. uni.reLaunch({
  65. url: '/pages/index/index'
  66. })
  67. return
  68. }
  69. this.path = options.path ? options.path : this.path
  70. this.path = this.path.replace('//', '/')
  71. this.path = this.path.indexOf('/') !== 0 ? '/' + this.path : this.path
  72. const query = options.query ? JSON.parse(decodeURI(options.query)) : {}
  73. const queryArr = []
  74. for (const queryKey in query) {
  75. queryArr.push(`${queryKey}=${query[queryKey]}`)
  76. }
  77. this.path = this.path + '?' + queryArr.join('&')
  78. this.login()
  79. },
  80. onShow() {
  81. if (this.$api.user.isLogin()) {
  82. // uni.reLaunch({
  83. // url: '/pages/index/index'
  84. // })
  85. }
  86. }
  87. }
  88. </script>
  89. <style lang="scss" scoped>
  90. .login{
  91. }
  92. </style>