login.vue 5.2 KB

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