From ee2ed1956d8776ed2acb9dbd4dd509717cb6561f Mon Sep 17 00:00:00 2001 From: yuhj Date: Thu, 26 May 2022 23:21:37 +0800 Subject: [PATCH] sync with uni market --- .gitignore | 3 + App.vue | 211 +++++++++++++------------ meteor/server/main.ts | 142 ++++++++--------- modules/chatroom/service/index.js | 59 ++++--- modules/core/ddp.js | 38 ++++- modules/user/service/index.js | 40 +++-- modules/user/signin/signin.vue | 25 +-- modules/user/signup/signup.vue | 146 +++++++++-------- pages.json | 15 +- uni_modules/hj-ddp/changelog.md | 2 + uni_modules/hj-ddp/js_sdk/hj-ddp.es.js | 21 ++- uni_modules/hj-ddp/package.json | 2 +- 12 files changed, 387 insertions(+), 317 deletions(-) diff --git a/.gitignore b/.gitignore index e69de29..47ff36d 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,3 @@ +uni_modules +hbuilderx +node_modules \ No newline at end of file diff --git a/App.vue b/App.vue index 16d75b7..19ccc11 100644 --- a/App.vue +++ b/App.vue @@ -1,107 +1,116 @@ diff --git a/meteor/server/main.ts b/meteor/server/main.ts index 52bf890..bda9022 100644 --- a/meteor/server/main.ts +++ b/meteor/server/main.ts @@ -4,87 +4,87 @@ import { publishComposite } from "meteor/reywood:publish-composite"; Meteor.startup(() => { - console.log(`Hi boot! -- ddp demo -- oh yeah -`); + console.log(`Hi boot! -- ddp demo -- oh yeah -`); }); const Users = Meteor.users; const Rooms = new Mongo.Collection("rooms"); const Messages = new Mongo.Collection("messages"); +Messages.allow({ + insert(user, doc) { + if (!user) throw new Meteor.Error("仅有登录用户能那啥"); + if (!doc.roomId) throw new Meteor.Error("需要提供roomId"); + doc.createdBy = user; + doc.createdAt = Date.now() + } +}) Meteor.methods({ - "message.add": function (data: any) { - if (!this.userId || !data.roomId) - throw new Meteor.Error("仅有登录用户能那啥"); - return Messages.insert({ - ...data, - user: this.userId, - }); - }, - "room.create": function (name: string) { - if (!this.userId) throw new Meteor.Error("仅有登录用户能那啥"); - return Rooms.insert({ - name, - createdBy: this.userId, - createdAt: Date.now(), - members: [this.userId], - }); - }, - "room.join": function (id: string) { - if (!this.userId) throw new Meteor.Error("仅有登录用户能那啥"); - const room = Rooms.findOne(id); - if (!room) throw new Meteor.Error("没房间不能那啥"); - return Rooms.update(id, { - $addToSet: { - members: this.userId, - }, - }); - }, - "room.left": function (id: string) { - if (!this.userId) throw new Meteor.Error("仅有登录用户能那啥"); - const room = Rooms.findOne(id); - if (!room) throw new Meteor.Error("没房间不能那啥"); - return Rooms.update(id, { - $pull: { - members: this.userId, - }, - }); - } + "room.create": function(name: string) { + if (!this.userId) throw new Meteor.Error("仅有登录用户能那啥"); + return Rooms.insert({ + name, + createdBy: this.userId, + createdAt: Date.now(), + members: [this.userId], + }); + }, + "room.join": function(id: string) { + if (!this.userId) throw new Meteor.Error("仅有登录用户能那啥"); + const room = Rooms.findOne(id); + if (!room) throw new Meteor.Error("没房间不能那啥"); + return Rooms.update(id, { + $addToSet: { + members: this.userId, + }, + }); + }, + "room.left": function(id: string) { + if (!this.userId) throw new Meteor.Error("仅有登录用户能那啥"); + const room = Rooms.findOne(id); + if (!room) throw new Meteor.Error("没房间不能那啥"); + return Rooms.update(id, { + $pull: { + members: this.userId, + }, + }); + } }); - -Meteor.publish("rooms.all", function () { - return Rooms.find({}, { fields: { name: 1, _id: 1 } }); + +Meteor.publish("rooms.all", function() { + return Rooms.find({}, { fields: { name: 1, _id: 1 } }); }); -publishComposite("rooms.mine", function () { - const userId = this.userId; - return { - find() { - return Rooms.find( - { members: userId }, - { fields: { name: 1, members: 1 } } - ); - }, - collectionName: `rooms-mine`, - children: [ - { - find(room) { - return Users.find( - { id: { $in: room.membsers } }, - { fields: { profile: 1 } } - ); - }, - }, - { - find(room) { - return Messages.find({ roomId: room._id }); - }, - }, - ], - }; +publishComposite("rooms.mine", function() { + const userId = this.userId; + console.log(userId + "mine") + return { + find() { + return Rooms.find( + { members: userId }, + { fields: { name: 1, members: 1, createdBy: 1 } } + ); + }, + children: [ + { + find(room) { + return Users.find( + { id: { $in: room.membsers } }, + { fields: { profile: 1 } } + ); + }, + }, + { + find(room) { + return Messages.find({ roomId: room._id }); + }, + }, + ], + }; }); Meteor.onConnection((con) => { - console.log(`${con.id} connected`); - con.onClose(() => { - console.log(`${con.id} closed`); - }); + console.log(`${con.id} connected`); + con.onClose(() => { + console.log(`${con.id} closed`); + }); }); diff --git a/modules/chatroom/service/index.js b/modules/chatroom/service/index.js index 1c12ced..081315a 100644 --- a/modules/chatroom/service/index.js +++ b/modules/chatroom/service/index.js @@ -1,36 +1,49 @@ import { + useMongo, ddp } from "../../core/ddp.js" +import { + mineInfo +} from "../../user/service/index.js" import { reactive, - computed + computed, + watchEffect } from "vue" -export const myRooms = reactive([]) -const allRooms = reactive([]) -console.log(ddp) -const MyRooms = ddp.db.collection('rooms-mine') -const AllRooms = ddp.db.collection('rooms') -const Messages = ddp.db.collection('messages') +ddp.subscribe("rooms.all") +ddp.subscribe("rooms.mine") +const db = useMongo(mineInfo._id, ddp) +const AllRooms = db.collection('rooms') +const Messages = db.collection('messages') let currentRoom = null; -ddp.map(MyRooms, myRooms) -ddp.map(AllRooms, allRooms) - +console.log(mineInfo._id) +export const myRooms = AllRooms.liveQuery({ + createdBy: mineInfo._id +}) export const roomMessages = reactive([]) -export const otherRooms = computed(() => allRooms.filter(v => !myRooms.some(e => e._id === v._id))) -export const sendMessage = txt => new Promise(resolve => { - if (!currentRoom || !txt) return resolve(false) +export const otherRooms = AllRooms.liveQuery({ + createdBy: { + $ne: mineInfo._id + } +}) +watchEffect(() => { + console.log(myRooms) + console.log(otherRooms) +}) +export const sendMessage = async txt => { + if (!currentRoom || !txt) return const data = { roomId: currentRoom, txt, - _id: Date.now() + _id: mineInfo._id + "-" + Date.now() } - ddp.call('message.add', { - ...data, - _id: undefined - }, (err, res) => { - resolve(!err) + const res = await Messages.insert({ + ...data + }, { + remote: 1 }) -}) + console.log(res) +} export const createRoom = name => new Promise(resolve => { { ddp.call('room.create', name, (err, res) => { @@ -69,11 +82,9 @@ Messages.observe({ index > -1 && roomMessages.splice(i, 1, nv) } }); -import { - user -} from "../../user/service" + export const joinRoom = id => new Promise(resolve => { - if (!user._id) return uni.navigateTo({ + if (!mineInfo._id) return uni.navigateTo({ url: '/modules/user/signin/signin' }) ddp.call('room.join', id, (err, res) => { diff --git a/modules/core/ddp.js b/modules/core/ddp.js index 511ea91..0f5b7bb 100644 --- a/modules/core/ddp.js +++ b/modules/core/ddp.js @@ -1,13 +1,37 @@ - -import { init } from "../../uni_modules/hj-ddp/js_sdk"; -import { hjMeteorAccount } from "../../uni_modules/hj-meteor-account/js_sdk"; - +import { + watchEffect +} from "vue"; +import { + connect +} from "../../uni_modules/hj-ddp/js_sdk"; +import { + useMeteorAccount +} from "../../uni_modules/hj-meteor-account/js_sdk"; +import { + useLocalMongo +} +from "../../uni_modules/hj-mongo-vue3/js_sdk"; +export { + useLocalMongo as useMongo +} +from "../../uni_modules/hj-mongo-vue3/js_sdk"; // #ifdef H5 -export const ddp = init('ws://localhost:3002') +export const ddp = connect('ws://localhost:3002') // #endif // #ifndef H5 -export const ddp = init('ws://10.0.2.2:3002') +export const ddp = connect('ws://10.0.2.2:3002') // #endif +export const account = useMeteorAccount(ddp) + +export const db = useLocalMongo("shared", ddp) -ddp.use(hjMeteorAccount) \ No newline at end of file +watchEffect(()=>account.data.state, (nv,ov) => { + if (nv === 2) { + uni.showLoading({ + title: "登陆中" + }) + }else if(ov!==2) { + uni.hideLoading() + } +}) diff --git a/modules/user/service/index.js b/modules/user/service/index.js index 17be153..71db7e6 100644 --- a/modules/user/service/index.js +++ b/modules/user/service/index.js @@ -1,30 +1,37 @@ - import { + computed, reactive } from "vue"; +export {account} from "../../core/ddp"; import { - ddp + ddp, + useMongo, + db, + account } from "../../core/ddp"; -export const user = reactive({}) +export const mineInfo = reactive({}) -export const Users = ddp.db.collection('users') -ddp.user.onChange((info) => { - if (info) { - for (const key in info) { - user[key] = info[key] - } - } else { - for (const key in user) { - delete user[key] +export const Users = db.collection('users') + +export const ready = computed(()=>account.state===1) + +account.onChange(nv => { + const removes =new Set( Object.keys(mineInfo)) + if (nv) { + for (const key in nv) { + mineInfo[key] = nv[key] + removes.delete(key) } } + removes.forEach(key=>delete mineInfo[key]) }) + export const signin = async (data) => { if (!data.username || !data.password) return - ddp.loginWithPassword(data.username, data.password).catch(err => { + account.loginWithPassword(data.username, data.password).catch(err => { uni.showToast({ title: '密码或者用户名错误' }) @@ -33,9 +40,10 @@ export const signin = async (data) => { export const signup = async (data) => { if (!data.username || !data.password || data.password !== data.password1) return - ddp.createAccount(data.username, data.password).catch(err => { + account.createAccount(data.username, data.password).catch(err => { + console.log(err) uni.showToast({ - title: '注册失败' + title: '注册失败' + err?.reason }) }); -}; \ No newline at end of file +}; diff --git a/modules/user/signin/signin.vue b/modules/user/signin/signin.vue index b3b8eb3..a8c0982 100644 --- a/modules/user/signin/signin.vue +++ b/modules/user/signin/signin.vue @@ -11,9 +11,11 @@ 登录 - - - + + + + + @@ -24,17 +26,18 @@ } from "vue" import { signin, - user + mineInfo, + ready, + account } from "../service"; const data = reactive({ username: "", password: "" }) - watch(user, (ov, nv) => { - console.log({ov,nv}) - if (nv?._id) { + watch(() => account.data.state, (nv, ov) => { + if (nv === 1) { uni.reLaunch({ - url: '/pages/index/index' + url: '/pages/index/index' }); } }, { @@ -67,13 +70,16 @@ width: 100%; } } - .btn,.dlbutton{ + + .btn, + .dlbutton { font-size: 34upx; width: 470upx; height: 100upx; border-radius: 50upx; line-height: 100upx; } + .dlbutton { color: #ffffff; background: linear-gradient(-90deg, @@ -89,5 +95,4 @@ rgba(63, 205, 235, 0.9), rgba(188, 226, 158, 0.9)); } - diff --git a/modules/user/signup/signup.vue b/modules/user/signup/signup.vue index dd86048..4df850f 100644 --- a/modules/user/signup/signup.vue +++ b/modules/user/signup/signup.vue @@ -18,88 +18,84 @@ diff --git a/pages.json b/pages.json index fcf8ea9..7fe261a 100644 --- a/pages.json +++ b/pages.json @@ -1,12 +1,6 @@ { "pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages { - "path": "pages/index/index", - "style": { - "navigationBarTitleText": "DDP Demo" - } - } - ,{ "path" : "modules/user/signin/signin", "style" : { @@ -14,7 +8,14 @@ "enablePullDownRefresh": false } - } + }, + { + "path": "pages/index/index", + "style": { + "navigationBarTitleText": "DDP Demo" + } + } + ,{ "path" : "modules/user/signup/signup", "style" : diff --git a/uni_modules/hj-ddp/changelog.md b/uni_modules/hj-ddp/changelog.md index 9ad439e..2dac01a 100644 --- a/uni_modules/hj-ddp/changelog.md +++ b/uni_modules/hj-ddp/changelog.md @@ -1,3 +1,5 @@ +## 1.2.3(2022-04-16) +提供一个callAsync方法,返回promise简化rpc调用回调模式 ## 1.2.2(2022-04-16) 更新readme ## 1.2.1(2022-04-16) diff --git a/uni_modules/hj-ddp/js_sdk/hj-ddp.es.js b/uni_modules/hj-ddp/js_sdk/hj-ddp.es.js index b9bfde6..0c0d67b 100644 --- a/uni_modules/hj-ddp/js_sdk/hj-ddp.es.js +++ b/uni_modules/hj-ddp/js_sdk/hj-ddp.es.js @@ -244,7 +244,7 @@ class DDPConnection extends EventEmitter { }); if (oldIndex > -1) { const oo = this.messages.splice(oldIndex, 1)[0]; - this.revokeCallBack(oo.id, [1, "ignored"]); + this.revokeCallBack(oo.id, "忽略了相同参数的相同方法调用"); } } this.messages.push(data); @@ -337,7 +337,7 @@ class DDPConnection extends EventEmitter { } } var IDDPConnectionState; -(function (IDDPConnectionState2) { +(function(IDDPConnectionState2) { IDDPConnectionState2[IDDPConnectionState2["CLOSED"] = 0] = "CLOSED"; IDDPConnectionState2[IDDPConnectionState2["CONNECTING"] = 1] = "CONNECTING"; IDDPConnectionState2[IDDPConnectionState2["CONNECTED"] = 2] = "CONNECTED"; @@ -362,7 +362,9 @@ class Client { if (this.connection.state === DDPConnectionState.CONNECTED) { return resolve() } - const cb = ({ state }) => { + const cb = ({ + state + }) => { if (state === DDPConnectionState.CONNECTED) { this.connection.off(DDPConnectionEvent.STATE_CHANGE, cb) resolve() @@ -374,7 +376,9 @@ class Client { if (this.connection.state !== DDPConnectionState.CONNECTED) { return resolve() } - const cb = ({ state }) => { + const cb = ({ + state + }) => { if (state !== DDPConnectionState.CONNECTED) { this.connection.off(DDPConnectionEvent.STATE_CHANGE, cb) resolve() @@ -397,7 +401,9 @@ class Client { return () => onCloses.delete(cb) } let connected = false; - this.connection.on(DDPConnectionEvent.STATE_CHANGE, ({ state }) => { + this.connection.on(DDPConnectionEvent.STATE_CHANGE, ({ + state + }) => { const newState = state === DDPConnectionState.CONNECTED; if (newState === connected) return; connected = newState; @@ -409,6 +415,11 @@ class Client { const callback = args.filter((el) => typeof el === "function"); return this.connection.call(name, callArgs, callback[0], callback[1]); } + callAsync(name, ...args) { + return new Promise((res, rej) => { + this.connection.call(name, callArgs, (err, data) => err ? rej(err) : res(data)); + }) + } subscribe(name, ...args) { const callArgs = args.filter((el) => typeof el !== "function"); const callback = args.filter((el) => typeof el === "function"); diff --git a/uni_modules/hj-ddp/package.json b/uni_modules/hj-ddp/package.json index 22ca24f..275c485 100644 --- a/uni_modules/hj-ddp/package.json +++ b/uni_modules/hj-ddp/package.json @@ -1,7 +1,7 @@ { "id": "hj-ddp", "displayName": "DDP协议的Uniapp插件实现【附带MiniMongo Selector】", - "version": "1.2.2", + "version": "1.2.3", "description": "一个uniapp的DDP客户端实现,助你成为全栈工程师,哈哈~", "keywords": [ "hj-ddp", -- GitLab