app.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. console.log(this.globalData.userInfo)
  24. const db = wx.cloud.database()
  25. db.collection("userInfoData").get({
  26. success: function (user) {
  27. if (user.data.length == 0) {
  28. db.collection("userInfoData").add({
  29. data: {
  30. publisherId: []
  31. },
  32. success: function () {
  33. this.globalData.publisherId = []
  34. if (this.userInfoReadyCallback) {
  35. this.userInfoReadyCallback(res)
  36. }
  37. }.bind(this)
  38. })
  39. } else {
  40. this.globalData.publisherId = user.data[0].publisherId
  41. if (this.userInfoReadyCallback) {
  42. this.userInfoReadyCallback(res)
  43. }
  44. }
  45. }.bind(this)
  46. })
  47. }
  48. })
  49. }
  50. }
  51. })
  52. if (!wx.cloud) {
  53. console.error('请使用 2.2.3 或以上的基础库以使用云能力')
  54. } else {
  55. wx.cloud.init({
  56. // env 参数说明:
  57. // env 参数决定接下来小程序发起的云开发调用(wx.cloud.xxx)会默认请求到哪个云环境的资源
  58. // 此处请填入环境 ID, 环境 ID 可打开云控制台查看
  59. // 如不填则使用默认环境(第一个创建的环境)
  60. env: "msg-push-9ga0xxc9c4aa5242",
  61. traceUser: true,
  62. })
  63. wx.cloud.callFunction({
  64. name: "getOpenId",
  65. success: function (res) {
  66. this.globalData.openId = res.result.openId
  67. }.bind(this)
  68. })
  69. }
  70. },
  71. globalData: {
  72. userInfo: null,
  73. openId: null,
  74. publisherId: null,
  75. noticeIndex: null,
  76. vibrate: null
  77. }
  78. })