邓淳远、崔鹏宇、翁思扬组云计算期末项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
11 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. var url_prefix = "http://127.0.0.1:5000/";
  2. function sendRequest(url_suffix, data, func) {
  3. var url = url_prefix + url_suffix;
  4. $.ajax({
  5. type: "post",
  6. url: url,
  7. data: data,
  8. dataType: "json",
  9. processData: false,
  10. contentType: false,
  11. success: function (data) {
  12. func(data);
  13. },
  14. error: function (XMLHttpRequest, textStatus, errorThrown) {
  15. console.log(this);
  16. }
  17. });
  18. return;
  19. }
  20. function GetRequest() {
  21. var url = location.search; //获取url中"?"符后的字串
  22. var theRequest = new Object();
  23. if (url.indexOf("?") != -1) {
  24. var str = url.substr(1);
  25. strs = str.split("&");
  26. for (var i = 0; i < strs.length; i++) {
  27. theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
  28. }
  29. }
  30. return theRequest;
  31. }
  32. function add(type) {
  33. var content = page.content;
  34. var name = page.name;
  35. var tag = page.tag;
  36. var order = page.textList.length;
  37. if (type == 'block') {
  38. if (content == null) {
  39. return;
  40. }
  41. if (CHECK_URL(content)) {
  42. console.log(content);
  43. submit(content, 'url');
  44. }
  45. else {
  46. submit(content, 'text');
  47. }
  48. page.content = null;
  49. return;
  50. }
  51. if (name == null) {
  52. return;
  53. }
  54. var data = new FormData();
  55. data.append('phonenum', phonenum);
  56. data.append('name', name);
  57. data.append('tag', tag);
  58. sendRequest('collection/add', data, function () {
  59. getContent('collection');
  60. });
  61. page.content = null;
  62. page.name = null;
  63. page.tag = null;
  64. }
  65. function submit(content, type, order) {
  66. var url = "block/add";
  67. var data = new FormData();
  68. data.append("content", content);
  69. data.append('type', type);
  70. data.append('collection_id', page.id);
  71. sendRequest(url, data, function () {
  72. getContent('block');
  73. })
  74. }
  75. function getContent(type) {
  76. var url;
  77. url = type + "/select";
  78. var data = new FormData();
  79. if ("undefined" != typeof phonenum) {
  80. data.append('phonenum', phonenum);
  81. }
  82. if (page.id != null) {
  83. data.append('id', page.id);
  84. }
  85. var name = $("#search").val();
  86. if (name != "") {
  87. data.append('name', name);
  88. }
  89. sendRequest(url, data, function (data) {
  90. if (type == 'collection') {
  91. page.textList = data.collections;
  92. checkLike(page);
  93. }
  94. else {
  95. page.textList = data.blocks;
  96. getWebName();
  97. }
  98. for (i in page.textList) {
  99. page.textList[i]['order'] = i;
  100. if (type == 'block' && page.textList[i]['type'] == 'picture') {
  101. page.textList[i]['content'] = get_url(page.textList[i]['content']);
  102. }
  103. }
  104. });
  105. }
  106. async function getWebName() {
  107. for (i in page.textList) {
  108. if (page.textList[i]['type'] != 'url') {
  109. continue;
  110. }
  111. (function (i) {
  112. var dt = new FormData();
  113. var obj = page.textList[i];
  114. dt.append('url', obj['content']);
  115. sendRequest('block/get_web_name', dt, function (data) {
  116. obj['title'] = data.name;
  117. Vue.set(page.textList, obj['order'], obj);
  118. });
  119. })(i);
  120. }
  121. }
  122. async function checkLike(list) {
  123. for (i in page.textList) {
  124. (function (i) {
  125. var dt = new FormData();
  126. var obj = list.textList[i];
  127. dt.append('collection_id', obj['id']);
  128. dt.append('phonenum', phonenum);
  129. sendRequest('collection/isLike', dt, function (data) {
  130. obj['isLike'] = data.isLike;
  131. Vue.set(list.textList, obj['order'], obj);
  132. });
  133. })(i);
  134. }
  135. }
  136. function get_url(f) {
  137. let reader = new FileReader();
  138. reader.readAsDataURL(f);
  139. reader.onload = function (e) {
  140. return e.target.result;
  141. };
  142. }
  143. var page = new Vue({
  144. el: "#textList",
  145. data: {
  146. textList: [],
  147. id: null,
  148. name: null,
  149. tag: null,
  150. content: null,
  151. },
  152. methods: {
  153. swap: function (obj_id, dir, type) {
  154. var url = type + '/swap';
  155. var obj = this.textList[obj_id];
  156. var pos = obj_id;
  157. var origin_pos = pos;
  158. if (dir == 'up' && pos > 0) {
  159. pos--;
  160. }
  161. if (dir == 'down' && pos < this.textList.length - 1) {
  162. pos++;
  163. }
  164. obj['order'] = pos;
  165. var swap_obj = this.textList[pos];
  166. swap_obj['order'] = origin_pos;
  167. Vue.set(this.textList, origin_pos, swap_obj);
  168. Vue.set(this.textList, pos, obj);
  169. var data = new FormData();
  170. data.append("new_order", pos);
  171. data.append("id", obj.id);
  172. if (type == 'block') {
  173. data.append('collection_id', page.id);
  174. }
  175. sendRequest(url, data, getContent(type));
  176. },
  177. delete_item: function (order, type) {
  178. var url = type + '/delete';
  179. var data = new FormData();
  180. data.append("collection_id", this.id);
  181. console.log(order);
  182. if (type == "block") {
  183. data.append("block_id", this.textList[order]['id']);
  184. }
  185. this.textList.splice(this.textList[order], 1);
  186. sendRequest(url, data, function () { ; });
  187. for (i in page.textList) {
  188. page.textList[i]['order'] = i;
  189. if (type == 'block' && page.textList[i]['type'] == 'picture') {
  190. page.textList[i]['content'] = get_url(page.textList[i]['content']);
  191. }
  192. }
  193. },
  194. jump_to: function (url, block_name, collection_id) {
  195. window.location.href = url + "?name=" + block_name + "&id=" + collection_id;
  196. },
  197. edit: function (order, type) {
  198. var obj = this.textList[order]
  199. var pos = obj['order'];
  200. var url = type + '/update';
  201. var data = new FormData();
  202. if (type == 'collection') {
  203. data.append('collection_id', obj['id']);
  204. if (this.name != null) {
  205. obj['name'] = this.name;
  206. data.append('name', this.name);
  207. }
  208. if (this.tag != null) {
  209. obj['tag'] = this.tag;
  210. data.append('tag', this.tag);
  211. }
  212. }
  213. else {
  214. data.append('collection_id', this.id);
  215. data.append('block_id', obj['id']);
  216. if (this.content != null) {
  217. obj['content'] = this.content;
  218. data.append('content', this.content);
  219. }
  220. }
  221. Vue.set(this.textList, pos, obj);
  222. sendRequest(url, data, function () { ; });
  223. this.name = null;
  224. this.tag = null;
  225. this.content = null;
  226. },
  227. add_item: function (type) {
  228. add(type);
  229. },
  230. like: function (order) {
  231. var obj = this.textList[order];
  232. obj['like']++;
  233. obj['isLike'] = true;
  234. Vue.set(this.textList, order, obj);
  235. var data = new FormData();
  236. data.append('collection_id', obj['id']);
  237. data.append('phonenum', phonenum);
  238. sendRequest('collection/like', data, function () { ; });
  239. },
  240. unlike: function (order) {
  241. var obj = this.textList[order];
  242. obj['like']--;
  243. obj['isLike'] = false;
  244. Vue.set(this.textList, order, obj);
  245. var data = new FormData();
  246. data.append('collection_id', obj['id']);
  247. data.append('phonenum', phonenum);
  248. sendRequest('collection/unlike', data, function () { ; });
  249. }
  250. }
  251. }
  252. )
  253. var recommend = new Vue({
  254. el: "#recommend",
  255. data: {
  256. textList: [],
  257. id: null,
  258. name: null,
  259. tag: null,
  260. content: null,
  261. },
  262. created: function () {
  263. var url;
  264. url = "collection/recommend";
  265. var data = new FormData();
  266. sendRequest(url, data, function (data) {
  267. recommend.textList = data.collections;
  268. checkLike(this);
  269. for (i in recommend.textList) {
  270. recommend.textList[i]['order'] = i;
  271. }
  272. });
  273. },
  274. methods: {
  275. jump_to: function (url, block_name) {
  276. window.location.href = url + "?name=" + block_name + "&id=null";
  277. },
  278. like: function (order) {
  279. var obj = this.textList[order];
  280. obj['like']++;
  281. obj['isLike'] = true;
  282. Vue.set(this.textList, order, obj);
  283. var data = new FormData();
  284. data.append('collection_id', obj['id']);
  285. data.append('phonenum', phonenum);
  286. sendRequest('collection/like', data, function () { ; });
  287. },
  288. unlike: function (order) {
  289. var obj = this.textList[order];
  290. obj['like']--;
  291. obj['isLike'] = false;
  292. Vue.set(this.textList, order, obj);
  293. var data = new FormData();
  294. data.append('collection_id', obj['id']);
  295. data.append('phonenum', phonenum);
  296. sendRequest('collection/unlike', data, function () { ; });
  297. }
  298. }
  299. }
  300. )
  301. function CHECK_URL(url) {
  302. //url= 协议://(ftp的登录信息)[IP|域名](:端口号)(/或?请求参数)
  303. var strRegex = '^((https|http|ftp)://)?'//(https或http或ftp):// 可有可无
  304. + '(([\\w_!~*\'()\\.&=+$%-]+: )?[\\w_!~*\'()\\.&=+$%-]+@)?' //ftp的user@ 可有可无
  305. + '(([0-9]{1,3}\\.){3}[0-9]{1,3}' // IP形式的URL- 3位数字.3位数字.3位数字.3位数字
  306. + '|' // 允许IP和DOMAIN(域名)
  307. + '(localhost)|' //匹配localhost
  308. + '([\\w_!~*\'()-]+\\.)*' // 域名- 至少一个[英文或数字_!~*\'()-]加上.
  309. + '\\w+\\.' // 一级域名 -英文或数字 加上.
  310. + '[a-zA-Z]{1,6})' // 顶级域名- 1-6位英文
  311. + '(:[0-9]{1,5})?' // 端口- :80 ,1-5位数字
  312. + '((/?)|' // url无参数结尾 - 斜杆或这没有
  313. + '(/[\\w_!~*\'()\\.;?:@&=+$,%#-]+)+/?)$';//请求参数结尾- 英文或数字和[]内的各种字符
  314. var strRegex1 = '^(?=^.{3,255}$)((http|https|ftp)?:\/\/)?(www\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\d+)*(\/)?(?:\/(.+)\/?$)?(\/\w+\.\w+)*([\?&]\w+=\w*|[\u4e00-\u9fa5]+)*$';
  315. var re = new RegExp(strRegex, 'i');//i不区分大小写
  316. console.log(re);
  317. //将url做uri转码后再匹配,解除请求参数中的中文和空字符影响
  318. if (re.test(encodeURI(url))) {
  319. return (true);
  320. } else {
  321. return (false);
  322. }
  323. }