index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // pages/exampleDetail/index.js
  2. Page({
  3. data: {
  4. type: '',
  5. envId: '',
  6. showTip: false,
  7. title:"",
  8. content:"",
  9. haveGetOpenId: false,
  10. openId: '',
  11. haveGetCodeSrc: false,
  12. codeSrc: '',
  13. haveGetRecord: false,
  14. record: '',
  15. haveGetImgSrc: false,
  16. imgSrc: '',
  17. },
  18. onLoad(options) {
  19. this.setData({ type: options?.type, envId: options?.envId });
  20. },
  21. getOpenId() {
  22. wx.showLoading({
  23. title: '',
  24. });
  25. wx.cloud.callFunction({
  26. name: 'quickstartFunctions',
  27. data: {
  28. type: 'getOpenId'
  29. }
  30. }).then((resp) => {
  31. this.setData({
  32. haveGetOpenId: true,
  33. openId: resp.result.openid
  34. });
  35. wx.hideLoading();
  36. }).catch((e) => {
  37. wx.hideLoading();
  38. const {errCode,errMsg}=e
  39. if(errMsg.includes('Environment not found')){
  40. this.setData({
  41. showTip: true,
  42. title:"云开发环境未找到",
  43. content:"如果已经开通云开发,请检查环境ID与 `miniprogram/app.js` 中的 `env` 参数是否一致。"
  44. });
  45. return
  46. }
  47. if(errMsg.includes('FunctionName parameter could not be found')){
  48. this.setData({
  49. showTip: true,
  50. title:"请上传云函数",
  51. content:"在'cloudfunctions/quickstartFunctions'目录右键,选择【上传并部署-云端安装依赖】,等待云函数上传完成后重试。"
  52. });
  53. return
  54. }
  55. });
  56. },
  57. clearOpenId() {
  58. this.setData({
  59. haveGetOpenId: false,
  60. openId: ''
  61. });
  62. },
  63. getCodeSrc() {
  64. wx.showLoading({
  65. title: '',
  66. });
  67. wx.cloud.callFunction({
  68. name: 'quickstartFunctions',
  69. data: {
  70. type: 'getMiniProgramCode'
  71. }
  72. }).then((resp) => {
  73. this.setData({
  74. haveGetCodeSrc: true,
  75. codeSrc: resp.result
  76. });
  77. wx.hideLoading();
  78. }).catch((e) => {
  79. wx.hideLoading();
  80. const {errCode,errMsg}=e
  81. if(errMsg.includes('Environment not found')){
  82. this.setData({
  83. showTip: true,
  84. title:"云开发环境未找到",
  85. content:"如果已经开通云开发,请检查环境ID与 `miniprogram/app.js` 中的 `env` 参数是否一致。"
  86. });
  87. return
  88. }
  89. if(errMsg.includes('FunctionName parameter could not be found')){
  90. this.setData({
  91. showTip: true,
  92. title:"请上传云函数",
  93. content:"在'cloudfunctions/quickstartFunctions'目录右键,选择【上传并部署-云端安装依赖】,等待云函数上传完成后重试。"
  94. });
  95. return
  96. }
  97. });
  98. },
  99. clearCodeSrc() {
  100. this.setData({
  101. haveGetCodeSrc: false,
  102. codeSrc: ''
  103. });
  104. },
  105. getRecord() {
  106. wx.showLoading({
  107. title: '',
  108. });
  109. wx.cloud.callFunction({
  110. name: 'quickstartFunctions',
  111. data: {
  112. type: 'selectRecord'
  113. }
  114. }).then((resp) => {
  115. this.setData({
  116. haveGetRecord: true,
  117. record: resp.result.data
  118. });
  119. wx.hideLoading();
  120. }).catch((e) => {
  121. this.setData({
  122. showTip: true
  123. });
  124. wx.hideLoading();
  125. });
  126. },
  127. clearRecord() {
  128. this.setData({
  129. haveGetRecord: false,
  130. record: ''
  131. });
  132. },
  133. uploadImg() {
  134. wx.showLoading({
  135. title: '',
  136. });
  137. // 让用户选择一张图片
  138. wx.chooseImage({
  139. count: 1,
  140. success: chooseResult => {
  141. // 将图片上传至云存储空间
  142. wx.cloud.uploadFile({
  143. // 指定上传到的云路径
  144. cloudPath: 'my-photo.png',
  145. // 指定要上传的文件的小程序临时文件路径
  146. filePath: chooseResult.tempFilePaths[0],
  147. }).then(res => {
  148. this.setData({
  149. haveGetImgSrc: true,
  150. imgSrc: res.fileID
  151. });
  152. wx.hideLoading();
  153. }).catch((e) => {
  154. wx.hideLoading();
  155. });
  156. },
  157. });
  158. },
  159. clearImgSrc() {
  160. this.setData({
  161. haveGetImgSrc: false,
  162. imgSrc: ''
  163. });
  164. },
  165. goOfficialWebsite() {
  166. const url = 'https://docs.cloudbase.net/toolbox/quick-start';
  167. wx.navigateTo({
  168. url: `../web/index?url=${url}`,
  169. });
  170. },
  171. })