store.js 3.3 KB
Newer Older
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
1 2
import pagesJson from '@/pages.json'
const uniIdCo = uniCloud.importObject("uni-id-co")
C
1.0.25  
chenruilong 已提交
3
const db = uniCloud.database();
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
4
const usersTable = db.collection('uni-id-users')
5 6


C
1.0.25  
chenruilong 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19

let hostUserInfo = uni.getStorageSync('uni-id-pages-userInfo')||{}
console.log( hostUserInfo);
const data = {
	userInfo: hostUserInfo,
	hasLogin: Object.keys(hostUserInfo).length != 0
}

console.log('data', data);
// 定义 mutations, 修改属性
export const mutations = {
	// data不为空,表示传递要更新的值(注意不是覆盖是合并),什么也不传时,直接查库获取更新
	async updateUserInfo(data = false) {
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
		if (data) {
			usersTable.where('_id==$env.uid').update(data).then(e => {
				console.log(e);
				if (e.result.updated) {
					uni.showToast({
						title: "更新成功",
						icon: 'none'
					});
					this.setUserInfo(data)
				} else {
					uni.showToast({
						title: "没有改变",
						icon: 'none'
					});
				}
C
1.0.25  
chenruilong 已提交
35 36 37
			})

		} else {
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
38 39
			try {
				let res = await usersTable.where("'_id' == $cloudEnv_uid")
DCloud_JSON's avatar
1.0.22  
DCloud_JSON 已提交
40
						.field('mobile,nickname,username,email,avatar_file')
C
1.0.25  
chenruilong 已提交
41 42 43 44 45 46 47 48 49 50
						.get()
				console.log('fromDbData',res.result.data);
				this.setUserInfo(res.result.data[0])
			} catch (e) {
				this.setUserInfo({},{cover:true})
				console.error(e.message, e.errCode);
			}
		}
	},
	async setUserInfo(data, {cover}={cover:false}) {
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
51 52
		console.log('set-userInfo', data);
		let userInfo = cover?data:Object.assign(store.userInfo,data)
C
1.0.25  
chenruilong 已提交
53 54 55 56 57 58 59 60
		store.userInfo = Object.assign({},userInfo)
		store.hasLogin = Object.keys(store.userInfo).length != 0
		console.log('store.userInfo', store.userInfo);
		uni.setStorage({
			key: "uni-id-pages-userInfo",
			data:store.userInfo
		})
		return data
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
61 62 63 64 65 66 67 68 69 70 71
	},
	async logout() {
		await uniIdCo.logout()
		uni.removeStorageSync('uni_id_token');
		uni.setStorageSync('uni_id_token_expired', 0)
		uni.redirectTo({
			url: `/${pagesJson.uniIdRouter?.loginPage ?? 'uni_modules/uni-id-pages/pages/login/login-withoutpwd'}`,
		});
		uni.$emit('uni-id-pages-logout')
		this.setUserInfo({},{cover:true})
	},
C
1.0.25  
chenruilong 已提交
72
	loginSuccess(e = {}){
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
		const {
			showToast = true, toastText = '登录成功', autoBack = true, uniIdRedirectUrl = ''
		} = e
		console.log({
			toastText,
			autoBack
		});
		if (showToast) {
			uni.showToast({
				title: toastText,
				icon: 'none'
			});
		}
		this.updateUserInfo()
		uni.$emit('uni-id-pages-login-success')
		if (autoBack) {
			let delta = 0; //判断需要返回几层
			let pages = getCurrentPages();
			// console.log(pages);
			pages.forEach((page, index) => {
				if (pages[pages.length - index - 1].route.split('/')[3] == 'login') {
					delta++
				}
			})
DCloud_JSON's avatar
1.0.24  
DCloud_JSON 已提交
97
			// console.log('判断需要返回几层:', delta);
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
98 99 100 101 102 103 104 105 106 107 108
			if (uniIdRedirectUrl) {
				return uni.reLaunch({
					url: uniIdRedirectUrl
				})
			}
			// #ifdef H5
			if (e.loginType == 'weixin') {
				console.log('window.history', window.history);
				return window.history.go(-3)
			}
			// #endif
C
1.0.25  
chenruilong 已提交
109

DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
110 111 112 113 114 115
			if (delta) {
				const page = pagesJson.pages[0]
				return uni.reLaunch({
					url: `/${page.path}`
				})
			}
C
1.0.25  
chenruilong 已提交
116

DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
117 118 119 120 121
			uni.navigateBack({
				delta
			})
		}
	}
C
1.0.25  
chenruilong 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

}

// #ifdef VUE2
import Vue from 'vue'
// 通过Vue.observable创建一个可响应的对象
export const store = Vue.observable(data)
// #endif

// #ifdef VUE3
import {
	reactive
} from 'vue'
// 通过Vue.observable创建一个可响应的对象
export const store = reactive(data)
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
137
// #endif