login.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. mounted() {},
  20. onLoad(option) {
  21. console.log(option)
  22. const {
  23. path,
  24. lanuch
  25. } = option;
  26. this.lanuch = (lanuch + '') === 'true';
  27. if (path) {
  28. this.path = decodeURIComponent(path);
  29. }
  30. },
  31. methods: {
  32. getPhoneNumber(e) {
  33. uni.showLoading({
  34. mask: true,
  35. title: '加载中'
  36. });
  37. if (e.target.errMsg === 'getPhoneNumber:ok') {
  38. uni.login({
  39. success(result) {
  40. if (result.code) {
  41. login({
  42. code: result.code,
  43. encryptedData: e.target.encryptedData,
  44. iv: e.target.iv
  45. }).then(res => {
  46. uni.hideLoading();
  47. if (res.status === '200') {
  48. console.log(res.data, 'login data')
  49. uni.setStorageSync('token', res.data.token);
  50. uni.setStorageSync('phone', res.data.phone);
  51. uni.setStorageSync('userId', res.data.userId);
  52. if (this.path === '/pages/index/main' || this.lanuch) {
  53. uni.reLaunch({
  54. url: this.path
  55. });
  56. } else {
  57. uni.redirectTo({
  58. url: this.path
  59. });
  60. }
  61. } else {
  62. uni.showToast({
  63. title: res.message,
  64. icon: 'none',
  65. duration: 2500
  66. });
  67. }
  68. });
  69. }
  70. }
  71. })
  72. } else {
  73. uni.showToast({
  74. title: '授权失败',
  75. icon: 'none',
  76. duration: 2500
  77. });
  78. }
  79. }
  80. }
  81. }
  82. </script>
  83. <style>
  84. .content {
  85. padding: 0;
  86. display: flex;
  87. align-items: center;
  88. justify-content: center;
  89. }
  90. </style>