setNickname.uvue 2.7 KB
Newer Older
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
1 2 3
<template>
	<view class="page">
		<uni-navbar-lite title="设置昵称" class="uni-navbar-lite">
DCloud_JSON's avatar
DCloud_JSON 已提交
4 5 6
      <!-- #ifndef MP-WEIXIN -->
      <template v-slot:right>
        <text class="submit" :class="{disabled:!hasChange || nickname == ''}" @click="setNickname">完成</text>
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
7
			</template>
DCloud_JSON's avatar
DCloud_JSON 已提交
8
      <!-- #endif -->
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
9 10 11 12
		</uni-navbar-lite>

		<uni-id-pages-x-input class="my-input" v-model="nickname" @confirm="setNickname" placeholder="请输入昵称" :focus="true"
			:maxlength="25"></uni-id-pages-x-input>
DCloud_JSON's avatar
DCloud_JSON 已提交
13 14 15
    <!-- #ifdef MP-WEIXIN -->
    <button class="submit-mp" :disabled="!hasChange || nickname == ''" type="primary" @click="setNickname">完成</button>
    <!-- #endif -->
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
	</view>
</template>

<script>
	import { state, mutations } from '@/uni_modules/uni-id-pages-x/store.uts';
	export default {
		data() {
			return {
				nickname: ""
			};
		},
		computed: {
			hasChange() : boolean {
				return this.currentNickname != this.nickname
			},
			currentNickname() : string {
DCloud_JSON's avatar
DCloud_JSON 已提交
32 33
        const nickname = state.userInfo["nickname"]
        return nickname == null ? '' : nickname as string
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
			}
		},
		onLoad() {
			this.nickname = this.currentNickname
		},
		methods: {
			setNickname() {
				if (this.nickname.length == 0) {
					uni.showToast({
						title: '昵称不能为空',
						icon: 'none'
					});
					this.nickname = this.currentNickname
					return
				}
				if (!this.hasChange) {
					// console.log('没有变化');
					return
				}

				uni.showLoading({
					title: '加载中',
					mask: false
				});
				const db = uniCloud.databaseForJQL()
				const uid = uniCloud.getCurrentUserInfo().uid
				if (uid != null) {
					db.collection('uni-id-users')
						.doc(uid)
						.update({
							"nickname": this.nickname
						})
A
Anne_LXM 已提交
66
						.then(() => {
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
67 68 69
							mutations.updateUserInfo({ "nickname": this.nickname } as UTSJSONObject)
							uni.navigateBack()
						})
A
Anne_LXM 已提交
70
						.catch((err : any | null) => {
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
							const error = err as UniCloudError
							console.error('error', error);
						})
						.finally(() => {
							// console.log('finally');
							uni.hideLoading()
						})
				}
			}
		}
	}
</script>

<style lang="scss">
	@import url("/uni_modules/uni-id-pages-x/common/common.scss");

	.page {
		background-color: #f8f8f8;
	}

	.my-input {
DCloud_JSON's avatar
DCloud_JSON 已提交
92
		background-color: #FFF!important;
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106
	}

	.uni-navbar-lite .right-content {
		width: 65px;
	}

	.submit {
		// height: 28px;
		line-height: 28px;
		width: 50px;
		text-align: center;
		font-size: 14px;
		border-radius: 5px;
		margin-right: 15px;
A
Anne_LXM 已提交
107 108
		background-color: #0070ff !important;
		color: #FFF!important;
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
109 110 111 112 113 114
	}

	.submit.disabled {
		background-color: #EFEFEF;
		color: #AAA;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
115 116 117 118 119 120 121 122 123
  /* #ifdef MP-WEIXIN */
  .submit-mp {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    border-radius: 0;
  }
  /* #endif */
A
Anne_LXM 已提交
124
</style>