userinfo.vue 7.3 KB
Newer Older
1 2 3
<template>
	<view>
		<uni-list>
4 5 6
			<uni-list-item class="item">
				<template v-slot:body>
					<view class="item">
7
						<text>{{$t('userinfo.ProfilePhoto')}}</text>
8 9
						<cloud-image @click="uploadAvatarImg" v-if="avatar_file" :src="avatar_file.url" width="50px" height="50px"></cloud-image>
						<uni-icons @click="uploadAvatarImg" v-else class="chooseAvatar" type="plusempty" size="30" color="#dddddd"></uni-icons>
10
					</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
11
				</template>
12
			</uni-list-item>
13
			<uni-list-item class="item" @click="setNickname('')" :title="$t('userinfo.nickname')" :rightText="userInfo.nickname||$t('userinfo.notSet')" link>
14
			</uni-list-item>
15
			<uni-list-item class="item" @click="bindMobile" :title="$t('userinfo.phoneNumber')" :rightText="userInfo.mobile||$t('userinfo.notSpecified')" link>
16
			</uni-list-item>
17
		</uni-list>
18
		<uni-popup ref="dialog" type="dialog">
19 20
			<uni-popup-dialog mode="input" :value="userInfo.nickname" @confirm="setNickname" :title="$t('userinfo.setNickname')"
				:placeholder="$t('userinfo.setNicknamePlaceholder')">
21
			</uni-popup-dialog>
22 23
		</uni-popup>
		<uni-bindMobileByMpWeixin ref="uni-bindMobileByMpWeixin"></uni-bindMobileByMpWeixin>
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
	</view>
