index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // index.js
  2. Page({
  3. data: {
  4. showTip: false,
  5. powerList: [
  6. {
  7. title: '云函数',
  8. tip: '安全、免鉴权运行业务代码',
  9. showItem: false,
  10. item: [
  11. {
  12. type: 'getOpenId',
  13. title: '获取OpenId',
  14. },
  15. {
  16. type: 'getMiniProgramCode',
  17. title: '生成小程序码',
  18. },
  19. ],
  20. },
  21. {
  22. title: '数据库',
  23. tip: '安全稳定的文档型数据库',
  24. showItem: false,
  25. item: [
  26. {
  27. type: 'createCollection',
  28. title: '创建集合',
  29. },
  30. {
  31. type: 'selectRecord',
  32. title: '查询记录',
  33. },
  34. {
  35. title: '更新记录',
  36. page: 'updateRecord',
  37. },
  38. {
  39. title: '聚合操作',
  40. page: 'sumRecord',
  41. },
  42. ],
  43. },
  44. {
  45. title: '云存储',
  46. tip: '自带CDN加速文件存储',
  47. showItem: false,
  48. item: [
  49. {
  50. type: 'uploadFile',
  51. title: '上传文件',
  52. },
  53. ],
  54. },
  55. {
  56. type: 'singleTemplate',
  57. title: '云模板',
  58. tip: '基于页面模板,快速配置、搭建小程序页面',
  59. tag: 'new',
  60. },
  61. {
  62. type: 'cloudBackend',
  63. title: '云后台',
  64. tip: '开箱即用的小程序后台管理系统',
  65. },
  66. {
  67. title: '云托管',
  68. tip: '不限语言的全托管容器服务',
  69. link: 'https://cloud.weixin.qq.com/cloudrun',
  70. },
  71. ],
  72. haveCreateCollection: false,
  73. title: "",
  74. content: ""
  75. },
  76. onClickPowerInfo(e) {
  77. const index = e.currentTarget.dataset.index;
  78. const powerList = this.data.powerList;
  79. const selectedItem = powerList[index];
  80. if (selectedItem.link) {
  81. wx.navigateTo({
  82. url: `../web/index?url=${selectedItem.link}&title=${selectedItem.title}`,
  83. });
  84. } else if (selectedItem.type) {
  85. wx.navigateTo({
  86. url: `/pages/exampleDetail/index?envId=${this.data.selectedEnv?.envId}&type=${selectedItem.type}`,
  87. });
  88. } else if (selectedItem.page) {
  89. wx.navigateTo({
  90. url: `/pages/${selectedItem.page}/index`,
  91. });
  92. } else if (
  93. selectedItem.title === '数据库' &&
  94. !this.data.haveCreateCollection
  95. ) {
  96. this.onClickDatabase(powerList,selectedItem);
  97. } else {
  98. selectedItem.showItem = !selectedItem.showItem;
  99. this.setData({
  100. powerList,
  101. });
  102. }
  103. },
  104. jumpPage(e) {
  105. const { type, page } = e.currentTarget.dataset;
  106. if (type) {
  107. wx.navigateTo({
  108. url: `/pages/exampleDetail/index?envId=${this.data.selectedEnv?.envId}&type=${type}`,
  109. });
  110. } else {
  111. wx.navigateTo({
  112. url: `/pages/${page}/index?envId=${this.data.selectedEnv?.envId}`,
  113. });
  114. }
  115. },
  116. onClickDatabase(powerList,selectedItem) {
  117. wx.showLoading({
  118. title: '',
  119. });
  120. wx.cloud
  121. .callFunction({
  122. name: 'quickstartFunctions',
  123. data: {
  124. type: 'createCollection',
  125. },
  126. })
  127. .then((resp) => {
  128. if (resp.result.success) {
  129. this.setData({
  130. haveCreateCollection: true,
  131. });
  132. }
  133. selectedItem.showItem = !selectedItem.showItem;
  134. this.setData({
  135. powerList,
  136. });
  137. wx.hideLoading();
  138. })
  139. .catch((e) => {
  140. wx.hideLoading();
  141. const { errCode, errMsg } = e
  142. if (errMsg.includes('Environment not found')) {
  143. this.setData({
  144. showTip: true,
  145. title: "云开发环境未找到",
  146. content: "如果已经开通云开发,请检查环境ID与 `miniprogram/app.js` 中的 `env` 参数是否一致。"
  147. });
  148. return
  149. }
  150. if (errMsg.includes('FunctionName parameter could not be found')) {
  151. this.setData({
  152. showTip: true,
  153. title: "请上传云函数",
  154. content: "在'cloudfunctions/quickstartFunctions'目录右键,选择【上传并部署-云端安装依赖】,等待云函数上传完成后重试。"
  155. });
  156. return
  157. }
  158. });
  159. },
  160. });