material.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. var Controller = {
  3. index: () => {
  4. const { reactive, onMounted } = Vue
  5. const index = {
  6. setup() {
  7. const state = reactive({
  8. data: [],
  9. type: 'news'
  10. })
  11. function getData() {
  12. Fast.api.ajax({
  13. url: 'shopro/wechat/material',
  14. type: 'GET',
  15. data: {
  16. type: state.type,
  17. page: pagination.page,
  18. list_rows: pagination.list_rows,
  19. },
  20. }, function (ret, res) {
  21. state.data = res.data.data
  22. pagination.total = res.data.total
  23. return false
  24. }, function (ret, res) { })
  25. }
  26. function onChangeTab() {
  27. state.data = []
  28. pagination.page = 1
  29. getData()
  30. }
  31. const pagination = reactive({
  32. page: 1,
  33. list_rows: 10,
  34. total: 0,
  35. })
  36. function onAdd() {
  37. Fast.api.open(`shopro/wechat/material/add?type=add&materialType=${state.type}`, "添加", {
  38. callback() {
  39. getData()
  40. }
  41. })
  42. }
  43. function onEdit(id) {
  44. Fast.api.open(`shopro/wechat/material/edit?type=edit&materialType=${state.type}&id=${id}`, "编辑", {
  45. callback() {
  46. getData()
  47. }
  48. })
  49. }
  50. function onDelete(id) {
  51. Fast.api.ajax({
  52. url: `shopro/wechat/material/delete/id/${id}`,
  53. type: 'DELETE',
  54. }, function (ret, res) {
  55. getData()
  56. }, function (ret, res) { })
  57. }
  58. onMounted(() => {
  59. getData()
  60. })
  61. return {
  62. state,
  63. getData,
  64. onChangeTab,
  65. pagination,
  66. onAdd,
  67. onEdit,
  68. onDelete
  69. }
  70. }
  71. }
  72. createApp('index', index);
  73. },
  74. add: () => {
  75. Controller.form();
  76. },
  77. edit: () => {
  78. Controller.form();
  79. },
  80. form: () => {
  81. const { reactive, onMounted, getCurrentInstance } = Vue
  82. const addEdit = {
  83. setup() {
  84. const { proxy } = getCurrentInstance();
  85. const state = reactive({
  86. type: new URLSearchParams(location.search).get('type'),
  87. materialType: new URLSearchParams(location.search).get('materialType'),
  88. id: new URLSearchParams(location.search).get('id')
  89. })
  90. const form = reactive({
  91. model: {
  92. type: state.materialType,
  93. content: ''
  94. },
  95. rules: {
  96. content: [{ required: true, message: '请输入内容', trigger: 'blur' }],
  97. 'content.title': [{ required: true, message: '请输入标题', trigger: 'blur' }],
  98. 'content.url': [{ required: true, message: '请输入链接地址', trigger: 'blur' }],
  99. 'content.image': [{ required: true, message: '请选择图片', trigger: 'blur' }],
  100. 'content.description': [{ required: true, message: '请输入描述', trigger: 'blur' }],
  101. },
  102. })
  103. // 初始化数据
  104. if (form.model.type == 'text') {
  105. form.model.content = ''
  106. } else if (form.model.type == 'link') {
  107. form.model.content = {
  108. title: '',
  109. url: '',
  110. image: '',
  111. description: ''
  112. }
  113. }
  114. function getDetail() {
  115. Fast.api.ajax({
  116. url: `shopro/wechat/material/detail/id/${state.id}`,
  117. type: 'GET',
  118. }, function (ret, res) {
  119. form.model = res.data;
  120. return false
  121. }, function (ret, res) { })
  122. }
  123. const linkPopover = reactive({
  124. flag: false,
  125. form: {
  126. model: {
  127. text: '',
  128. href: '',
  129. },
  130. rules: {
  131. text: [{ required: true, message: '请输入文本内容', trigger: 'blur' }],
  132. href: [{ required: true, message: '请输入链接地址', trigger: 'blur' }],
  133. },
  134. },
  135. });
  136. function onConfirmLinkPopover() {
  137. proxy.$refs['linkFormRef'].validate((valid) => {
  138. if (valid) {
  139. form.model.content += `<a href="${linkPopover.form.model.href}" target="_blank">${linkPopover.form.model.text}</a>`;
  140. onCancelLinkPopover();
  141. }
  142. });
  143. }
  144. function onCancelLinkPopover() {
  145. linkPopover.flag = false;
  146. linkPopover.form.model = {
  147. text: '',
  148. href: '',
  149. };
  150. }
  151. function onConfirm() {
  152. proxy.$refs['formRef'].validate((valid) => {
  153. if (valid) {
  154. Fast.api.ajax({
  155. url: state.type == 'add' ? 'shopro/wechat/material/add' : `shopro/wechat/material/edit/id/${state.id}`,
  156. type: 'POST',
  157. data: form.model,
  158. }, function (ret, res) {
  159. Fast.api.close()
  160. }, function (ret, res) { })
  161. }
  162. });
  163. }
  164. onMounted(() => {
  165. state.type == 'edit' && getDetail()
  166. })
  167. return {
  168. state,
  169. form,
  170. linkPopover,
  171. onCancelLinkPopover,
  172. onConfirmLinkPopover,
  173. onConfirm
  174. }
  175. }
  176. }
  177. createApp('addEdit', addEdit);
  178. }
  179. };
  180. return Controller;
  181. });