auth.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <template>
  2. <view>
  3. </view>
  4. </template>
  5. <script>
  6. export default {
  7. data() {
  8. return {
  9. }
  10. },
  11. onLoad(option) {
  12. uni.showLoading({
  13. title: 'loading'
  14. })
  15. //this.$store.commit('setToken',{access_token: 'dsfdsfsdfdsfds',info: {id: 2323}});
  16. sessionStorage.setItem('option', option)
  17. let code = sessionStorage.getItem('code')
  18. if(code == option.code){
  19. option.code = ''
  20. }
  21. // 授权调用
  22. if(typeof(option.code) != 'undefined' && option.code){
  23. this.userAuth(option.code);
  24. }else{
  25. sessionStorage.setItem('redirceUrl', option.reback_url)
  26. this.getAuthUrl();
  27. }
  28. },
  29. methods: {
  30. // 获取授权地址
  31. getAuthUrl() {
  32. this.$request.api('/api/auth/url', {url: document.location.protocol+'//'+document.location.host+'/pages/entry/auth'}).then(res => {
  33. uni.hideLoading()
  34. if(res.success == true){
  35. location.href = res.data
  36. }else{
  37. uni.showToast({
  38. title: res.msg,
  39. icon: 'none'
  40. })
  41. }
  42. }).catch(parmas => {
  43. console.log(params)
  44. })
  45. },
  46. // 用户授权登录
  47. userAuth(code) {
  48. let _uni = this
  49. let patt = /\/pages\/entry\/auth/;
  50. let redirceUrl = sessionStorage.getItem('redirceUrl')
  51. redirceUrl = typeof(redirceUrl)!= 'undefined' && redirceUrl != 'undefined'? redirceUrl : document.location.host+'/pages/index/index';
  52. if(redirceUrl && patt.test(redirceUrl)){
  53. redirceUrl = document.location.host+'/pages/index/index'
  54. }
  55. this.$request.api('/api/auth', {code: code}).then(res => {
  56. console.log(res)
  57. uni.hideLoading()
  58. let accessToken = typeof(res.data.access_token)!= 'undefined'? res.data.access_token : ''
  59. if(res.success == true && accessToken){
  60. _uni.$store.commit('setToken', res.data);
  61. location.href = redirceUrl
  62. }else{
  63. uni.showModal({
  64. title: '错误提示',
  65. content: accessToken? '授权失败,点击确定重试或者取消返回': res.msg+',点击确定重试或者取消返回',
  66. success: function (res) {
  67. if (res.confirm) {
  68. uni.navigateTo({
  69. url: '/pages/entry/auth'
  70. })
  71. } else if (res.cancel) {
  72. location.href = redirceUrl
  73. }
  74. }
  75. });
  76. }
  77. }).catch(parmas => {
  78. console.log(parmas)
  79. })
  80. }
  81. }
  82. }
  83. </script>
  84. <style>
  85. .nav-bar {
  86. padding-top: 50rpx;
  87. padding-bottom: 10rpx;
  88. display: flex;
  89. justify-content: space-between;
  90. align-items: center;
  91. background: linear-gradient(rgb(255, 152, 101), rgb(255, 122, 85));
  92. height: 125rpx;
  93. }
  94. .btn {
  95. width: 50%;
  96. height: 80rpx;
  97. line-height: 80rpx;
  98. margin: 0 auto;
  99. background-color: #18B566;
  100. color: #fff;
  101. text-align: center;
  102. margin-top: 50%;
  103. border-radius: 6rpx;
  104. }
  105. </style>