Authorize.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <view>
  3. <view class='Popup' v-if='isShowAuth'>
  4. <image :src='logoUrl'></image>
  5. <view class='title'>授权提醒</view>
  6. <view class='tip'>请授权头像等信息,以便为您提供更好的服务</view>
  7. <view class='bottom flex'>
  8. <view class='item' @click='close'>随便逛逛</view>
  9. <!-- #ifdef APP-PLUS -->
  10. <button class='item grant' @click="setUserInfo">去授权</button>
  11. <!-- #endif -->
  12. <!-- #ifdef MP -->
  13. <button class='item grant' type="primary" open-type="getUserInfo" lang="zh_CN" @getuserinfo="setUserInfo">去授权</button>
  14. <!-- #endif -->
  15. </view>
  16. </view>
  17. <view class='mask' v-if='isShowAuth' @click='close'></view>
  18. </view>
  19. </template>
  20. <script>
  21. const app = getApp();
  22. import Cache from '../utils/cache';
  23. import { getLogo } from '../api/public';
  24. import { LOGO_URL } from '../config/cache';
  25. import { mapGetters } from 'vuex';
  26. import Routine from '../libs/routine';
  27. export default {
  28. name:'Authorize',
  29. props:{
  30. isAuto:{
  31. type:Boolean,
  32. default:true
  33. },
  34. isGoIndex:{
  35. type:Boolean,
  36. default:true
  37. },
  38. isShowAuth:{
  39. type:Boolean,
  40. default:false
  41. }
  42. },
  43. data(){
  44. return {
  45. logoUrl:''
  46. }
  47. },
  48. computed:mapGetters(['isLogin','userInfo']),
  49. watch:{
  50. isLogin(n){
  51. n === true && this.$emit('onLoadFun',this.userInfo);
  52. }
  53. },
  54. created() {
  55. this.getLogoUrl();
  56. this.setAuthStatus();
  57. },
  58. methods:{
  59. setAuthStatus(){
  60. Routine.authorize().then(res=>{
  61. if(res.islogin === false)
  62. this.setUserInfo();
  63. else
  64. this.$emit('onLoadFun',this.userInfo);
  65. }).catch(res=>{
  66. if (this.isAuto)
  67. this.$emit('authColse',true);
  68. })
  69. },
  70. getUserInfo(code){
  71. Routine.getUserInfo().then(res=>{
  72. let userInfo = res.userInfo
  73. userInfo.code = code;
  74. userInfo.spread_spid = app.globalData.spid;//获取推广人ID
  75. userInfo.spread_code = app.globalData.code;//获取推广人分享二维码ID
  76. userInfo.avatar = userInfo.userInfo.avatarUrl;
  77. userInfo.city = userInfo.userInfo.city;
  78. userInfo.country = userInfo.userInfo.country;
  79. userInfo.nickName = userInfo.userInfo.nickName;
  80. userInfo.province = userInfo.userInfo.province;
  81. userInfo.sex = userInfo.userInfo.gender;
  82. Routine.authUserInfo(code,userInfo).then(res=>{
  83. uni.hideLoading();
  84. this.$emit('authColse',false);
  85. this.$emit('onLoadFun',this.userInfo);
  86. }).catch(res=>{
  87. uni.hideLoading();
  88. uni.showToast({
  89. title:res.message,
  90. icon:'none',
  91. duration:2000
  92. });
  93. })
  94. }).catch(res =>{
  95. uni.hideLoading();
  96. })
  97. },
  98. setUserInfo(){
  99. uni.showLoading({title:'正在登录中'});
  100. Routine.getCode().then(code=>{
  101. this.getUserInfo(code);
  102. }).catch(res=>{
  103. uni.hideLoading();
  104. })
  105. },
  106. getLogoUrl(){
  107. let that = this;
  108. if (Cache.has(LOGO_URL)) {
  109. this.logoUrl = Cache.get(LOGO_URL);
  110. return;
  111. }
  112. getLogo().then(res=>{
  113. that.logoUrl = res.data.logoUrl
  114. Cache.set(LOGO_URL,that.logoUrl);
  115. })
  116. },
  117. close(){
  118. let pages = getCurrentPages(), currPage = pages[pages.length - 1];
  119. if(this.isGoIndex){
  120. uni.switchTab({url:'/pages/index/index'});
  121. } else {
  122. this.$emit('authColse',false);
  123. }
  124. // if (currPage && currPage.isShowAuth != undefined){
  125. // currPage.isShowAuth = true;
  126. // }
  127. },
  128. }
  129. }
  130. </script>
  131. <style scoped lang='scss'>
  132. .Popup{width:500rpx;background-color:#fff;position:fixed;top:50%;left:50%;margin-left:-250rpx;transform:translateY(-50%);z-index:320;}
  133. .Popup image{width:150rpx;height:150rpx;margin:-67rpx auto 0 auto;display:block;border: 8rpx solid #fff;border-radius: 50%}
  134. .Popup .title{font-size:28rpx;color:#000;text-align:center;margin-top: 30rpx}
  135. .Popup .tip{font-size:22rpx;color:#555;padding:0 24rpx;margin-top:25rpx;}
  136. .Popup .bottom .item{width:50%;height:80rpx;background-color:#eeeeee;text-align:center;line-height:80rpx;font-size:24rpx;color:#666;margin-top:54rpx;}
  137. .Popup .bottom .item.on{width: 100%}
  138. .flex{display:flex;}
  139. .Popup .bottom .item.grant{font-size:28rpx;color:#fff;font-weight:bold;background-color:$theme-color;border-radius:0;padding:0;}
  140. .mask{position:fixed;top:0;right:0;left:0;bottom:0;background-color:rgba(0,0,0,0.65);z-index:310;}
  141. </style>