app.js 4.2 KB

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