classify.vue 6.8 KB

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