app.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //app.js
  2. App({
  3. onLaunch: function () {
  4. // 展示本地存储能力
  5. var logs = wx.getStorageSync('logs') || []
  6. logs.unshift(Date.now())
  7. wx.setStorageSync('logs', logs)
  8. // 登录
  9. wx.login({
  10. success: res => {
  11. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  12. }
  13. })
  14. // 获取用户信息
  15. wx.getSetting({
  16. success: res => {
  17. if (res.authSetting['scope.userInfo']) {
  18. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  19. wx.getUserInfo({
  20. success: res => {
  21. // 可以将 res 发送给后台解码出 unionId
  22. this.globalData.userInfo = res.userInfo
  23. const db = wx.cloud.database()
  24. db.collection("userInfoData").get({
  25. success: function (user) {
  26. if (user.data.length == 0) {
  27. db.collection("userInfoData").add({
  28. data: {
  29. publisherId: []
  30. },
  31. success: function () {
  32. this.globalData.publisherId = []
  33. if (this.userInfoReadyCallback) {
  34. this.userInfoReadyCallback(res)
  35. }
  36. }.bind(this)
  37. })
  38. } else {
  39. this.globalData.publisherId = user.data[0].publisherId
  40. if (this.userInfoReadyCallback) {
  41. this.userInfoReadyCallback(res)
  42. }
  43. }
  44. }.bind(this)
  45. })
  46. }
  47. })
  48. }
  49. }
  50. })
  51. if (!wx.cloud) {
  52. console.error('请使用 2.2.3 或以上的基础库以使用云能力')
  53. } else {
  54. wx.cloud.init({
  55. // env 参数说明:
  56. // env 参数决定接下来小程序发起的云开发调用(wx.cloud.xxx)会默认请求到哪个云环境的资源
  57. // 此处请填入环境 ID, 环境 ID 可打开云控制台查看
  58. // 如不填则使用默认环境(第一个创建的环境)
  59. env: "msg-push-9ga0xxc9c4aa5242",
  60. traceUser: true,
  61. })
  62. wx.cloud.callFunction({
  63. name: "getOpenId",
  64. success: function (res) {
  65. this.globalData.openId = res.result.openId
  66. }.bind(this)
  67. })
  68. }
  69. },
  70. globalData: {
  71. userInfo: null,
  72. openId: null,
  73. publisherId: null,
  74. noticeIndex: null,
  75. vibrate: null
  76. }
  77. })