提交 ee2ed195 编写于 作者: yu's avatar yu

sync with uni market

上级 9dfdd183
uni_modules
hbuilderx
node_modules
\ No newline at end of file
<script>
import { ddp } from "./modules/core/ddp";
console.log(ddp)
ddp.subscribe("rooms.all")
ddp.subscribe("rooms.mine")
export default {
onLaunch: function () {
console.log("App Launch");
},
onShow: function () {
console.log("App Show");
},
onHide: function () {
console.log("App Hide");
},
};
import {
ddp
} from "./modules/core/ddp";
export default {
onLaunch: function() {
console.log("App Launch");
// #ifdef H5
uni.reLaunch({
url: "/modules/user/signin/signin"
})
// #endif
},
onShow: function() {
console.log("App Show");
},
onHide: function() {
console.log("App Hide");
},
};
</script>
<style>
/*每个页面公共css */
page {
display: flex;
flex-direction: column;
/*
/*每个页面公共css */
page {
display: flex;
flex-direction: column;
/*
min-height: 100%; */
background-color: #eeeeff;
min-height: 100%;
}
page-body {
background-color: red;
}
view{
box-sizing: border-box;
}
.frow,
.fcol {
display: flex;
}
.frow {
flex-direction: row;
}
.fcol {
flex-direction: column;
}
.fcnt,
.cntx {
justify-content: center;
}
.fcnt,
.cnty {
align-items: center;
}
.fbtw {
justify-content: space-between;
}
.fsc {
align-self: center;
}
.fse {
align-self: flex-end;
}
.fss {
align-self: flex-start;
}
.pd {
padding: 0.5rem;
}
.pdh {
padding: 0.5rem 0;
}
.f1 {
flex: 1;
}
.pdv {
padding: 0 0.5rem;
}
.mg {
margin: 0.5rem;
}
.mgh {
margin: 0 0.5rem;
}
.mgv {
margin: 0.5rem 0;
}
.mgt {
margin-top: 0.5rem;
}
.mgb {
margin-bottom: 0.5rem;
}
background-color: #eeeeff;
min-height: 100%;
}
page-body {
background-color: red;
}
view {
box-sizing: border-box;
}
.frow,
.fcol {
display: flex;
}
.frow {
flex-direction: row;
}
.fcol {
flex-direction: column;
}
.fcnt,
.cntx {
justify-content: center;
}
.fcnt,
.cnty {
align-items: center;
}
.fbtw {
justify-content: space-between;
}
.fsc {
align-self: center;
}
.fse {
align-self: flex-end;
}
.fss {
align-self: flex-start;
}
.pd {
padding: 0.5rem;
}
.pdh {
padding: 0.5rem 0;
}
.f1 {
flex: 1;
}
.pdv {
padding: 0 0.5rem;
}
.mg {
margin: 0.5rem;
}
.mgh {
margin: 0 0.5rem;
}
.mgv {
margin: 0.5rem 0;
}
.mgt {
margin-top: 0.5rem;
}
.mgb {
margin-bottom: 0.5rem;
}
</style>
......@@ -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<any>("rooms");
const Messages = new Mongo.Collection<any>("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`);
});
});
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) => {
......
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()
}
})
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
};
......@@ -11,9 +11,11 @@
<view class="dlbutton fsc" hover-class="dlbutton-hover" @tap="signin(data)">
<text>登录</text>
</view>
<navigator url="../signup/signup" class="btn fsc mgv">
<button type="default">注册</button>
</navigator>
<view class=" fsc">
<navigator url="../signup/signup" class="btn mgv">
<button type="default">注册</button>
</navigator>
</view>
</view>
</template>
......@@ -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));
}
</style>
......@@ -18,88 +18,84 @@
</template>
<script setup>
import {
reactive,
watch
} from "vue"
import {
signup,
user
} from "../service";
const data = reactive({
username: "",
password: "",
password1: ''
})
watch(user, (ov, nv) => {
console.log({ ov, nv })
if (nv?._id) {
uni.reLaunch({
url: '/pages/index/index'
});
}
}, {
imediate: true
})
import {
reactive,
watch
} from "vue"
import {
signup,
user,
account
} from "../service";
const data = reactive({
username: "",
password: "",
password1: ''
})
watch(() => account.data.state, (nv, ov) => {
if (nv === 1) {
uni.reLaunch({
url: '/pages/index/index'
});
}
}, {
imediate: true
})
</script>
<style scoped="" lang="scss">
.input-list {
padding: 50upx 70upx;
}
.input-list {
padding: 50upx 70upx;
}
.list-call {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
height: 100upx;
color: #333333;
border-bottom: 1upx solid rgba(230, 230, 230, 1);
.list-call {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
height: 100upx;
color: #333333;
border-bottom: 1upx solid rgba(230, 230, 230, 1);
input {
width: 100%;
input {
width: 100%;
}
}
}
.dlbutton {
color: #ffffff;
font-size: 34upx;
width: 470upx;
height: 100upx;
background: linear-gradient(
-90deg,
rgba(63, 205, 235, 1),
rgba(188, 226, 158, 1)
);
box-shadow: 0upx 0upx 13upx 0upx rgba(164, 217, 228, 0.2);
border-radius: 50upx;
line-height: 100upx;
text-align: center;
margin-left: auto;
margin-right: auto;
margin-top: 100upx;
}
.dlbutton {
color: #ffffff;
font-size: 34upx;
width: 470upx;
height: 100upx;
background: linear-gradient(-90deg,
rgba(63, 205, 235, 1),
rgba(188, 226, 158, 1));
box-shadow: 0upx 0upx 13upx 0upx rgba(164, 217, 228, 0.2);
border-radius: 50upx;
line-height: 100upx;
text-align: center;
margin-left: auto;
margin-right: auto;
margin-top: 100upx;
}
.dlbutton-hover {
background: linear-gradient(
-90deg,
rgba(63, 205, 235, 0.9),
rgba(188, 226, 158, 0.9)
);
}
.dlbutton-hover {
background: linear-gradient(-90deg,
rgba(63, 205, 235, 0.9),
rgba(188, 226, 158, 0.9));
}
.addition {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
font-size: 30upx;
margin-top: 80upx;
color: #ffa800;
text-align: center;
height: 40upx;
line-height: 40upx;
}
.addition {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
font-size: 30upx;
margin-top: 80upx;
color: #ffa800;
text-align: center;
height: 40upx;
line-height: 40upx;
}
</style>
{
"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" :
......
## 1.2.3(2022-04-16)
提供一个callAsync方法,返回promise简化rpc调用回调模式
## 1.2.2(2022-04-16)
更新readme
## 1.2.1(2022-04-16)
......
......@@ -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");
......
{
"id": "hj-ddp",
"displayName": "DDP协议的Uniapp插件实现【附带MiniMongo Selector】",
"version": "1.2.2",
"version": "1.2.3",
"description": "一个uniapp的DDP客户端实现,助你成为全栈工程师,哈哈~",
"keywords": [
"hj-ddp",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册