app.js 5.5 KB

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