login.vue 1.8 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. },
  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. success(res) {
  43. login({
  44. code: res.code,
  45. encryptedData: e.target.encryptedData,
  46. iv: e.target.iv
  47. }).then(res => {
  48. uni.hideLoading();
  49. console.log(res, 'login data')
  50. if (res.status == '200') {
  51. console.log(res.data, 'login data')
  52. uni.setStorageSync('token', res.data);
  53. if (_this.path === '/pages/index/main' || _this.lanuch) {
  54. uni.reLaunch({
  55. url: _this.path
  56. });
  57. } else {
  58. uni.redirectTo({
  59. url: _this.path
  60. });
  61. }
  62. } else {
  63. uni.showToast({
  64. title: res.message,
  65. icon: 'none',
  66. duration: 2500
  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>