app.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //app.js
  2. import {
  3. initSocket
  4. } from './utils/socket-handle'
  5. import {
  6. loadToken, loadUserInfo
  7. } from './utils/storage'
  8. import ImApi from './apis/im'
  9. import eventEmitter from './utils/eventEmitter'
  10. import AppIMDelegate from "./delegate/app-im-delegate";
  11. import { fotmatDate } from './utils/date'
  12. import { autoSubcrebe } from './utils/utils'
  13. var QQMapWX = require('/utils/qqmap-wx-jssdk.min.js')
  14. var timer = null
  15. App({
  16. onLaunch: function (options) {
  17. //检查是否存在新版本
  18. this.appIMDelegate = new AppIMDelegate(this);
  19. this.appIMDelegate.onLaunch(options);
  20. this.globalData.qqmapsdk = new QQMapWX({
  21. key: 'RG7BZ-G2KRV-XWPP2-U5GWL-WWQPF-RIBUW'
  22. });
  23. if (this.globalData.token) {
  24. wx.nextTick((callback => {
  25. this.getContact()
  26. }))
  27. }
  28. },
  29. /**
  30. * 当小程序启动,或从后台进入前台显示,会触发 onShow
  31. */
  32. onShow: function (options) {
  33. setTimeout(() => {
  34. this.appIMDelegate.onShow()
  35. }, 1000)
  36. },
  37. SetProvinceCity(province, city, district) {
  38. this.globalData.province = province;
  39. this.globalData.city = city;
  40. this.globalData.district = district;
  41. this.globalData.raw_city = false;
  42. },
  43. wxshowloading(title) {
  44. wx.showLoading({
  45. title: title,
  46. mask: true
  47. });
  48. },
  49. /**
  50. * 当小程序从前台进入后台,会触发 onHide
  51. */
  52. onHide: function () {
  53. this.appIMDelegate.onHide();
  54. },
  55. /**
  56. * 当小程序发生脚本错误,或者 api 调用失败时,会触发 onError 并带上错误信息
  57. */
  58. onError: function (msg) {
  59. console.log("onError")
  60. },
  61. ShowToast(title) {
  62. // Toast提示封装 app.showToast('')
  63. wx.showToast({
  64. title: title,
  65. icon: 'none',
  66. duration: 2000,
  67. })
  68. },
  69. ShowModel(title, content) {
  70. // Model 封装 app.ShowModel('')
  71. wx.showModal({
  72. title: title,
  73. content: content,
  74. showCancel: false,
  75. });
  76. },
  77. InterError(res) {
  78. this.ShowModel('网络错误', '请稍后再试~');
  79. wx.hideLoading()
  80. },
  81. makePhoneCall(phone) {
  82. wx.makePhoneCall({
  83. phoneNumber: phone
  84. })
  85. },
  86. getIMHandler() {
  87. return this.appIMDelegate.getIMHandlerDelegate();
  88. },
  89. async getNewMessage (msg) {
  90. const { conversations } = this.globalData
  91. let item = conversations.find(item => item.id === msg.fromId)
  92. if (item) {
  93. item.latestMsgContent = msg.content
  94. item.latestMsgTime = msg.sendTime
  95. item.unReadNum ? item.unReadNum++ : item.unReadNum = 1
  96. this.globalData.conversations = this.dealConversations(conversations)
  97. } else {
  98. await this.getContact(true)
  99. }
  100. },
  101. getContact(isNewMsg) {
  102. return ImApi.getContacts().then(res => {
  103. const friends = res.data.friends
  104. const {
  105. conversations
  106. } = this.globalData
  107. this.globalData.unViewMsg = 0
  108. this.globalData.conversations = this.dealConversations(friends.map(item => {
  109. const con = conversations.find(con => con.id === item.id)
  110. if (con) {
  111. item = Object.assign(con, item)
  112. } else if (isNewMsg){
  113. item.unReadNum = 1
  114. }
  115. this.globalData.unViewMsg += item.unReadNum
  116. return item
  117. }))
  118. })
  119. },
  120. dealConversations(conversations) {
  121. return conversations.map(item => {
  122. let content = item.latestMsgContent
  123. try {
  124. let parseContent = JSON.parse(content)
  125. if (parseContent.house_name) {
  126. content = `【云带看】${JSON.parse(content).house_name}`
  127. } else if (parseContent.duration) {
  128. content = '【语音】'
  129. } else if (parseContent.content) {
  130. content = '【图片】'
  131. }
  132. } catch (err) {}
  133. item.latestMsgContent = content
  134. const now = new Date()
  135. if (now.getMonth() === new Date(item.latestMsgTime).getMonth() && now.getDate() === new Date(item.latestMsgTime).getDate()) {
  136. item.timeStr = fotmatDate(item.latestMsgTime, 'hh:mm')
  137. } else {
  138. item.timeStr = fotmatDate(item.latestMsgTime, 'MM/dd')
  139. }
  140. return item
  141. }).sort((a, b) => new Date(b.latestMsgTime) - new Date(a.latestMsgTime))
  142. },
  143. autoSubcrebe () {
  144. return autoSubcrebe()
  145. },
  146. globalData: {
  147. token: loadToken() ,
  148. raw_city: true,
  149. city: '珠海', // 默认进入首页的地址
  150. province: '广东省',
  151. district: '',
  152. userinfo: loadUserInfo() || {} ,
  153. io: '',
  154. messageList: [],
  155. conversations: [],
  156. unViewMsg: 0
  157. }
  158. });