main.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import Vue from 'vue';
  2. import App from './App';
  3. import store from "./store";
  4. import helper from "./extends/helpers";
  5. import {
  6. mapMutations
  7. } from 'vuex';
  8. import LoadMore from './components/load-more';
  9. import Result from './components/result-page';
  10. import Sender from './components/sender';
  11. import Payment from './components/payment';
  12. import Contracts from './components/contacts';
  13. import socket from "./library/socket"
  14. import config from "./config.js";
  15. import action from "./library/action.js";
  16. import chat from "./library/chat.js";
  17. import ican from './js_sdk/ican-H5Api/ican-H5Api.js'
  18. helper(Vue);
  19. uni.setStorageSync('storeykey',"index");
  20. //打包消息数据
  21. const packMsg = function(res) {
  22. return {
  23. id: res.cache_key,
  24. msg: res
  25. };
  26. };
  27. //连接
  28. socket.connect(config.socketUri);
  29. //初始化
  30. socket.on('init', () => { //连接初始化
  31. if( uni.getStorageSync('access_token')>0){
  32. socket.send({
  33. type: 'login',
  34. token: uni.getStorageSync('access_token'),
  35. imgUri:config.imgUri
  36. });
  37. }
  38. }).on('no_auth', () => {
  39. //收到这条指令需要重新验证身份
  40. if( uni.getStorageSync('access_token')>0){
  41. socket.send({
  42. type: 'login',
  43. token: uni.getStorageSync('access_token'),
  44. imgUri:config.imgUri
  45. });
  46. }
  47. }).on('userinfo', (res) => { //收到自己的用户信息
  48. //console.log(res.data);
  49. uni.setStorageSync('userinfo',JSON.parse(res.data));
  50. store.commit('update_userinfo', JSON.parse(res.data));
  51. }).on('message', (res) => { //有用户或群发来的消息
  52. console.log(res);
  53. //store.commit('chat/push', packMsg(res));
  54. }).on('setting', (res) => {
  55. store.commit('chat/setting', res.data);
  56. }).on('chat', (res) => { //有用户或群发来的消息
  57. //console.log(res);
  58. chat.push(res,'index');
  59. // try{
  60. // store.commit('chat/push', packMsg(res.data));
  61. // }catch(e){
  62. // store.commit('chat/push', packMsg(JSON.parse(res.data)));
  63. // }
  64. //
  65. }).on('request', (res) => {
  66. //接收好友申请人数
  67. if(parseInt(res.num)>parseInt(uni.getStorageSync('newfriendnum'))){
  68. var userinfo=uni.getStorageSync('userInfo');
  69. if(parseInt(userinfo.note2)!=0) uni.vibrateLong();
  70. if(parseInt(userinfo.note1)!=0) action.playVoice('/static/voice/friend.mp3');
  71. }
  72. uni.setStorageSync('newfriendnum',res.num);
  73. action.setStatusTips();
  74. }).on('data_circle_tips', (res) => {
  75. uni.setStorageSync('data_circle_tips',res.data);
  76. action.setStatusTips();
  77. }).on('circle_push', (res) => {//更新朋友圈消息
  78. action.circleUpate(res.data)
  79. }).on('otherlogin', (res) => {//另一台设备登录
  80. socket.send({
  81. type: 'logout',
  82. token: uni.getStorageSync('access_token'),
  83. });
  84. action.quitcid();
  85. uni.removeStorageSync('userInfo');
  86. uni.removeStorageSync('access_token');
  87. uni.showModal({
  88. title:'强制下线提醒',
  89. content:'您的账号于'+res.time+'在另一台设备登录',
  90. })
  91. setTimeout(function(){
  92. uni.reLaunch({
  93. url:'/pages/game/index',
  94. })
  95. },1500)
  96. }).on('clearmsg', (res) => { //清空聊天记录
  97. //console.log(res);
  98. store.commit('chat/delete_msglist',res.key);
  99. }).on('response', (res) => { //接收响应消息
  100. console.log(res.message);
  101. });
  102. Vue.prototype.sendmsg = function (data){
  103. // console.log(packMsg(data))
  104. store.commit('chat/push', packMsg(data));
  105. }
  106. Vue.prototype.clearchatlist = function (msg){
  107. store.commit('chat/clearchatlist',msg);
  108. }
  109. Vue.prototype.delete_msg = function (cache_key){
  110. store.commit('chat/delete_msglist',cache_key);
  111. }
  112. Vue.prototype.update_msglist = function (data){
  113. store.commit('chat/update_msglist',data);
  114. }
  115. Vue.prototype.image_cache= function(image_url) {
  116. // #ifndef APP-PLUS
  117. if(image_url==undefined) return false;
  118. if(image_url.indexOf('http')<=-1) image_url=config.imgUri+image_url;
  119. return image_url;
  120. // #endif
  121. // #ifdef APP-PLUS
  122. if(image_url==undefined) return false;
  123. if(image_url.indexOf('http')<=-1) image_url=config.imgUri+image_url;
  124. var store=uni.getStorageSync('file_cache_'+image_url);
  125. if(store.length>10){
  126. uni.getSavedFileInfo({
  127. filePath:store,
  128. success: (res) => {
  129. },
  130. fail: (res) => {
  131. uni.removeStorageSync('file_cache_'+image_url)
  132. return image_url;
  133. }
  134. })
  135. // console.log('本地存在缓存',store);
  136. return store;
  137. }else{
  138. uni.downloadFile({
  139. url: image_url,
  140. success: (res) => {
  141. if (res.statusCode === 200) {
  142. uni.saveFile({
  143. tempFilePath:res.tempFilePath,
  144. success: function (res1) {
  145. var savedFilePath = res1.savedFilePath;
  146. uni.setStorageSync('file_cache_'+image_url,savedFilePath);
  147. }
  148. });
  149. }
  150. }
  151. });
  152. return image_url;
  153. }
  154. // #endif
  155. }
  156. Vue.prototype.GetUserinfo = function(userid){
  157. var friendlist = uni.getStorageSync(uni.getStorageSync('access_token')+'_frienddata');
  158. if(!friendlist) friendlist=[];
  159. for(var i=0;i<friendlist.length;i++){
  160. if(friendlist[i].id==userid) return friendlist[i];
  161. }
  162. return [];
  163. }
  164. Vue.prototype.$socket = socket;
  165. Vue.prototype.$store = store;
  166. Vue.prototype.$packMsg = packMsg;
  167. Vue.prototype.$action = action;
  168. Vue.component('load-more', LoadMore);
  169. Vue.component('result', Result);
  170. Vue.component('v-sender', Sender);
  171. Vue.component('v-payment', Payment);
  172. Vue.component('v-contracts', Contracts);
  173. Vue.config.productionTip = false;
  174. App.mpType = 'app';
  175. const app = new Vue({
  176. ...App,
  177. store
  178. });
  179. app.$mount();