like.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. function add(liketype,restype,resid) {
  41. fetch('/api/v2/like',{
  42. method: 'POST',
  43. credentials: 'include',
  44. headers: {
  45. 'Content-Type': 'application/json'
  46. },
  47. body: JSON.stringify({
  48. type:liketype,
  49. target_type:restype,
  50. target_id:resid
  51. })
  52. })
  53. .then(response => response.json())
  54. .then(function(data){
  55. console.log(data);
  56. let result = data.data;
  57. if(data.ok==true){
  58. for (let it of arrElement) {
  59. if(result.type===it.like_type &&
  60. result.target_id===it.resource_id){
  61. it.like++;
  62. it.me=result.id;
  63. }
  64. }
  65. Render();
  66. }
  67. });
  68. }
  69. function remove(id,liketype,resid) {
  70. fetch('/api/v2/like',{
  71. method: 'DELETE',
  72. credentials: 'include',
  73. headers: {
  74. 'Content-Type': 'application/json'
  75. },
  76. body: JSON.stringify({
  77. id:id,
  78. type:liketype,
  79. target_id:resid
  80. })
  81. })
  82. .then(response => response.json())
  83. .then(function(data){
  84. console.log(data);
  85. let result = data.data;
  86. if(data.ok==true){
  87. for (let it of arrElement) {
  88. if(liketype===it.like_type &&
  89. resid===it.resource_id){
  90. it.like = result.count;
  91. it.me=false;
  92. }
  93. }
  94. Render();
  95. }
  96. });
  97. /*
  98. $.getJSON(
  99. "../api/like.php",
  100. {
  101. _method:"delete",
  102. like_type:liketype,
  103. resource_type:restype,
  104. resource_id:resid
  105. }
  106. ).done(function (data) {
  107. console.log("delete",data);
  108. if(data.ok){
  109. LikeRefresh(data.data);
  110. }
  111. }).fail(function(jqXHR, textStatus, errorThrown){
  112. switch (textStatus) {
  113. case "timeout":
  114. break;
  115. case "error":
  116. switch (jqXHR.status) {
  117. case 404:
  118. break;
  119. case 500:
  120. break;
  121. default:
  122. break;
  123. }
  124. break;
  125. case "abort":
  126. break;
  127. case "parsererror":
  128. console.log("delete-parsererror",jqXHR.responseText);
  129. break;
  130. default:
  131. break;
  132. }
  133. });
  134. */
  135. }
  136. function LikeRefresh(data){
  137. $.ajaxSetup({contentType: "application/json; charset=utf-8"});
  138. $.post(
  139. "../api/like.php?_method=list",
  140. JSON.stringify([data])
  141. ).done(function (data) {
  142. console.log(data);
  143. let result = JSON.parse(data);
  144. for (let it of arrElement) {
  145. if(result["data"][0].resource_type===it.resource_type &&
  146. result["data"][0].resource_id===it.resource_id &&
  147. result["data"][0].like_type===it.like_type){
  148. it.like=result["data"][0].like;
  149. it.me=result["data"][0].me;
  150. }
  151. }
  152. Render();
  153. });
  154. }
  155. function LikeRefreshAll(){
  156. $.ajaxSetup({contentType: "application/json; charset=utf-8"});
  157. $.post(
  158. "../api/like.php?_method=list",
  159. JSON.stringify(arrElement)
  160. ).done(function (data) {
  161. console.log(data);
  162. arrElement = JSON.parse(data).data;
  163. Render();
  164. });
  165. }
  166. function Render(){
  167. for (const it of arrElement) {
  168. let html=" ";
  169. let meClass="";
  170. let likeIcon="";
  171. switch (it.like_type) {
  172. case "like":
  173. likeIcon = "👍Like";
  174. break;
  175. case "favorite":
  176. likeIcon = "⭐Favorite";
  177. break;
  178. case "watch":
  179. likeIcon = "👁️Watch";
  180. break;
  181. default:
  182. break;
  183. }
  184. if(it.me){
  185. meClass = " like_mine";
  186. }
  187. html +="<div class='like_inner "+meClass+"'>"+likeIcon;
  188. html +="<span class='number'>"+it.like+"<span>";
  189. html +="</div>";
  190. $("like[liketype='"+it.like_type+"'][restype='"+it.resource_type+"'][resid='"+it.resource_id+"']").html(html);
  191. }
  192. }