Initial Commit

This commit is contained in:
Rain Sun 2019-04-25 08:23:30 +08:00
commit 21d26a3d58
29 changed files with 858 additions and 0 deletions

14
.gitignore vendored Normal file
View File

@ -0,0 +1,14 @@
# Windows
[Dd]esktop.ini
Thumbs.db
$RECYCLE.BIN/
# macOS
.DS_Store
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
# Node.js
node_modules/

12
README.md Normal file
View File

@ -0,0 +1,12 @@
# 云开发 quickstart
这是云开发的快速启动指引,其中演示了如何上手使用云开发的三大基础能力:
- 数据库:一个既可在小程序前端操作,也能在云函数中读写的 JSON 文档型数据库
- 文件存储:在小程序前端直接上传/下载云端文件,在云开发控制台可视化管理
- 云函数:在云端运行的代码,微信私有协议天然鉴权,开发者只需编写业务逻辑代码
## 参考文档
- [云开发文档](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/basis/getting-started.html)

View File

@ -0,0 +1,39 @@
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
const wxContext = cloud.getWXContext()
var commonUpload = async (data) => {
let rightAnswer = []
await db.collection('sD_common').doc('XKg1i1sqTi00tq1N').get().then(res => {
rightAnswer = res.data.rightAnswer //这玩意的返回值莫名其妙会套上一层right但是不影响使用
})
//event.data是对象妈的坑人
for (let i in data) {
i = parseInt(i)
if (data[i] === rightAnswer[i]) {
let object = {}
object[i + 1] = _.inc(1)//不能直接传进去,在外边构造然后传进去
db.collection('sD_common').doc('XKjJSXkPDdDCJ8rz').update({
data: object
}).catch(err => {
return err;
})
}
}
db.collection('sD_common').doc('XKjJSXkPDdDCJ8rz').update({
data: {
total: _.inc(1)
}
}).catch(err => {
return err;
})
}
// 云函数入口函数
exports.main = async (event, context) => {
let common = await commonUpload(event.data);
return common;
}

View File

@ -0,0 +1,14 @@
{
"name": "sD_submit",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "latest"
}
}

View File

@ -0,0 +1,84 @@
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
//评论
var comment = async (event) => {
var res = await db.collection("sM_productInfo").doc(event.prodectId).update({
data:{
comment: db.command.push({
name:event.detailInfo.userName,
avatar:event.detailInfo.avatar,
userid:event.detailInfo._id,
time: Date.now(),
msg: event.msg,
reply: []
})
}
}).then(res => {
return res;
}).catch(err => {
return err;
})
return res;
}
//回复
var reply = async(event) => {
//取出原有的comment加以修改
var oldInfo = await db.collection("sM_productInfo").doc(event.prodectId).get();
oldInfo.data.comment[event.replyNum].reply.push({
name:event.detailInfo.userName,
avatar: event.detailInfo.avatar,
userId: event.detailInfo._id,
msg: event.msg,
time: Date.now(),
})
//放回去
await db.collection("sM_productInfo").doc(event.prodectId).update({
data:{
comment: db.command.set(oldInfo.data.comment)
}
})
return oldInfo.data;
}
//通知
var notice = async (event) => {
//取出,修改
var userInfo = await db.collection('sM_userInfo').doc(event.replyId).get();
//没有就初始化
if(!userInfo.data.newMsg.reply) userInfo.data.newMsg.reply = [];
userInfo.data.newMsg.reply.push({
prodectId:event.prodectId,
prodectName:event.prodectName,
time: Date.now(),
})
//放回去
await db.collection("sM_userInfo").doc(event.replyId).update({
data:{
newMsg: db.command.set(userInfo.data.newMsg)
}
})
return ;
}
// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
if (event.iscomment) {
var res = await comment(event);
} else {
var res = await reply(event);
}
notice(event);
return res;
// return {
// event,
// openid: wxContext.OPENID,
// appid: wxContext.APPID,
// unionid: wxContext.UNIONID,
// }
}

View File

@ -0,0 +1,14 @@
{
"name": "sM_comment",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "latest"
}
}

View File

@ -0,0 +1,60 @@
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
//新建商品返回新商品id
var addProduct = async (event, wxContext) => {
var res = await db.collection("sM_productInfo").add({
data: {
userInfo: {
name: event.detailInfo.userName,
avatar: event.detailInfo.avatar,
adds: event.adds,
id: event.detailInfo._id,
},
time: Date.now(),
productName: event.productName,
details: event.details,
price: event.price,
isfree: event.isfree,
tags: event.tags,
viewNum: 0,
wantUser: [],
photos: event.photos,
comment: [],
warning: false,
}
}).then(res => {
return res
}).catch(err => {
return err
})
return res._id;
}
//把这个新商品id和名称push到用户信息里
var rewriteUserInfo = async (id, wxContext,event) => {
var res = await db.collection('sM_userInfo').doc(event.detailInfo._id).update({
data: {
issue: db.command.push({
id:id,
name: event.productName
})
}
}).then(res => {
return res;
}).catch(err => {
return err;
})
return res;
}
// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
var newId = await addProduct(event, wxContext);
await rewriteUserInfo(newId, wxContext, event);
var res =await db.collection('sM_userInfo').doc(event.detailInfo._id).get();
return res;
}

