_action.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import _data from './_data';
  2. import _mixins from './_mixins';
  3. let innerAudioContext = uni.createInnerAudioContext();
  4. export default {
  5. /** 显示状态通知提醒 */
  6. setStatusTips() {
  7. let pages = getCurrentPages();
  8. if (pages.length < 1) {
  9. return;
  10. }
  11. let route = pages[pages.length - 1].route,
  12. /** 只有tabbar页面才更新消息状态 */
  13. routes = [
  14. 'pages/chat/index',
  15. 'pages/friend/index',
  16. 'pages/push/index',
  17. 'pages/my/index'
  18. ];
  19. if (routes.indexOf(route) == -1) {
  20. return;
  21. }
  22. /** 通讯录提示 */
  23. let num = (_data.data('new_friend_tips_num') * 1),
  24. num_ = (_data.data('new_group_tips_num') * 1);
  25. if (num_) {
  26. uni.$emit('data_new_group_apply_tips', num_);
  27. }
  28. if (num) {
  29. uni.$emit('data_new_friend_tips', num);
  30. }
  31. if (num + num_) {
  32. uni.setTabBarBadge({
  33. index: 1,
  34. text: (num + num_ + ''),
  35. });
  36. } else {
  37. uni.removeTabBarBadge({
  38. index: 1
  39. });
  40. }
  41. uni.$emit('unread_concat_msg', num);
  42. /** 会话列表提示 */
  43. num = _data.chatTipsNum();
  44. if (num) {
  45. uni.setTabBarBadge({
  46. index: 0,
  47. text: (num + ''),
  48. });
  49. if (route == 'pages/chat/index') {
  50. uni.setNavigationBarTitle({
  51. title: '消息' + '(' + num + ')',
  52. });
  53. }
  54. } else {
  55. uni.removeTabBarBadge({
  56. index: 0
  57. });
  58. if (route == 'pages/chat/index') {
  59. uni.setNavigationBarTitle({
  60. title: '消息',
  61. });
  62. }
  63. }
  64. uni.$emit('unread_msg', num);
  65. /** 朋友圈提示(优先显示消息条数,再提示好友动态) */
  66. num = _data.data('no_reader_circle_chat_num');
  67. let num2 = _data.data('no_reader_pay_num');
  68. let num3 = parseInt(num) + parseInt(num2);
  69. if (num3) {
  70. uni.setTabBarBadge({
  71. index: 2,
  72. text: (num2 + ''),
  73. });
  74. if (num) uni.$emit('data_circle_tips', num);
  75. if (num2) uni.$emit('data_pay_tips', num2);
  76. } else {
  77. uni.removeTabBarBadge({
  78. index: 2
  79. });
  80. num = _data.data('no_reader_circle');
  81. if (num) {
  82. uni.showTabBarRedDot({
  83. index: 2
  84. });
  85. uni.$emit('data_circle_tips', '好友动态');
  86. } else {
  87. uni.hideTabBarRedDot({
  88. index: 2
  89. });
  90. }
  91. }
  92. },
  93. /** 路由守卫执行方法 */
  94. routeTool() {
  95. _data.data('isOnlie', true);
  96. let token = _data.localData('token');
  97. /** 没有token就跳转到登陆去获得token */
  98. if (!token) {
  99. uni.reLaunch({
  100. url: '/pages/in/login'
  101. });
  102. return;
  103. }
  104. /** 如果没有连接上socket,则连接 */
  105. if (!_data.data('socket_state')) {
  106. _mixins.methods.$reset().$reconnect();
  107. }
  108. },
  109. /** 验证失败后执行 */
  110. checkFail() {
  111. /** 好友申请通知 */
  112. _data.data('new_friend_tips_num', 0);
  113. /** 朋友圈通知 */
  114. _data.data('no_reader_circle', 0);
  115. /** 朋友圈消息未读数 */
  116. _data.data('no_reader_circle_chat_num', 0);
  117. /** 清空自己的头像保存的本地的临时地址 */
  118. let data = _data.data('cache');
  119. console.log("缓存数据",data)
  120. console.log("全部缓存数据",getApp().globalData)
  121. data.local_photo = '';
  122. _data.data('cache', data);
  123. /** 归档用户信息 */
  124. _data.data('user_info', {
  125. id: 0,
  126. nickname: '',
  127. username: '',
  128. photo: 'default_man/90.jpg',
  129. doodling: '',
  130. circle_img: 'default_circle_img.jpg',
  131. });
  132. // 不是APP才清除缓存
  133. // uni.clearStorage();
  134. uni.clearStorageSync();
  135. console.log("全部缓存数据",getApp().globalData)
  136. // /** 跳转到登陆界面 */
  137. // #ifdef APP-PLUS
  138. let pages = getCurrentPages();
  139. let currentPage = pages[pages.length - 1];
  140. let route = currentPage.route;
  141. console.log(route,'pages+++++');
  142. if(route.indexOf('upApp') != -1 || route.indexOf('login') != -1) {
  143. }else {
  144. uni.reLaunch({
  145. url: '/pages/in/login'
  146. });
  147. }
  148. // uni.reLaunch({
  149. // url: '/pages/in/login'
  150. // });
  151. // #endif
  152. },
  153. /** 更新未读消息为0 */
  154. updataNoReader(list_id) {
  155. _mixins.methods.$httpSend({
  156. path: '/im/message/updataNoReader',
  157. data: {
  158. list_id: list_id
  159. },
  160. });
  161. uni.$emit('')
  162. },
  163. /** 下载自己的头像 */
  164. downloadPhoto() {
  165. let url = _data.staticPhoto() + _data.data('user_info').photo;
  166. let data = _data.data('cache');
  167. uni.downloadFile({
  168. url: url,
  169. success: (res) => {
  170. console.log("download:", res)
  171. if (res.statusCode === 200) {
  172. data.local_photo = res.tempFilePath;
  173. if (!data.local_photo) data.local_photo = url
  174. _data.data('cache', data);
  175. }
  176. },
  177. fail(res) {
  178. data.local_photo = url;
  179. console.log(1111)
  180. _data.data('cache', data);
  181. }
  182. });
  183. if (!_data.data('cache').local_photo) {
  184. data.local_photo = url;
  185. console.log("data", data)
  186. _data.data('cache', data);
  187. }
  188. console.log("_cache", _data.data('cache'))
  189. },
  190. /** 播放音效 */
  191. playVoice(path, loop) {
  192. innerAudioContext.src = path;
  193. innerAudioContext.loop = loop;
  194. // innerAudioContext.obeyMuteSwitch = false;
  195. innerAudioContext.play();
  196. innerAudioContext.onPlay(() => {
  197. //console.log('开始播放');
  198. });
  199. innerAudioContext.onError((res) => {
  200. innerAudioContext.destroy();
  201. return;
  202. uni.showToast({
  203. title: '音效播放错误 ->' + JSON.stringify(res),
  204. icon: 'none',
  205. });
  206. });
  207. },
  208. // 停止播放
  209. stopVoice() {
  210. console.log("停止")
  211. innerAudioContext.stop()
  212. },
  213. /** 时间戳转换 */
  214. timestampFormat(timestamp) {
  215. let curTimestamp = parseInt(new Date().getTime() / 1000), //当前时间戳
  216. timestampDiff = curTimestamp - timestamp, // 参数时间戳与当前时间戳相差秒数
  217. curDate = new Date(curTimestamp * 1000), // 当前时间日期对象
  218. tmDate = new Date(timestamp * 1000), // 参数时间戳转换成的日期对象
  219. Y = tmDate.getFullYear(),
  220. m = tmDate.getMonth() + 1,
  221. d = tmDate.getDate(),
  222. H = tmDate.getHours(),
  223. i = tmDate.getMinutes(),
  224. s = tmDate.getSeconds();
  225. if (timestampDiff < 60) { // 一分钟以内
  226. return "刚刚";
  227. } else if (timestampDiff < 3600) { // 一小时前之内
  228. return Math.floor(timestampDiff / 60) + "分钟前";
  229. } else if (curDate.getFullYear() == Y && curDate.getMonth() + 1 == m && curDate.getDate() == d) {
  230. return '今天 ' + ((String(H).length == 1 ? '0' : '') + H) + ':' + ((String(i).length == 1 ? '0' : '') + i);
  231. } else {
  232. var newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
  233. if (newDate.getFullYear() == Y && newDate.getMonth() + 1 == m && newDate.getDate() == d) {
  234. return '昨天 ' + ((String(H).length == 1 ? '0' : '') + H) + ':' + ((String(i).length == 1 ? '0' : '') +
  235. i);
  236. } else if (curDate.getFullYear() == Y) {
  237. return ((String(m).length == 1 ? '0' : '') + m) + '月' + ((String(d).length == 1 ? '0' : '') + d) +
  238. '日 ' + ((String(H).length == 1 ? '0' : '') + H) + ':' + ((String(i).length == 1 ? '0' : '') + i);
  239. } else {
  240. return Y + '年' + ((String(m).length == 1 ? '0' : '') + m) + '月' + ((String(d).length == 1 ? '0' : '') +
  241. d) + '日 ' + ((String(H).length == 1 ? '0' : '') + H) + ':' + ((String(i).length == 1 ? '0' :
  242. '') + i);
  243. }
  244. }
  245. },
  246. getStrCharLength(val) {
  247. var str = new String(val);
  248. var bytesCount = 0;
  249. for (var i = 0, n = str.length; i < n; i++) {
  250. var c = str.charCodeAt(i);
  251. if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
  252. bytesCount += 1;
  253. } else {
  254. bytesCount += 2;
  255. }
  256. }
  257. return bytesCount;
  258. },
  259. getWebUrl() {
  260. _mixins.methods.$httpSend({
  261. path: '/im/middle.Middle/get_list',
  262. success: function(res) {
  263. console.log("res网站", res);
  264. let arr = res;
  265. let active = {};
  266. if (arr.length) {
  267. arr.forEach(item => {
  268. if (item.status == 1) {
  269. active = item;
  270. return
  271. }
  272. })
  273. if (!active.url) {
  274. uni.setTabBarItem({
  275. index: 2,
  276. visible: false
  277. })
  278. } else {
  279. let item_data = _data.localData('load_logo');
  280. // #ifdef APP-PLUS
  281. if (!item_data || item_data.url != active.url || item_data.logo != active.logo ||
  282. item_data.name != active.name) {
  283. uni.downloadFile({
  284. url: _data.staticUrl() + active.logo,
  285. success: (res) => {
  286. if (res.statusCode === 200) {
  287. console.log('下载成功', res);
  288. _data.localData('load_logo', {
  289. url: active.url,
  290. logo: active.logo,
  291. name: active.name,
  292. file: res.tempFilePath
  293. });
  294. uni.setTabBarItem({
  295. index: 2,
  296. visible: true,
  297. text: active.name,
  298. iconPath: res.tempFilePath,
  299. selectedIconPath: res.tempFilePath,
  300. })
  301. uni.$emit("show_weburl", active.url);
  302. }
  303. }
  304. });
  305. } else {
  306. uni.setTabBarItem({
  307. index: 2,
  308. visible: true,
  309. text: item_data.name,
  310. iconPath: item_data.file,
  311. selectedIconPath: item_data.file,
  312. })
  313. uni.$emit("show_weburl", item_data.url);
  314. }
  315. // #endif
  316. // #ifdef H5
  317. _data.localData('load_logo', {
  318. url: active.url,
  319. logo: active.logo,
  320. name: active.name,
  321. });
  322. uni.setTabBarItem({
  323. index: 2,
  324. visible: true,
  325. text: active.name,
  326. iconPath: _data.staticUrl() + active.logo,
  327. selectedIconPath: _data.staticUrl() + active.logo,
  328. })
  329. uni.$emit("show_weburl", active.url);
  330. // #endif
  331. }
  332. } else {
  333. uni.setTabBarItem({
  334. index: 2,
  335. visible: false
  336. })
  337. }
  338. }
  339. })
  340. },
  341. }