userinfo.vue 6.9 KB
Newer Older
1 2 3
<template>
	<view>
		<uni-list>
DCloud_JSON's avatar
DCloud_JSON 已提交
4 5 6 7 8 9 10 11
			<uni-list-item class="item">
				<template v-slot:body>
					<view class="item">
						<text>头像</text>
						<uni-file-picker @click.native="uploadAvatarImg" @delete="removeAvatar" v-model="avatar_file"
							fileMediatype="image" return-type="object" :image-styles="listStyles" disabled />
					</view>
				</template>
12
			</uni-list-item>
13 14 15 16
			<uni-list-item class="item" @click="setNickname('')" title="昵称" :rightText="userInfo.nickname||'未设置'" link>
			</uni-list-item>
			<uni-list-item class="item" @click="bindMobile" title="手机号" :rightText="userInfo.mobile||'未绑定'" link>
			</uni-list-item>
17 18
		</uni-list>
		<uni-popup ref="dialog" type="dialog">
19 20
			<uni-popup-dialog mode="input" :value="userInfo.nickname" @confirm="setNickname" title="设置昵称"
				placeholder="请输入要设置的昵称">
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
			</uni-popup-dialog>
		</uni-popup>
	</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": "其他号码绑定",
					}
42 43 44 45 46 47 48 49 50 51
				},
				listStyles: {
					"height": 80, // 边框高度
					"width": 80, // 边框宽度
					"border": { // 如果为 Boolean 值,可以控制边框显示与否
						"color": "#eee", // 边框颜色
						"width": "1px", // 边框宽度
						"style": "solid", // 边框样式
						"radius": "10px" // 边框圆角,支持百分比
					}
52 53 54 55 56 57 58
				}
			}
		},
		computed: {
			...mapGetters({
				userInfo: 'user/info',
				login: 'user/hasLogin'
59
			}),
DCloud_JSON's avatar
DCloud_JSON 已提交
60 61
			avatar_file() {
				if (this.userInfo.avatar_file && this.userInfo.avatar_file.url) {
62 63
					return this.userInfo.avatar_file
				}
64
			}
65
		},
