유니버설 프로그램 클라우드 개발 로그인편 베스트 실천

4635 단어

첫 번째, 로그인 여부 확인

// App.vue

	global.isLogin = <span class="hljs-function"><span class="hljs-title">function</span></span>() {
		try {
			var openid = uni.getStorageSync(<span class="hljs-string">'openid'</span>);
		} catch (e) {

		}
		<span class="hljs-keyword">if</span> (openid === <span class="hljs-string">''</span>) {
			<span class="hljs-built_in">return</span> <span class="hljs-literal">false</span>
		} <span class="hljs-keyword">else</span> {
			<span class="hljs-built_in">return</span> {
				openid
			}
		}
	};
	<span class="hljs-built_in">export</span> default {
		onLaunch: <span class="hljs-function"><span class="hljs-title">function</span></span>() {
			console.log(<span class="hljs-string">'App Launch'</span>)
			wx.cloud.init({
				traceUser: <span class="hljs-literal">true</span>
			});
		}
	}


2단계, 페이지 로그인

// login.vue


	const db = wx.cloud.database();
	<span class="hljs-built_in">export</span> default {
		<span class="hljs-function"><span class="hljs-title">data</span></span>() {
			<span class="hljs-built_in">return</span> {}
		},
		methods: {
			wxGetUserInfo(msg) {
				console.log(msg)
				var that = this;
				<span class="hljs-keyword">if</span> (!msg.detail.iv) {
					uni.showToast({
						title: <span class="hljs-string">' , '</span>,
						icon: <span class="hljs-string">'none'</span>
					});
					<span class="hljs-built_in">return</span> <span class="hljs-literal">false</span>
				}
				uni.showLoading({
				    title: <span class="hljs-string">' '</span>
				});
				uni.login({
					provider: <span class="hljs-string">'weixin'</span>,
					success: <span class="hljs-keyword">function</span>(loginRes) {
						console.log(loginRes)
						that.getOpenid(msg);
					},
					fail: <span class="hljs-keyword">function</span>(r) {
						console.log(r)
					}
				})
			},
			getOpenid(msg) {
				var that = this;
				wx.cloud.callFunction({
					name: <span class="hljs-string">'getOpenid'</span>,
					complete: res => {
						console.log(<span class="hljs-string">' openid: '</span>, res);
						try {
							uni.setStorageSync(<span class="hljs-string">'openid'</span>, res.result.openId);
						} catch (e) {
							// error
						}
						that.saveToDb(msg, res.result.openId)
					},
				});
			},
			saveToDb(msg, openid) {
				console.log(openid)
				db.collection(<span class="hljs-string">'user'</span>).where({
					_openid: openid
				}).get().then(res => {
					console.log(res)
					<span class="hljs-keyword">if</span> (res.errMsg === <span class="hljs-string">'collection.get:ok'</span>) {
						<span class="hljs-keyword">if</span> (res.data.length === 0) {
							db.collection(<span class="hljs-string">'user'</span>).add({
								data: {
									...msg.detail.userInfo,
									_openid: res.result.openId
								}
							}).then(res => {
								console.log(res);
							});
						}
						uni.hideLoading();
						uni.showToast({
							title: <span class="hljs-string">' '</span>,
							icon: <span class="hljs-string">'success'</span>
						});
						<span class="hljs-built_in">set</span>Timeout(() => {
							uni.navigateBack();
						}, 1500);
					} <span class="hljs-keyword">else</span> {}
				})

			}
		},
		<span class="hljs-function"><span class="hljs-title">onLoad</span></span>() {

		}
	}


다음으로 전송:https://juejin.im/post/5d5f5ca96fb9a06ade1118e6

좋은 웹페이지 즐겨찾기