show_picture.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. $(function () {
  2. var request = getRequest();
  3. if (
  4. JSON.stringify(request) == "{}" ||
  5. request.id === undefined ||
  6. request.q === undefined ||
  7. request.t === undefined
  8. ) {
  9. console.log("argument error.");
  10. return;
  11. }
  12. var keywordControl = $("#keyword");
  13. var currentPage = 0;
  14. var maxPage = -1;
  15. if (request.q !== undefined) keywordControl.val(request.q);
  16. $("#search").on("click", function (e) {
  17. e.preventDefault();
  18. var inputValue = keywordControl.val().trim();
  19. if (inputValue === "" || inputValue.replace(/[-_—]/g, "") === "") {
  20. keywordControl.focus();
  21. return;
  22. }
  23. var keylist = encodeURIComponent(inputValue);
  24. window.location.href = "list_index.html?q=" + keylist + (request.t === undefined ? "" : "&t=" + request.t);
  25. });
  26. $("#keyword").on("keyup", function (e) {
  27. e.preventDefault();
  28. if (e.keyCode === 13) {
  29. $("#search").click();
  30. }
  31. });
  32. $('#loading').modal('show');
  33. fetchJSON("/api/resource/up/view?id=" + request.id, {}, "get", function (ret) {
  34. $('#loading').modal('hide');
  35. if (ret.code !== 0) console.log(ret.msg);
  36. });
  37. fetchJSON("/api/resource/info?id=" + request.id, {}, "get", function (ret) {
  38. if (ret.code === 0) {
  39. var resource = ret.data;
  40. fetchJSON(
  41. "/api/query/query",
  42. {keyword: combineKeysToArray(resource.resourceName), type: request.t, page: 0, size: 10},
  43. "post",
  44. function (ret) {
  45. if (ret.code === 0) {
  46. if (ret.data.resources.length != 0) {
  47. $("#relatedResourcesWrapper").css("display", "block");
  48. fillRelatedResourceList(ret.data.resources, request.t, $("#relatedResources"), request.q);
  49. }
  50. } else console.log(ret.msg);
  51. }
  52. );
  53. $("#title").html(resource.resourceName);
  54. $("#department").html(resource.submitDepartment);
  55. $("#author").html(resource.resourceAuthor);
  56. $("#submitDate").html(toDateString(resource.resourceCompletionDate));
  57. $("#format").html(getFileExt(resource.resourceUrl));
  58. var starWrapper = $("<div>", {class: "star pull-left"});
  59. var starSpan = $("<span>", {text: "满意度:"});
  60. starWrapper.append(starSpan);
  61. var b = parseInt(resource.satisfaction / 20);
  62. var c = resource.satisfaction % 20 == 0 ? 0 : 1;
  63. var rank = b + c;
  64. for (var j = 0; j < 5; j++) {
  65. if (j < rank)
  66. starWrapper.append(
  67. $("<span>", {
  68. id: "star" + j,
  69. class: "satisfaction icon cur",
  70. style: "cursor: pointer;",
  71. html: "&#xe644; &nbsp;"
  72. })
  73. );
  74. else
  75. starWrapper.append(
  76. $("<span>", {
  77. id: "star" + j,
  78. class: "satisfaction icon",
  79. style: "cursor: pointer;",
  80. html: "&#xe644; &nbsp;"
  81. })
  82. );
  83. }
  84. var img = $("<img>", {
  85. id: "player",
  86. width: "100%",
  87. height: "500px",
  88. src: urlBase + "/file/" + resource.resourceUrl
  89. });
  90. $("#playerWrapper").append(img);
  91. $("#playerWrapper")
  92. .append($("<br>", {id: "placeholder"}));
  93. fetchJSON("/api/resource/imgRelationList?id=" + resource.id, {}, "get", function (ret) {
  94. if (ret.code === 0) {
  95. currentPage = 0;
  96. maxPage = ret.data.totalPage;
  97. $("<div>", {id: "imgListWrapper"}).insertAfter($("#placeholder"));
  98. var originImg = $("#player").prop('src');
  99. $("#imgListWrapper").append($("<h4>", {
  100. id: "imgListInfo",
  101. text: "相关图片(共" + ret.data.totalElement + "项 / 第" + (parseInt(currentPage, 10) + 1) + "页 / 共" + maxPage + "页 )",
  102. style: "margin-top: 40px; margin-bottom: 20px; color: blue;"
  103. }));
  104. $("#imgListWrapper").append($("<div>", {
  105. id: "imgContainer",
  106. style: "height: 100px; text-align: center;"
  107. }));
  108. for (var i = 0; i < ret.data.content.length; i++) {
  109. var img = $("<img>", {
  110. src: decodeURIComponent(ret.data.content[i]),
  111. style: "padding: 5px",
  112. width: 100,
  113. height: 80
  114. });
  115. img.on('click', function () {
  116. $("#player").prop('src', this.src);
  117. });
  118. $("#imgContainer").append(img);
  119. }
  120. $("#imgListWrapper").append($("<div>", {id: "btnContainer", style: "text-align: center;"}));
  121. $("#btnContainer").append($("<div>"))
  122. .append(
  123. $("<button>", {
  124. id: "prevPage",
  125. class: "btn btn-primary",
  126. html: "上一页",
  127. style: "margin: 15px;"
  128. }))
  129. .append(
  130. $("<button>", {
  131. id: "origin",
  132. class: "btn btn-primary",
  133. html: "原图",
  134. style: "margin: 15px;"
  135. }))
  136. .append(
  137. $("<button>", {
  138. id: "nextPage",
  139. class: "btn btn-primary",
  140. html: "下一页",
  141. style: "margin: 15px;"
  142. }))
  143. .append($("<br>"));
  144. $('#origin').on('click', function () {
  145. $("#player").prop('src', originImg);
  146. });
  147. $("#prevPage").on('click', function () {
  148. if (currentPage > 0) {
  149. currentPage--;
  150. $("#imgListInfo").html("相关图片(共" + ret.data.totalElement + "项 / 第" + (parseInt(currentPage, 10) + 1) + "页 / 共" + maxPage + "页 )");
  151. fetchJSON("/api/resource/imgRelationList?id=" + resource.id + "&page=" + currentPage, {}, "get", function (ret) {
  152. if (ret.code === 0) {
  153. $("#imgContainer").empty();
  154. for (var i = 0; i < ret.data.content.length; i++) {
  155. var img = $("<img>", {
  156. src: decodeURIComponent(ret.data.content[i]),
  157. style: "padding: 5px",
  158. width: 100
  159. });
  160. img.on('click', function () {
  161. $("#player").prop('src', this.src);
  162. });
  163. $("#imgContainer").append(img);
  164. }
  165. $("imgListInfo").text("相关图片(共" + ret.data.totalElement + "项 / 第" + parseInt(currentPage) + 1 + "页)");
  166. } else {
  167. console.log(ret.msg);
  168. }
  169. });
  170. }
  171. });
  172. $("#nextPage").on('click', function () {
  173. if (currentPage + 1 < maxPage) {
  174. currentPage++;
  175. $("#imgListInfo").html("相关图片(共" + ret.data.totalElement + "项 / 第" + (parseInt(currentPage, 10) + 1) + "页 / 共" + maxPage + "页 )");
  176. fetchJSON("/api/resource/imgRelationList?id=" + resource.id + "&page=" + currentPage, {}, "get", function (ret) {
  177. if (ret.code === 0) {
  178. $("#imgContainer").empty();
  179. for (var i = 0; i < ret.data.content.length; i++) {
  180. var img = $("<img>", {
  181. src: decodeURIComponent(ret.data.content[i]),
  182. style: "padding: 5px",
  183. width: 100
  184. });
  185. img.on('click', function () {
  186. $("#player").prop('src', this.src);
  187. });
  188. $("#imgContainer").append(img);
  189. }
  190. } else {
  191. console.log(ret.msg);
  192. }
  193. });
  194. }
  195. });
  196. } else {
  197. console.log(ret.msg);
  198. }
  199. });
  200. $("#playerWrapper")
  201. .append($("<h4>", {text: "可用操作", style: "margin-top: 40px; color: blue;"}))
  202. .append($("<hr>"))
  203. .append(starWrapper);
  204. fetchJSON("/api/resource/downloadPermission", {}, "get", function (ret) {
  205. if (ret.code === 0) {
  206. if (ret.data === true) {
  207. $("#playerWrapper").append(
  208. $("<button>", {
  209. id: "download",
  210. class: "btn btn-primary icon pull-right",
  211. download: "",
  212. html: "&#xe6fe; 点击下载 (已下载" + resource.downloadTimes + "次)"
  213. })
  214. );
  215. var times = resource.downloadTimes;
  216. $("#download").on("click", function (e) {
  217. downloadResource("/api/resource/download?id=" + request.id);
  218. times++;
  219. $("#download").html("&#xe6fe; 点击下载 (已下载" + times + "次)");
  220. });
  221. }
  222. } else {
  223. console.log(ret.msg);
  224. }
  225. });
  226. fetchJSON("/api/user/isCollected?id="+resource.id, {},"get", function (ret) {
  227. if (ret.code === 0) {
  228. if (ret.code === 0) {
  229. $("#playerWrapper").prepend(
  230. $("<button>", {
  231. id: "collect",
  232. class: "btn btn-primary icon pull-right",
  233. html: "&#xe6fe; 点击收藏"
  234. })
  235. );
  236. $("#collect").on("click", function (e) {
  237. collectResource(resource.id, 1);
  238. $("#collect").hide();
  239. $("#collect1").show();
  240. });
  241. $("#playerWrapper").prepend(
  242. $("<button>", {
  243. id: "collect1",
  244. class: "btn btn-danger icon pull-right",
  245. html: "&#xe6fe; 取消收藏"
  246. })
  247. );
  248. $("#collect1").on("click", function (e) {
  249. collectResource(resource.id, 0);
  250. $("#collect").show();
  251. $("#collect1").hide();
  252. });
  253. if (ret.data === false) {
  254. $("#collect1").hide();
  255. } else {
  256. $("#collect").hide();
  257. }
  258. } else {
  259. console.log(ret.msg);
  260. }
  261. }
  262. });
  263. // .append(
  264. // $("<button>", {
  265. // id: "download",
  266. // class: "btn btn-primary icon pull-right",
  267. // download: "",
  268. // html: "&#xe6fe; 点击下载 (已下载" + resource.downloadTimes + "次)",
  269. // style: "margin-top: 5px;"
  270. // })
  271. // );
  272. // var times = resource.downloadTimes;
  273. // $("#download").on("click", function (e) {
  274. // downloadResource("/api/resource/download?id=" + request.id);
  275. // times++;
  276. // $("#download").html("&#xe6fe; 点击下载 (已下载" + times + "次)");
  277. // });
  278. $(".satisfaction").hover(
  279. function (e) {
  280. var id = this.id.replace("star", "");
  281. for (var i = 0; i <= id; i++) $("#star" + i).addClass("cur");
  282. },
  283. function (e) {
  284. for (var i = 0; i < 5; i++) if (i > rank - 1) $("#star" + i).removeClass("cur");
  285. }
  286. );
  287. $(".satisfaction").on("click", function (e) {
  288. var id = parseInt(this.id.replace("star", ""));
  289. rank = parseInt(id + 1);
  290. fetchJSON(
  291. "/api/resource/satisfaction?id=" + resource.id + "&satisfaction=" + (parseInt(id) + 1) * 20,
  292. {},
  293. "post",
  294. function (ret) {
  295. if (ret.code !== 0) console.log(ret.msg);
  296. }
  297. );
  298. });
  299. } else {
  300. console.log(ret.msg);
  301. }
  302. });
  303. fetchJSON("/api/query/related", {keyword: combineKeysToArray(request.q)}, "post", function (ret) {
  304. if (ret.code === 0) {
  305. if (ret.data.length != 0) {
  306. $("#relatedKeywordsWrapper").css("display", "block");
  307. fillRelatedKeywordList(ret.data, request.t, $("#relatedKeywords"));
  308. }
  309. } else {
  310. console.log(ret.msg);
  311. }
  312. });
  313. });