room.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const app = getApp()
  2. Page({
  3. data: {
  4. avatarUrl: './user-unlogin.png',
  5. userInfo: null,
  6. logged: false,
  7. takeSession: false,
  8. requestResult: '',
  9. // chatRoomEnvId: 'release-f8415a',
  10. chatRoomCollection: 'chatroom',
  11. chatRoomGroupId: 'demo',
  12. chatRoomGroupName: '聊天室',
  13. // functions for used in chatroom components
  14. onGetUserInfo: null,
  15. getOpenID: null,
  16. },
  17. onLoad: function() {
  18. // 获取用户信息
  19. wx.getSetting({
  20. success: res => {
  21. if (res.authSetting['scope.userInfo']) {
  22. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  23. wx.getUserInfo({
  24. success: res => {
  25. this.setData({
  26. avatarUrl: res.userInfo.avatarUrl,
  27. userInfo: res.userInfo
  28. })
  29. }
  30. })
  31. }
  32. }
  33. })
  34. this.setData({
  35. onGetUserInfo: this.onGetUserInfo,
  36. getOpenID: this.getOpenID,
  37. })
  38. wx.getSystemInfo({
  39. success: res => {
  40. console.log('system info', res)
  41. if (res.safeArea) {
  42. const { top, bottom } = res.safeArea
  43. this.setData({
  44. containerStyle: `padding-top: ${(/ios/i.test(res.system) ? 10 : 20) + top}px; padding-bottom: ${20 + res.windowHeight - bottom}px`,
  45. })
  46. }
  47. },
  48. })
  49. },
  50. getOpenID: async function() {
  51. if (this.openid) {
  52. return this.openid
  53. }
  54. const { result } = await wx.cloud.callFunction({
  55. name: 'login',
  56. })
  57. return result.openid
  58. },
  59. onGetUserInfo: function(e) {
  60. if (!this.logged && e.detail.userInfo) {
  61. this.setData({
  62. logged: true,
  63. avatarUrl: e.detail.userInfo.avatarUrl,
  64. userInfo: e.detail.userInfo
  65. })
  66. }
  67. },
  68. onShareAppMessage() {
  69. return {
  70. title: '即时通信 Demo',
  71. path: '/pages/im/room/room',
  72. }
  73. },
  74. })