material.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. var Controller = {
  3. index: function () {
  4. $(window).resize(function () {
  5. init();
  6. })
  7. function init() {
  8. var itemWidth = $(".news-item").outerWidth(true);
  9. var cols = parseInt($(window).width() / itemWidth);
  10. var heightArr = [];
  11. for (var i = 0; i < cols; i++) {
  12. heightArr.push(0);
  13. }
  14. $('.news-item').each(function (index, item) {
  15. var idex = 0; //初始索引为0
  16. var minHeight = heightArr[0]; //初始设置最小高度是数组的第一个
  17. for (var i = 0; i < heightArr.length; i++) {
  18. if (heightArr[i] < minHeight) { //判断数组中的每一个是否比默认设置的最小高度小,小于直接赋值给最小高度
  19. minHeight = heightArr[i]; //最小高度
  20. idex = i; //当前索引
  21. }
  22. }
  23. //设置每个图片的样式
  24. $(item).css({
  25. left: itemWidth * idex,
  26. top: minHeight
  27. })
  28. heightArr[idex] += $(item).outerHeight(true); //高度对应的索引值就是当前图片高度的值
  29. })
  30. }
  31. var materialIndex = new Vue({
  32. el: "#materialIndex",
  33. data() {
  34. return {
  35. listData: [],
  36. currentPage: 1,
  37. totalPage: 0,
  38. offset: 0,
  39. activeName: 'news',
  40. scrollTop: 0
  41. }
  42. },
  43. mounted() {
  44. this.getlistData();
  45. window.addEventListener('scroll', this.handleScroll, true)
  46. },
  47. methods: {
  48. handleScroll() {
  49. this.scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
  50. var contentH = $(document).height();
  51. var contents = $('.custom-body').height();
  52. if (contentH - contents - this.scrollTop == 0) {
  53. this.offset += 20
  54. this.getlistData()
  55. }
  56. },
  57. operation(opttype, id) {
  58. let that = this;
  59. switch (opttype) {
  60. case 'delete':
  61. that.$confirm('此操作将删除菜单, 是否继续?', '提示', {
  62. confirmButtonText: '确定',
  63. cancelButtonText: '取消',
  64. type: 'warning'
  65. }).then(() => {
  66. Fast.api.ajax({
  67. url: 'shopro/wechat/material/del/ids/' + id,
  68. loading: true,
  69. type: 'POST',
  70. }, function (ret, res) {
  71. that.getlistData()
  72. })
  73. }).catch(() => {
  74. that.$message({
  75. type: 'info',
  76. message: '已取消删除'
  77. });
  78. });
  79. break;
  80. case 'create':
  81. Fast.api.open(`shopro/wechat/material/add?type=${that.activeName}`, "新建", {
  82. callback(data) {
  83. that.getlistData();
  84. }
  85. });
  86. break;
  87. case 'edit':
  88. Fast.api.open(`shopro/wechat/material/edit?id=${id}&type=${that.activeName}`, "编辑", {
  89. callback(data) {
  90. that.getlistData();
  91. }
  92. });
  93. break;
  94. }
  95. },
  96. getlistData() {
  97. var that = this;
  98. Fast.api.ajax({
  99. url: 'shopro/wechat/material/index',
  100. loading: true,
  101. type: 'GET',
  102. data: {
  103. offset: that.offset,
  104. type: that.activeName,
  105. }
  106. }, function (ret, res) {
  107. that.listData = res.data.rows;
  108. if (that.activeName == 'image') {
  109. that.listData.forEach(e => {
  110. e.arr = [];
  111. e.arr.push(e.url)
  112. })
  113. }
  114. that.totalPage = res.data.total;
  115. that.$nextTick(function () {
  116. init();
  117. })
  118. return false;
  119. })
  120. },
  121. pageCurrentChange(val) {
  122. this.offset = (val - 1) * 20
  123. this.getlistData()
  124. },
  125. tableRowClassName({
  126. rowIndex
  127. }) {
  128. if (rowIndex % 2 == 1) {
  129. return 'bg-color';
  130. }
  131. return '';
  132. },
  133. tableCellClassName({
  134. columnIndex
  135. }) {
  136. if (columnIndex == 0 || columnIndex == 1) {
  137. return 'cell-left';
  138. }
  139. return '';
  140. },
  141. },
  142. destroyed() {
  143. // 离开该页面需要移除这个监听的事件,不然会报错
  144. window.removeEventListener('scroll', this.handleScroll)
  145. },
  146. watch: {
  147. activeName() {
  148. this.currentPage = 1;
  149. this.totalPage = 0;
  150. this.offset = 0;
  151. this.listData = [];
  152. this.getlistData();
  153. },
  154. },
  155. })
  156. },
  157. add: function () {
  158. Controller.detailInit('add');
  159. },
  160. edit: function () {
  161. Controller.detailInit('edit');
  162. },
  163. detailInit: function (type) {
  164. var materialDetail = new Vue({
  165. el: "#materialDetail",
  166. data() {
  167. return {
  168. optType: type,
  169. sourceType: new URLSearchParams(location.search).get('type'),
  170. detailForm: {
  171. type: new URLSearchParams(location.search).get('type'),
  172. name: '',
  173. content: '',
  174. image:'',
  175. url:''
  176. },
  177. detailFormInit: {
  178. type: new URLSearchParams(location.search).get('type'),
  179. name: '',
  180. content: '',
  181. image:'',
  182. url:'',
  183. description:''
  184. },
  185. rules: {
  186. name: [{
  187. required: true,
  188. message: '请输入标题',
  189. trigger: 'blur'
  190. }],
  191. content: [{
  192. required: true,
  193. message: '请输入内容',
  194. trigger: 'blur'
  195. }],
  196. description: [{
  197. required: true,
  198. message: '请输入内容',
  199. trigger: 'blur'
  200. }],
  201. image: [{
  202. required: true,
  203. message: '请选择图片',
  204. trigger: 'blur'
  205. }],
  206. url: [{
  207. required: true,
  208. message: '请输入链接地址',
  209. trigger: 'blur'
  210. }],
  211. },
  212. storeOptions: [],
  213. text_id: null,
  214. hrefMsg: '',
  215. hrefA: '',
  216. visible: false,
  217. }
  218. },
  219. mounted() {
  220. if (this.optType == 'add') {
  221. this.detailForm = JSON.parse(JSON.stringify(this.detailFormInit));
  222. } else {
  223. this.text_id = Config.row.id;
  224. this.detailForm = JSON.parse(JSON.stringify(this.detailFormInit));
  225. this.detailForm.name = Config.row.name
  226. if(this.sourceType=='text'){
  227. this.detailForm.content = JSON.parse(Config.row.content);
  228. }else{
  229. let content=JSON.parse(Config.row.content)
  230. this.detailForm.image=content.image
  231. this.detailForm.description=content.description
  232. this.detailForm.url=content.url
  233. }
  234. }
  235. },
  236. methods: {
  237. addHref(type) {
  238. if (type) {
  239. if (this.hrefMsg && this.hrefA) {
  240. this.visible = false
  241. this.detailForm.content = this.detailForm.content + `<a href="${this.hrefA}">${this.hrefMsg}</a>`
  242. this.hrefMsg = ''
  243. this.hrefA = ''
  244. } else {
  245. this.$notify({
  246. title: '警告',
  247. message: '超链接请填写完整',
  248. type: 'warning'
  249. });
  250. }
  251. } else {
  252. this.visible = false
  253. this.hrefMsg = ''
  254. this.hrefA = ''
  255. }
  256. },
  257. dispatchSub(type, issub) {
  258. let that = this;
  259. if (type == 'yes') {
  260. this.$refs[issub].validate((valid) => {
  261. if (valid) {
  262. let subData=JSON.parse(JSON.stringify(that.detailForm));
  263. if(that.sourceType=='link'){
  264. subData.content={}
  265. subData.content.url=subData.url;
  266. subData.content.image=subData.image;
  267. subData.content.description=subData.description;
  268. }
  269. delete subData.url;
  270. delete subData.image;
  271. delete subData.description;
  272. if (this.optType != 'add') {
  273. Fast.api.ajax({
  274. url: 'shopro/wechat/material/edit?id=' + that.text_id,
  275. loading: true,
  276. type: "POST",
  277. data: {
  278. data: JSON.stringify(subData)
  279. }
  280. }, function (ret, res) {
  281. Fast.api.close({
  282. data: true
  283. })
  284. })
  285. } else {
  286. Fast.api.ajax({
  287. url: 'shopro/wechat/material/add',
  288. loading: true,
  289. type: "POST",
  290. data: {
  291. data: JSON.stringify(subData)
  292. }
  293. }, function (ret, res) {
  294. Fast.api.close({
  295. data: true
  296. })
  297. })
  298. }
  299. } else {
  300. return false;
  301. }
  302. });
  303. } else {
  304. Fast.api.close({
  305. data: false
  306. })
  307. }
  308. },
  309. addImg() {
  310. let that = this;
  311. parent.Fast.api.open("general/attachment/select?multiple=false", "选择图片", {
  312. callback: function (data) {
  313. that.detailForm.image = data.url;
  314. }
  315. });
  316. return false;
  317. },
  318. delImg() {
  319. this.detailForm.image = '';
  320. },
  321. },
  322. })
  323. },
  324. api: {
  325. bindevent: function () {
  326. Form.api.bindevent($("form[role=form]"));
  327. }
  328. }
  329. };
  330. return Controller;
  331. });