websocketHelper.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { Notification } from 'element-ui';
  2. const websocketHelper = {
  3. WS_APP_BONUS: 'bonus',
  4. HAND_SHAKE: 'setWebSocketFd',
  5. SEND_MESSAGE: 'sendMessage',
  6. handShakeFormat(userId){
  7. return JSON.stringify({
  8. cmd: this.HAND_SHAKE,
  9. app: this.WS_APP_BONUS,
  10. userId: userId,
  11. });
  12. },
  13. parse(response){
  14. if(!response['data']){
  15. return false;
  16. }
  17. let data = response.data;
  18. return JSON.parse(data);
  19. },
  20. onMessage(response,userData){
  21. console.log('-----')
  22. console.log(response)
  23. console.log('-----')
  24. let data = this.parse(response);
  25. console.log(data)
  26. if(!data || !data['app'] || !data['cmd']){
  27. return false;
  28. }
  29. //不是前台的通讯,就不执行下面的操作了
  30. if(data.app !== this.WS_APP_BONUS || data.cmd === this.HAND_SHAKE){
  31. return false;
  32. }
  33. if(data.cmd === this.SEND_MESSAGE ){
  34. let user = userData
  35. if(!this.allowPull(user, data.pullType, data.typeValue)){
  36. return false;
  37. }
  38. }
  39. let options = Object.assign(data, {
  40. title: data.title,
  41. message: data.message,
  42. type: 'info',
  43. duration:0,
  44. });
  45. console.log(111)
  46. console.log(options)
  47. Notification(options);
  48. },
  49. pushData(data){
  50. if(!data['message']){
  51. data['message'] = '您有一条新的消息';
  52. }
  53. return JSON.stringify(data);
  54. },
  55. allowPull(user, pullType, typeValue){
  56. let result = false;
  57. switch (pullType){
  58. case 'all':
  59. result = true;
  60. break;
  61. case 'dec_lv':
  62. result = (typeValue === user.DEC_LV) ? true : false;
  63. break;
  64. case 'emp_lv':
  65. result = (typeValue === user.EMP_LV) ? true : false;
  66. break;
  67. case 'user_name':
  68. result = (typeValue === user.USER_NAME) ? true : false;
  69. break;
  70. case 'location':
  71. if(typeof typeValue === 'string'){
  72. typeValue = JSON.parse(typeValue);
  73. }
  74. let userArea = [user.PROVINCE, user.CITY, user.COUNTY], len, needLen = 3, diff;
  75. typeValue.forEach(value=>{
  76. if(value[0] === ''){
  77. result = true;
  78. return false;
  79. }
  80. len = value.length;
  81. diff = needLen - len;
  82. if(diff > 0){
  83. value = value.concat(new Array(num).fill(''));
  84. }
  85. if(value[0] == userArea[0] && (value[1]=='' || value[1]==userArea[1]) && (value[2]=='' || value[2]==userArea[2])){
  86. result = true;
  87. return false;
  88. }
  89. });
  90. break;
  91. }
  92. return result;
  93. }
  94. };
  95. export default websocketHelper;