login.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. _this.handleBind()
  61. _this.getConfig();
  62. if(loginRes.user.nickname === '微信用户'){
  63. _this.modal.show = true;
  64. }else{
  65. _this.handleRedirect();
  66. }
  67. })
  68. },
  69. fail: error => {
  70. _this.$u.toast('获取用户信息失败')
  71. }
  72. });
  73. }
  74. });
  75. },
  76. handleLogin(){
  77. let _this = this;
  78. wx.getUserProfile({
  79. desc: '获取用户信息',
  80. success: data => {
  81. console.log('-->data',data)
  82. let params = {
  83. encryptedData: data.encryptedData,
  84. iv: data.iv,
  85. }
  86. _this.$u.api.userUpdate(params).then(res => {
  87. _this.$u.vuex(_this.$const.USER_DATA, res)
  88. _this.handleRedirect();
  89. })
  90. },
  91. fail: err => {
  92. _this.$u.toast('获取用户信息失败')
  93. uni.reLaunch({
  94. url: this.redirect
  95. });
  96. }
  97. });
  98. },
  99. handleRedirect(){
  100. console.log('-->data',this.redirect)
  101. uni.reLaunch({
  102. url: this.redirect
  103. });
  104. },
  105. handleBind(){
  106. this.$u.api.userBind({scene:this.vuex_user_scene,parent_id:this.vuex_user_parent_id}).then(res => {
  107. this.$u.vuex(this.$const.USER_SCENE, null);
  108. this.$u.vuex(this.$const.USER_PARENT_ID, null);
  109. })
  110. },
  111. getConfig(){
  112. if(!this.vuex_user_config){
  113. this.$u.api.settingConfig().then(data => {
  114. this.$u.vuex(this.$const.USER_CONFIG,data)
  115. })
  116. }
  117. }
  118. },
  119. onLoad(options) {
  120. if(options.redirect){
  121. let redirect = options.redirect;
  122. delete options.redirect;
  123. let arr = [];
  124. for (const key in options) {
  125. arr.push(`${key}=${options[key]}`)
  126. }
  127. this.redirect = `/${redirect}?${arr.join("&")}`
  128. }else{
  129. this.redirect = '/pages/index/index'
  130. }
  131. if(!this.vuex_user_token){
  132. this.login()
  133. }
  134. }
  135. }
  136. </script>
  137. <style lang="scss" scoped>
  138. .login{
  139. position: relative;
  140. height: 100vh;
  141. overflow: hidden;
  142. .bg{
  143. position: absolute;
  144. top: 0;
  145. left: 0;
  146. width: 100%;
  147. height: 100vh;
  148. }
  149. }
  150. .popup-content{
  151. height: 280rpx;
  152. position: relative;
  153. .title{
  154. padding: 20rpx 30rpx;
  155. font-weight: 600;
  156. font-size: 42rpx;
  157. }
  158. .btn-popup{
  159. position: absolute;
  160. bottom: 0;
  161. width: 100%;
  162. }
  163. .btn{
  164. background: $main-color;
  165. color: #fff;
  166. border-radius: 0;
  167. font-size: 28rpx;
  168. height: 100rpx;
  169. flex: 1;
  170. &.cancel{
  171. background: #f5f5f5;
  172. color: #666;
  173. }
  174. &:after{
  175. content: unset;
  176. }
  177. }
  178. }
  179. </style>