View File

@ -0,0 +1,14 @@
{
"name": "sM_createProduct",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "latest"
}
}

View File

@ -0,0 +1,17 @@
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database();
// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
var openid = wxContext.OPENID
var userInfo = await db.collection("sM_userInfo").where({
_openid: openid,
}).get()
userInfo = userInfo.data;
if(userInfo.length === 0) return {issign: false, userInfo: null};
else return {isSign: true, userInfo: userInfo};
}

View File

@ -0,0 +1,14 @@
{
"name": "sM_login",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "latest"
}
}

View File

@ -0,0 +1,137 @@
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
//100 时间排序
var timeSort = (status) => {
//101 正序 102 倒序
if (status == 101) return db.collection('sM_productInfo').orderBy('time', 'asc').get();
else return db.collection('sM_productInfo').orderBy('time', 'desc').get();
}
//200 浏览量排序
var viewSort = (status) => {
//201 正序 202 倒序
if (status == 201) return db.collection('sM_productInfo').orderBy('viewNum', 'asc').get();
else return db.collection('sM_productInfo').orderBy('viewNum', 'desc').get();
}
//300 价格排序
var priceSort = (status) => {
//301 正序 302 倒序
if (status == 301) return db.collection('sM_productInfo').orderBy('price', 'asc').get();
else return db.collection('sM_productInfo').orderBy('price', 'desc').get();
}
//400 免费
var freeSort = (status) => {
//401 时间正序 402 时间倒序
if (status == 401) return db.collection('sM_productInfo').where({isfree: true,}).orderBy('time', 'asc').get();
else return db.collection('sM_productInfo').where({ isfree: true, }).orderBy('time', 'desc').get();
}
//500 模糊搜索(默认时间倒序)
var fuzzySearch = (argument) => {
return db.collection('sM_productInfo').where({
productName: db.RegExp({ //使用正则表达式
regexp: argument,
options: 'i', //大小写不区分
})
}).orderBy('time', 'desc').get();
}
//600 标签分类
var tagClassify = async (status) => {
/*
目前的标签分为 610:#图书# 620:#电子# 630:#体育# 640:#乐器# 650:#生活#
*/
var res = null;
switch (parseInt(status / 10)) {
case 61:
// 611 时间正序 612 时间倒序 613 价格正序 614 价格倒序 615 浏览量正序 616 浏览量倒序
if (status = 611) res = await tagSearch("图书","time","asc");
else if (status = 612) res = await tagSearch("图书", "time", "desc");
else if (status = 613) res = await tagSearch("图书", "price", "asc");
else if (status = 614) res = await tagSearch("图书", "price", "desc");
else if (status = 615) res = await tagSearch("图书", "viewNum", "asc");
else if (status = 616) res = await tagSearch("图书","viewNum","desc");
break;
case 62:
// 621 时间正序 622 时间倒序 623 价格正序 624 价格倒序 625 浏览量正序 626 浏览量倒序
if (status = 621) res = await tagSearch("电子", "time", "asc");
else if (status = 622) res = await tagSearch("电子", "time", "desc");
else if (status = 623) res = await tagSearch("电子", "price", "asc");
else if (status = 624) res = await tagSearch("电子", "price", "desc");
else if (status = 625) res = await tagSearch("电子", "viewNum", "asc");
else if (status = 626) res = await tagSearch("电子", "viewNum", "desc");
break;
case 63:
// 631 时间正序 632 时间倒序 633 价格正序 634 价格倒序 635 浏览量正序 636 浏览量倒序
if (status = 631) res = await tagSearch("体育", "time", "asc");
else if (status = 632) res = await tagSearch("体育", "time", "desc");
else if (status = 633) res = await tagSearch("体育", "price", "asc");
else if (status = 634) res = await tagSearch("体育", "price", "desc");
else if (status = 635) res = await tagSearch("体育", "viewNum", "asc");
else if (status = 636) res = await tagSearch("体育", "viewNum", "desc");
break;
case 64:
// 641 时间正序 642 时间倒序 643 价格正序 644 价格倒序 645 浏览量正序 646 浏览量倒序
if (status = 641) res = await tagSearch("乐器", "time", "asc");
else if (status = 642) res = await tagSearch("乐器", "time", "desc");
else if (status = 643) res = await tagSearch("乐器", "price", "asc");
else if (status = 644) res = await tagSearch("乐器", "price", "desc");
else if (status = 645) res = await tagSearch("乐器", "viewNum", "asc");
else if (status = 646) res = await tagSearch("乐器", "viewNum", "desc");
break;
case 65:
// 651 时间正序 652 时间倒序 653 价格正序 654 价格倒序 655 浏览量正序 656 浏览量倒序
if (status = 651) res = await tagSearch("生活", "time", "asc");
else if (status = 652) res = await tagSearch("生活", "time", "desc");
else if (status = 653) res = await tagSearch("生活", "price", "asc");
else if (status = 654) res = await tagSearch("生活", "price", "desc");
else if (status = 655) res = await tagSearch("生活", "viewNum", "asc");
else if (status = 656) res = await tagSearch("生活", "viewNum", "desc");
break;
default:
return false;
}
return res;
}
//标签查询
var tagSearch = (tag,type,order) => {
return db.collection('sM_productInfo').where({
tag:tag
}).orderBy(type, order).get();
}
// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
var res;
switch (parseInt(event.status / 100)) {
case 1:
res = await timeSort(event.status);
break;
case 2:
res = await viewSort(event.status);
break;
case 3:
res = await priceSort(event.status);
break;
case 4:
res = await freeSort(event.status);
break;
case 5:
res = await fuzzySearch(event.argument);
break;
case 6:
res = await tagClassify(event.status);
break;
default:
res = false;
}
return res;
}

