login.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <view class="content">
  3. <u-form :model="form" ref="uForm">
  4. <u-button type="success" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信一键登录</u-button>
  5. </u-form>
  6. </view>
  7. </template>
  8. <script>
  9. import {
  10. login
  11. } from "@/api/login.js"
  12. export default {
  13. components: {},
  14. data() {
  15. return {
  16. path: '/pages/index/index',
  17. lanuch: true
  18. }
  19. },
  20. onShow() {},
  21. mounted() {},
  22. onLoad(option) {
  23. console.log(option)
  24. const {
  25. path,
  26. lanuch
  27. } = option;
  28. this.lanuch = (lanuch + '') === 'true';
  29. if (path) {
  30. this.path = decodeURIComponent(path);
  31. }
  32. },
  33. methods: {
  34. getPhoneNumber(e) {
  35. uni.showLoading({
  36. mask: true,
  37. title: '加载中'
  38. });
  39. if (e.target.errMsg === 'getPhoneNumber:ok') {
  40. uni.login({
  41. success(result) {
  42. if (result.code) {
  43. login({
  44. code: result.code,
  45. encryptedData: e.target.encryptedData,
  46. iv: e.target.iv
  47. }).then(res => {
  48. uni.hideLoading();
  49. if (res.status === '200') {
  50. console.log(res.data, 'login data')
  51. uni.setStorageSync('token', res.data.token);
  52. uni.setStorageSync('phone', res.data.phone);
  53. uni.setStorageSync('userId', res.data.userId);
  54. if (this.path === '/pages/index/main' || this.lanuch) {
  55. uni.reLaunch({
  56. url: this.path
  57. });
  58. } else {
  59. uni.redirectTo({
  60. url: this.path
  61. });
  62. }
  63. } else {
  64. uni.showToast({
  65. title: res.message,
  66. icon: 'none',
  67. duration: 2500
  68. });
  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>