11import Taro , { Component } from '@tarojs/taro'
2- import { View , Image , Text } from '@tarojs/components'
2+ import {
3+ View ,
4+ Image ,
5+ Text ,
6+ CheckboxGroup ,
7+ Checkbox ,
8+ Label
9+ } from '@tarojs/components'
10+ import errorIcon from '../../static/icon/error.png'
311import './index.scss'
412
513export default class Index extends Component {
@@ -11,7 +19,9 @@ export default class Index extends Component {
1119 super ( ...arguments )
1220 this . state = {
1321 storeBooks : [ ] ,
14- isLoading : true
22+ isLoading : true ,
23+ isEdit : false ,
24+ isSelected : [ ]
1525 }
1626 }
1727
@@ -75,35 +85,119 @@ export default class Index extends Component {
7585 } )
7686 }
7787
88+ removeBook ( ) {
89+ const { isSelected, isEdit } = this . state
90+ const self = this
91+ if ( isSelected . length ) {
92+ Taro . showLoading ( {
93+ title : '移除中...'
94+ } )
95+ isSelected . map ( item => {
96+ // eslint-disable-next-line no-undef
97+ wx . cloud
98+ . callFunction ( {
99+ name : 'delBook' ,
100+ data : {
101+ bookId : item
102+ }
103+ } )
104+ . then ( ( ) => {
105+ self . getStoreBooks ( )
106+ self . setState ( {
107+ isEdit : ! isEdit
108+ } )
109+ Taro . hideLoading ( )
110+ Taro . showToast ( {
111+ title : '已移除'
112+ } )
113+ } )
114+ . catch ( err => {
115+ throw err
116+ } )
117+ } )
118+ } else {
119+ Taro . showToast ( {
120+ title : '请先选择书籍' ,
121+ image : errorIcon
122+ } )
123+ }
124+ }
125+
126+ editOrJump ( bookId , bookTitle ) {
127+ const { isEdit } = this . state
128+ if ( ! isEdit ) {
129+ this . jumpReadBookPage ( bookId , bookTitle )
130+ }
131+ }
132+
133+ toggleBookEdit ( ) {
134+ this . setState ( {
135+ isEdit : ! this . state . isEdit
136+ } )
137+ }
138+
139+ handleCheckBoxChange ( e ) {
140+ this . setState ( {
141+ isSelected : e . detail . value
142+ } )
143+ }
144+
78145 render ( ) {
79- const { storeBooks, isLoading } = this . state
146+ const { storeBooks, isLoading, isEdit } = this . state
80147 const baseImageUrl = 'http://statics.zhuishushenqi.com'
81148 if ( ! isLoading ) {
82149 return (
83150 < View className = 'book-store' >
84151 < View className = 'book-header' >
85- < View className = 'book-edit' >
86- < Text > 编辑 </ Text >
152+ < View className = 'book-edit' onClick = { this . toggleBookEdit } >
153+ < Text > { isEdit ? '取消' : '编辑' } </ Text >
87154 </ View >
88155 </ View >
89- < View className = 'book-content' >
90- { storeBooks . map ( item => {
91- return (
92- < View className = 'book-info' key = { item . _id } onClick = { this . jumpReadBookPage . bind ( this , item . bookId , item . title ) } >
93- < Image
94- mode = 'aspectFill'
95- src = { `${ baseImageUrl } ${ item . cover } ` }
96- />
97- < View className = 'book-title' >
98- { item . title }
99- </ View >
100- < View className = 'book-author' >
101- { item . author }
156+ < View >
157+ < CheckboxGroup
158+ onChange = { this . handleCheckBoxChange }
159+ className = 'book-content'
160+ >
161+ { storeBooks . map ( item => {
162+ return (
163+ < View
164+ key = { item . _id }
165+ className = 'book-info'
166+ onClick = { this . editOrJump . bind (
167+ this ,
168+ item . bookId ,
169+ item . title
170+ ) }
171+ >
172+ < Image
173+ mode = 'aspectFill'
174+ src = { `${ baseImageUrl } ${ item . cover } ` }
175+ />
176+ < View className = 'book-title' > { item . title } </ View >
177+ < View className = 'book-author' > { item . author } </ View >
178+ { isEdit && (
179+ < Label className = 'book-label' >
180+ < Checkbox
181+ className = 'book-checkbox'
182+ value = { item . bookId }
183+ />
184+ </ Label >
185+ ) }
102186 </ View >
103- </ View >
104- )
105- } ) }
187+ )
188+ } ) }
189+ </ CheckboxGroup >
106190 </ View >
191+ { isEdit && (
192+ < View className = 'book-action' >
193+ < View className = 'book-cancel' onClick = { this . toggleBookEdit } >
194+ < Text > 取消</ Text >
195+ </ View >
196+ < View className = 'book-del' onClick = { this . removeBook } >
197+ < Text > 删除</ Text >
198+ </ View >
199+ </ View >
200+ ) }
107201 </ View >
108202 )
109203 }
0 commit comments