Skip to content

Commit

Permalink
是否在线交给云函数判断
Browse files Browse the repository at this point in the history
  • Loading branch information
name-jerry committed Apr 11, 2023
1 parent 64c86e5 commit d90d61f
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 22 deletions.
2 changes: 0 additions & 2 deletions App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
let main = useMainStore();
main.artList = list
onLaunch(() => {
console.log("App onLaunch");
// #ifndef H5 || APP-PLUS
Expand Down
1 change: 1 addition & 0 deletions components/Login/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
let pwd = SHA1(form.pwd1).toString();
let res = await cf({ type: 'login', acc: form.acc, pwd })
if (res.success) {
main.online = res.isOnline
uni.showToast({
title: '登录成功',
})
Expand Down
1 change: 1 addition & 0 deletions components/ReadFilePicker/ReadFilePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
}
// 在线是同步到网络
if (main.isOnline) {
delete a._id
let res = await curdArt("add", a)
a._id = res.id;
}
Expand Down
5 changes: 1 addition & 4 deletions pages/home/home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
// 设置
let setOptions = reactive<Option[]>([])
function swith() {
uni.showModal({
title: '提示',
content: '读写资源有限,暂不开放读写功能'
})
main.switchOnlineStatus()
}
function initSet() {
for (let [k, v] of Object.entries(show.value)) {
Expand Down
29 changes: 27 additions & 2 deletions stores/useMainStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineStore } from 'pinia';
import { ref } from "vue";
import { ref, watch } from "vue";
import { Article } from "@/type"
import { checkLogin } from "@/utils/checkLogin"

Expand All @@ -11,7 +11,30 @@ import { checkLogin } from "@/utils/checkLogin"
const useMainStore = defineStore('main', () => {
let isLogin = ref<boolean>(checkLogin());
let artList = ref<Article[]>([])
let isOnline = ref<boolean>(false)
/**只在login时变更online,用于设定是否允许在线*/
let online = ref<boolean>(false);
let isOnline = ref<boolean>(false);
watch(isLogin, () => {
if (!isLogin.value) {
online.value = false
} else {
let o = uni.getStorageSync('online')
online.value = o ? true : false
}
}, { immediate: true })
watch(online, () => {
let b = online.value
uni.setStorageSync('online', b)
isOnline.value = b
}, { immediate: true });
/**在允许在线的时候可以切换isOnline*/
function switchOnlineStatus() : void {
if (online.value) {
isOnline.value = !isOnline.value;
} else {
uni.showModal({ title: '提示', content: '读写资源有限,暂不开放读写功能' });
}
}
// 退出时清空数据,选择退出时才清空
// watch(isLogin, () => {
// if (!isLogin.value) {
Expand All @@ -22,6 +45,8 @@ const useMainStore = defineStore('main', () => {
isLogin,
artList,
isOnline,
online,
switchOnlineStatus,
}
});
export default useMainStore
2 changes: 1 addition & 1 deletion uniCloud-aliyun/cloudfunctions/fun/apis/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ module.exports = async function login(event, context, dbJQL, user, uniIDIns) {
//创建一个新token
let { token, tokenExpired } = await uniIDIns.createToken({ uid, role })
return { uid, role, token }
}
}
6 changes: 0 additions & 6 deletions uniCloud-aliyun/cloudfunctions/fun/fun.param.json

This file was deleted.

4 changes: 3 additions & 1 deletion uniCloud-aliyun/cloudfunctions/fun/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let path = require('path')
let p = path.join(__dirname + '/apis')
let apis = fs.readdirSync(p);
let funs = new Map()
let env = require('./env')
// 加载模块
apis.map(item => {
let type = item.replace('.js', '')
Expand Down Expand Up @@ -33,6 +34,7 @@ exports.main = async (event, context) => {
try {
// 传入原有的两个属性,dbJQL操作对象,token中含有的角色信息,uni-id-common模块
let res = await funs.get(type)(event, context, dbJQL, { uid, role, permission }, uniIDIns)
if (type == "login") res.isOnline = env.online ? true : false;
// 此处利用用展开符的特性,遇到登录接口等返回的res中含有token属性将覆盖此处的token
return { success: true, errCode: 0, token, ...res }
} catch (e) {
Expand All @@ -48,4 +50,4 @@ exports.main = async (event, context) => {
errCode: '无此api',
errMsg: '无此api',
}
};
};
12 changes: 6 additions & 6 deletions utils/useRefresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import useMainStore from "@/stores/useMainStore";
export function useRefresh<T>(curd : CURDFn, key : string,) : { list : Ref<T[]> } {
let main = useMainStore();
let list : Ref<T[]> = ref([]);
let stop : WatchStopHandle;
watchUpdateAndSetData();
let stopUpdateStorage : WatchStopHandle;
updateStorageByList();
watch(() => main.isLogin, updateWithLoginStatus, { immediate: true })
return { list }
function watchUpdateAndSetData() {
stop = watch(list, () => { uni.setStorageSync(key, JSON.stringify(list.value)) }, { deep: true })
function updateStorageByList() {
stopUpdateStorage = watch(list, () => { uni.setStorageSync(key, JSON.stringify(list.value)) }, { deep: true })
}
async function updateWithLoginStatus() {
// 未登录状态时清空列表
if (!main.isLogin) {
typeof stop == 'function' && stop()
typeof stopUpdateStorage == 'function' && stopUpdateStorage()
list.value = [];
watchUpdateAndSetData()
updateStorageByList()
return
}
// 登录状态时
Expand Down

0 comments on commit d90d61f

Please sign in to comment.