库存管理SaaS小程序的开发目的是让中小企业可以使用小程序来管理库存,省去开发、部署系统的成本。
为了让同一个公司的用户能看见共享的数据,他们需要有一个共同的companyId,具体的实现方法是,某一个用户注册一个公司,然后将companyId分享给其他人加入(将companyId作为邀请码,或者生成二维码供用户扫码),然后这些人成了同一个公司的用户,相互之间能看到该看到的数据,实现库存数据共享。
注册公司代码:
confirmRegist() {
let that = this
//检查重名
companyCollection.where({
name: that.data.regCompanyName
}).get()
.then(res => {
if (res.data.length > 0) {
wx.showToast({
title: '公司名已存在!',
icon: 'error'
})
return false
} else {
companyCollection.add({
data: {
name: that.data.regCompanyName,
status: 1,
enableJoin: true //用于控制是否允许加入公司,防止非本公司人员使用邀请码加入
}
}).then(res => {
let joinTime = new Date().getTime()
// 将公司写入用户所属公司列表,用于一个用户可以加入多个公司/组织
let companys = that.data.user.companys ? that.data.user.companys : []
const permission = { sales: true, po: true, product: true, inventory: true, report: true, invOpt: true }
companys.push({ name: that.data.regCompanyName, id: res._id, userType: 1, vip: true, permission, joinTime })
//更新用户信息,用户当前可操作的公司
userCollection.doc(that.data.user._id).update({
data: {
companyId: res._id,
companyName: that.data.regCompanyName,
joinTime,
userType: 1,
vip: true,
companys
}
})
wx.showToast({
title: '注册成功!',
})
setTimeout(() => {
wx.reLaunch({
url: '../index/index',
})
}, 300);
})
}
})
},
加入公司代码:
confirmJoin() {
let that = this
// 检查是否加过了
let companys = that.data.user.companys ? that.data.user.companys : []
let isExist = false
let myIndex = 0
companys.map((item, index) => {
if (item.id == that.data.joinCompanyId) {
isExist = true
myIndex = index
}
})
// 如果已经是成员,切换公司
if (isExist) {
userCollection.doc(that.data.user._id).update({
data: {
companyId: that.data.joinCompanyId,
companyName: companys[myIndex].name,
userType: companys[myIndex].userType,
vip: companys[myIndex].vip,
permission: companys[myIndex].permission
}
})
.then(res => {
wx.reLaunch({
url: '/pages/index/index',
})
})
return false
}
//检查公司是否存在
companyCollection.where({
_id: that.data.joinCompanyId
}).get()
.then(res => {
if (res.data.length <= 0) {
wx.showToast({
title: '邀请码不存在!',
icon: 'error'
})
return false
}
else if (res.data[0].enableJoin == false) {
wx.showToast({
title: '不允许加入!',
icon: 'error'
})
return false
}
else {
//加入公司
let joinTime = new Date().getTime()
// 将公司写入用户所属公司列表
let companys = that.data.user.companys ? that.data.user.companys : []
const permission = { sales: true, po: true, product: true, inventory: true, report: true, invOpt: true }
companys.push({ name: res.data[0].name, id: res.data[0]._id, userType: 2, vip: false, permission, joinTime })
userCollection.doc(that.data.user._id).update({
data: {
companyId: res.data[0]._id,
companyName: res.data[0].name,
joinTime: joinTime,
userType: 2,
vip: false,
companys,
permission
}
})
wx.showToast({
title: '加入成功!',
})
wx.reLaunch({
url: '../index/index',
})
}
})
},
以上就是库存管理SaaS小程序的公司注册和输入邀请码加入功能,下一篇介绍管理员生成二维码供用户扫码加入的更为便捷的方式。
功能演示,可以在微信中搜索小程序“库存小管家”。