store.js 4.3 KB
Newer Older
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
1
import pagesJson from '@/pages.json'
2 3
import config from '@/uni_modules/uni-id-pages/config.js'

DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
4
const uniIdCo = uniCloud.importObject("uni-id-co")
C
1.0.25  
chenruilong 已提交
5
const db = uniCloud.database();
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
6
const usersTable = db.collection('uni-id-users')
7

C
1.0.25  
chenruilong 已提交
8
let hostUserInfo = uni.getStorageSync('uni-id-pages-userInfo')||{}
study夏羽's avatar
1.0.37  
study夏羽 已提交
9
// console.log( hostUserInfo);
C
1.0.25  
chenruilong 已提交
10 11
const data = {
	userInfo: hostUserInfo,
12
	hasLogin: Object.keys(hostUserInfo).length != 0
C
1.0.25  
chenruilong 已提交
13 14
}

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

		} else {
C
1.1.7  
chenruilong 已提交
40 41 42
			const uniIdCo = uniCloud.importObject("uni-id-co", {
				customUI: true
			})
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
43 44
			try {
				let res = await usersTable.where("'_id' == $cloudEnv_uid")
C
1.1.7  
chenruilong 已提交
45 46 47 48 49
					.field('mobile,nickname,username,email,avatar_file')
					.get()

				const realNameRes = await uniIdCo.getRealNameInfo()

study夏羽's avatar
1.0.37  
study夏羽 已提交
50
				// console.log('fromDbData',res.result.data);
C
1.1.7  
chenruilong 已提交
51 52 53 54
				this.setUserInfo({
					...res.result.data[0],
					realNameAuth: realNameRes
				})
C
1.0.25  
chenruilong 已提交
55 56 57 58 59 60 61
			} catch (e) {
				this.setUserInfo({},{cover:true})
				console.error(e.message, e.errCode);
			}
		}
	},
	async setUserInfo(data, {cover}={cover:false}) {
study夏羽's avatar
1.0.37  
study夏羽 已提交
62
		// console.log('set-userInfo', data);
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
63
		let userInfo = cover?data:Object.assign(store.userInfo,data)
C
1.0.25  
chenruilong 已提交
64 65
		store.userInfo = Object.assign({},userInfo)
		store.hasLogin = Object.keys(store.userInfo).length != 0
study夏羽's avatar
1.0.37  
study夏羽 已提交
66
		// console.log('store.userInfo', store.userInfo);
67
		uni.setStorageSync('uni-id-pages-userInfo', store.userInfo)
C
1.0.25  
chenruilong 已提交
68
		return data
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
69 70
	},
	async logout() {
DCloud_JSON's avatar
DCloud_JSON 已提交
71 72 73 74 75 76 77 78
		// 1. 已经过期就不需要调用服务端的注销接口	2.即使调用注销接口失败,不能阻塞客户端
		if(uniCloud.getCurrentUserInfo().tokenExpired > Date.now()){
			try{
				await uniIdCo.logout()
			}catch(e){
				console.error(e);
			}
		}
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
79 80 81 82 83 84 85 86
		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})
	},
87 88 89 90 91 92 93 94 95 96 97 98 99

	loginBack (e = {}) {
		const {uniIdRedirectUrl = ''} = e
		let delta = 0; //判断需要返回几层
		let pages = getCurrentPages();
		// console.log(pages);
		pages.forEach((page, index) => {
			if (pages[pages.length - index - 1].route.split('/')[3] == 'login') {
				delta++
			}
		})
		// console.log('判断需要返回几层:', delta);
		if (uniIdRedirectUrl) {
100
			return uni.redirectTo({
101 102 103 104 105
				url: uniIdRedirectUrl
			})
		}
		// #ifdef H5
		if (e.loginType == 'weixin') {
study夏羽's avatar
1.0.37  
study夏羽 已提交
106
			// console.log('window.history', window.history);
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
			return window.history.go(-3)
		}
		// #endif

		if (delta) {
			const page = pagesJson.pages[0]
			return uni.reLaunch({
				url: `/${page.path}`
			})
		}

		uni.navigateBack({
			delta
		})
	},
C
1.0.25  
chenruilong 已提交
122
	loginSuccess(e = {}){
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
123
		const {
124
			showToast = true, toastText = '登录成功', autoBack = true, uniIdRedirectUrl = '', passwordConfirmed
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
125
		} = e
study夏羽's avatar
1.0.37  
study夏羽 已提交
126
		// console.log({toastText,autoBack});
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
127 128 129
		if (showToast) {
			uni.showToast({
				title: toastText,
130 131
				icon: 'none',
				duration: 3000
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
132 133 134
			});
		}
		this.updateUserInfo()
135

DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
136
		uni.$emit('uni-id-pages-login-success')
137 138 139 140 141 142

		if (config.setPasswordAfterLogin && !passwordConfirmed) {
			return uni.redirectTo({
				url: uniIdRedirectUrl ? `/uni_modules/uni-id-pages/pages/userinfo/set-pwd/set-pwd?uniIdRedirectUrl=${uniIdRedirectUrl}&loginType=${e.loginType}`: `/uni_modules/uni-id-pages/pages/userinfo/set-pwd/set-pwd?loginType=${e.loginType}`,
				fail: (err) => {
					console.log(err)
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
143 144
				}
			})
145
		}
C
1.0.25  
chenruilong 已提交
146

147
		if (autoBack) {
148
			this.loginBack({uniIdRedirectUrl})
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
149 150
		}
	}
C
1.0.25  
chenruilong 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

}

// #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 已提交
166
// #endif