behavior.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. const CONFIG = require("../config.js");
  2. const Parse = getApp().Parse
  3. async function saveLog(gid, uid, className) {
  4. let BehaviorLog = new Parse.Query('BehaviorLog')
  5. BehaviorLog.equalTo('user', uid)
  6. BehaviorLog.equalTo('targetId', gid)
  7. BehaviorLog.equalTo('targetClassName', className)
  8. BehaviorLog.select('objectId')
  9. let exist = await BehaviorLog.first()
  10. if (exist && exist.id) {
  11. return
  12. }
  13. let Behavior = Parse.Object.extend('BehaviorLog')
  14. let behavior = new Behavior()
  15. if (uid) {
  16. behavior.set('user', {
  17. __type: "Pointer",
  18. className: "_User",
  19. objectId: uid
  20. })
  21. }
  22. behavior.set('company', {
  23. __type: "Pointer",
  24. className: "Company",
  25. objectId: CONFIG.default.company
  26. })
  27. behavior.set('targetId', gid)
  28. behavior.set('targetClassName', className)
  29. behavior.set('targetObject', [{ "__type": "Pointer", "className": className, "objectId": gid }])
  30. await behavior.save()
  31. }
  32. async function collect(cid, className, type, name, image, uid) {
  33. let Collect = Parse.Object.extend('Collect')
  34. let collect = new Collect()
  35. collect.set('collectId', cid)
  36. collect.set('schemaName', className)
  37. collect.set('collectTarget', [{
  38. __type: 'Pointer',
  39. className: className,
  40. objectId: cid
  41. }])
  42. collect.set('type', type)
  43. collect.set('name', name)
  44. collect.set('image', image)
  45. if (uid) {
  46. collect.set('user', {
  47. __type: 'Pointer',
  48. className: '_User',
  49. objectId: uid
  50. })
  51. }
  52. collect.set('company', {
  53. __type: 'Pointer',
  54. className: 'Company',
  55. objectId: CONFIG.default.company
  56. })
  57. let col = await collect.save()
  58. if (col && col.id) {
  59. return true
  60. } else {
  61. return false
  62. }
  63. }
  64. async function cancelCollect(cid, className, uid) {
  65. let Collect = new Parse.Query('Collect')
  66. Collect.equalTo('user', uid)
  67. Collect.equalTo('collectId', cid)
  68. Collect.equalTo('schemaName', className)
  69. Collect.equalTo('company', CONFIG.default.company)
  70. let collect = await Collect.first()
  71. if (collect) {
  72. await collect.destroy()
  73. return true
  74. } else {
  75. return false
  76. }
  77. }
  78. module.exports = {
  79. saveLog: saveLog,
  80. collect: collect,
  81. cancelCollect: cancelCollect,
  82. }