Authorize.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 { mapState } 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: {
  49. ...mapState(['hasLogin','userInfo'])
  50. },
  51. watch:{
  52. isLogin(n){
  53. n === true && this.$emit('onLoadFun',this.userInfo);
  54. }
  55. },
  56. created() {
  57. this.getLogoUrl();
  58. this.setAuthStatus();
  59. },
  60. methods:{
  61. setAuthStatus(){
  62. Routine.authorize().then(res=>{
  63. if(res.islogin === false)
  64. this.setUserInfo();
  65. else
  66. this.$emit('onLoadFun',this.userInfo);
  67. }).catch(res=>{
  68. if (this.isAuto)
  69. this.$emit('authColse',true);
  70. })
  71. },
  72. getUserInfo(code){
  73. Routine.getUserInfo().then(res=>{
  74. let userInfo = res.userInfo
  75. userInfo.code = code;
  76. userInfo.spread_spid = app.globalData.spid;//获取推广人ID
  77. userInfo.spread_code = app.globalData.code;//获取推广人分享二维码ID
  78. Routine.authUserInfo(userInfo).then(res=>{
  79. uni.hideLoading();
  80. this.$emit('authColse',false);
  81. this.$emit('onLoadFun',this.userInfo);
  82. }).catch(res=>{
  83. uni.hideLoading();
  84. uni.showToast({
  85. title:res.msg,
  86. icon:'none',
  87. duration:2000
  88. });
  89. })
  90. }).catch(res =>{
  91. uni.hideLoading();
  92. })
  93. },
  94. setUserInfo(){
  95. uni.showLoading({title:'正在登陆中'});
  96. Routine.getCode().then(code=>{
  97. this.getUserInfo(code);
  98. }).catch(res=>{
  99. uni.hideLoading();
  100. })
  101. },
  102. getLogoUrl(){
  103. let that = this;
  104. if (Cache.has(LOGO_URL)) {
  105. this.logoUrl = Cache.get(LOGO_URL);
  106. return;
  107. }
  108. getLogo().then(res=>{
  109. that.logoUrl = res.data.logo_url
  110. Cache.set(LOGO_URL,that.logoUrl);
  111. })
  112. },
  113. close(){
  114. let pages = getCurrentPages(), currPage = pages[pages.length - 1];
  115. if(this.isGoIndex){
  116. uni.switchTab({url:'/pages/index/index'});
  117. } else {
  118. this.$emit('authColse',false);
  119. }
  120. // if (currPage && currPage.isShowAuth != undefined){
  121. // currPage.isShowAuth = true;
  122. // }
  123. },
  124. }
  125. }
  126. </script>
  127. <style scoped lang='scss'>
  128. .Popup{width:500rpx;background-color:#fff;position:fixed;top:50%;left:50%;margin-left:-250rpx;transform:translateY(-50%);z-index:320;}
  129. .Popup image{width:150rpx;height:150rpx;margin:-67rpx auto 0 auto;display:block;border: 8rpx solid #fff;border-radius: 50%}
  130. .Popup .title{font-size:28rpx;color:#000;text-align:center;margin-top: 30rpx}
  131. .Popup .tip{font-size:22rpx;color:#555;padding:0 24rpx;margin-top:25rpx;}
  132. .Popup .bottom .item{width:50%;height:80rpx;background-color:#eeeeee;text-align:center;line-height:80rpx;font-size:24rpx;color:#666;margin-top:54rpx;}
  133. .Popup .bottom .item.on{width: 100%}
  134. .flex{display:flex;}
  135. .Popup .bottom .item.grant{font-size:28rpx;color:#fff;font-weight:bold;background-color:#e93323;border-radius:0;padding:0;}
  136. .mask{position:fixed;top:0;right:0;left:0;bottom:0;background-color:rgba(0,0,0,0.65);z-index:310;}
  137. </style>