reply.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. group: 'keywords',
  10. })
  11. function getData() {
  12. Fast.api.ajax({
  13. url: 'shopro/wechat/reply',
  14. type: 'GET',
  15. data: {
  16. group: state.group,
  17. },
  18. }, function (ret, res) {
  19. state.data = res.data
  20. return false
  21. }, function (ret, res) { })
  22. }
  23. function onAdd() {
  24. Fast.api.open(`shopro/wechat/reply/add?type=add&group=${state.group}`, "添加", {
  25. callback() {
  26. getData()
  27. }
  28. })
  29. }
  30. function onEdit(id) {
  31. Fast.api.open(`shopro/wechat/reply/edit?type=edit&group=${state.group}&id=${id}`, "编辑", {
  32. callback() {
  33. getData()
  34. }
  35. })
  36. }
  37. function onDelete(id) {
  38. Fast.api.ajax({
  39. url: `shopro/wechat/reply/delete/id/${id}`,
  40. type: 'DELETE',
  41. }, function (ret, res) {
  42. getData()
  43. }, function (ret, res) { })
  44. }
  45. function onChangeStatus(item) {
  46. Fast.api.ajax({
  47. url: `shopro/wechat/reply/edit/id/${item.id}`,
  48. type: 'POST',
  49. data: {
  50. status: item.status,
  51. },
  52. }, function (ret, res) {
  53. getData()
  54. }, function (ret, res) { })
  55. }
  56. onMounted(() => {
  57. getData()
  58. })
  59. return {
  60. state,
  61. getData,
  62. onAdd,
  63. onEdit,
  64. onDelete,
  65. onChangeStatus,
  66. }
  67. }
  68. }
  69. createApp('index', index);
  70. },
  71. add: () => {
  72. Controller.form();
  73. },
  74. edit: () => {
  75. Controller.form();
  76. },
  77. form: () => {
  78. const { reactive, ref, onMounted, getCurrentInstance } = Vue
  79. const addEdit = {
  80. setup() {
  81. const { proxy } = getCurrentInstance();
  82. const state = reactive({
  83. type: new URLSearchParams(location.search).get('type'),
  84. group: new URLSearchParams(location.search).get('group'),
  85. id: new URLSearchParams(location.search).get('id')
  86. })
  87. const form = reactive({
  88. model: {
  89. group: state.group,
  90. keywords: [],
  91. type: 'news',
  92. content: '1',
  93. status: 'enable',
  94. },
  95. rules: {
  96. keywords: [{ required: true, message: '请输入关键字', trigger: 'blur' }],
  97. content: [{ required: true, message: '请选择回复内容', trigger: 'blur' }],
  98. },
  99. })
  100. function getDetail() {
  101. Fast.api.ajax({
  102. url: `shopro/wechat/reply/detail/id/${state.id}`,
  103. type: 'GET',
  104. }, function (ret, res) {
  105. form.model = res.data;
  106. getMaterialSelect()
  107. return false
  108. }, function (ret, res) { })
  109. }
  110. const keywords = ref('')
  111. function onAddKeywords(val) {
  112. if (val.trim()) {
  113. if (form.model.keywords.indexOf(val.trim()) == -1) {
  114. form.model.keywords.push(val.trim());
  115. keywords.value = '';
  116. } else {
  117. ElMessage({
  118. message: '已存在不可再次添加.',
  119. type: 'warning',
  120. });
  121. }
  122. } else {
  123. ElMessage({
  124. message: '请输入关键字.',
  125. type: 'warning',
  126. });
  127. }
  128. }
  129. function onDeleteKeywords(index) {
  130. form.model.keywords.splice(index, 1);
  131. }
  132. function onChangeType() {
  133. material.pagination.page = 1
  134. getMaterialSelect()
  135. }
  136. const material = reactive({
  137. select: [],
  138. pagination: {
  139. page: 1,
  140. list_rows: 10,
  141. total: 0,
  142. }
  143. })
  144. function getMaterialSelect() {
  145. Fast.api.ajax({
  146. url: 'shopro/wechat/material/select',
  147. type: 'GET',
  148. data: {
  149. type: form.model.type,
  150. page: material.pagination.page,
  151. list_rows: material.pagination.list_rows,
  152. },
  153. }, function (ret, res) {
  154. material.select = initMaterialData(res.data.data,form.model.type)
  155. material.pagination.total = res.data.total
  156. return false
  157. }, function (ret, res) { })
  158. }
  159. function initMaterialData(data, type) {
  160. let options = [];
  161. if (type == 'news') {
  162. data.forEach((i) => {
  163. i.content.news_item.forEach((e) => {
  164. options.push({
  165. media_id: i.media_id,
  166. title: e.title,
  167. thumb_url: e.thumb_url,
  168. type,
  169. });
  170. });
  171. });
  172. } else if (type == 'image') {
  173. data.forEach((i) => {
  174. options.push({
  175. media_id: i.media_id,
  176. title: i.name,
  177. thumb_url: i.url,
  178. type,
  179. });
  180. });
  181. } else if (type == 'video') {
  182. data.forEach((i) => {
  183. options.push({
  184. media_id: i.media_id,
  185. title: i.name,
  186. thumb_url: i.cover_url,
  187. type,
  188. });
  189. });
  190. } else if (type == 'voice') {
  191. data.forEach((i) => {
  192. options.push({
  193. media_id: i.media_id,
  194. title: i.name,
  195. thumb_url: '',
  196. type,
  197. });
  198. });
  199. } else if (type == 'text') {
  200. data.forEach((i) => {
  201. options.push({
  202. media_id: i.id,
  203. title: i.content,
  204. thumb_url: i.content,
  205. type,
  206. });
  207. });
  208. } else if (type == 'link') {
  209. data.forEach((i) => {
  210. options.push({
  211. media_id: i.id,
  212. title: i.content.title,
  213. thumb_url: i.content.image,
  214. description: i.content.description,
  215. type,
  216. });
  217. });
  218. }
  219. return options;
  220. }
  221. function onConfirm() {
  222. proxy.$refs['formRef'].validate((valid) => {
  223. if (valid) {
  224. Fast.api.ajax({
  225. url: state.type == 'add' ? 'shopro/wechat/reply/add' : `shopro/wechat/reply/edit/id/${state.id}`,
  226. type: 'POST',
  227. data: form.model,
  228. }, function (ret, res) {
  229. Fast.api.close()
  230. }, function (ret, res) { })
  231. }
  232. });
  233. }
  234. onMounted(() => {
  235. state.type == 'add' && getMaterialSelect()
  236. state.type == 'edit' && getDetail()
  237. })
  238. return {
  239. state,
  240. form,
  241. keywords,
  242. onAddKeywords,
  243. onDeleteKeywords,
  244. onChangeType,
  245. material,
  246. getMaterialSelect,
  247. onConfirm
  248. }
  249. }
  250. }
  251. createApp('addEdit', addEdit);
  252. }
  253. };
  254. return Controller;
  255. });