area.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. order: 'asc',
  10. sort: '',
  11. })
  12. function getData() {
  13. Fast.api.ajax({
  14. url: 'shopro/data/area',
  15. type: 'GET',
  16. data: {
  17. order: state.order,
  18. sort: state.sort,
  19. },
  20. }, function (ret, res) {
  21. state.data = res.data
  22. return false
  23. }, function (ret, res) { })
  24. }
  25. function onAdd() {
  26. Fast.api.open("shopro/data/area/add?type=add", "添加", {
  27. callback() {
  28. getData()
  29. }
  30. })
  31. }
  32. function onEdit(id) {
  33. Fast.api.open(`shopro/data/area/edit?type=edit&id=${id}`, "编辑", {
  34. callback() {
  35. getData()
  36. }
  37. })
  38. }
  39. function onDelete(id) {
  40. Fast.api.ajax({
  41. url: `shopro/data/area/delete/id/${id}`,
  42. type: 'DELETE',
  43. }, function (ret, res) {
  44. getData()
  45. }, function (ret, res) { })
  46. }
  47. onMounted(() => {
  48. getData()
  49. })
  50. return {
  51. state,
  52. getData,
  53. onAdd,
  54. onEdit,
  55. onDelete
  56. }
  57. }
  58. }
  59. createApp('index', index);
  60. },
  61. add: () => {
  62. Controller.form();
  63. },
  64. edit: () => {
  65. Controller.form();
  66. },
  67. form: () => {
  68. const { reactive, onMounted, getCurrentInstance } = Vue
  69. const addEdit = {
  70. setup() {
  71. const { proxy } = getCurrentInstance();
  72. const state = reactive({
  73. type: new URLSearchParams(location.search).get('type'),
  74. id: new URLSearchParams(location.search).get('id')
  75. })
  76. const form = reactive({
  77. model: {
  78. pid: 0,
  79. id: '',
  80. name: '',
  81. },
  82. rules: {
  83. id: [{ required: true, message: '请输入行政区ID', trigger: 'blur' }],
  84. name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
  85. },
  86. })
  87. function getDetail() {
  88. Fast.api.ajax({
  89. url: `shopro/data/area/detail/id/${state.id}`,
  90. type: 'GET',
  91. }, function (ret, res) {
  92. form.model = res.data;
  93. return false
  94. }, function (ret, res) { })
  95. }
  96. const area = reactive({
  97. select: []
  98. })
  99. function getAreaSelect() {
  100. Fast.api.ajax({
  101. url: 'shopro/data/area/select',
  102. type: 'GET',
  103. }, function (ret, res) {
  104. area.select = res.data;
  105. return false
  106. }, function (ret, res) { })
  107. }
  108. function onConfirm() {
  109. proxy.$refs['formRef'].validate((valid) => {
  110. if (valid) {
  111. Fast.api.ajax({
  112. url: state.type == 'add' ? 'shopro/data/area/add' : `shopro/data/area/edit/old_id/${state.id}`,
  113. type: 'POST',
  114. data: form.model,
  115. }, function (ret, res) {
  116. Fast.api.close()
  117. }, function (ret, res) { })
  118. }
  119. });
  120. }
  121. onMounted(() => {
  122. getAreaSelect()
  123. state.type == 'edit' && getDetail()
  124. })
  125. return {
  126. state,
  127. form,
  128. area,
  129. onConfirm
  130. }
  131. }
  132. }
  133. createApp('addEdit', addEdit);
  134. },
  135. select: () => {
  136. const { reactive, computed, onMounted, getCurrentInstance, nextTick } = Vue
  137. const select = {
  138. setup() {
  139. const { proxy } = getCurrentInstance();
  140. const state = reactive({
  141. selected: JSON.parse(new URLSearchParams(location.search).get('selected')),
  142. data: [],
  143. ids: [],
  144. label: {},
  145. checkedAll: false,
  146. })
  147. function getSelect() {
  148. Fast.api.ajax({
  149. url: 'shopro/data/area/select',
  150. type: 'GET',
  151. }, function (ret, res) {
  152. state.data = res.data
  153. state.checkedAll = state.selected.province.split(',').length == state.data.length;
  154. nextTick(() => {
  155. proxy.$refs['treeRef']?.getCheckedNodes().forEach((data) => {
  156. if (state.ids.includes(data.id)) {
  157. if (!state.label[data.level]) {
  158. state.label[data.level] = {};
  159. }
  160. state.label[data.level][data.id] = data.name;
  161. }
  162. });
  163. });
  164. return false
  165. }, function (ret, res) { })
  166. }
  167. const isIndeterminate = computed(() => (state.ids.length > 0 && !state.checkedAll ? true : false));
  168. function onChange() {
  169. if (state.checkedAll) {
  170. nextTick(() => {
  171. state.ids = [];
  172. proxy.$refs['treeRef']?.setCheckedNodes(state.data, false);
  173. state.data.forEach((d) => {
  174. state.ids.push(d.id);
  175. if (!state.label[d.level]) {
  176. state.label[d.level] = {};
  177. }
  178. state.label[d.level][d.id] = d.name;
  179. });
  180. });
  181. } else {
  182. proxy.$refs['treeRef']?.setCheckedKeys([], false);
  183. state.ids = [];
  184. state.label = {
  185. province: {},
  186. city: {},
  187. district: {},
  188. };
  189. }
  190. }
  191. function onChangeCheck(data, checked, indeterminate) {
  192. // 全选
  193. if (!state.checkedAll) {
  194. // 选中(把自己放进去)
  195. if (checked) {
  196. state.ids.push(data.id);
  197. if (!state.label[data.level]) {
  198. state.label[data.level] = {};
  199. }
  200. state.label[data.level][data.id] = data.name;
  201. if (!indeterminate) {
  202. deleteIds(data);
  203. }
  204. }
  205. }
  206. // 未选中(把自己删除)
  207. if (!checked) {
  208. if (state.ids.includes(data.id)) {
  209. state.ids.splice(state.ids.indexOf(data.id), 1);
  210. for (var key in state.label[data.level]) {
  211. if (Number(key) == Number(data.id)) {
  212. delete state.label[data.level][key];
  213. }
  214. }
  215. }
  216. if (indeterminate) {
  217. addIds(data);
  218. }
  219. }
  220. if (state.label.province)
  221. state.checkedAll = Object.keys(state.label.province).length == state.data.length;
  222. }
  223. function deleteIds(data) {
  224. let keys = proxy.$refs['treeRef'].getCheckedKeys();
  225. if (data.children && data.children.length > 0) {
  226. data.children.forEach((d) => {
  227. if (keys.includes(d.id) && state.ids.includes(d.id)) {
  228. state.ids.splice(state.ids.indexOf(d.id), 1);
  229. delete state.label[d.level][d.id];
  230. }
  231. deleteIds(d);
  232. });
  233. }
  234. // TODO: 需要修复
  235. }
  236. function addIds(data) {
  237. let keys = proxy.$refs['treeRef'].getCheckedKeys();
  238. if (data.children && data.children.length > 0) {
  239. data.children.forEach((d) => {
  240. if (keys.includes(d.id) && !state.ids.includes(d.id)) {
  241. state.ids.push(d.id);
  242. if (!state.label[d.level]) {
  243. state.label[d.level] = {};
  244. }
  245. state.label[d.level][d.id] = d.name;
  246. }
  247. });
  248. }
  249. }
  250. function onConfirm() {
  251. Fast.api.close(state.label)
  252. }
  253. onMounted(() => {
  254. // state.ids 赋值
  255. for (var level in state.selected) {
  256. if (state.selected[level]) {
  257. state.selected[level].split(',').forEach((id) => {
  258. state.ids.push(Number(id));
  259. });
  260. }
  261. }
  262. getSelect()
  263. })
  264. return {
  265. state,
  266. isIndeterminate,
  267. onChange,
  268. onChangeCheck,
  269. onConfirm,
  270. }
  271. }
  272. }
  273. createApp('select', select);
  274. },
  275. };
  276. return Controller;
  277. });