index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. const _Http = getApp().globalData.http;
  2. Page({
  3. data: {
  4. list: [],
  5. loading: false,
  6. showModal: false,
  7. isEdit: false,
  8. formData: {
  9. sys_enterprise_stockid: 0,
  10. stockno: '',
  11. stockname: '',
  12. isused: '1'
  13. },
  14. content: {
  15. nocache: true,
  16. pageNumber: 1,
  17. pageSize: 20,
  18. pageTotal: 1,
  19. total: null
  20. }
  21. },
  22. onLoad() {
  23. this.getList(true);
  24. },
  25. onSearch(e) {
  26. const keyword = e.detail.value || "";
  27. this.setData({
  28. "content.pageNumber": 1,
  29. "content.where": {
  30. condition: keyword
  31. }
  32. });
  33. this.getList(true);
  34. },
  35. getList(init) {
  36. if (this.data.loading) return;
  37. if (init.detail != undefined) init = init.detail;
  38. let content = this.data.content;
  39. if (init) {
  40. content.pageNumber = 1;
  41. }
  42. this.setData({ loading: true });
  43. _Http.basic({
  44. "id": "2026031714375701",
  45. content
  46. }).then(res => {
  47. this.setData({ loading: false });
  48. console.log("仓库列表", res)
  49. this.selectComponent('#ListBox').RefreshToComplete();
  50. if (res.code != 1) return wx.showToast({
  51. title: res.msg,
  52. icon: "none"
  53. })
  54. this.setData({
  55. list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
  56. "content.pageNumber": res.pageNumber + 1,
  57. "content.pageSize": res.pageSize,
  58. "content.pageTotal": res.pageTotal,
  59. "content.total": res.total
  60. })
  61. })
  62. },
  63. deleteWarehouse(e) {
  64. const id = e.currentTarget.dataset.id;
  65. wx.showModal({
  66. title: '确认删除',
  67. content: '确定要删除这个仓库吗?',
  68. success: (res) => {
  69. if (res.confirm) {
  70. // 调用API删除仓库
  71. _Http.basic({
  72. "id": "2026031714454901",
  73. content: {
  74. sys_enterprise_stockid: id
  75. }
  76. }).then(res => {
  77. console.log("删除仓库", res)
  78. if (res.code != 1) {
  79. return wx.showToast({
  80. title: res.msg,
  81. icon: "none"
  82. });
  83. }
  84. // 删除成功,刷新列表
  85. wx.showToast({ title: '删除成功', icon: 'success' });
  86. this.getList(true);
  87. }).catch(() => {
  88. wx.showToast({ title: '网络错误', icon: 'none' });
  89. })
  90. }
  91. }
  92. });
  93. },
  94. addWarehouse() {
  95. // 重置表单数据
  96. this.initFormData();
  97. this.setData({
  98. showModal: true,
  99. isEdit: false
  100. });
  101. },
  102. editWarehouse(e) {
  103. const id = e.currentTarget.dataset.id;
  104. // 查找要编辑的仓库数据
  105. const warehouse = this.data.list.find(item => item.sys_enterprise_stockid == id);
  106. if (warehouse) {
  107. this.setData({
  108. showModal: true,
  109. isEdit: true,
  110. formData: {
  111. sys_enterprise_stockid: warehouse.sys_enterprise_stockid,
  112. stockno: warehouse.stockno || '',
  113. stockname: warehouse.stockname || '',
  114. isused: warehouse.isused.toString()
  115. }
  116. });
  117. }
  118. },
  119. closeModal() {
  120. this.setData({ showModal: false });
  121. // 关闭后初始化表单
  122. this.initFormData();
  123. },
  124. initFormData() {
  125. this.setData({
  126. formData: {
  127. sys_enterprise_stockid: 0,
  128. stockno: '',
  129. stockname: '',
  130. isused: '1'
  131. }
  132. });
  133. },
  134. handleInput(e) {
  135. const field = e.currentTarget.dataset.field;
  136. const value = e.detail;
  137. this.setData({
  138. [`formData.${field}`]: value
  139. });
  140. },
  141. handleRadioChange(e) {
  142. this.setData({
  143. 'formData.isused': e.detail
  144. });
  145. },
  146. saveWarehouse() {
  147. const { stockno, stockname } = this.data.formData;
  148. // 验证必填字段
  149. if (!stockno) {
  150. return wx.showToast({ title: '请输入仓库编码', icon: 'none' });
  151. }
  152. if (!stockname) {
  153. return wx.showToast({ title: '请输入仓库名称', icon: 'none' });
  154. }
  155. // 调用API保存仓库
  156. _Http.basic({
  157. "id": "2026031714422501",
  158. content: {
  159. ...this.data.formData,
  160. isused: parseInt(this.data.formData.isused)
  161. }
  162. }).then(res => {
  163. console.log("保存仓库", res)
  164. if (res.code != 1) {
  165. return wx.showToast({
  166. title: res.msg,
  167. icon: "none"
  168. });
  169. }
  170. // 保存成功,关闭模态框并刷新列表
  171. wx.showToast({ title: '保存成功', icon: 'success' });
  172. this.setData({ showModal: false });
  173. this.initFormData();
  174. this.getList(true);
  175. }).catch(() => {
  176. wx.showToast({ title: '网络错误', icon: 'none' });
  177. })
  178. }
  179. });