login.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <view class="login">
  3. <view class="bg">
  4. <u-image :src="bgSrc" width="100%" height="100%" mode="aspectFit"></u-image>
  5. </view>
  6. <u-popup v-model="modal.show" mode="bottom" :mask-close-able="false" border-radius="15">
  7. <view class="popup-content">
  8. <view class="title">需要获取您的用户信息</view>
  9. <view class="btn-popup main-between">
  10. <button @click="handleLogin" class="btn main-center cross-center">确定</button>
  11. </view>
  12. </view>
  13. </u-popup>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. data() {
  19. return {
  20. bgSrc: this.$site.root+"assets/images/formula/formula-cover.png",
  21. redirect: '',
  22. code: '',
  23. modal: {
  24. show: false
  25. },
  26. }
  27. },
  28. methods: {
  29. login(){
  30. let _this = this;
  31. uni.showLoading({title: '登录中...'})
  32. uni.login({
  33. provider: 'weixin',
  34. success: res => {
  35. _this.code = res.code;
  36. uni.getUserInfo({
  37. provider: 'weixin',
  38. success: data => {
  39. let params = {
  40. code: res.code,
  41. encryptedData: data.encryptedData,
  42. iv:data.iv,
  43. signature:data.signature
  44. }
  45. _this.$u.api.login(params).then(loginRes => {
  46. uni.hideLoading();
  47. _this.$u.vuex(_this.$const.USER_TOKEN,loginRes.token)
  48. _this.$u.vuex(_this.$const.USER_DATA,loginRes.user)
  49. if(loginRes.user.nickname === '微信用户'){
  50. _this.modal.show = true;
  51. }else{
  52. _this.handleRedirect();
  53. }
  54. })
  55. },
  56. fail: error => {
  57. _this.$u.toast('获取用户信息失败')
  58. }
  59. });
  60. }
  61. });
  62. },
  63. handleLogin(){
  64. let _this = this;
  65. uni.getUserProfile({
  66. desc: '获取用户信息',
  67. success: data => {
  68. console.log('-->data',data)
  69. let params = {
  70. encryptedData: data.encryptedData,
  71. iv: data.iv,
  72. }
  73. _this.$u.api.userUpdate(params).then(res => {
  74. _this.$u.vuex(_this.$const.USER_DATA, res)
  75. _this.handleRedirect();
  76. })
  77. },
  78. fail: err => {
  79. _this.$u.toast('获取用户信息失败')
  80. uni.reLaunch({
  81. url: this.redirect
  82. });
  83. }
  84. });
  85. },
  86. handleRedirect(){
  87. console.log('-->data',this.redirect)
  88. uni.reLaunch({
  89. url: this.redirect
  90. });
  91. }
  92. },
  93. onLoad(options) {
  94. if(options.redirect){
  95. let redirect = options.redirect;
  96. delete options.redirect;
  97. let arr = [];
  98. for (const key in options) {
  99. arr.push(`${key}=${options[key]}`)
  100. }
  101. this.redirect = `/${redirect}?${arr.join("&")}`
  102. }else{
  103. this.redirect = '/pages/index/index'
  104. }
  105. if(!this.vuex_user_token){
  106. this.login()
  107. }
  108. }
  109. }
  110. </script>
  111. <style lang="scss" scoped>
  112. .login{
  113. position: relative;
  114. .bg{
  115. position: absolute;
  116. top: 0;
  117. left: 0;
  118. width: 100%;
  119. height: 100vh;
  120. }
  121. }
  122. .popup-content{
  123. height: 450rpx;
  124. position: relative;
  125. .title{
  126. padding: 20rpx 30rpx;
  127. font-weight: 600;
  128. font-size: 42rpx;
  129. }
  130. .btn-popup{
  131. position: absolute;
  132. bottom: 0;
  133. width: 100%;
  134. }
  135. .btn{
  136. background: $main-color;
  137. color: #fff;
  138. border-radius: 0;
  139. font-size: 28rpx;
  140. height: 100rpx;
  141. flex: 1;
  142. &.cancel{
  143. background: #f5f5f5;
  144. color: #666;
  145. }
  146. &:after{
  147. content: unset;
  148. }
  149. }
  150. }
  151. </style>