auth.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. export default {
  20. name: "auth",
  21. props: {
  22. authShow: {
  23. type: Boolean,
  24. default: true
  25. },
  26. authToken: {
  27. type: String,
  28. default: ''
  29. },
  30. authMobile: {
  31. type: String,
  32. default: ''
  33. }
  34. },
  35. data() {
  36. return {
  37. authIng: false
  38. };
  39. },
  40. methods: {
  41. getInfo() {
  42. let _this = this
  43. if (this.authIng) {
  44. return
  45. }
  46. _this.authIng = true
  47. uni.getUserProfile({
  48. desc: '用于填充用户默认信息',
  49. success(info) {
  50. uni.login({
  51. success(res) {
  52. $http.post('/api/v1/mp/user/wxauth', {
  53. ...{
  54. identity: 1,
  55. code: res.code
  56. },
  57. ...info.userInfo
  58. }).then(res => {
  59. _this.authIng = false
  60. if (res.code === 0) {
  61. uni.$u.toast('授权成功');
  62. _this.authIng = false
  63. _this.$emit('close')
  64. }
  65. }).catch(() => {
  66. _this.authIng = false
  67. })
  68. }
  69. })
  70. },
  71. fail() {
  72. _this.authIng = false
  73. }
  74. })
  75. },
  76. getInfoAli(){
  77. let _this = this
  78. if (this.authIng) {
  79. return
  80. }
  81. _this.authIng = true
  82. my.getAuthCode ({
  83. scopes : ['auth_user'],
  84. success: (res) => {
  85. console.log("res2",res);
  86. $http.post('/api/v1/mp/user/aliAuth', {code: res.authCode}).then(res => {
  87. _this.authIng = false
  88. if (res.code === 0) {
  89. uni.$u.toast('授权成功');
  90. _this.authIng = false
  91. _this.$emit('close')
  92. }
  93. }).catch(() => {
  94. _this.authIng = false
  95. })
  96. }
  97. })
  98. }
  99. }
  100. }
  101. </script>
  102. <style lang="scss" scoped>
  103. .auth-wrap {
  104. /* #ifndef MP-ALIPAY */
  105. padding: 32rpx 32rpx 50rpx;
  106. /* #endif */
  107. /* #ifdef MP-ALIPAY */
  108. padding: 32rpx 32rpx 0;
  109. /* #endif */
  110. ::v-deep button {
  111. line-height: 76rpx;
  112. font-size: 28rpx;
  113. height: 76rpx;
  114. color: #fff;
  115. background-color: $uni-bg-color;
  116. border: none;
  117. border-radius: 100rpx;
  118. }
  119. }
  120. .tip {
  121. padding: 50rpx 16rpx;
  122. font-size: 26rpx;
  123. color: #333;
  124. }
  125. </style>