like.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. var arrElement = new Array();
  2. function Like() {
  3. $("like").each(function () {
  4. if ($(this).attr("init") != "true") {
  5. let likeItem = {
  6. like_type: $(this).attr("liketype"),
  7. resource_type: $(this).attr("restype"),
  8. resource_id: $(this).attr("resid"),
  9. like: $(this).attr("count"),
  10. me: $(this).attr("mine"),
  11. init: false
  12. };
  13. arrElement.push(likeItem);
  14. }
  15. });
  16. $("like").on("click", function () {
  17. let liketype = $(this).attr("liketype");
  18. let rettype = $(this).attr("restype");
  19. let resid = $(this).attr("resid");
  20. let readonly = $(this).attr("readonly");
  21. if (readonly == 'true') {
  22. return;
  23. }
  24. let e = arrElement.find(function (item) {
  25. if (liketype === item.like_type && rettype === item.resource_type && resid === item.resource_id) {
  26. return true;
  27. }
  28. else {
  29. return false;
  30. }
  31. });
  32. if (e.me) {
  33. remove(e.me, e.like_type, e.resource_id);
  34. } else {
  35. add(e.like_type, e.resource_type, e.resource_id);
  36. }
  37. })
  38. Render();
  39. }
  40. /**
  41. * /api/v2/like
  42. * @method POST
  43. * @param {string} liketype 下列值之一 like,favorite,watch
  44. * @param {string} restype 资源类型 下列值之一 chapter,article,course
  45. * @param {string} resid 资源 uuid
  46. *
  47. * @responce {json}
  48. * data{
  49. ok: bool,
  50. message: string,
  51. data:{
  52. type: string, 资源类型 下列值之一 chapter,article,course
  53. target_id: string, 资源 uuid
  54. id: string 点赞记录的uuid
  55. }
  56. }
  57. */
  58. function add(liketype, restype, resid) {
  59. fetch('/api/v2/like', {
  60. method: 'POST',
  61. credentials: 'include',
  62. headers: {
  63. 'Content-Type': 'application/json'
  64. },
  65. body: JSON.stringify({
  66. type: liketype,
  67. target_type: restype,
  68. target_id: resid
  69. })
  70. })
  71. .then(response => response.json())
  72. .then(function (data) {
  73. console.log(data);
  74. let result = data.data;
  75. if (data.ok == true) {
  76. for (let it of arrElement) {
  77. if (result.type === it.like_type &&
  78. result.target_id === it.resource_id) {
  79. it.like++;
  80. it.me = result.id;
  81. }
  82. }
  83. Render();
  84. }
  85. });
  86. }
  87. function remove(id, liketype, resid) {
  88. fetch('/api/v2/like', {
  89. method: 'DELETE',
  90. credentials: 'include',
  91. headers: {
  92. 'Content-Type': 'application/json'
  93. },
  94. body: JSON.stringify({
  95. id: id,
  96. type: liketype,
  97. target_id: resid
  98. })
  99. })
  100. .then(response => response.json())
  101. .then(function (data) {
  102. console.log(data);
  103. let result = data.data;
  104. if (data.ok == true) {
  105. for (let it of arrElement) {
  106. if (liketype === it.like_type &&
  107. resid === it.resource_id) {
  108. it.like = result.count;
  109. it.me = false;
  110. }
  111. }
  112. Render();
  113. }
  114. });
  115. }
  116. function LikeRefresh(data) {
  117. $.ajaxSetup({ contentType: "application/json; charset=utf-8" });
  118. $.post(
  119. "../api/like.php?_method=list",
  120. JSON.stringify([data])
  121. ).done(function (data) {
  122. console.log(data);
  123. let result = JSON.parse(data);
  124. for (let it of arrElement) {
  125. if (result["data"][0].resource_type === it.resource_type &&
  126. result["data"][0].resource_id === it.resource_id &&
  127. result["data"][0].like_type === it.like_type) {
  128. it.like = result["data"][0].like;
  129. it.me = result["data"][0].me;
  130. }
  131. }
  132. Render();
  133. });
  134. }
  135. function LikeRefreshAll() {
  136. $.ajaxSetup({ contentType: "application/json; charset=utf-8" });
  137. $.post(
  138. "../api/like.php?_method=list",
  139. JSON.stringify(arrElement)
  140. ).done(function (data) {
  141. console.log(data);
  142. arrElement = JSON.parse(data).data;
  143. Render();
  144. });
  145. }
  146. function Render() {
  147. for (const it of arrElement) {
  148. let html = " ";
  149. let meClass = "";
  150. let likeIcon = "";
  151. switch (it.like_type) {
  152. case "like":
  153. likeIcon = "👍Like";
  154. break;
  155. case "favorite":
  156. likeIcon = "⭐Favorite";
  157. break;
  158. case "watch":
  159. likeIcon = "👁️Watch";
  160. break;
  161. default:
  162. break;
  163. }
  164. if (it.me) {
  165. meClass = " like_mine";
  166. }
  167. html += "<div class='like_inner " + meClass + "'>" + likeIcon;
  168. html += "<span class='number'>" + it.like + "<span>";
  169. html += "</div>";
  170. $("like[liketype='" + it.like_type + "'][restype='" + it.resource_type + "'][resid='" + it.resource_id + "']").html(html);
  171. }
  172. }