classify.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <template>
  2. <view class="classify-list-page">
  3. <drag
  4. ref="dragIns"
  5. generic:item="classify-item"
  6. :columns="1"
  7. :list-data="list"
  8. :itemHeight="88"
  9. @sortend="sortend"
  10. @click="ItemClick"
  11. @scroll="chartScroll"
  12. :scroll-top="scrollTop"
  13. ></drag>
  14. <view class="add-classify-btn" @click="showAdd=true">添加分类</view>
  15. <van-dialog
  16. use-slot
  17. :title="editId?'编辑分类名称':'添加分类名称'"
  18. :show="showAdd"
  19. confirmButtonText="确定"
  20. show-cancel-button
  21. @close="showAdd=false"
  22. @confirm="handleConfirmAdd"
  23. >
  24. <input class="add-input" v-model="inputClassifyVal" maxlength="10" type="text" placeholder="请输入分类名称">
  25. <view style="color:#999;padding-left:10%;margin-bottom:40rpx;font-size:12px">注:字数控制在10个字以内!</view>
  26. </van-dialog>
  27. <van-dialog id="van-dialog" />
  28. </view>
  29. </template>
  30. <script>
  31. import classifyItem from './components/classifyItem.vue'
  32. import {
  33. apiMyChartClassifyAdd,
  34. apiMyChartClassifyEdit,
  35. apiMyChartClassifyDel,
  36. apiMyChartClassifySort,
  37. apiMyChartClassifyList
  38. } from '@/api/myChart'
  39. export default {
  40. components:{
  41. 'classify-item':classifyItem
  42. },
  43. watch:{
  44. showAdd(n){
  45. if(!n){
  46. this.editId=0
  47. this.inputClassifyVal=''
  48. }
  49. }
  50. },
  51. data() {
  52. return {
  53. dragIns:null,
  54. list:[],
  55. showAdd:false,
  56. inputClassifyVal:'',
  57. editId:0,
  58. }
  59. },
  60. onLoad(){
  61. this.getClassifyList()
  62. },
  63. methods: {
  64. // 获取分类数据
  65. async getClassifyList(){
  66. const res=await apiMyChartClassifyList()
  67. if(res.code===200){
  68. const arr=res.data||[]
  69. this.list=arr.map(item=>{
  70. return {
  71. ...item,
  72. dragId:item.my_chart_classify_id
  73. }
  74. })
  75. setTimeout(() => {
  76. this.dragIns=this.$refs.dragIns
  77. this.dragIns.init();// 初始化列表
  78. }, 100);
  79. }
  80. },
  81. ItemClick(e){
  82. const item=e.detail.data
  83. const type=e.detail.extra.__args__[0]?.optType||''
  84. if(type==='del'){
  85. this.handleDel(item)
  86. }else if(type==='edit'){
  87. this.handleEdit(item)
  88. }else{
  89. uni.navigateTo({
  90. url: `/pages-myChart/list?classifyId=${item.my_chart_classify_id}&classifyName=${item.my_chart_classify_name}`,
  91. success: (result) => {},
  92. fail: () => {},
  93. complete: () => {}
  94. });
  95. }
  96. },
  97. // 删除
  98. handleDel(item){
  99. this.$dialog.confirm({
  100. title: '',
  101. message: `是否确认删除分类${item.my_chart_classify_name}?`,
  102. showCancelButton:true,
  103. confirmButtonText:'确定'
  104. })
  105. .then(() => {
  106. // on confirm
  107. apiMyChartClassifyDel({
  108. classify_id:item.my_chart_classify_id
  109. }).then(res=>{
  110. if(res.code===200){
  111. uni.showToast({
  112. title:"删除成功",
  113. icon:"none"
  114. })
  115. this.getClassifyList()
  116. }else if(res.code===4001){
  117. this.$dialog.alert({
  118. title: '',
  119. message: '删除失败,该分类下有图表',
  120. showConfirmButton:false,
  121. showCancelButton:true,
  122. cancelButtonText:'知道了'
  123. }).then(() => {
  124. // on close
  125. });
  126. }
  127. })
  128. })
  129. .catch(() => {
  130. // on cancel
  131. });
  132. },
  133. //编辑
  134. handleEdit(item){
  135. this.editId=item.my_chart_classify_id
  136. this.inputClassifyVal=item.my_chart_classify_name
  137. this.showAdd=true
  138. },
  139. //排序
  140. async sortend(e){
  141. // curIndex 为排序前元素所在位置 listData为排序后的数组
  142. let {curIndex,listData}=e.detail
  143. console.log(listData);
  144. const arr=listData.map((item,index)=>{
  145. return {
  146. sort:index+1,
  147. classify_id:item.my_chart_classify_id
  148. }
  149. })
  150. const res=await apiMyChartClassifySort([...arr])
  151. if(res.code!==200){
  152. uni.showToast({
  153. title: res.msg,
  154. icon: 'none'
  155. })
  156. }
  157. },
  158. async handleConfirmAdd(){
  159. if(!this.inputClassifyVal){
  160. uni.showToast({
  161. title: '请输入分类名称',
  162. icon: 'none',
  163. });
  164. return
  165. }
  166. let res
  167. if(this.editId){
  168. res=await apiMyChartClassifyEdit({
  169. classify_name:this.inputClassifyVal,
  170. classify_id:this.editId
  171. })
  172. }else{
  173. res=await apiMyChartClassifyAdd({
  174. classify_name:this.inputClassifyVal
  175. })
  176. }
  177. if(res.code===200){
  178. uni.showToast({
  179. title: `${this.editId?'编辑':'新增'}成功`,
  180. icon: 'none',
  181. });
  182. this.showAdd=false
  183. this.getClassifyList()
  184. }
  185. }
  186. },
  187. }
  188. </script>
  189. <style lang="scss" scoped>
  190. .classify-list-page{
  191. padding: 34rpx 34rpx 120rpx 34rpx;
  192. }
  193. .add-classify-btn{
  194. position: fixed;
  195. left: 0;
  196. bottom: 0;
  197. right: 0;
  198. font-size: 32rpx;
  199. padding-top: 18rpx;
  200. text-align: center;
  201. color: #E3B377;
  202. background: #333333;
  203. box-shadow: 0px 4rpx 20rpx rgba(160, 126, 84, 0.25);
  204. z-index: 99;
  205. padding-bottom: calc(18rpx + constant(safe-area-inset-bottom));
  206. padding-bottom: calc(18rpx + env(safe-area-inset-bottom));
  207. }
  208. .add-input{
  209. display: block;
  210. width: 80%;
  211. background: #F7F8FA;
  212. border-radius: 8px;
  213. padding: 20rpx;
  214. margin: 30rpx auto 10rpx auto;
  215. }
  216. </style>