_action.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. // uni.reLaunch({
  138. // url: '/pages/in/login'
  139. // });
  140. },
  141. /** 更新未读消息为0 */
  142. updataNoReader(list_id) {
  143. _mixins.methods.$httpSend({
  144. path: '/im/message/updataNoReader',
  145. data: {
  146. list_id: list_id
  147. },
  148. });
  149. uni.$emit('')
  150. },
  151. /** 下载自己的头像 */
  152. downloadPhoto() {
  153. let url = _data.staticPhoto() + _data.data('user_info').photo;
  154. let data = _data.data('cache');
  155. uni.downloadFile({
  156. url: url,
  157. success: (res) => {
  158. console.log("download:", res)
  159. if (res.statusCode === 200) {
  160. data.local_photo = res.tempFilePath;
  161. if (!data.local_photo) data.local_photo = url
  162. _data.data('cache', data);
  163. }
  164. },
  165. fail(res) {
  166. data.local_photo = url;
  167. console.log(1111)
  168. _data.data('cache', data);
  169. }
  170. });
  171. if (!_data.data('cache').local_photo) {
  172. data.local_photo = url;
  173. console.log("data", data)
  174. _data.data('cache', data);
  175. }
  176. console.log("_cache", _data.data('cache'))
  177. },
  178. /** 播放音效 */
  179. playVoice(path, loop) {
  180. innerAudioContext.src = path;
  181. innerAudioContext.loop = loop;
  182. // innerAudioContext.obeyMuteSwitch = false;
  183. innerAudioContext.play();
  184. innerAudioContext.onPlay(() => {
  185. //console.log('开始播放');
  186. });
  187. innerAudioContext.onError((res) => {
  188. innerAudioContext.destroy();
  189. return;
  190. uni.showToast({
  191. title: '音效播放错误 ->' + JSON.stringify(res),
  192. icon: 'none',
  193. });
  194. });
  195. },
  196. // 停止播放
  197. stopVoice() {
  198. console.log("停止")
  199. innerAudioContext.stop()
  200. },
  201. /** 时间戳转换 */
  202. timestampFormat(timestamp) {
  203. let curTimestamp = parseInt(new Date().getTime() / 1000), //当前时间戳
  204. timestampDiff = curTimestamp - timestamp, // 参数时间戳与当前时间戳相差秒数
  205. curDate = new Date(curTimestamp * 1000), // 当前时间日期对象
  206. tmDate = new Date(timestamp * 1000), // 参数时间戳转换成的日期对象
  207. Y = tmDate.getFullYear(),
  208. m = tmDate.getMonth() + 1,
  209. d = tmDate.getDate(),
  210. H = tmDate.getHours(),
  211. i = tmDate.getMinutes(),
  212. s = tmDate.getSeconds();
  213. if (timestampDiff < 60) { // 一分钟以内
  214. return "刚刚";
  215. } else if (timestampDiff < 3600) { // 一小时前之内
  216. return Math.floor(timestampDiff / 60) + "分钟前";
  217. } else if (curDate.getFullYear() == Y && curDate.getMonth() + 1 == m && curDate.getDate() == d) {
  218. return '今天 ' + ((String(H).length == 1 ? '0' : '') + H) + ':' + ((String(i).length == 1 ? '0' : '') + i);
  219. } else {
  220. var newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
  221. if (newDate.getFullYear() == Y && newDate.getMonth() + 1 == m && newDate.getDate() == d) {
  222. return '昨天 ' + ((String(H).length == 1 ? '0' : '') + H) + ':' + ((String(i).length == 1 ? '0' : '') +
  223. i);
  224. } else if (curDate.getFullYear() == Y) {
  225. return ((String(m).length == 1 ? '0' : '') + m) + '月' + ((String(d).length == 1 ? '0' : '') + d) +
  226. '日 ' + ((String(H).length == 1 ? '0' : '') + H) + ':' + ((String(i).length == 1 ? '0' : '') + i);
  227. } else {
  228. return Y + '年' + ((String(m).length == 1 ? '0' : '') + m) + '月' + ((String(d).length == 1 ? '0' : '') +
  229. d) + '日 ' + ((String(H).length == 1 ? '0' : '') + H) + ':' + ((String(i).length == 1 ? '0' :
  230. '') + i);
  231. }
  232. }
  233. },
  234. getStrCharLength(val) {
  235. var str = new String(val);
  236. var bytesCount = 0;
  237. for (var i = 0, n = str.length; i < n; i++) {
  238. var c = str.charCodeAt(i);
  239. if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
  240. bytesCount += 1;
  241. } else {
  242. bytesCount += 2;
  243. }
  244. }
  245. return bytesCount;
  246. },
  247. getWebUrl() {
  248. _mixins.methods.$httpSend({
  249. path: '/im/middle.Middle/get_list',
  250. success: function(res) {
  251. console.log("res网站", res);
  252. let arr = res;
  253. let active = {};
  254. if (arr.length) {
  255. arr.forEach(item => {
  256. if (item.status == 1) {
  257. active = item;
  258. return
  259. }
  260. })
  261. if (!active.url) {
  262. uni.setTabBarItem({
  263. index: 2,
  264. visible: false
  265. })
  266. } else {
  267. let item_data = _data.localData('load_logo');
  268. // #ifdef APP-PLUS
  269. if (!item_data || item_data.url != active.url || item_data.logo != active.logo ||
  270. item_data.name != active.name) {
  271. uni.downloadFile({
  272. url: _data.staticUrl() + active.logo,
  273. success: (res) => {
  274. if (res.statusCode === 200) {
  275. console.log('下载成功', res);
  276. _data.localData('load_logo', {
  277. url: active.url,
  278. logo: active.logo,
  279. name: active.name,
  280. file: res.tempFilePath
  281. });
  282. uni.setTabBarItem({
  283. index: 2,
  284. visible: true,
  285. text: active.name,
  286. iconPath: res.tempFilePath,
  287. selectedIconPath: res.tempFilePath,
  288. })
  289. uni.$emit("show_weburl", active.url);
  290. }
  291. }
  292. });
  293. } else {
  294. uni.setTabBarItem({
  295. index: 2,
  296. visible: true,
  297. text: item_data.name,
  298. iconPath: item_data.file,
  299. selectedIconPath: item_data.file,
  300. })
  301. uni.$emit("show_weburl", item_data.url);
  302. }
  303. // #endif
  304. // #ifdef H5
  305. _data.localData('load_logo', {
  306. url: active.url,
  307. logo: active.logo,
  308. name: active.name,
  309. });
  310. uni.setTabBarItem({
  311. index: 2,
  312. visible: true,
  313. text: active.name,
  314. iconPath: _data.staticUrl() + active.logo,
  315. selectedIconPath: _data.staticUrl() + active.logo,
  316. })
  317. uni.$emit("show_weburl", active.url);
  318. // #endif
  319. }
  320. } else {
  321. uni.setTabBarItem({
  322. index: 2,
  323. visible: false
  324. })
  325. }
  326. }
  327. })
  328. },
  329. }