classify.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <template>
  2. <view class="classify-list-page">
  3. <classifyItem :itemData="list.find(i=>i.my_chart_classify_id===-1)" @click="clickItem"></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. clickItem(){
  99. const item = this.list.find(i=>i.my_chart_classify_id===-1)
  100. if(!item) return
  101. uni.navigateTo({
  102. url: `/pages-myChart/list?classifyId=${item.my_chart_classify_id}&classifyName=${item.my_chart_classify_name}`,
  103. success: (result) => {},
  104. fail: () => {},
  105. complete: () => {}
  106. });
  107. },
  108. // 删除
  109. handleDel(item){
  110. this.$dialog.confirm({
  111. title: '',
  112. message: `是否确认删除分类${item.my_chart_classify_name}?`,
  113. showCancelButton:true,
  114. confirmButtonText:'确定'
  115. })
  116. .then(() => {
  117. // on confirm
  118. apiMyChartClassifyDel({
  119. classify_id:item.my_chart_classify_id
  120. }).then(res=>{
  121. if(res.code===200){
  122. uni.showToast({
  123. title:"删除成功",
  124. icon:"none"
  125. })
  126. this.getClassifyList()
  127. }else if(res.code===4001){
  128. this.$dialog.alert({
  129. title: '',
  130. message: '删除失败,该分类下有图表',
  131. showConfirmButton:false,
  132. showCancelButton:true,
  133. cancelButtonText:'知道了'
  134. }).then(() => {
  135. // on close
  136. });
  137. }
  138. })
  139. })
  140. .catch(() => {
  141. // on cancel
  142. });
  143. },
  144. //编辑
  145. handleEdit(item){
  146. this.editId=item.my_chart_classify_id
  147. this.inputClassifyVal=item.my_chart_classify_name
  148. this.showAdd=true
  149. },
  150. //排序
  151. async sortend(e){
  152. // curIndex 为排序前元素所在位置 listData为排序后的数组
  153. let {curIndex,listData}=e.detail
  154. console.log(listData);
  155. const arr=listData.map((item,index)=>{
  156. return {
  157. sort:index+1,
  158. classify_id:item.my_chart_classify_id
  159. }
  160. })
  161. const res=await apiMyChartClassifySort([...arr])
  162. if(res.code!==200){
  163. uni.showToast({
  164. title: res.msg,
  165. icon: 'none'
  166. })
  167. }
  168. },
  169. async handleConfirmAdd(){
  170. if(!this.inputClassifyVal){
  171. uni.showToast({
  172. title: '请输入分类名称',
  173. icon: 'none',
  174. });
  175. return
  176. }
  177. let res
  178. if(this.editId){
  179. res=await apiMyChartClassifyEdit({
  180. classify_name:this.inputClassifyVal,
  181. classify_id:this.editId
  182. })
  183. }else{
  184. res=await apiMyChartClassifyAdd({
  185. classify_name:this.inputClassifyVal
  186. })
  187. }
  188. if(res.code===200){
  189. uni.showToast({
  190. title: `${this.editId?'编辑':'新增'}成功`,
  191. icon: 'none',
  192. });
  193. this.showAdd=false
  194. this.getClassifyList()
  195. }
  196. }
  197. },
  198. }
  199. </script>
  200. <style lang="scss" scoped>
  201. .classify-list-page{
  202. padding: 34rpx 34rpx 120rpx 34rpx;
  203. }
  204. .add-classify-btn{
  205. position: fixed;
  206. left: 0;
  207. bottom: 0;
  208. right: 0;
  209. font-size: 32rpx;
  210. padding-top: 18rpx;
  211. text-align: center;
  212. color: #E3B377;
  213. background: #333333;
  214. box-shadow: 0px 4rpx 20rpx rgba(160, 126, 84, 0.25);
  215. z-index: 99;
  216. padding-bottom: calc(18rpx + constant(safe-area-inset-bottom));
  217. padding-bottom: calc(18rpx + env(safe-area-inset-bottom));
  218. }
  219. .add-input{
  220. display: block;
  221. width: 80%;
  222. background: #F7F8FA;
  223. border-radius: 8px;
  224. padding: 20rpx;
  225. margin: 30rpx auto 10rpx auto;
  226. }
  227. </style>