Authorize.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. console.log("获取授权信息toutiao")
  101. this.getUserInfo();
  102. // #endif
  103. // #ifdef MP-WEIXIN
  104. if (res.islogin === false) {
  105. this.$emit('onLoadFun', this.userInfo);
  106. }
  107. // #endif
  108. })
  109. .catch(res => {
  110. if (this.isAuto) {
  111. this.$emit('authColse', true);
  112. }
  113. });
  114. },
  115. getUserInfo(code) {
  116. Routine.getUserInfo()
  117. .then(res => {
  118. console.log(res, '授权数据');
  119. let userInfo = res.userInfo;
  120. //#ifndef MP-TOUTIAO
  121. userInfo.code = code;
  122. //#endif
  123. userInfo.spread_spid = this.$Cache.get('spread') || app.globalData.spid; //获取推广人ID
  124. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  125. Routine.authUserInfo(userInfo)
  126. .then(res => {
  127. uni.hideLoading();
  128. this.$emit('authColse', false);
  129. this.$emit('onLoadFun', this.userInfo);
  130. })
  131. .catch(res => {
  132. uni.hideLoading();
  133. uni.showToast({
  134. title: res.msg,
  135. icon: 'none',
  136. duration: 2000
  137. });
  138. });
  139. })
  140. .catch(res => {
  141. uni.hideLoading();
  142. });
  143. },
  144. getUserProfile() {
  145. let self = this;
  146. Routine.getUserProfile()
  147. .then(res => {
  148. let userInfo = res.userInfo;
  149. userInfo.code = this.code;
  150. userInfo.spread_spid = app.globalData.spid; //获取推广人ID
  151. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  152. Routine.authUserInfo(userInfo)
  153. .then(res => {
  154. if (res.data.key !== undefined && res.data.key) {
  155. uni.hideLoading();
  156. self.authKey = res.data.key;
  157. self.isPhoneBox = true;
  158. } else {
  159. uni.hideLoading();
  160. let time = res.data.expires_time - self.$Cache.time();
  161. self.$store.commit('LOGIN', {
  162. token: res.data.token,
  163. time: time
  164. });
  165. }
  166. })
  167. .catch(res => {
  168. uni.hideLoading();
  169. uni.showToast({
  170. title: res.msg,
  171. icon: 'none',
  172. duration: 2000
  173. });
  174. });
  175. })
  176. .catch(res => {
  177. uni.hideLoading();
  178. });
  179. },
  180. setUserInfo() {
  181. uni.showLoading({
  182. title: '正在登录中'
  183. });
  184. this.getUserInfo(this.code);
  185. this.$emit('authColse', false);
  186. },
  187. getLogoUrl() {
  188. this.logoUrl = app.globalData.routine_logo;
  189. // let that = this;
  190. // if (Cache.has(LOGO_URL)) {
  191. // this.logoUrl = Cache.get(LOGO_URL);
  192. // return;
  193. // }
  194. // getLogo().then(res=>{
  195. // that.logoUrl = res.data.logo_url
  196. // Cache.set(LOGO_URL,that.logoUrl);
  197. // })
  198. },
  199. close() {
  200. let pages = getCurrentPages(),
  201. currPage = pages[pages.length - 1];
  202. if (this.isGoIndex) {
  203. uni.switchTab({
  204. url: '/pages/index/index'
  205. });
  206. } else {
  207. this.$emit('authColse', false);
  208. }
  209. // if (currPage && currPage.isShowAuth != undefined){
  210. // currPage.isShowAuth = true;
  211. // }
  212. }
  213. }
  214. };
  215. </script>
  216. <style scoped lang="scss">
  217. .Popup {
  218. width: 500rpx;
  219. background-color: #fff;
  220. position: fixed;
  221. top: 50%;
  222. left: 50%;
  223. margin-left: -250rpx;
  224. transform: translateY(-50%);
  225. z-index: 1000;
  226. }
  227. .Popup {
  228. .logo-auth {
  229. z-index: -1;
  230. position: absolute;
  231. left: 50%;
  232. top: 0%;
  233. transform: translate(-50%, -50%);
  234. width: 150rpx;
  235. height: 150rpx;
  236. display: flex;
  237. align-items: center;
  238. justify-content: center;
  239. border: 8rpx solid #fff;
  240. border-radius: 50%;
  241. background: #fff;
  242. }
  243. image {
  244. height: 42rpx;
  245. margin-top: -54rpx;
  246. }
  247. }
  248. .Popup .title {
  249. font-size: 28rpx;
  250. color: #000;
  251. text-align: center;
  252. margin-top: 30rpx;
  253. }
  254. .Popup .tip {
  255. font-size: 22rpx;
  256. color: #555;
  257. padding: 0 24rpx;
  258. margin-top: 25rpx;
  259. }
  260. .Popup .bottom .item {
  261. width: 50%;
  262. height: 80rpx;
  263. background-color: #eeeeee;
  264. text-align: center;
  265. line-height: 80rpx;
  266. font-size: 24rpx;
  267. color: #666;
  268. margin-top: 54rpx;
  269. }
  270. .Popup .bottom .item.on {
  271. width: 100%;
  272. }
  273. .flex {
  274. display: flex;
  275. }
  276. .Popup .bottom .item.grant {
  277. font-size: 28rpx;
  278. color: #fff;
  279. font-weight: bold;
  280. background-color: #e93323;
  281. border-radius: 0;
  282. padding: 0;
  283. }
  284. .mask {
  285. position: fixed;
  286. top: 0;
  287. right: 0;
  288. left: 0;
  289. bottom: 0;
  290. background-color: rgba(0, 0, 0, 0.65);
  291. z-index: 999;
  292. }
  293. </style>