index.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. Page({
  2. data: {
  3. goodsList: [
  4. {
  5. _id: "1",
  6. title: "商品1",
  7. price: 1,
  8. },
  9. ],
  10. showTip: false,
  11. title: "",
  12. content: "",
  13. },
  14. onLoad() {
  15. // this.fetchGoodsList();
  16. },
  17. async fetchGoodsList() {
  18. this.setData({ isLoading: true });
  19. try {
  20. const res = await wx.cloud.callFunction({
  21. name: "quickstartFunctions",
  22. data: { type: "fetchGoodsList" },
  23. });
  24. const goodsList = res?.result?.dataList || [];
  25. this.setData({
  26. isLoading: false,
  27. goodsList,
  28. });
  29. } catch (e) {
  30. const { errCode, errMsg } = e;
  31. if (errMsg.includes("Environment not found")) {
  32. this.setData({
  33. showTip: true,
  34. title: "云开发环境未找到",
  35. content:
  36. "如果已经开通云开发,请检查环境ID与 `miniprogram/app.js` 中的 `env` 参数是否一致。",
  37. });
  38. return;
  39. }
  40. if (errMsg.includes("FunctionName parameter could not be found")) {
  41. this.setData({
  42. showTip: true,
  43. title: "请上传云函数",
  44. content:
  45. "按照教程指引更新云函数,保存完成后,在'cloudfunctions/quickstartFunctions'目录右键,选择【上传并部署-云端安装依赖】,等待云函数上传完成后重试。",
  46. });
  47. return;
  48. }
  49. }
  50. },
  51. async generateMPCode() {
  52. wx.showLoading();
  53. try {
  54. const resp = await wx.cloud.callFunction({
  55. name: "quickstartFunctions",
  56. data: {
  57. type: "genMpQrcode",
  58. pagePath: "pages/goods-list/index",
  59. },
  60. });
  61. this.setData({ codeModalVisible: true, codeImageSrc: resp?.result });
  62. wx.hideLoading();
  63. } catch (e) {
  64. wx.hideLoading();
  65. const { errCode, errMsg } = e;
  66. if (errMsg.includes("Environment not found")) {
  67. this.setData({
  68. showTip: true,
  69. title: "云开发环境未找到",
  70. content:
  71. "如果已经开通云开发,请检查环境ID与 `miniprogram/app.js` 中的 `env` 参数是否一致。",
  72. });
  73. return;
  74. }
  75. if (errMsg.includes("FunctionName parameter could not be found")) {
  76. this.setData({
  77. showTip: true,
  78. title: "请上传云函数",
  79. content:
  80. "按照教程指引更新云函数,保存完成后,在'cloudfunctions/quickstartFunctions'目录右键,选择【上传并部署-云端安装依赖】,等待云函数上传完成后重试。",
  81. });
  82. return;
  83. }
  84. }
  85. },
  86. });