dispatch.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. dispatch_type: 'express',
  9. data: [],
  10. })
  11. function getData() {
  12. Fast.api.ajax({
  13. url: 'shopro/dispatch/dispatch',
  14. type: 'GET',
  15. data: {
  16. page: pagination.page,
  17. list_rows: pagination.list_rows,
  18. type: state.dispatch_type
  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. const pagination = reactive({
  27. page: 1,
  28. list_rows: 10,
  29. total: 0,
  30. })
  31. function onChangeTab() {
  32. pagination.page = 1
  33. getData()
  34. }
  35. function onAdd() {
  36. Fast.api.open(`shopro/dispatch/dispatch/add?type=add&dispatch_type=${state.dispatch_type}`, "添加", {
  37. callback() {
  38. getData()
  39. }
  40. })
  41. }
  42. function onEdit(id) {
  43. Fast.api.open(`shopro/dispatch/dispatch/edit?type=edit&id=${id}&dispatch_type=${state.dispatch_type}`, "编辑", {
  44. callback() {
  45. getData()
  46. }
  47. })
  48. }
  49. function onCopy(id) {
  50. Fast.api.open(`shopro/dispatch/dispatch/add?type=copy&id=${id}&dispatch_type=${state.dispatch_type}`, "复制", {
  51. callback() {
  52. getData()
  53. }
  54. })
  55. }
  56. function onDelete(id) {
  57. Fast.api.ajax({
  58. url: `shopro/dispatch/dispatch/delete/id/${id}?type=${state.dispatch_type}`,
  59. type: 'DELETE',
  60. }, function (ret, res) {
  61. getData()
  62. }, function (ret, res) { })
  63. }
  64. onMounted(() => {
  65. getData()
  66. })
  67. return {
  68. state,
  69. getData,
  70. pagination,
  71. onChangeTab,
  72. onAdd,
  73. onEdit,
  74. onCopy,
  75. onDelete
  76. }
  77. }
  78. }
  79. createApp('index', index);
  80. },
  81. add: () => {
  82. Controller.form();
  83. },
  84. edit: () => {
  85. Controller.form();
  86. },
  87. form: () => {
  88. const { reactive, onMounted, getCurrentInstance } = Vue
  89. const addEdit = {
  90. setup() {
  91. const { proxy } = getCurrentInstance();
  92. const state = reactive({
  93. type: new URLSearchParams(location.search).get('type'),
  94. id: new URLSearchParams(location.search).get('id'),
  95. dispatch_type: new URLSearchParams(location.search).get('dispatch_type'),
  96. priceType: 'number',
  97. })
  98. const form = reactive({
  99. model: {
  100. // dispatch_type: state.dispatch_type,
  101. name: '',
  102. type: state.dispatch_type,
  103. express: [],
  104. autosend: {
  105. type: "text",
  106. content: ""
  107. }
  108. },
  109. rules: {
  110. name: [{ required: true, message: '请输入模板名称', trigger: 'blur' }],
  111. express: {
  112. first_num: [{ required: true, message: '请输入数量', trigger: 'blur' }],
  113. first_price: [{ required: true, message: '请输入运费', trigger: 'blur' }],
  114. additional_num: [{ required: true, message: '请输入数量', trigger: 'blur' }],
  115. additional_price: [{ required: true, message: '请输入续费', trigger: 'blur' }],
  116. district_text: [{ required: true, message: '请选择可配送区域', trigger: 'blur' }],
  117. }
  118. },
  119. })
  120. function getDetail() {
  121. Fast.api.ajax({
  122. url: `shopro/dispatch/dispatch/detail/id/${state.id}`,
  123. type: 'GET',
  124. data: {
  125. type: state.dispatch_type,
  126. }
  127. }, function (ret, res) {
  128. form.model = res.data;
  129. if (state.dispatch_type == 'express') {
  130. state.priceType = form.model.express.length > 0 ? form.model.express[0].type : 'number';
  131. }
  132. return false
  133. }, function (ret, res) { })
  134. }
  135. function onAddTemplate() {
  136. form.model.express.push({
  137. type: state.priceType,
  138. first_num: 0,
  139. first_price: 0,
  140. additional_num: 0,
  141. additional_price: 0,
  142. province_ids: '',
  143. city_ids: '',
  144. district_ids: '',
  145. });
  146. }
  147. function onDeleteTemplate(index) {
  148. form.model.express.splice(index, 1);
  149. }
  150. function onSelectArea(index) {
  151. let selected = {
  152. province: form.model.express[index].province_ids,
  153. city: form.model.express[index].city_ids,
  154. district: form.model.express[index].district_ids,
  155. }
  156. Fast.api.open(`shopro/data/area/select?selected=${encodeURI(JSON.stringify(selected))}`, "选择地区", {
  157. callback(data) {
  158. let text = [];
  159. for (var key in data) {
  160. let ids = [];
  161. for (var id in data[key]) {
  162. ids.push(id);
  163. text.push(data[key][id]);
  164. }
  165. form.model.express[index][key + '_ids'] = ids.join(',');
  166. }
  167. form.model.express[index].district_text = text.join(',');
  168. }
  169. })
  170. }
  171. function onChangeAutosendType(type) {
  172. form.model.autosend.content = type == 'text' ? '' : []
  173. }
  174. function onAddContent() {
  175. console.log(123, form.model.autosend.content)
  176. if(!form.model.autosend.content){
  177. form.model.autosend.content=[]
  178. }
  179. form.model.autosend.content.push({
  180. title: '',
  181. content: '',
  182. });
  183. }
  184. function onDeleteContent(index) {
  185. form.model.autosend.content.splice(index, 1);
  186. }
  187. function onConfirm() {
  188. proxy.$refs['formRef'].validate((valid) => {
  189. if (valid) {
  190. let submitForm = JSON.parse(JSON.stringify(form.model));
  191. if (state.dispatch_type == 'express') {
  192. submitForm.express.forEach((item) => {
  193. item.type = state.priceType;
  194. });
  195. if (state.type == 'copy') {
  196. delete submitForm.id;
  197. submitForm.express.forEach((item) => {
  198. delete item.id;
  199. });
  200. }
  201. }else if(state.dispatch_type == 'autosend'){
  202. if (state.type == 'copy') {
  203. delete submitForm.id;
  204. }
  205. }
  206. Fast.api.ajax({
  207. url: state.type == 'add' || state.type == 'copy' ? 'shopro/dispatch/dispatch/add' : `shopro/dispatch/dispatch/edit/id/${state.id}`,
  208. type: 'POST',
  209. data: submitForm,
  210. }, function (ret, res) {
  211. Fast.api.close()
  212. }, function (ret, res) { })
  213. }
  214. });
  215. }
  216. onMounted(() => {
  217. (state.type == 'edit' || state.type == 'copy') && getDetail()
  218. })
  219. return {
  220. state,
  221. form,
  222. onAddTemplate,
  223. onDeleteTemplate,
  224. onSelectArea,
  225. onChangeAutosendType,
  226. onAddContent,
  227. onDeleteContent,
  228. onConfirm
  229. }
  230. }
  231. }
  232. createApp('addEdit', addEdit);
  233. }
  234. };
  235. return Controller;
  236. });