</template>
<script>
	import {
		mapMutations,
		mapGetters
	} from 'vuex';
	const db = uniCloud.database();
	const usersTable = db.collection('uni-id-users')
	export default {
		data() {
			return {
				univerifyStyle: {
					authButton: {
						"title": "本机号码一键绑定", // 授权按钮文案
					},
					otherLoginButton: {
						"title": "其他号码绑定",
					}
study夏羽's avatar
study夏羽 已提交
43 44
				},
				uniToken:''
45
			}
46
		},
study夏羽's avatar
study夏羽 已提交
47 48 49 50
		onLoad() {
			this.uniToken = uni.getStorageSync('uni_id_token')
			console.log("uniToken: ",this.uniToken);
			
51 52 53 54 55
			this.univerifyStyle.authButton.title = this.$t('userinfo.bindPhoneNumber')
			this.univerifyStyle.otherLoginButton.title = this.$t('userinfo.bindOtherLogin')
			uni.setNavigationBarTitle({
				title: this.$t('userinfo.navigationBarTitle')
			})
56 57 58 59 60
		},
		computed: {
			...mapGetters({
				userInfo: 'user/info',
				login: 'user/hasLogin'
61
			}),
DCloud_JSON's avatar
DCloud_JSON 已提交
62
			avatar_file() {
63
				if (this.userInfo.avatar_file && this.userInfo.avatar_file.url) {
64 65
					return this.userInfo.avatar_file
				}
66
			}
67
		},
68
		methods: {
69 70
			...mapMutations({
				setUserInfo: 'user/login'
71
			}),
72 73 74 75 76
			bindMobile() {
				// #ifdef APP-PLUS
				uni.preLogin({
					provider: 'univerify',
					success: this.univerify(), //预登录成功
77
					fail: (res) => { // 预登录失败
78
						// 不显示一键登录选项(或置灰)
79
						console.log(res)
80
						this.bindMobileBySmsCode()
81 82
					}
				})
83 84 85 86 87 88 89 90
				// #endif
				
				// #ifdef MP-WEIXIN
				this.$refs['uni-bindMobileByMpWeixin'].open()
				// #endif
				
				// #ifdef H5
					//...去用验证码绑定
91
				this.bindMobileBySmsCode()
92 93 94 95 96 97 98 99
				// #endif
			},
			univerify() {
				uni.login({
					"provider": 'univerify',
					"univerifyStyle": this.univerifyStyle,
					success: async e => {
						console.log(e.authResult);
100 101 102
						uniCloud.callFunction({
							name: 'uni-id-cf',
							data: {
103
								action: 'bindMobileByUniverify',
104 105 106 107 108
								params: e.authResult,
							},
							success: ({
								result
							}) => {
109
								console.log(result);
110 111 112 113
								if (result.code === 0) {
									this.setUserInfo({
										"mobile": result.mobile
									})
114
									uni.closeAuthView()
115
								} else {
116
									uni.showModal({
117
										content: result.msg,
118 119 120 121 122
										showCancel: false,
										complete() {
											uni.closeAuthView()
										}
									});
123 124
								}
							}
125
						})
126
					},
127 128
					fail: (err) => {
						console.log(err);
129
						if (err.code == '30002' || err.code == '30001') {
130 131
							this.bindMobileBySmsCode()
						}
132 133 134
					}
				})
			},
135
			bindMobileBySmsCode() {
136
				uni.navigateTo({
137
					url: '/pages/ucenter/userinfo/bind-mobile/bind-mobile'
138 139
				})
			},
study夏羽's avatar
study夏羽 已提交
140
			async setNickname(nickname) {
DCloud_JSON's avatar
DCloud_JSON 已提交
141
				console.log(nickname);
142
				if (nickname) {
study夏羽's avatar
study夏羽 已提交
143
					return await usersTable.where('_id==$env.uid').update({
144 145
						nickname
					}).then(e => {
study夏羽's avatar
study夏羽 已提交
146
						console.log(e.result,"e.result---------");
147 148
						if (e.result.updated) {
							uni.showToast({
149
								title:this.$t('common.updateSucceeded'),
150 151 152 153 154 155 156
								icon: 'none'
							});
							this.setUserInfo({
								nickname
							});
						} else {
							uni.showToast({
157
								title: this.$t('userinfo.noChange'),
158 159 160
								icon: 'none'
							});
						}
study夏羽's avatar
study夏羽 已提交
161
						return e.result
162 163 164 165 166
					})
					this.$refs.dialog.close()
				} else {
					this.$refs.dialog.open()
				}
167
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
168
			setAvatarFile(avatar_file) {
169
				uni.showLoading({
170
					title: this.$t('userinfo.setting'),
171 172 173 174 175 176 177
					mask: true
				});
				// 使用 clientDB 提交数据
				usersTable.where('_id==$env.uid').update({
					avatar_file
				}).then((res) => {
					console.log(res);
DCloud_JSON's avatar
DCloud_JSON 已提交
178
					if (avatar_file) {
179 180
						uni.showToast({
							icon: 'none',
181
							title: this.$t('userinfo.setSucceeded')
182
						})
DCloud_JSON's avatar
DCloud_JSON 已提交
183
					} else {
184 185
						uni.showToast({
							icon: 'none',
186
							title: this.$t('userinfo.deleteSucceeded')
187 188 189 190 191 192 193
						})
					}
					this.setUserInfo({
						avatar_file
					});
				}).catch((err) => {
					uni.showModal({
194
						content: err.message ||this.$t('userinfo.requestFail'),
195 196 197 198 199
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
200
			},
201
			uploadAvatarImg(res) {
202 203 204 205 206
				const crop = {
					quality: 100,
					width: 600,
					height: 600,
					resize: true
207
				};
208
				uni.chooseImage({
DCloud_JSON's avatar
DCloud_JSON 已提交
209 210
					count: 1,
					crop,
211 212 213
					success: async (res) => {
						console.log(res);
						let tempFile = res.tempFiles[0],
DCloud_JSON's avatar
DCloud_JSON 已提交
214 215 216 217 218 219 220 221 222
							avatar_file = {
								// #ifdef H5
								extname: tempFile.name.split('.')[tempFile.name.split('.').length - 1],
								// #endif
								// #ifndef H5
								extname: tempFile.path.split('.')[tempFile.path.split('.').length - 1]
								// #endif
							},
							filePath = res.tempFilePaths[0]
223 224 225
						// #ifndef APP-PLUS
						//非app端用前端组件剪裁头像,app端用内置的原生裁剪
						filePath = await new Promise((callback) => {
226 227 228 229 230
							uni.navigateTo({
								url: '/pages/ucenter/userinfo/cropImage?path=' + filePath +
									`&options=${JSON.stringify(crop)}`,
								animationType: "fade-in",
								events: {
DCloud_JSON's avatar
DCloud_JSON 已提交
231
									success: url => {
232 233 234
										callback(url)
									}
								}
235 236 237
							});
						})
						// #endif
238
						console.log(this.userInfo);
DCloud_JSON's avatar
DCloud_JSON 已提交
239
						let cloudPath = this.userInfo._id + '' + Date.now()
240 241
						avatar_file.name = cloudPath
						uni.showLoading({
242
							title:this.$t('userinfo.uploading'),
243 244
							mask: true
						});
DCloud_JSON's avatar
DCloud_JSON 已提交
245 246 247 248 249 250
						let {
							fileID
						} = await uniCloud.uploadFile({
							filePath,
							cloudPath,
							fileType: "image"
251 252 253
						});
						// console.log(result)
						avatar_file.url = fileID
DCloud_JSON's avatar
DCloud_JSON 已提交
254 255 256
						console.log({
							avatar_file
						});
257
						uni.hideLoading()
DCloud_JSON's avatar
DCloud_JSON 已提交
258

259
						this.setAvatarFile(avatar_file)
260 261
					}
				})
262
			}
263 264 265
		}
	}
</script>
DCloud_JSON's avatar
DCloud_JSON 已提交
266
<style lang="scss" scoped>
267 268 269 270 271 272 273
	/* #ifndef APP-NVUE */
	view {
		display: flex;
		box-sizing: border-box;
		flex-direction: column;
	}
	/* #endif */
274 275 276 277
	.item {
		width: 750rpx;
		flex-direction: row;
		justify-content: space-between;
278
		align-items: center;
279
	}
280 281

	.chooseAvatar {
282
		border: dotted 1px #ddd;
283 284
		border-radius: 10px;
		text-align: center;
285 286 287
		width: 50px;
		height: 50px;
		line-height: 50px;
288
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
289
</style>