login.vue 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. export default {
  13. components: {},
  14. data() {
  15. return {
  16. path: '/pages/index/index',
  17. query: {}
  18. }
  19. },
  20. computed: {},
  21. methods: {
  22. login() {
  23. // this.$loading('登陆中...')
  24. this.$api.user.login().then(async res => {
  25. this.$hideLoading()
  26. const { token, user_info } = res.data
  27. await this.$store.dispatch('user/token', token)
  28. await this.$store.dispatch('user/info', user_info)
  29. this.relaunch()
  30. }).catch(async() => {
  31. await this.$store.dispatch('user/token', null)
  32. await this.$store.dispatch('user/info', null)
  33. this.relaunch()
  34. })
  35. },
  36. relaunch() {
  37. uni.reLaunch({
  38. url: this.path,
  39. success() {
  40. console.log('-->success')
  41. },
  42. fail(err) {
  43. uni.reLaunch({
  44. url: '/pages/index/index'
  45. })
  46. console.log('-->error', err)
  47. }
  48. })
  49. }
  50. },
  51. onLoad(options) {
  52. console.log('-->data', options)
  53. if (this.$api.user.isLogin()) {
  54. uni.reLaunch({
  55. url: '/pages/index/index'
  56. })
  57. return
  58. }
  59. this.path = options.path ? options.path : this.path
  60. this.path = this.path.replace('//', '/')
  61. this.path = this.path.indexOf('/') !== 0 ? '/' + this.path : this.path
  62. const query = options.query ? JSON.parse(decodeURI(options.query)) : {}
  63. const queryArr = []
  64. for (const queryKey in query) {
  65. queryArr.push(`${queryKey}=${query[queryKey]}`)
  66. }
  67. this.path = this.path + '?' + queryArr.join('&')
  68. this.login()
  69. },
  70. onShow() {
  71. if (this.$api.user.isLogin()) {
  72. uni.reLaunch({
  73. url: '/pages/index/index'
  74. })
  75. }
  76. }
  77. }
  78. </script>
  79. <style lang="scss" scoped>
  80. .login{
  81. }
  82. </style>