View File

@ -0,0 +1,14 @@
{
"name": "sM_searchProductList",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "latest"
}
}

99
miniprogram/api/api.js Normal file
View File

@ -0,0 +1,99 @@
wx.cloud.callFunction({
name: 'sM_createProduct',
data: {
adds: '',
productName: '',
details: '',
price: 0,
isfree: true,
tags: '',
photos: ["", ""],
detailInfo:detailInfo
}
}).then(res => {
console.log(res);
}).catch(err => {
console.log(err);
})
wx.cloud.callFunction({
name: 'sM_searchProductList',
data: {
status: 621,
argument: "台灯", //仅在使用500模糊查询时使用
}
}).then(res => {
console.log(res.result)
}).catch(err => {
console.log(err)
})
//注册
db.collection("sM_userInfo").add({
data: {
userName: userInfo.nickName,
avatar: userInfo.avatarUrl,
phone: "15143211127",
wxid: "15143211127",
adds: "南区二舍",
want: [],
issue: [],
newMsg: {
want: [],
reply: []
},
warning: [],
}
}).then(res => {
console.log(res);
}).catch(err => {
console.log(err);
})
//登录
wx.cloud.callFunction({
name: "sM_login"
}).then(res => {
console.log(res.result.userInfo[0])
detailInfo = res;
}).catch(err => {
console.log(err)
})
//comment 前端判断是回复还是评论
var iscomment = true;
wx.cloud.callFunction({
name: 'sM_comment',
data: {
iscomment:iscomment,
detailInfo:detailInfo,
prodectId:"96c1cbbe5cbfbcd80599b5253d4fe6ba",
msg:"小东西真精致",
}
}).then(res => {
console.log(res.result)
}).catch(err => {
console.log(err)
})
//comment 前端判断是回复还是评论
var iscomment = false,replyName = "英树",replyNum = 1, replyId = 'ee3099285cc0247005d90def39a28f1b', prodectId = "96c1cbbe5cbfbcd80599b5253d4fe6ba", msg = '小东西真精致', prodectName = "大碗宽面";
wx.cloud.callFunction({
name: 'sM_comment',
data: {
iscomment:iscomment,
detailInfo:detailInfo,
prodectId: prodectId,
msg:'@'+replyName+':'+msg,
replyNum : replyNum,
replyId : replyId,
prodectName: prodectName,
}
}).then(res => {
console.log(res.result) //新的当前商品信息
}).catch(err => {
console.log(err)
})

38
miniprogram/app.js Normal file
View File

@ -0,0 +1,38 @@
//app.js
App({
onLaunch: function () {
var userInfo = null, that = this, detailInfo = null;
wx.getSetting({
success(res) {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称
wx.getUserInfo({
success(res) {
userInfo = res.userInfo
wx.setStorageSync('userInfo', res.userInfo);
wx.cloud.init({
traceUser: true,
})
const db = wx.cloud.database();
console.log(userInfo)
//登录
wx.cloud.callFunction({
name: "sM_login"
}).then(res => {
console.log(res.result.userInfo[0])
detailInfo = res.result.userInfo[0];
}).catch(err => {
console.log(err)
})
}
})
}
}
})
this.globalData = {}
}
})