66 67 68 69 70 71 72 73 74
		methods: {
			...mapMutations({
				setUserInfo: 'user/login'
			}),
			bindMobile() {
				// #ifdef APP-PLUS
				uni.preLogin({
					provider: 'univerify',
					success: this.univerify(), //预登录成功
75
					fail: (res) => { // 预登录失败
76
						// 不显示一键登录选项(或置灰)
77
						console.log(res)
78
						this.bindMobileBySmsCode()
79 80 81
					}
				})
				// #endif
82
				// #ifndef APP-PLUS
83 84
				this.bindMobileBySmsCode()
				//...去用验证码绑定
85 86 87 88 89 90 91 92
				// #endif
			},
			univerify() {
				uni.login({
					"provider": 'univerify',
					"univerifyStyle": this.univerifyStyle,
					success: async e => {
						console.log(e.authResult);
93 94 95
						uniCloud.callFunction({
							name: 'uni-id-cf',
							data: {
96
								action: 'bindMobileByUniverify',
97 98 99 100 101
								params: e.authResult,
							},
							success: ({
								result
							}) => {
102
								console.log(result);
103 104 105 106
								if (result.code === 0) {
									this.setUserInfo({
										"mobile": result.mobile
									})
107
									uni.closeAuthView()
108
								} else {
109
									uni.showModal({
110
										content: result.msg,
111 112 113 114 115
										showCancel: false,
										complete() {
											uni.closeAuthView()
										}
									});
116 117
								}
							}
118
						})
119
					},
120 121
					fail: (err) => {
						console.log(err);
122
						if (err.code == '30002' || err.code == '30001') {
123 124
							this.bindMobileBySmsCode()
						}
125 126 127 128
					}
				})
			},
			bindMobileBySmsCode() {
129
				uni.navigateTo({
130
					url: '/pages/ucenter/userinfo/bind-mobile/bind-mobile'
131 132 133
				})
			},
			setNickname(nickname) {
DCloud_JSON's avatar
DCloud_JSON 已提交
134
				console.log(nickname);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
				if (nickname) {
					usersTable.where('_id==$env.uid').update({
						nickname
					}).then(e => {
						console.log(e);
						if (e.result.updated) {
							uni.showToast({
								title: '更新成功',
								icon: 'none'
							});
							this.setUserInfo({
								nickname
							});
						} else {
							uni.showToast({
								title: '没有变化',
								icon: 'none'
							});
						}
					})
					this.$refs.dialog.close()
				} else {
					this.$refs.dialog.open()
				}
159
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
160
			removeAvatar() {
161 162 163 164 165 166 167 168
				this.setAvatarFile({
					"extname": "jpg",
					"fileType": "image",
					"name": "",
					"size": 0,
					"url": ""
				})
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
169
			setAvatarFile(avatar_file) {
170 171 172 173 174 175 176 177 178
				uni.showLoading({
					title: '设置中',
					mask: true
				});
				// 使用 clientDB 提交数据
				usersTable.where('_id==$env.uid').update({
					avatar_file
				}).then((res) => {
					console.log(res);
DCloud_JSON's avatar
DCloud_JSON 已提交
179
					if (avatar_file) {
180 181 182 183
						uni.showToast({
							icon: 'none',
							title: '设置成功'
						})
DCloud_JSON's avatar
DCloud_JSON 已提交
184
					} else {
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
						uni.showToast({
							icon: 'none',
							title: '删除成功'
						})
					}
					this.setUserInfo({
						avatar_file
					});
				}).catch((err) => {
					uni.showModal({
						content: err.message ||
							'请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
202
			},
203
			uploadAvatarImg(res) {
204 205 206 207 208
				const crop = {
					quality: 100,
					width: 600,
					height: 600,
					resize: true
209
				};
210
				uni.chooseImage({
DCloud_JSON's avatar
DCloud_JSON 已提交
211 212
					count: 1,
					crop,
213 214 215
					success: async (res) => {
						console.log(res);
						let tempFile = res.tempFiles[0],
DCloud_JSON's avatar
DCloud_JSON 已提交
216 217 218 219 220 221 222 223 224
							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]
225 226 227
						// #ifndef APP-PLUS
						//非app端用前端组件剪裁头像,app端用内置的原生裁剪
						filePath = await new Promise((callback) => {
228 229 230 231 232
							uni.navigateTo({
								url: '/pages/ucenter/userinfo/cropImage?path=' + filePath +
									`&options=${JSON.stringify(crop)}`,
								animationType: "fade-in",
								events: {
DCloud_JSON's avatar
DCloud_JSON 已提交
233
									success: url => {
234 235 236
										callback(url)
									}
								}
237 238 239
							});
						})
						// #endif
240
						console.log(this.userInfo);
DCloud_JSON's avatar
DCloud_JSON 已提交
241
						let cloudPath = this.userInfo._id + '' + Date.now()
242 243 244 245 246
						avatar_file.name = cloudPath
						uni.showLoading({
							title: '正在上传',
							mask: true
						});
DCloud_JSON's avatar
DCloud_JSON 已提交
247 248 249 250 251 252
						let {
							fileID
						} = await uniCloud.uploadFile({
							filePath,
							cloudPath,
							fileType: "image"
253 254 255
						});
						// console.log(result)
						avatar_file.url = fileID
DCloud_JSON's avatar
DCloud_JSON 已提交
256 257 258
						console.log({
							avatar_file
						});
259
						uni.hideLoading()
DCloud_JSON's avatar
DCloud_JSON 已提交
260

261
						this.setAvatarFile(avatar_file)
262 263
					}
				})
264
			}
265 266 267
		}
	}
</script>
DCloud_JSON's avatar
DCloud_JSON 已提交
268 269
<style lang="scss" scoped>
	@import '@/common/all-flex.css';
270 271 272 273
	.item {
		width: 750rpx;
		flex-direction: row;
		justify-content: space-between;
274
		align-items: center;
275
	}
276

277 278
	.avatarUrl {
		width: 50px;
279
		height: 50px;
280
		border-radius: 6px;
281
	}
282 283 284 285 286 287 288 289

	.chooseAvatar {
		border: solid 1px #ddd;
		border-radius: 10px;
		width: 130rpx;
		height: 130rpx;
		line-height: 130rpx;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
290
</style>