Authorize.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <template>
  2. <view>
  3. <view class="Popup" v-if="isShowAuth && code">
  4. <view class="logo-auth"><image :src="logoUrl" mode="aspectFit"></image></view>
  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 v-if="canUseGetUserProfile" class="item grant" hover-class="none" @tap="getUserProfile">去授权</button>
  14. <button v-else class="item grant" type="primary" open-type="getUserInfo" lang="zh_CN" @getuserinfo="setUserInfo">去授权</button>
  15. <!-- #endif -->
  16. </view>
  17. </view>
  18. <view class="mask" v-if="isShowAuth && code" @click="close"></view>
  19. </view>
  20. </template>
  21. <script>
  22. const app = getApp();
  23. import Cache from '../utils/cache';
  24. import { getLogo } from '../api/public';
  25. import { LOGO_URL } from '../config/cache';
  26. import { mapGetters } from 'vuex';
  27. import Routine from '../libs/routine';
  28. export default {
  29. name: 'Authorize',
  30. props: {
  31. isAuto: {
  32. type: Boolean,
  33. default: true
  34. },
  35. isGoIndex: {
  36. type: Boolean,
  37. default: true
  38. },
  39. isShowAuth: {
  40. type: Boolean,
  41. default: false
  42. }
  43. },
  44. data() {
  45. return {
  46. logoUrl: app.globalData.routine_logo,
  47. canUseGetUserProfile: false,
  48. code: null
  49. };
  50. },
  51. computed: mapGetters(['isLogin', 'userInfo']),
  52. watch: {
  53. isLogin(n) {
  54. console.log(n, '判断是否登录');
  55. n === true && this.$emit('onLoadFun', this.userInfo);
  56. },
  57. isShowAuth(n) {
  58. if (n) {
  59. uni.showLoading({
  60. title: '正在登录中'
  61. });
  62. Routine.getCode()
  63. .then(code => {
  64. uni.hideLoading();
  65. this.code = code;
  66. })
  67. .catch(e => {
  68. uni.hideLoading();
  69. uni.showToast({
  70. title: '登录失败',
  71. duration: 2000
  72. });
  73. });
  74. } else {
  75. this.code = null;
  76. }
  77. }
  78. },
  79. created() {
  80. if (wx.getUserProfile) {
  81. this.canUseGetUserProfile = true;
  82. }
  83. this.getLogoUrl();
  84. this.setAuthStatus();
  85. uni.$on('update', data => {
  86. this.logoUrl = data.login_logo;
  87. });
  88. },
  89. mounted: function() {
  90. this.$nextTick(() => {
  91. this.logoUrl = app.globalData.login_logo;
  92. });
  93. },
  94. methods: {
  95. setAuthStatus() {
  96. Routine.authorize()
  97. .then(res => {
  98. console.log('获取授权信息',res);
  99. // #ifdef MP-TOUTIAO
  100. this.getUserInfo();
  101. // #endif
  102. // #ifdef MP-WEIXIN
  103. if (res.islogin === false) {
  104. this.$emit('onLoadFun', this.userInfo);
  105. }
  106. // #endif
  107. })
  108. .catch(res => {
  109. if (this.isAuto) {
  110. this.$emit('authColse', true);
  111. }
  112. });
  113. },
  114. getUserInfo(code) {
  115. Routine.getUserInfo()
  116. .then(res => {
  117. console.log(res, '授权数据');
  118. let userInfo = res.userInfo;
  119. userInfo.code = code;
  120. userInfo.spread_spid = this.$Cache.get('spread') || app.globalData.spid; //获取推广人ID
  121. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  122. Routine.authUserInfo(userInfo)
  123. .then(res => {
  124. uni.hideLoading();
  125. this.$emit('authColse', false);
  126. this.$emit('onLoadFun', this.userInfo);
  127. })
  128. .catch(res => {
  129. uni.hideLoading();
  130. uni.showToast({
  131. title: res.msg,
  132. icon: 'none',
  133. duration: 2000
  134. });
  135. });
  136. })
  137. .catch(res => {
  138. uni.hideLoading();
  139. });
  140. },
  141. getUserProfile() {
  142. let self = this;
  143. Routine.getUserProfile()
  144. .then(res => {
  145. let userInfo = res.userInfo;
  146. userInfo.code = this.code;
  147. userInfo.spread_spid = app.globalData.spid; //获取推广人ID
  148. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  149. Routine.authUserInfo(userInfo)
  150. .then(res => {
  151. if (res.data.key !== undefined && res.data.key) {
  152. uni.hideLoading();
  153. self.authKey = res.data.key;
  154. self.isPhoneBox = true;
  155. } else {
  156. uni.hideLoading();
  157. let time = res.data.expires_time - self.$Cache.time();
  158. self.$store.commit('LOGIN', {
  159. token: res.data.token,
  160. time: time
  161. });
  162. }
  163. })
  164. .catch(res => {
  165. uni.hideLoading();
  166. uni.showToast({
  167. title: res.msg,
  168. icon: 'none',
  169. duration: 2000
  170. });
  171. });
  172. })
  173. .catch(res => {
  174. uni.hideLoading();
  175. });
  176. },
  177. setUserInfo() {
  178. uni.showLoading({
  179. title: '正在登录中'
  180. });
  181. this.getUserInfo(this.code);
  182. this.$emit('authColse', false);
  183. },
  184. getLogoUrl() {
  185. this.logoUrl = app.globalData.routine_logo;
  186. // let that = this;
  187. // if (Cache.has(LOGO_URL)) {
  188. // this.logoUrl = Cache.get(LOGO_URL);
  189. // return;
  190. // }
  191. // getLogo().then(res=>{
  192. // that.logoUrl = res.data.logo_url
  193. // Cache.set(LOGO_URL,that.logoUrl);
  194. // })
  195. },
  196. close() {
  197. let pages = getCurrentPages(),
  198. currPage = pages[pages.length - 1];
  199. if (this.isGoIndex) {
  200. uni.switchTab({
  201. url: '/pages/index/index'
  202. });
  203. } else {
  204. this.$emit('authColse', false);
  205. }
  206. // if (currPage && currPage.isShowAuth != undefined){
  207. // currPage.isShowAuth = true;
  208. // }
  209. }
  210. }
  211. };
  212. </script>
  213. <style scoped lang="scss">
  214. .Popup {
  215. width: 500rpx;
  216. background-color: #fff;
  217. position: fixed;
  218. top: 50%;
  219. left: 50%;
  220. margin-left: -250rpx;
  221. transform: translateY(-50%);
  222. z-index: 1000;
  223. }
  224. .Popup {
  225. .logo-auth {
  226. z-index: -1;
  227. position: absolute;
  228. left: 50%;
  229. top: 0%;
  230. transform: translate(-50%, -50%);
  231. width: 150rpx;
  232. height: 150rpx;
  233. display: flex;
  234. align-items: center;
  235. justify-content: center;
  236. border: 8rpx solid #fff;
  237. border-radius: 50%;
  238. background: #fff;
  239. }
  240. image {
  241. height: 42rpx;
  242. margin-top: -54rpx;
  243. }
  244. }
  245. .Popup .title {
  246. font-size: 28rpx;
  247. color: #000;
  248. text-align: center;
  249. margin-top: 30rpx;
  250. }
  251. .Popup .tip {
  252. font-size: 22rpx;
  253. color: #555;
  254. padding: 0 24rpx;
  255. margin-top: 25rpx;
  256. }
  257. .Popup .bottom .item {
  258. width: 50%;
  259. height: 80rpx;
  260. background-color: #eeeeee;
  261. text-align: center;
  262. line-height: 80rpx;
  263. font-size: 24rpx;
  264. color: #666;
  265. margin-top: 54rpx;
  266. }
  267. .Popup .bottom .item.on {
  268. width: 100%;
  269. }
  270. .flex {
  271. display: flex;
  272. }
  273. .Popup .bottom .item.grant {
  274. font-size: 28rpx;
  275. color: #fff;
  276. font-weight: bold;
  277. background-color: #e93323;
  278. border-radius: 0;
  279. padding: 0;
  280. }
  281. .mask {
  282. position: fixed;
  283. top: 0;
  284. right: 0;
  285. left: 0;
  286. bottom: 0;
  287. background-color: rgba(0, 0, 0, 0.65);
  288. z-index: 999;
  289. }
  290. </style>