12
miniprogram/app.json Normal file
View File

@ -0,0 +1,12 @@
{
"pages": [
"components/homePage/homePage"
],
"window": {
"backgroundColor": "#F6F6F6",
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#ffffff",
"navigationBarTitleText": "二手交易",
"navigationBarTextStyle": "black"
}
}

7
miniprogram/app.wxss Normal file
View File

@ -0,0 +1,7 @@
/**app.wxss**/
.container {
display: flex;
flex-direction: column;
align-items: center;
box-sizing: border-box;
}

View File

@ -0,0 +1,72 @@
// components/homePage/homePage.js
Page({
/**
* 页面的初始数据
*/
data: {
canIUse: wx.canIUse('button.open-type.getUserInfo'),
userInfo:null,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
bindGetUserInfo(e) {
console.log(e.detail.userInfo)
}
})

View File

@ -0,0 +1,3 @@
{
"usingComponents": {}
}

View File

@ -0,0 +1,8 @@
<button
wx:if="{{canIUse}}"
open-type="getUserInfo"
bindgetuserinfo="bindGetUserInfo"
>
授权登录
</button>

View File

@ -0,0 +1 @@
/* components/homePage/homePage.wxss */

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 143 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 139 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View File

@ -0,0 +1,144 @@
page {
background: #f6f6f6;
display: flex;
flex-direction: column;
justify-content: flex-start;
}
.list {
margin-top: 40rpx;
height: auto;
width: 100%;
background: #fff;
padding: 0 40rpx;
border: 1px solid rgba(0, 0, 0, 0.1);
border-left: none;
border-right: none;
transition: all 300ms ease;
display: flex;
flex-direction: column;
align-items: flex-start;
box-sizing: border-box;
}
.list-item {
width: 100%;
padding: 0;
line-height: 104rpx;
font-size: 34rpx;
color: #007aff;
border-top: 1px solid rgba(0, 0, 0, 0.1);
display: flex;
flex-direction: row;
align-content: center;
justify-content: space-between;
box-sizing: border-box;
}
.list-item:first-child {
border-top: none;
}
.list-item image {
max-width: 100%;
max-height: 20vh;
margin: 20rpx 0;
}
.request-text {
color: #222;
padding: 20rpx 0;
font-size: 24rpx;
line-height: 36rpx;
word-break: break-all;
}
.guide {
width: 100%;
padding: 40rpx;
box-sizing: border-box;
display: flex;
flex-direction: column;
}
.guide .headline {
font-size: 34rpx;
font-weight: bold;
color: #555;
line-height: 40rpx;
}
.guide .p {
margin-top: 20rpx;
font-size: 28rpx;
line-height: 36rpx;
color: #666;
}
.guide .code {
margin-top: 20rpx;
font-size: 28rpx;
line-height: 36rpx;
color: #666;
background: white;
white-space: pre;
}
.guide .code-dark {
margin-top: 20rpx;
background: rgba(0, 0, 0, 0.8);
padding: 20rpx;
font-size: 28rpx;
line-height: 36rpx;
border-radius: 6rpx;
color: #fff;
white-space: pre
}
.guide image {
max-width: 100%;
}
.guide .image1 {
margin-top: 20rpx;
max-width: 100%;
width: 356px;
height: 47px;
}
.guide .image2 {
margin-top: 20rpx;
width: 264px;
height: 100px;
}
.guide .flat-image {
height: 100px;
}
.guide .code-image {
max-width: 100%;
}
.guide .copyBtn {
width: 180rpx;
font-size: 20rpx;
margin-top: 16rpx;
margin-left: 0;
}
.guide .nav {
margin-top: 50rpx;
display: flex;
flex-direction: row;
align-content: space-between;
}
.guide .nav .prev {
margin-left: unset;
}
.guide .nav .next {
margin-right: unset;
}

41
project.config.json Normal file
View File

@ -0,0 +1,41 @@
{
"miniprogramRoot": "miniprogram/",
"cloudfunctionRoot": "cloudfunctions/",
"setting": {
"urlCheck": true,
"es6": true,
"postcss": true,
"minified": true,
"newFeature": true
},
"appid": "wx0df150c438e4c8f0",
"projectname": "schoolMarket",
"libVersion": "2.2.5",
"condition": {
"search": {
"current": -1,
"list": []
},
"conversation": {
"current": -1,
"list": []
},
"plugin": {
"current": -1,
"list": []
},
"game": {
"list": []
},
"miniprogram": {
"current": 0,
"list": [
{
"id": -1,
"name": "db guide",
"pathName": "pages/databaseGuide/databaseGuide"
}
]
}
}
}