前端analysis | 3w & 1h

《微信小程序》- 从自建后端迁移到云函数、云数据库实践总结

2020-04-12

小程序代码修改

  • 之前调用服务端代码,修改为调用云函数
  • 新建云函数,并upload部署
  • 云函数,不要求域名配置

添加云函数

  • 之前后端请求的js逻辑,迁移到云函数,
  • 难点就是云函数如何发出get,post请求
  • get请求方案如下:
    1
    2
    3
    4
    5
    # package.json
    "dependencies": {
    "wx-server-sdk": "latest",
    "request-promise": "^4.2.5"
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    # index.js
    // 云函数入口文件
    const cloud = require('wx-server-sdk')
    const req = require("request-promise");
    const prefix = "http://xxxx"

    cloud.init()

    // 云函数入口函数
    exports.main = async (event, context) => {
    let url = `${prefix}`;
    const res = await req(url);
    return {
    res,
    event,
    };
    }

云数据库

  • mysql数据库,如何转换为nosql数据库
    方案一:
    云函数数据库增量,先调用云函数,发现没有,请求后端服务,然后增量添加到数据库中。
    方案二:
    利用云数据库,导入/导出功能,先转换已有数据,批量导入
  • 为应对快速上线更新,采用方案一

storage封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 module.exports = {
/**
* 异步存储,支持promise
*/
setLocalStorage: function (key, data) {
if (!data) { return; }
wx.setStorage({
key: key,
data: data,
})
},
/**
* 同步存储
*/
setLocalStorageSync: function (key, data) {
return wx.setStorageSync(key, data);
},
/**
* 异步获取存储,支持promise
*/
getLocalStorage: function (key) {
return wx.getStorage({
key: key
});
},
/**
* 同步获取
*/
getLocalStorageSync: function (key) {
const res = wx.getStorageSync(key);
console.log(`[本地] storage sync get ${key} 成功`, res);
return res;
},
/**
* 同步删除,会等待
*/
removeStorageSync: function (key) {
return wx.removeStorageSync(key);
},
/**
* 异步删除,返回promise
*/
removeLocalStorage: function (key) {
wx.removeStorage({
key: key
}).then(res => {
console.log(`[本地] storage 删除 ${key} 成功`);
}).catch(err => {
console.error(`[本地] storage 删除 ${key} 失败`);
})
}
}

云端数据库db工具封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
module.exports = {
/**
* params:{
* data:xxxx
* }
*/
addItem: function (dbName, params){
if (!params){
console.error(`[数据库]${dbName} [新增记录],参数空`);
}
const data = {
data: params
}
const db = wx.cloud.database();
return db.collection(dbName).add(data).then((res) => {
console.log(`[数据库]${dbName} [新增记录] 成功,记录 _id:`, params, res._id)
}).catch(err => {
console.error(`[数据库]${dbName} [新增记录] 失败:`, params, err)
})
},
/**
* where:{
* _openid:openid
* }
*/
queryItem:function(dbName,where){
const db = wx.cloud.database();
return new Promise((resolve,reject) => {
db.collection(dbName).where(where).get().then(res => {
const data = res.data;
console.log(`[数据库]${dbName} [查询记录] 成功:`, where, data);
resolve(data)
}).catch(err => {
console.log(`[数据库]${dbName} [查询记录] 失败:`, where, err);
reject(err);
})
});
},
/**
* where:{
* _openid:openid
* }
*/
queryLimitItem: function (dbName, where, { skip, limit }) {
const db = wx.cloud.database();
return new Promise((resolve, reject) => {
db.collection(dbName).where(where).skip(skip).orderBy('subIndex','asc').limit(limit).get().then(res => {
const data = res.data;
console.log(`[数据库]${dbName} [查询记录] 成功:`, where, data);
resolve(data)
}).catch(err => {
console.log(`[数据库]${dbName} [查询记录] 失败:`, where, err);
reject(err);
})
});
},
/**
* where:{
* _openid:openid
* }
* params:{
* data:xxx
* }
*/
updateItem:function(dbName,where,params){
if (!params) {
console.error(`[数据库]${dbName} [更新记录],参数空`);
}
const data = {
data: params
}
const db = wx.cloud.database();
return db.collection(dbName).where(where).update(data).then(res => {
console.log(`[数据库]${dbName} [更新记录] 成功:`, where, params ,res);
}).catch(err => {
console.error(`[数据库]${dbName} [更新记录] 失败:`, where, params, err);
});
},
/**
* where:{
* _openid:openid
* }
*/
deleteItem: function(dbName, where) {
const db = wx.cloud.database();
return db.collection(dbName).where(where).then(res => {
console.log(`[数据库]${dbName} [删除记录]${uniqueId} 成功:`, params, res);
}).catch(err => {
onsole.error(`[数据库]${dbName} [删除记录] ${uniqueId} 失败:`, params, err);
});
},
}
使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