app.js 4.4 KB

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