login.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <view>
  3. <u-top-tips ref="uTips"></u-top-tips>
  4. </view>
  5. </template>
  6. <script>
  7. const NET = require('../../utils/request')
  8. const API = require('../../config/api')
  9. export default {
  10. data() {
  11. return {}
  12. },
  13. onReady() {
  14. this.appLoginWx()
  15. },
  16. methods: {
  17. // 获取个人数据
  18. getUserInfo(info) {
  19. NET.request(API.WxLogin, uni.getStorageSync("wxLoginData"), 'POST').then(res => {
  20. uni.setStorage({
  21. key: 'token',
  22. data: res.data.token
  23. });
  24. uni.setStorage({
  25. key: 'userData',
  26. data: {
  27. headImage: info.userInfo.avatarUrl,
  28. userName: res.data.name,
  29. userId: res.data.userId,
  30. }
  31. });
  32. uni.switchTab({
  33. url: '/pages/index/index'
  34. });
  35. }).catch(error => {
  36. this.$refs.uTips.show({
  37. title: '微信登录授权失败',
  38. type: 'warning',
  39. })
  40. })
  41. },
  42. // 获取登录权限
  43. appLoginWx() {
  44. uni.getProvider({
  45. service: 'oauth',
  46. success: (res) => {
  47. if (~res.provider.indexOf('weixin')) {
  48. uni.login({
  49. provider: 'weixin',
  50. success: (res2) => {
  51. uni.getUserInfo({
  52. provider: 'weixin',
  53. success: (info) => {
  54. uni.setStorage({
  55. key: 'wxLoginData',
  56. data: {
  57. code: res2.code,
  58. encryptedData: info.encryptedData,
  59. iv: info.iv,
  60. rawData: info.rawData,
  61. signature: info.signature,
  62. }
  63. });
  64. this.getUserInfo(info)
  65. },
  66. fail: () => {
  67. this.$refs.uTips.show({
  68. title: '微信登录授权失败',
  69. type: 'warning',
  70. })
  71. }
  72. })
  73. },
  74. fail: () => {
  75. this.$refs.uTips.show({
  76. title: '微信登录授权失败',
  77. type: 'warning',
  78. })
  79. }
  80. })
  81. } else {
  82. this.$refs.uTips.show({
  83. title: '请先安装微信或升级版本',
  84. type: 'warning',
  85. })
  86. }
  87. }
  88. });
  89. },
  90. },
  91. }
  92. </script>
  93. <style>
  94. </style>