login.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <view class="content">
  3. <u-button type="success" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信一键登录</u-button>
  4. </view>
  5. </template>
  6. <script>
  7. import {
  8. login
  9. } from "@/api/login.js"
  10. export default {
  11. components: {},
  12. data() {
  13. return {
  14. path: '/pages/index/index',
  15. lanuch: true
  16. }
  17. },
  18. onShow() {
  19. },
  20. mounted() {},
  21. onLoad(option) {
  22. console.log(option)
  23. const {
  24. path,
  25. lanuch
  26. } = option;
  27. this.lanuch = (lanuch + '') === 'true';
  28. if (path) {
  29. this.path = decodeURIComponent(path);
  30. }
  31. },
  32. methods: {
  33. getPhoneNumber(e) {
  34. let _this = this
  35. uni.showLoading({
  36. mask: true,
  37. title: '加载中'
  38. });
  39. console.log(e.target.encryptedData,'eeeeeee')
  40. if (e.target.errMsg === 'getPhoneNumber:ok') {
  41. uni.login({
  42. provider: 'weixin',
  43. success(res) {
  44. login({
  45. code: res.code,
  46. encryptedData: e.target.encryptedData,
  47. iv: e.target.iv
  48. }).then(res => {
  49. uni.hideLoading();
  50. console.log(res, 'login data')
  51. if (res.status == '200') {
  52. console.log(res.data, 'login data')
  53. getApp().globalData.token = res.data
  54. _this.$u.vuex('vuex_token',res.data)
  55. if (_this.path === '/pages/index/main' || _this.lanuch) {
  56. uni.reLaunch({
  57. url: _this.path
  58. });
  59. } else {
  60. uni.redirectTo({
  61. url: _this.path
  62. });
  63. }
  64. } else {
  65. uni.showToast({
  66. title: res.message,
  67. icon: 'none',
  68. duration: 2500
  69. });
  70. }
  71. });
  72. }
  73. })
  74. } else {
  75. uni.showToast({
  76. title: '授权失败',
  77. icon: 'none',
  78. duration: 2500
  79. });
  80. }
  81. }
  82. }
  83. }
  84. </script>
  85. <style>
  86. .content {
  87. padding: 0;
  88. display: flex;
  89. align-items: center;
  90. justify-content: center;
  91. }
  92. </style>