login.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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="handleRedirect" class="cancel btn main-center cross-center">取消</button>
  11. <button @click="handleLogin" class="btn main-center cross-center">确定</button>
  12. </view>
  13. </view>
  14. </u-popup>
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. data() {
  20. return {
  21. bgSrc: this.$site.root+"assets/images/formula/formula-cover.png",
  22. redirect: '',
  23. code: '',
  24. modal: {
  25. show: false
  26. },
  27. }
  28. },
  29. methods: {
  30. login(){
  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. console.log('-->data 获取用户信息')
  66. let _this = this;
  67. uni.getUserProfile({
  68. desc: '获取用户信息',
  69. success: data => {
  70. console.log('-->data',data)
  71. let params = {
  72. encryptedData: data.encryptedData,
  73. iv: data.iv,
  74. }
  75. _this.$u.api.userUpdate(params).then(res => {
  76. _this.$u.vuex(_this.$const.USER_DATA, res)
  77. _this.handleRedirect();
  78. })
  79. },
  80. fail: err => {
  81. _this.$u.toast('获取用户信息失败')
  82. uni.reLaunch({
  83. url: this.redirect
  84. });
  85. }
  86. });
  87. },
  88. handleRedirect(){
  89. console.log('-->data',this.redirect)
  90. uni.reLaunch({
  91. url: this.redirect
  92. });
  93. }
  94. },
  95. onLoad(options) {
  96. if(options.redirect){
  97. let redirect = options.redirect;
  98. delete options.redirect;
  99. let arr = [];
  100. for (const key in options) {
  101. arr.push(`${key}=${options[key]}`)
  102. }
  103. this.redirect = `/${redirect}?${arr.join("&")}`
  104. }else{
  105. this.redirect = '/pages/index/index'
  106. }
  107. if(!this.vuex_user_token){
  108. this.login()
  109. }
  110. }
  111. }
  112. </script>
  113. <style lang="scss" scoped>
  114. .login{
  115. position: relative;
  116. .bg{
  117. position: absolute;
  118. top: 0;
  119. left: 0;
  120. width: 100%;
  121. height: 100vh;
  122. }
  123. }
  124. .popup-content{
  125. height: 450rpx;
  126. position: relative;
  127. .title{
  128. padding: 20rpx 30rpx;
  129. font-weight: 600;
  130. font-size: 42rpx;
  131. }
  132. .btn-popup{
  133. position: absolute;
  134. bottom: 0;
  135. width: 100%;
  136. }
  137. .btn{
  138. background: $main-color;
  139. color: #fff;
  140. border-radius: 0;
  141. font-size: 28rpx;
  142. height: 100rpx;
  143. flex: 1;
  144. &.cancel{
  145. background: #f5f5f5;
  146. color: #666;
  147. }
  148. &:after{
  149. content: unset;
  150. }
  151. }
  152. }
  153. </style>