queue.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /**
  2. *
  3. * @author maxd
  4. * @date 2019.8.1
  5. */
  6. import configdata from './config'
  7. import cache from './cache'
  8. // #ifdef APP-PLUS
  9. import permision from "../js_sdk/wa-permission/permission.js"
  10. // #endif
  11. function getHost(name) {
  12. const webConfig = cache.get('web_config')
  13. if (webConfig && webConfig[name]) {
  14. return webConfig[name]
  15. }
  16. return configdata[name]
  17. }
  18. module.exports = {
  19. //微信的appId
  20. getWxAppid() {
  21. return 'wxd0bec3a917085e78'
  22. },
  23. //全局邀请码
  24. getInvitation() {
  25. return uni.getStorageSync("publicRelation")
  26. },
  27. //获取APP下载地址
  28. getAppDownUrl() {
  29. return uni.getStorageSync("appurl")
  30. },
  31. //全局域名 部分html中需要单独替换 需要修改config中的网络请求域名
  32. publicYuMing() {
  33. return configdata.h5Url
  34. },
  35. // 登录判断
  36. toLogin(type) {
  37. let url = '/pages/public/loginV2'
  38. // #ifdef H5
  39. url = '/pages/public/login'
  40. // #endif
  41. switch (type) {
  42. case 'navigateTo':
  43. uni.navigateTo({
  44. url
  45. })
  46. break
  47. default:
  48. uni.reLaunch({
  49. url
  50. })
  51. }
  52. },
  53. logout() {
  54. this.remove("token");
  55. this.remove("userId");
  56. this.remove("mobile");
  57. this.remove("openid");
  58. this.remove("nickName");
  59. this.remove("relation");
  60. this.remove("image_url");
  61. this.remove("relation_id");
  62. this.remove("isInvitation");
  63. this.remove("userType");
  64. this.remove("member");
  65. this.remove('companyId')
  66. this.remove('avatar')
  67. this.remove('age')
  68. this.remove('companyName')
  69. this.remove('companyStatus')
  70. this.remove('isVip')
  71. this.remove('vipMsgNum')
  72. this.remove('vipMsgPrice')
  73. this.remove('vipDueTimes')
  74. this.remove('msgNum')
  75. this.remove('msgPrice')
  76. this.remove('invitationCode')
  77. this.remove('inviterCode')
  78. this.remove('hadShowPoster')
  79. },
  80. loginClear() {
  81. this.remove("token");
  82. this.remove("userId");
  83. this.remove("mobile");
  84. this.remove("nickName");
  85. this.remove("image_url");
  86. this.remove("relation_id");
  87. this.remove("isInvitation");
  88. this.remove("member");
  89. },
  90. showLoading(title) {
  91. uni.showLoading({
  92. title: title
  93. });
  94. },
  95. showToast(title) {
  96. uni.showToast({
  97. title: title,
  98. mask: false,
  99. duration: 2000,
  100. icon: "none"
  101. });
  102. },
  103. getChatSearchKeys: function(key) {
  104. let list = uni.getStorageSync("chatSearchKeys");
  105. let keys = key.replace(/\s*/g, "")
  106. let over = false
  107. for (var i = 0; i < keys.length; i++) {
  108. // console.error(keys[i])
  109. if (list.indexOf(keys[i]) != -1) {
  110. over = true;
  111. // return over;
  112. }
  113. }
  114. return over;
  115. },
  116. setJson: function(key, value) {
  117. let jsonString = JSON.stringify(value);
  118. try {
  119. uni.setStorageSync(key, jsonString);
  120. } catch (e) {
  121. // error
  122. }
  123. },
  124. setData: function(key, value) {
  125. try {
  126. uni.setStorageSync(key, value);
  127. } catch (e) {
  128. // error
  129. }
  130. },
  131. getData: function(key) {
  132. try {
  133. const value = uni.getStorageSync(key);
  134. if (value) {
  135. return value;
  136. }
  137. } catch (e) {
  138. // error
  139. }
  140. },
  141. getJson: function(key) {
  142. try {
  143. const value = uni.getStorageSync(key);
  144. if (value) {
  145. return JSON.parse(value);
  146. }
  147. } catch (e) {
  148. // error
  149. }
  150. },
  151. clear: function() {
  152. uni.clearStorage();
  153. },
  154. get: function(key) { //获取队列里面全部的数据
  155. let data = this.getJson(key);
  156. if (data instanceof Array) {
  157. return data;
  158. }
  159. return [];
  160. },
  161. insert: function(param) { //队列插入数据
  162. param.capacityNum = param.capacityNum || 100; //队列容量 默认队列中超过100条数据,自动删除尾部
  163. let data = this.getJson(param.key);
  164. if (data instanceof Array) {
  165. if (data.length > param.capacityNum) {
  166. let total = data.length - param.capacityNum;
  167. for (let i = 0; i < total; i++) {
  168. data.pop();
  169. }
  170. }
  171. data.unshift(param.value);
  172. } else {
  173. data = [];
  174. data.push(param.value);
  175. }
  176. this.setJson(param.key, data);
  177. },
  178. removeItem: function(key, itemIds) { //提供itemIds数组 批量删除队列中的某项数据
  179. let data = this.getJson(key);
  180. if (data instanceof Array) {
  181. for (let i = 0; i < itemIds.length; i++) {
  182. for (let p = 0; p < data.length; p++) {
  183. if (itemIds[i] === data[p].itemid) {
  184. data.splice(p, 1);
  185. break;
  186. }
  187. }
  188. }
  189. this.setJson(key, data);
  190. }
  191. },
  192. isExist: function(key, itemId) { //检测某条数据在队列中是否存在
  193. let data = this.getJson(key);
  194. if (data instanceof Array) {
  195. for (let p = 0; p < data.length; p++) {
  196. if (itemId === data[p].itemid) {
  197. return true;
  198. }
  199. }
  200. }
  201. return false;
  202. },
  203. isExistPdd: function(key, itemId) { //检测某条数据在队列中是否存在
  204. let data = this.getJson(key);
  205. if (data instanceof Array) {
  206. for (let p = 0; p < data.length; p++) {
  207. if (itemId === data[p].goodsId) {
  208. return true;
  209. }
  210. }
  211. }
  212. return false;
  213. },
  214. isExistJd: function(key, itemId) { //检测某条数据在队列中是否存在
  215. let data = this.getJson(key);
  216. if (data instanceof Array) {
  217. for (let p = 0; p < data.length; p++) {
  218. if (itemId === data[p].skuId) {
  219. return true;
  220. }
  221. }
  222. }
  223. return false;
  224. },
  225. remove: function(key) { //删除某条队列
  226. try {
  227. uni.removeStorageSync(key);
  228. //localStorage.removeItem(key)
  229. } catch (e) {
  230. // error
  231. }
  232. },
  233. getCount: function(key) { //获取队列中全部数据数量
  234. let data = this.getJson(key);
  235. if (data instanceof Array) {
  236. return data.length;
  237. }
  238. return 0;
  239. },
  240. uploadFile: function(path, callback) {
  241. const host = getHost('APIHOST1')
  242. const uploadTask = uni.uploadFile({ // 上传接口
  243. url: host + '/alioss/upload', //真实的接口地址
  244. filePath: path,
  245. name: 'file',
  246. success: (uploadFileRes) => {
  247. let content = JSON.parse(uploadFileRes.data).data;
  248. uni.hideLoading();
  249. callback(content)
  250. },
  251. fail: (err) => {
  252. callback(false)
  253. console.log(err)
  254. }
  255. });
  256. uploadTask.onProgressUpdate((res) => {
  257. uni.showLoading({
  258. title: '上传进度' + res.progress + '%',
  259. })
  260. if (res.progress >= 100) {
  261. uni.hideLoading()
  262. }
  263. })
  264. },
  265. /**
  266. * 上传文件
  267. * @param {Object} params 参数
  268. * url:服务器地址
  269. * path:文件本地地址
  270. * name:文件名称
  271. * formData:上传参数
  272. * @param {Object} callback
  273. */
  274. uploadFiles: function(params, callback) {
  275. let url = params.url ? params.url : ''
  276. let filePath = params.path ? params.path : ''
  277. let name = params.name ? params.name : ''
  278. let formData = params.data ? params.data : {}
  279. const host = getHost('APIHOST')
  280. const uploadTask = uni.uploadFile({ // 上传接口
  281. url: `${host}${url}`, //真实的接口地址
  282. filePath,
  283. name,
  284. formData,
  285. header: {
  286. token: uni.getStorageSync('token')
  287. },
  288. success: (uploadFileRes) => {
  289. let content = JSON.parse(uploadFileRes.data)
  290. uni.hideLoading();
  291. callback(content)
  292. },
  293. fail: (err) => {
  294. callback(false)
  295. console.log(err)
  296. }
  297. });
  298. uploadTask.onProgressUpdate((res) => {
  299. uni.showLoading({
  300. title: '上传进度' + res.progress + '%',
  301. })
  302. if (res.progress >= 100) {
  303. uni.hideLoading()
  304. }
  305. })
  306. },
  307. array_column: function(arr, column_key, index_key = null) {
  308. if (index_key === null) {
  309. return arr.map(item => item[column_key]);
  310. }
  311. return arr.reduce((result, item) => {
  312. result[item[index_key]] = item[column_key];
  313. return result;
  314. }, {});
  315. },
  316. isCurrentMonth: function(date) {
  317. const now = new Date();
  318. return now.getFullYear() === date.getFullYear() &&
  319. now.getMonth() === date.getMonth();
  320. },
  321. getHighestEducation: function(educations) {
  322. // 定义学历等级映射(从低到高)
  323. const educationLevels = {
  324. '小学': 1,
  325. '初中': 2,
  326. '高中': 3,
  327. '中专': 4,
  328. '大专': 5,
  329. '本科': 6,
  330. '硕士': 7,
  331. '博士': 8,
  332. '博士后': 9
  333. };
  334. let highestEducation = null;
  335. let highestLevel = -1;
  336. educations.forEach(edu => {
  337. const level = educationLevels[edu.degree] || 0;
  338. if (level > highestLevel) {
  339. highestLevel = level;
  340. highestEducation = edu.degree;
  341. }
  342. });
  343. return highestEducation;
  344. },
  345. //职业状态
  346. resumesStatus() {
  347. return [{
  348. value: '0',
  349. text: '离职-随时到岗',
  350. recommended: true
  351. },
  352. {
  353. value: '1',
  354. text: '在职-月内到岗',
  355. recommended: true
  356. },
  357. {
  358. value: '2',
  359. text: '在职-考虑机会',
  360. recommended: false
  361. },
  362. {
  363. value: '3',
  364. text: '在职-暂不考虑',
  365. recommended: false
  366. },
  367. {
  368. value: '4',
  369. text: '实习',
  370. recommended: false
  371. }
  372. ]
  373. },
  374. appConfirm: function(content = '请确认您的操作?', callback, title = '操作提示', button = ["继续", "取消"]) {
  375. // #ifndef APP-PLUS
  376. uni.showModal({
  377. title: title,
  378. content: content,
  379. success: function(res) {
  380. if (res.confirm) {
  381. callback(true)
  382. } else
  383. callback(false)
  384. }
  385. });
  386. return
  387. //#endif
  388. // #ifdef APP-PLUS
  389. plus.nativeUI.confirm(content, function(e) {
  390. if (e.index == 0)
  391. callback(true)
  392. else
  393. callback(false)
  394. }, title, button);
  395. //#endif
  396. },
  397. isDateExpired: function(targetDate) {
  398. const currentTimestamp = new Date().getTime();
  399. const targetTimestamp = new Date(targetDate).getTime();
  400. const rest = targetTimestamp < currentTimestamp
  401. if (rest)
  402. return [true, 0]
  403. // 计算毫秒差
  404. const diffInMs = targetTimestamp - currentTimestamp;
  405. // 转换为小时(向上取整确保显示完整小时)
  406. const hours = Math.ceil(diffInMs / (1000 * 60 * 60));
  407. return [rest, hours > 0 ? hours : 0]; // 返回非负值
  408. },
  409. convert12to24: function(time12h) {
  410. // 使用正则表达式匹配12小时制格式
  411. const regex = /^(0?[1-9]|1[0-2]):([0-5]\d)\s?(AM|PM)$/i;
  412. const match = time12h.match(regex);
  413. if (!match) {
  414. throw new Error('无效的时间格式,请使用如 "2:00 PM" 或 "02:00 PM" 的格式');
  415. }
  416. let hour = parseInt(match[1], 10);
  417. const minute = match[2];
  418. const period = match[3].toUpperCase();
  419. // 处理AM/PM转换逻辑
  420. if (period === 'PM' && hour !== 12) {
  421. hour += 12;
  422. } else if (period === 'AM' && hour === 12) {
  423. hour = 0;
  424. }
  425. // 格式化为两位数
  426. const hour24 = hour.toString().padStart(2, '0');
  427. return `${hour24}:${minute}`;
  428. },
  429. changeTabbar: function(res) {
  430. setTimeout(() => {
  431. if (res == 1) {
  432. uni.setTabBarItem({
  433. index: 0,
  434. text: '首页',
  435. iconPath: "/static/tabbar/Home.png",
  436. selectedIconPath: "/static/tabbar/Iconly_Bold_Home.png",
  437. })
  438. uni.setTabBarItem({
  439. index: 1,
  440. text: '急聘',
  441. pagePath: "/pages/index/game/gameList",
  442. iconPath: "/static/tabbar/jipin.png",
  443. selectedIconPath: "/static/tabbar/ACjipin.png",
  444. })
  445. } else {
  446. uni.setTabBarItem({
  447. index: 0,
  448. text: '牛人',
  449. iconPath: "/static/tabbar/niu.png",
  450. selectedIconPath: "/static/tabbar/niu2.png",
  451. })
  452. uni.setTabBarItem({
  453. index: 1,
  454. pagePath: "/pages/talentSearch/index",
  455. iconPath: "/static/tabbar/Hrsearch.png",
  456. selectedIconPath: "/static/tabbar/ACsearch.png",
  457. text: "搜索"
  458. })
  459. }
  460. // 可以在这里添加 tabbar 更新后的后续操作
  461. console.log('tabbar 更新完成');
  462. }, 100);
  463. },
  464. // 核心年龄计算函数
  465. calculateAgeFromBirthday: function(birthday) {
  466. const birthDate = new Date(birthday);
  467. const currentDate = new Date();
  468. // 验证日期有效性
  469. if (isNaN(birthDate.getTime())) {
  470. throw new Error('无效的生日日期');
  471. }
  472. if (birthDate > currentDate) {
  473. throw new Error('生日日期不能晚于当前日期');
  474. }
  475. let years = currentDate.getFullYear() - birthDate.getFullYear();
  476. let months = currentDate.getMonth() - birthDate.getMonth();
  477. let days = currentDate.getDate() - birthDate.getDate();
  478. // 处理天数负数情况
  479. if (days < 0) {
  480. months--;
  481. // 获取上个月的天数
  482. const lastMonth = new Date(currentDate.getFullYear(), currentDate.getMonth(), 0);
  483. days += lastMonth.getDate();
  484. }
  485. // 处理月份负数情况
  486. if (months < 0) {
  487. years--;
  488. months += 12;
  489. }
  490. return years;
  491. },
  492. // 筛选求职列表
  493. getFilterData: function() {
  494. const condition = {
  495. education: '', //学历
  496. experience: '', //经验
  497. industry: '', //行业
  498. salaryRange: '', //薪资
  499. companyPeople: '' //公司规模
  500. }
  501. //获取选中的筛选条件
  502. if (uni.getStorageSync('filter') && (uni.getStorageSync('filter')).length > 0) {
  503. let filter = uni.getStorageSync('filter')
  504. filter.map(item => {
  505. let arr = []
  506. item.list.map(ite => {
  507. if (ite.value != '不限') {
  508. arr.push(ite.value)
  509. }
  510. })
  511. switch (item.name) {
  512. case '学历要求':
  513. condition.education = arr.join(',')
  514. break;
  515. case '薪资范围(单选)':
  516. condition.salaryRange = arr.join(',')
  517. break;
  518. case '经验要求':
  519. condition.experience = arr.join(',')
  520. break;
  521. case '公司规模':
  522. condition.companyPeople = arr.join(',')
  523. break;
  524. case '行业':
  525. condition.industry = arr.join(',')
  526. break;
  527. }
  528. })
  529. // console.log(filter)
  530. }
  531. return condition
  532. },
  533. //APP获取手机权限鉴权
  534. // APP获取手机权限鉴权(优化版,兼容全权限+多系统)
  535. async checkPermission(permission, tip, that, perpop = 'permission') {
  536. // 权限映射配置
  537. let permisionID = '';
  538. let permisionIosID = '';
  539. return new Promise(async (resolve) => {
  540. // #ifdef H5
  541. resolve(true);
  542. return;
  543. // #endif
  544. const appAuthorizeSetting = uni.getAppAuthorizeSetting()
  545. if (permission == 'location') {
  546. permisionID = 'android.permission.ACCESS_FINE_LOCATION';
  547. permisionIosID = 'location';
  548. if (appAuthorizeSetting.locationAuthorized == 'authorized') {
  549. resolve(true);
  550. return
  551. }
  552. }
  553. if (permission == 'camera') {
  554. permisionID = 'android.permission.CAMERA,android.permission.READ_EXTERNAL_STORAGE';
  555. permisionIosID = 'camera,photoLibrary';
  556. if (appAuthorizeSetting.cameraAuthorized == 'authorized') {
  557. resolve(true);
  558. return
  559. }
  560. }
  561. if (permission == 'microphone') {
  562. permisionID = 'android.permission.RECORD_AUDIO';
  563. permisionIosID = 'record';
  564. if (appAuthorizeSetting.microphoneAuthorized == 'authorized') {
  565. resolve(true);
  566. return
  567. }
  568. }
  569. // 特殊处理:相机权限在非iOS平台显示自定义弹窗
  570. if (uni.getSystemInfoSync().platform != 'ios') {
  571. switch (perpop) {
  572. case 'permission':
  573. that.$refs.permission.open();
  574. break
  575. case 'microphonePermission':
  576. that.$refs.microphonePermission.open();
  577. break
  578. case 'locationPermission':
  579. that.$refs.locationPermission.open();
  580. break
  581. default:
  582. console.warn('未知权限类型:', perpop);
  583. }
  584. }
  585. // Android 平台处理
  586. if (uni.getSystemInfoSync().platform === 'android') {
  587. if (permisionID == '') {
  588. resolve(true);
  589. return;
  590. }
  591. try {
  592. const result = await permision.requestAndroidPermission(permisionID);
  593. console.log(result);
  594. switch (perpop) {
  595. case 'permission':
  596. that.$refs.permission.close();
  597. break
  598. case 'microphonePermission':
  599. that.$refs.microphonePermission.close();
  600. break
  601. case 'locationPermission':
  602. that.$refs.locationPermission.close();
  603. break
  604. default:
  605. console.warn('未知权限类型:', perpop);
  606. }
  607. if (result == 1) {
  608. console.log("已获得授权");
  609. resolve(true);
  610. } else if (result == 0) {
  611. console.log("未获得授权");
  612. resolve(false);
  613. } else {
  614. console.log("权限被拒绝");
  615. // 显示确认对话框
  616. this.appConfirm("权限被拒绝," + tip, function(res) {
  617. if (res) {
  618. uni.openAppAuthorizeSetting();
  619. }
  620. resolve(false);
  621. });
  622. }
  623. } catch (error) {
  624. console.error("权限请求失败:", error);
  625. resolve(false);
  626. }
  627. }
  628. // iOS 平台处理
  629. else {
  630. if (permisionIosID == '') {
  631. resolve(true);
  632. return;
  633. }
  634. const iosPermissions = permisionIosID.split(',');
  635. let allGranted = 0;
  636. for (let i = 0; i < iosPermissions.length; i++) {
  637. const currentPermission = iosPermissions[i];
  638. allGranted = permision.judgeIosPermission(currentPermission);
  639. console.log('权限状态码:', allGranted)
  640. }
  641. if (allGranted == 0) {
  642. console.log('第一次授权')
  643. resolve(true);
  644. } else if (allGranted == 3) {
  645. console.log('已授权')
  646. resolve(true);
  647. } else {
  648. this.appConfirm("权限被拒绝," + tip, function(res) {
  649. if (res) {
  650. uni.openAppAuthorizeSetting();
  651. }
  652. resolve(false);
  653. });
  654. }
  655. }
  656. });
  657. },
  658. appShare(scene, that) {
  659. uni.share({
  660. provider: "weixin", //分享服务提供商(即weixin|qq|sinaweibo)
  661. scene: scene, //场景,可取值参考下面说明。
  662. type: 0, //分享形式 0:图文 1:纯文字 2:纯图片 3:音乐 4:视频 5:小程序
  663. summary: '亿职赞APP新上线,欢迎前来体验',
  664. href: configdata.h5Url + '/pages/index/download',
  665. imageUrl: configdata.h5Url + '/40x40.png',
  666. success: function(res) {
  667. that.shows = false; //成功后关闭底部弹框
  668. },
  669. fail: (err) => {
  670. this.showToast('分享失败')
  671. that.shows = false;
  672. }
  673. });
  674. },
  675. connectSocket() {
  676. let that = this
  677. let userId = this.getData('userId');
  678. const wsHost = getHost('WSHOST1')
  679. if (!userId)
  680. return;
  681. uni.closeSocket()
  682. uni.connectSocket({
  683. url: wsHost + '' + userId,
  684. data() {
  685. return {
  686. msg: 'Hello'
  687. }
  688. },
  689. header: {
  690. 'content-type': 'application/json'
  691. },
  692. method: 'GET',
  693. success(res) {
  694. // 这里是接口调用成功的回调,不是连接成功的回调,请注意
  695. console.log('连接成功', res)
  696. that.onSocketMsg();
  697. },
  698. fail(err) {
  699. console.log('连接失败', res)
  700. // 这里是接口调用失败的回调,不是连接失败的回调,请注意
  701. }
  702. })
  703. },
  704. onSocketMsg() {
  705. let that = this
  706. uni.onSocketMessage((res) => {
  707. if (res.data === 'HeartBeat') return // 忽略心跳包
  708. let msg
  709. try {
  710. msg = JSON.parse(res.data)
  711. } catch (e) {
  712. // console.log('消息不是JSON格式:', res.data)
  713. return
  714. }
  715. if (msg.type === 'kickOut') {
  716. uni.showModal({
  717. title: '提示',
  718. content: msg.content,
  719. showCancel: false,
  720. success() {
  721. that.logout();
  722. // #ifdef H5
  723. uni.reLaunch({
  724. url: '/pages/public/loginphone?type=1'
  725. })
  726. // #endif
  727. // #ifndef H5
  728. uni.reLaunch({
  729. url: '/pages/public/loginV2'
  730. })
  731. // #endif
  732. }
  733. })
  734. }
  735. })
  736. },
  737. sendImMessage(userId, content, messageType, chatConversationId, recordId) {
  738. let data = {
  739. userId,
  740. content,
  741. messageType,
  742. chatConversationId
  743. }
  744. if (recordId) {
  745. data = {
  746. ...data,
  747. recordId
  748. }
  749. }
  750. data = JSON.stringify(data);
  751. return new Promise(async (resolve, reject) => {
  752. uni.sendSocketMessage({
  753. data: data,
  754. success(res) {
  755. resolve(res)
  756. },
  757. fail(err) {
  758. reject(err);
  759. }
  760. })
  761. })
  762. },
  763. /**
  764. * 深合并两个对象:将前一个对象的数据合并到后一个对象(支持嵌套对象)
  765. * @param {Object} sourceObj - 源对象(提供数据的对象,前一个对象)
  766. * @param {Object} targetObj - 目标对象(被合并的对象,后一个对象)
  767. * @returns {Object} 合并后的目标对象
  768. */
  769. deepMergeObject(sourceObj, targetObj) {
  770. // 1. 参数校验
  771. const isValidObj = (obj) => obj !== null && typeof obj === 'object' && !Array.isArray(obj);
  772. if (!isValidObj(sourceObj)) return isValidObj(targetObj) ? targetObj : {};
  773. if (!isValidObj(targetObj)) return JSON.parse(JSON.stringify(sourceObj)); // 简单深拷贝源对象
  774. // 2. 遍历源对象属性
  775. Object.keys(sourceObj).forEach((key) => {
  776. const sourceValue = sourceObj[key];
  777. const targetValue = targetObj[key];
  778. // 3. 处理嵌套对象(递归合并)
  779. if (isValidObj(sourceValue) && isValidObj(targetValue)) {
  780. deepMergeObject(sourceValue, targetValue); // 递归处理嵌套对象
  781. return; // 嵌套对象处理完毕,跳过后续逻辑
  782. }
  783. // 4. 非嵌套对象,按照原规则处理
  784. if (!(key in targetObj) || targetValue === null || targetValue === undefined) {
  785. targetObj[key] = sourceValue;
  786. }
  787. });
  788. return targetObj;
  789. }
  790. };