Authorize.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <template>
  2. <view :style="viewColor">
  3. <view class='Popup' v-if='isShowAuth && code'>
  4. <view class="logo-auth">
  5. <image :src='routine_logo' 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 MP -->
  12. <button class="item grant" hover-class="none"
  13. @tap="getUserProfile">去授权</button>
  14. <!-- #endif -->
  15. <!-- #ifdef H5 || APP-PLUS -->
  16. <button class="item grant"
  17. @tap="toWecahtAuth">去授权</button>
  18. <!-- #endif -->
  19. </view>
  20. </view>
  21. <view class='mask' v-if='isShowAuth && code' @click='close'></view>
  22. </view>
  23. </template>
  24. <script>
  25. // +----------------------------------------------------------------------
  26. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  27. // +----------------------------------------------------------------------
  28. // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
  29. // +----------------------------------------------------------------------
  30. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  31. // +----------------------------------------------------------------------
  32. // | Author: CRMEB Team <admin@crmeb.com>
  33. // +----------------------------------------------------------------------
  34. const app = getApp();
  35. import Cache from '../utils/cache';
  36. import {
  37. getLogo, commonAuth
  38. } from '../api/public';
  39. import { LOGO_URL, USER_INFO, EXPIRES_TIME} from '../config/cache';
  40. import { mapGetters } from 'vuex';
  41. import Routine from '../libs/routine';
  42. import { configMap } from '@/utils/index';
  43. import Auth from '../libs/wechat';
  44. import { toLogin } from '../libs/login';
  45. export default {
  46. name: 'Authorize',
  47. props: {
  48. isAuto: {
  49. type: Boolean,
  50. default: true
  51. },
  52. isGoIndex: {
  53. type: Boolean,
  54. default: true
  55. },
  56. isShowAuth: {
  57. type: Boolean,
  58. default: false
  59. }
  60. },
  61. data() {
  62. return {
  63. canUseGetUserProfile: false,
  64. code: null,
  65. }
  66. },
  67. computed: {
  68. ...mapGetters(['isLogin', 'userInfo', 'viewColor']),
  69. ...configMap(['routine_logo'])
  70. },
  71. watch: {
  72. isLogin(n) {
  73. n === true && this.$emit('onLoadFun', this.userInfo);
  74. },
  75. isShowAuth(n) {
  76. this.getCode(this.isShowAuth)
  77. }
  78. },
  79. created() {
  80. if (wx.getUserProfile) {
  81. this.canUseGetUserProfile = true
  82. }
  83. this.setAuthStatus();
  84. this.getCode(this.isShowAuth)
  85. },
  86. methods: {
  87. setAuthStatus() {
  88. //#ifdef MP
  89. Routine.authorize().then(res => {
  90. if (res.islogin === false)
  91. this.$emit('onLoadFun', this.userInfo);
  92. }).catch(res => {
  93. if (this.isAuto)
  94. this.$emit('authColse', true);
  95. })
  96. //#endif
  97. },
  98. getCode(n){
  99. // #ifdef MP
  100. if (n) {
  101. uni.showLoading({
  102. title: '正在登录中'
  103. });
  104. Routine.getCode().then(code => {
  105. uni.hideLoading();
  106. this.code = code;
  107. }).catch(e => {
  108. uni.hideLoading();
  109. uni.showToast({
  110. title: '登录失败',
  111. duration: 2000
  112. });
  113. })
  114. } else {
  115. this.code = null;
  116. }
  117. // #endif
  118. // #ifndef MP
  119. if(n){
  120. this.code = 1;
  121. }
  122. // #endif
  123. },
  124. toWecahtAuth(){
  125. toLogin(true);
  126. },
  127. getUserProfile() {
  128. let self = this;
  129. Routine.getUserProfile()
  130. .then(res => {
  131. let userInfo = res.userInfo;
  132. userInfo.code = this.code;
  133. userInfo.spread = app.globalData.spid; //获取推广人ID
  134. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  135. commonAuth({
  136. auth: {
  137. type:'routine',
  138. auth: userInfo
  139. }
  140. }).then(res=>{
  141. if(res.data.status == 200){
  142. let time = res.data.result.expires_time - Cache.time();
  143. self.$store.commit('UPDATE_USERINFO', res.data.result.user);
  144. self.$store.commit('LOGIN', {token:res.data.result.token, time:time});
  145. self.$store.commit('SETUID', res.data.result.user.uid);
  146. Cache.set(EXPIRES_TIME,res.data.result.expires_time,time);
  147. Cache.set(USER_INFO,res.data.result.user,time);
  148. this.$emit('onLoadFun', res.data.result.user);
  149. }else{
  150. uni.setStorageSync('auth_token',res.data.result.key);
  151. return uni.navigateTo({
  152. url:'/pages/users/login/index'
  153. })
  154. }
  155. }).catch(res => {
  156. uni.hideLoading();
  157. uni.showToast({
  158. title: res.message,
  159. icon: 'none',
  160. duration: 2000,
  161. });
  162. });
  163. })
  164. .catch(res => {
  165. uni.hideLoading();
  166. });
  167. },
  168. close() {
  169. let pages = getCurrentPages(),
  170. currPage = pages[pages.length - 1];
  171. this.$emit('authColse', false);
  172. // if (this.isGoIndex) {
  173. // uni.switchTab({
  174. // url: '/pages/index/index'
  175. // });
  176. // } else {
  177. // this.$emit('authColse', false);
  178. // }
  179. },
  180. }
  181. }
  182. </script>
  183. <style scoped lang='scss'>
  184. .Popup {
  185. width: 500rpx;
  186. background-color: #fff;
  187. position: fixed;
  188. top: 50%;
  189. left: 50%;
  190. margin-left: -250rpx;
  191. transform: translateY(-50%);
  192. z-index: 1000;
  193. }
  194. .Popup {
  195. .logo-auth {
  196. z-index: -1;
  197. position: absolute;
  198. left: 50%;
  199. top: 0%;
  200. transform: translate(-50%, -50%);
  201. width: 150rpx;
  202. height: 150rpx;
  203. display: flex;
  204. align-items: center;
  205. justify-content: center;
  206. border: 8rpx solid #fff;
  207. border-radius: 50%;
  208. background: #fff;
  209. }
  210. image {
  211. height: 42rpx;
  212. margin-top: -54rpx;
  213. }
  214. }
  215. .Popup .title {
  216. font-size: 28rpx;
  217. color: #000;
  218. text-align: center;
  219. margin-top: 30rpx
  220. }
  221. .Popup .tip {
  222. font-size: 22rpx;
  223. color: #555;
  224. padding: 0 24rpx;
  225. margin-top: 25rpx;
  226. }
  227. .Popup .bottom .item {
  228. width: 50%;
  229. height: 80rpx;
  230. background-color: #eeeeee;
  231. text-align: center;
  232. line-height: 80rpx;
  233. font-size: 24rpx;
  234. color: #666;
  235. margin-top: 54rpx;
  236. }
  237. .Popup .bottom .item.on {
  238. width: 100%
  239. }
  240. .flex {
  241. display: flex;
  242. }
  243. .Popup .bottom .item.grant {
  244. font-size: 28rpx;
  245. color: #fff;
  246. font-weight: bold;
  247. background-color: var(--view-theme);
  248. border-radius: 0;
  249. padding: 0;
  250. }
  251. .mask {
  252. position: fixed;
  253. top: 0;
  254. right: 0;
  255. left: 0;
  256. bottom: 0;
  257. background-color: rgba(0, 0, 0, 0.65);
  258. z-index: 999;
  259. }
  260. </style>