login.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <template>
  2. <app-layout>
  3. </app-layout>
  4. </template>
  5. <script>
  6. import appLayout from "@/components/app-layout"
  7. export default {
  8. components:{
  9. appLayout,
  10. },
  11. data() {
  12. return {
  13. redirect: ''
  14. }
  15. },
  16. methods: {
  17. login(){
  18. let _this = this;
  19. uni.showLoading({title: '登录中...'})
  20. uni.login({
  21. provider: 'weixin',
  22. success: res => {
  23. uni.getUserInfo({
  24. provider: 'weixin',
  25. success: data => {
  26. let params = {
  27. code: res.code,
  28. encryptedData: data.encryptedData,
  29. iv:data.iv,
  30. signature:data.signature
  31. }
  32. _this.$u.api.login(params).then(loginRes => {
  33. _this.$u.vuex(_this.$const.USER_TOKEN,loginRes.token)
  34. _this.$u.vuex(_this.$const.USER_DATA,loginRes.user)
  35. uni.hideLoading()
  36. uni.reLaunch({
  37. url: _this.redirect
  38. });
  39. })
  40. }
  41. });
  42. }
  43. });
  44. },
  45. },
  46. onLoad(options) {
  47. let redirect = options.redirect;
  48. delete options.redirect;
  49. let arr = [];
  50. for (const key in options) {
  51. arr.push(`${key}=${options[key]}`)
  52. }
  53. this.redirect = `/${redirect}?${arr.join("&")}`
  54. if(!this.redirect){
  55. this.redirect = '/pages/index/index'
  56. }
  57. if(!this.vuex_user_token){
  58. this.login()
  59. }
  60. }
  61. }
  62. </script>
  63. <style lang="scss" scoped>
  64. </style>