login.vue 5.4 KB

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