auth.vue 2.4 KB

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