app.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // app.js
  2. App({
  3. onLaunch() {
  4. // 初始化云开发环境
  5. if (!wx.cloud) {
  6. console.error('请使用 2.2.3 或以上的基础库以使用云能力')
  7. wx.showToast({
  8. title: '请升级微信开发者工具',
  9. icon: 'none'
  10. })
  11. return
  12. }
  13. // 检查云开发环境是否可用
  14. wx.cloud.init({
  15. appid: 'wxc45a0df13aec69ae', // 请替换为创建云开发环境的小程序AppID
  16. envid: 'cloud1',
  17. traceUser: true,
  18. success: res => {
  19. console.log('云开发初始化成功', res)
  20. // 检查云函数是否可用
  21. wx.cloud.callFunction({
  22. name: 'getTestData',
  23. data: {},
  24. success: () => console.log('云函数连接测试成功'),
  25. fail: err => {
  26. console.error('云函数连接测试失败', err)
  27. wx.showToast({
  28. title: '云函数未部署,请上传云函数',
  29. icon: 'none',
  30. duration: 3000
  31. })
  32. }
  33. })
  34. },
  35. fail: err => {
  36. console.error('云开发初始化失败', err)
  37. let errMsg = '云开发初始化失败'
  38. if (err.errCode === -601002) {
  39. errMsg = '环境ID无效或未开通云服务'
  40. } else if (err.errCode === -502001) {
  41. errMsg = '网络连接失败,请检查网络'
  42. } else if (err.errCode === -601003) {
  43. errMsg = 'AppID与云环境不匹配'
  44. }
  45. wx.showToast({
  46. title: errMsg,
  47. icon: 'none',
  48. duration: 3000
  49. })
  50. }
  51. })
  52. // 展示本地存储能力
  53. const logs = wx.getStorageSync('logs') || []
  54. logs.unshift(Date.now())
  55. wx.setStorageSync('logs', logs)
  56. // 登录
  57. wx.login({
  58. success: res => {
  59. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  60. }
  61. })
  62. },
  63. globalData: {
  64. userInfo: null
  65. }
  66. })