소스 검색

feat(view): 优化获取用户信息

Zhou Jianjia 4 년 전
부모
커밋
4b3849a028
4개의 변경된 파일108개의 추가작업 그리고 16개의 파일을 삭제
  1. 2 0
      core/apiList.js
  2. 3 0
      core/http.api.js
  3. 74 15
      pages/login/login.vue
  4. 29 1
      pages/my/index.vue

+ 2 - 0
core/apiList.js

xqd
@@ -23,6 +23,8 @@ module.exports = {
     userShares: '/user/shares',
     // 绑定上级
     userBind: '/user/bind',
+    // 更新
+    userUpdate: '/user/update',
     // 用户绑定手机号
     userBindPhone: '/user/bindPhone',
 

+ 3 - 0
core/http.api.js

xqd xqd
@@ -30,6 +30,8 @@ const install = (Vue, vm) => {
 	const userBind = (data = {}) => vm.$u.post(apiList.userBind, data)
 	// 用户绑定手机号
 	const userBindPhone = (data = {}) => vm.$u.post(apiList.userBindPhone, data)
+	// 用户绑定手机号
+	const userUpdate = (data = {}) => vm.$u.post(apiList.userUpdate, data)
 
 	//将各个定义的接口名称,统一放进对象挂载到vm.$u.api(因为vm就是this,也即this.$u.api)下
 	vm.$u.api = {
@@ -46,6 +48,7 @@ const install = (Vue, vm) => {
 		userShares,
 		userBind,
 		userBindPhone,
+		userUpdate,
 
 		withdrawApply,
 		withdrawLists,

+ 74 - 15
pages/login/login.vue

xqd xqd xqd xqd xqd
@@ -1,6 +1,18 @@
 <template>
     <app-layout>
-
+        <u-modal v-model="modal.show"
+                 title="提示"
+                 content="需要获取您的用户信息"
+                 cancel-color="#CCCCCC"
+                 confirm-color="#046E64"
+                 :async-close="true"
+                 :show-cancel-button="true"
+                 @cancel="handleRedirect"
+        >
+            <view slot="confirm-button">
+                <button @click="handleLogin" class="btn main-center cross-center">确定</button>
+            </view>
+        </u-modal>
     </app-layout>
 </template>
 
@@ -12,7 +24,11 @@
         },
         data() {
             return {
-                redirect: ''
+                redirect: '',
+                code: '',
+                modal: {
+                    show: false
+                }
             }
         },
         methods: {
@@ -22,6 +38,7 @@
                 uni.login({
                     provider: 'weixin',
                     success: res => {
+                        _this.code = res.code;
                         uni.getUserInfo({
                             provider: 'weixin',
                             success: data =>  {
@@ -32,28 +49,61 @@
                                     signature:data.signature
                                 }
                                 _this.$u.api.login(params).then(loginRes => {
+                                    uni.hideLoading();
                                     _this.$u.vuex(_this.$const.USER_TOKEN,loginRes.token)
                                     _this.$u.vuex(_this.$const.USER_DATA,loginRes.user)
-                                    uni.hideLoading()
-                                    uni.reLaunch({
-                                        url: _this.redirect
-                                    });
+                                    if(loginRes.user.nickname === '微信用户'){
+                                        _this.modal.show = true;
+                                    }else{
+                                        _this.handleRedirect();
+                                    }
                                 })
+                            },
+                            fail: error => {
+                                _this.$u.toast('获取用户信息失败')
                             }
                         });
                     }
                 });
             },
+            handleLogin(){
+                let _this = this;
+                uni.getUserProfile({
+                    desc: '获取用户信息',
+                    success: data =>  {
+                        console.log('-->data',data)
+                        let params = {
+                            encryptedData: data.encryptedData,
+                            iv: data.iv,
+                        }
+                        _this.$u.api.userUpdate(params).then(res => {
+                            _this.$u.vuex(_this.$const.USER_DATA, res)
+                            _this.handleRedirect();
+                        })
+                    },
+                    fail: err => {
+                        console.log('-->data',err)
+                        _this.$u.toast('获取用户信息失败')
+                    }
+                });
+            },
+            handleRedirect(){
+                console.log('-->data',this.redirect)
+                uni.reLaunch({
+                    url: this.redirect
+                });
+            }
         },
         onLoad(options) {
-            let redirect = options.redirect;
-            delete options.redirect;
-            let arr = [];
-            for (const key in options) {
-                arr.push(`${key}=${options[key]}`)
-            }
-            this.redirect = `/${redirect}?${arr.join("&")}`
-            if(!this.redirect){
+            if(options.redirect){
+                let redirect = options.redirect;
+                delete options.redirect;
+                let arr = [];
+                for (const key in options) {
+                    arr.push(`${key}=${options[key]}`)
+                }
+                this.redirect = `/${redirect}?${arr.join("&")}`
+            }else{
                 this.redirect = '/pages/index/index'
             }
             if(!this.vuex_user_token){
@@ -64,5 +114,14 @@
 </script>
 
 <style lang="scss" scoped>
-
+    .btn{
+        background: #fff;
+        color: $main-color;
+        border-radius: 0;
+        font-size: 28rpx;
+        height: 100rpx;
+        &:after{
+            content: unset;
+        }
+    }
 </style>

+ 29 - 1
pages/my/index.vue

xqd xqd
@@ -11,7 +11,15 @@
 								:src="userData.head_img"
 								shape="circle"></u-image>
 					</view>
-					<view class="nickname">{{userData.nickname}}</view>
+					<view class="nickname">
+						<template v-if="userData.nickname !== '微信用户'">{{userData.nickname}}</template>
+						<template v-else>
+							<u-button @click="handleGetInfo"
+									  type="success"
+									  size="mini"
+									  :custom-style="{backgroundColor: '#097268'}">获取头像/昵称</u-button>
+						</template>
+					</view>
 					<view class="mobile">
 						<!--#ifdef MP-WEIXIN-->
 						<u-button size="mini"
@@ -109,6 +117,26 @@
 					}
 				})
 			},
+			handleGetInfo(){
+				let _this = this;
+				uni.getUserProfile({
+					desc: '获取用户信息',
+					success: data =>  {
+						let params = {
+							encryptedData: data.encryptedData,
+							iv: data.iv,
+						}
+						_this.$u.api.userUpdate(params).then(res => {
+							_this.$u.vuex(_this.$const.USER_DATA, res)
+							_this.handleRedirect();
+						})
+					},
+					fail: err => {
+						console.log('-->data',err)
+						_this.$u.toast('获取用户信息失败')
+					}
+				});
+			},
 			getUser(){
 				this.$u.api.userGet().then(data => {
 					console.log('-->data',data)