Authorize.vue 6.6 KB

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