constants.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**
  2. * 快速开始教程知识点
  3. */
  4. const QuickStartPoints = [
  5. { id: '1', title: '无需搭建服务器,快速构建小程序' },
  6. { id: '2', title: '免登录、免鉴权调用微信开放服务' },
  7. ];
  8. function highlightText(content) {
  9. return `<span> \`${content}\` </span>`;
  10. }
  11. /**
  12. * 快速开始教程步骤
  13. */
  14. const QuickStartSteps = [
  15. {
  16. id: '1',
  17. title: '创建列表页面并初始化数据',
  18. contents: [
  19. {
  20. type: 'text',
  21. content: `编辑教程内置的页面${highlightText('miniprogram/pages/goods-list/index.js')},在${highlightText('Page')}的${highlightText('data')}配置项中添加初始化数据${highlightText('goodsList')},代码如下所示。该页面将用于展示商品列表。`,
  22. },
  23. {
  24. type: 'code',
  25. content: `
  26. Page({
  27. data: {
  28. goodsList: [{
  29. _id: '1',
  30. title: '商品1',
  31. price: 1,
  32. }],
  33. },
  34. })
  35. `,
  36. },
  37. {
  38. type: 'text-link',
  39. content: ['保存文件,','查看页面',',可以看到列表渲染出初始数据。'],
  40. },
  41. ],
  42. },
  43. {
  44. id: '2',
  45. title: '实现并部署一个后台接口',
  46. contents: [
  47. {
  48. type: 'text',
  49. content: `编辑教程内置的后台接口文件${highlightText('cloudfunctions/quickstartFunctions/fetchGoodsList/index.js')},使用下面代码覆盖文件内容,返回一些模拟的商品列表数据。`,
  50. },
  51. {
  52. type: 'code',
  53. content: `
  54. const cloud = require('wx-server-sdk');
  55. cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });
  56. exports.main = async (event, context) => {
  57. return {
  58. dataList: [
  59. { _id: '1', title: '微信气泡徽章', price: 1800 },
  60. { _id: '2', title: '微信地球鼠标垫', price: 5800 },
  61. { _id: '3', title: '微信黄脸大贴纸', price: 500 }
  62. ],
  63. }
  64. };
  65. `
  66. },
  67. {
  68. type: 'text',
  69. content: `保存文件后,在${highlightText('cloudfunctions/quickstartFunctions')}目录右键,选择<span style="color:red;">【上传并部署-云端安装依赖】</span>,等待进度完成,即完成后端接口的开发与部署。`,
  70. },
  71. {
  72. type: 'image',
  73. content: 'function_deploy.png',
  74. },
  75. {
  76. type: 'text',
  77. content: `注:新用户部署时会提示创建云开发环境。<span style="color: red;">新用户可免费开通云开发环境并试用。</span>`,
  78. },
  79. {
  80. type: 'text',
  81. content: `新用户开通环境后在${highlightText('cloudfunctions')}目录右键,选择当前环境为新建的环境。`,
  82. },
  83. {
  84. type: 'image',
  85. content: 'env-select.png',
  86. },
  87. ],
  88. },
  89. {
  90. id: '3',
  91. title: '小程序端调用后台接口',
  92. contents: [
  93. {
  94. type: 'text',
  95. content: `编辑列表页${highlightText('miniprogram/pages/goods-list/index.js')},在 Page 下新增一个方法${highlightText('fetchGoodsList')},用于调用后端接口,并在 Page 的${highlightText('onLoad')}生命周期调用该方法:`,
  96. },
  97. {
  98. type: 'code',
  99. content: `
  100. async fetchGoodsList() {
  101. this.setData({ isLoading: true });
  102. const res = await wx.cloud.callFunction({
  103. name: 'quickstartFunctions',
  104. data: { type: 'fetchGoodsList' },
  105. });
  106. const goodsList = res?.result?.dataList || [];
  107. this.setData({
  108. isLoading: false,
  109. goodsList
  110. });
  111. },
  112. `
  113. },
  114. {
  115. type: 'code',
  116. content: `
  117. onLoad() {
  118. this.fetchGoodsList();
  119. },
  120. `,
  121. },
  122. {
  123. type: 'text-link',
  124. content: ['保存文件后,','查看列表页',',可以看到调用后台接口获取到了模拟数据并正确显示。'],
  125. },
  126. ],
  127. },
  128. {
  129. id: '4',
  130. title: '从数据库中读取真实数据',
  131. contents: [
  132. {
  133. type: 'text',
  134. content: '前面步骤中,后台接口返回的是模拟数据,实际开发中,我们需要利用数据库实现持久存储,下面我们来通过云开发数据库能力实现这个效果。',
  135. },
  136. {
  137. type: 'text',
  138. content: `点击开发者工具顶部的【云开发】按钮,打开云开发控制台,选中【数据库】,新增一个商品集合命名${highlightText('goods')},并添加若干条记录。注:本示例中,集合中的记录请保证具有${highlightText('title')}和${highlightText('price')}字段。`,
  139. },
  140. {
  141. type: 'image',
  142. content: 'scf-enter.png',
  143. },
  144. {
  145. type: 'image',
  146. content: 'database_add.png',
  147. },
  148. {
  149. type: 'text',
  150. content: `编辑后台接口代码${highlightText('cloudfunctions/quickstartFunctions/fetchGoodsList/index.js')},用下面代码覆盖文件内容,用于读取数据库中数据:`,
  151. },
  152. {
  153. type: 'code',
  154. content: `
  155. const cloud = require('wx-server-sdk');
  156. cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });
  157. const db = cloud.database();
  158. exports.main = async (event, context) => {
  159. const result = await db.collection('goods')
  160. .skip(0)
  161. .limit(10)
  162. .get();
  163. return {
  164. dataList: result?.data,
  165. };
  166. };
  167. `,
  168. },
  169. {
  170. type: 'text',
  171. content: `保存文件后,在${highlightText('cloudfunctions/quickstartFunctions')}目录右键,选择<span style="color:red;">【上传并部署-云端安装依赖】</span>,重新部署后台接口。`,
  172. },
  173. {
  174. type: 'text-link',
  175. content: ['','查看页面',',可以看到正确获取数据库中的数据并显示在列表中。'],
  176. },
  177. ],
  178. },
  179. {
  180. id: '5',
  181. title: '调用开放接口生成小程序码',
  182. contents: [
  183. {
  184. type: 'text',
  185. content: '实际小程序开发中,我们通常会对小程序进行传播分享。下面我们利用免鉴权的云调用能力实现小程序码。',
  186. },
  187. {
  188. type: 'text',
  189. content: `编辑教程内置的接口文件${highlightText('cloudfunctions/quickstartFunctions/genMpQrcode/index.js')},用以下代码覆盖文件内容。该接口用于生成小程序码图片并上传到云存储保存。`,
  190. },
  191. {
  192. type: 'code',
  193. content: `
  194. const cloud = require('wx-server-sdk');
  195. cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });
  196. exports.main = async (event, context) => {
  197. const pagePath = event.pagePath;
  198. // 获取小程序二维码的buffer
  199. const resp = await cloud.openapi.wxacode.get({
  200. path: pagePath,
  201. });
  202. const { buffer } = resp;
  203. // 将图片上传云存储空间
  204. const upload = await cloud.uploadFile({
  205. cloudPath: String(pagePath).replaceAll('/', '_') + '.png',
  206. fileContent: buffer
  207. });
  208. return upload.fileID;
  209. };
  210. `,
  211. },
  212. {
  213. type: 'text',
  214. content: `保存文件后,在${highlightText('cloudfunctions/quickstartFunctions')}目录右键,选择【上传并部署-云端安装依赖】,部署该接口。`,
  215. },
  216. {
  217. type: 'text',
  218. content: `编辑商品列表页${highlightText('miniprogram/pages/goods-list/index.js')},在 Page 配置中新增一个方法${highlightText('generateMPCode')},用于调用接口获取小程序码:`,
  219. },
  220. {
  221. type: 'code',
  222. content: `
  223. async generateMPCode() {
  224. wx.showLoading();
  225. const resp = await wx.cloud.callFunction({
  226. name: 'quickstartFunctions',
  227. data: {
  228. type: 'genMpQrcode',
  229. pagePath: 'pages/goods-list/index',
  230. }
  231. });
  232. this.setData({ codeModalVisible: true, codeImageSrc: resp?.result });
  233. wx.hideLoading();
  234. },
  235. `
  236. },
  237. {
  238. type: 'text-link',
  239. content: ['保存文件后,在','商品列表页','点击【分享】按钮,会调用 generateMPCode 方法获取小程序码并弹框显示。'],
  240. },
  241. ],
  242. },
  243. ];
  244. module.exports = {
  245. QuickStartPoints,
  246. QuickStartSteps,
  247. }