auth.vue 2.3 KB

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