auth.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <view>
  3. <u-popup :show="authShow" mode="bottom">
  4. <view class="auth-wrap">
  5. <view class="tip">为了正常使用功能,请授权用户信息。</view>
  6. <!-- #ifdef MP-ALIPAY -->
  7. <button :loading="authIng" @click="getInfoAli()" type="default">授权</button>
  8. <view style="height: 100rpx;"></view>
  9. <!-- #endif -->
  10. <!-- #ifndef MP-ALIPAY -->
  11. <button :loading="authIng" @click="getInfo()" type="default">授权</button>
  12. <!-- #endif -->
  13. </view>
  14. </u-popup>
  15. </view>
  16. </template>
  17. <script>
  18. import $http from '../../utils/request.js'
  19. import appId from '@/config/appId.js'
  20. export default {
  21. name: "auth",
  22. props: {
  23. authShow: {
  24. type: Boolean,
  25. default: true
  26. },
  27. authToken: {
  28. type: String,
  29. default: ''
  30. },
  31. authMobile: {
  32. type: String,
  33. default: ''
  34. }
  35. },
  36. data() {
  37. return {
  38. authIng: false
  39. };
  40. },
  41. methods: {
  42. getInfo() {
  43. let _this = this
  44. if (this.authIng) {
  45. return
  46. }
  47. _this.authIng = true
  48. uni.getUserProfile({
  49. desc: '用于填充用户默认信息',
  50. success(info) {
  51. uni.login({
  52. success(res) {
  53. $http.post('/api/v1/mp/user/wxauth', {
  54. ...{
  55. identity: 1,
  56. code: res.code
  57. },
  58. ...info.userInfo,
  59. appSource: appId
  60. }).then(res => {
  61. _this.authIng = false
  62. if (res.code === 0) {
  63. uni.$u.toast('授权成功');
  64. _this.authIng = false
  65. _this.$emit('close')
  66. }
  67. }).catch(() => {
  68. _this.authIng = false
  69. })
  70. }
  71. })
  72. },
  73. fail() {
  74. _this.authIng = false
  75. }
  76. })
  77. },
  78. getInfoAli() {
  79. let _this = this
  80. if (this.authIng) {
  81. return
  82. }
  83. _this.authIng = true
  84. my.getAuthCode({
  85. scopes: ['auth_user'],
  86. success: (res) => {
  87. $http.post('/api/v1/mp/user/aliAuth', {
  88. code: res.authCode
  89. }).then(res => {
  90. _this.authIng = false
  91. if (res.code === 0) {
  92. uni.$u.toast('授权成功');
  93. _this.authIng = false
  94. _this.$emit('close')
  95. }
  96. }).catch(() => {
  97. _this.authIng = false
  98. })
  99. },
  100. fail: (res) => {
  101. uni.$u.toast('授权失败');
  102. uni.removeStorageSync('token')
  103. _this.authIng = false
  104. _this.$emit('fail')
  105. }
  106. })
  107. }
  108. }
  109. }
  110. </script>
  111. <style lang="scss" scoped>
  112. .auth-wrap {
  113. /* #ifndef MP-ALIPAY */
  114. padding: 32rpx 32rpx 50rpx;
  115. /* #endif */
  116. /* #ifdef MP-ALIPAY */
  117. padding: 32rpx 32rpx 0;
  118. /* #endif */
  119. ::v-deep button {
  120. line-height: 76rpx;
  121. font-size: 28rpx;
  122. height: 76rpx;
  123. color: #fff;
  124. background-color: $uni-bg-color;
  125. border: none;
  126. border-radius: 100rpx;
  127. }
  128. }
  129. .tip {
  130. padding: 50rpx 16rpx;
  131. font-size: 26rpx;
  132. color: #333;
  133. }
  134. </style>