login.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. uni.setStorageSync('loginTimeOut', 'NO')
  56. if (_this.path === '/pages/index/main' || _this.lanuch) {
  57. uni.reLaunch({
  58. url: _this.path
  59. });
  60. } else {
  61. uni.redirectTo({
  62. url: _this.path
  63. });
  64. }
  65. } else {
  66. uni.showToast({
  67. title: res.message,
  68. icon: 'none',
  69. duration: 2500
  70. });
  71. }
  72. });
  73. }
  74. })
  75. } else {
  76. uni.showToast({
  77. title: '授权失败',
  78. icon: 'none',
  79. duration: 2500
  80. });
  81. }
  82. }
  83. }
  84. }
  85. </script>
  86. <style>
  87. .content {
  88. padding: 0;
  89. display: flex;
  90. align-items: center;
  91. justify-content: center;
  92. }
  93. </style>