push.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import http from '@/http/api.js'
  2. /**
  3. * 获取推送CID及设备信息
  4. * @param {Boolean} showToast 是否显示错误提示,默认 true
  5. * @returns {Promise<Object>} 返回 Promise,成功时 resolve({cid, platform, brand})
  6. */
  7. const getPushCid = (showToast = true) => {
  8. return new Promise((resolve, reject) => {
  9. // 获取系统信息以得到平台和品牌
  10. const systemInfo = uni.getSystemInfoSync();
  11. const platform = systemInfo.platform;
  12. // 手机品牌
  13. const brand = systemInfo.brand || 'unknown';
  14. uni.getPushClientId({
  15. success: (res) => {
  16. const cid = res.cid;
  17. console.log('[PushUtil] 获取CID成功:', cid, '平台:', platform, '品牌:', brand);
  18. // 存储到本地缓存
  19. uni.setStorageSync('push_cid', cid);
  20. uni.setStorageSync('push_platform', platform);
  21. uni.setStorageSync('push_brand', brand);
  22. // 返回包含设备信息的对象
  23. resolve({
  24. cid: cid,
  25. platform: platform,
  26. brand: brand
  27. });
  28. },
  29. fail: (err) => {
  30. console.error('[PushUtil] 获取CID失败:', err);
  31. // 如果需要弹窗提示(方便调试)
  32. if (showToast) {
  33. uni.showModal({
  34. title: '提示',
  35. content: '获取推送标识失败,请确认已使用自定义基座运行。错误: ' + JSON.stringify(err),
  36. showCancel: false
  37. });
  38. }
  39. reject(err);
  40. }
  41. });
  42. });
  43. };
  44. /**
  45. * 将 CID 及设备信息上传到业务服务器
  46. * @param {String} cid 设备标识
  47. * @param {String} platform 平台信息
  48. * @param {String} brand 设备品牌
  49. */
  50. const uploadCidToServer = (cid, platform, brand) => {
  51. if (!cid) {
  52. console.warn('[PushUtil] CID为空,停止上传');
  53. return Promise.reject('CID为空');
  54. }
  55. return new Promise((resolve, reject) => {
  56. // 使用POST请求,将设备信息一起上传
  57. http.request({
  58. url: '/blade-sales-part/app/push/saveUserCid',
  59. method: 'post',
  60. data: {
  61. cid: cid,
  62. platform: platform || '',
  63. deviceBrand: brand || ''
  64. }
  65. }).then(res => {
  66. resolve(res);
  67. }).catch(err => {
  68. reject(err);
  69. });
  70. });
  71. };
  72. /**
  73. * 完整的推送初始化流程(建议在App.vue调用)
  74. * 获取CID并上传到服务器,包含平台和品牌信息
  75. */
  76. const initPushService = (showToast = true) => {
  77. getPushCid(showToast)
  78. .then(deviceInfo => {
  79. console.log('[PushUtil] 设备信息:', deviceInfo);
  80. return uploadCidToServer(deviceInfo.cid, deviceInfo.platform, deviceInfo.brand);
  81. })
  82. .then(res => {
  83. console.log('[PushUtil] 设备信息上传成功:', res);
  84. })
  85. .catch(err => {
  86. console.error('[PushUtil] 推送服务初始化失败:', err);
  87. });
  88. };
  89. /**
  90. * 监听推送消息 (建议在 App.vue 调用一次)
  91. */
  92. const onPushMessage = () => {
  93. uni.onPushMessage((res) => {
  94. console.log('[PushUtil] 收到推送消息:', res);
  95. // 您可以在这里做一些全局处理,比如播放提示音、增加角标等
  96. });
  97. };
  98. // 导出方法
  99. export default {
  100. getPushCid,
  101. uploadCidToServer,
  102. initPushService,
  103. onPushMessage
  104. };