clientdb.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. const events = {
  2. load: 'load',
  3. error: 'error'
  4. }
  5. const pageMode = {
  6. add: 'add',
  7. replace: 'replace'
  8. }
  9. const attrs = [
  10. 'pageCurrent',
  11. 'pageSize',
  12. 'collection',
  13. 'action',
  14. 'field',
  15. 'getcount',
  16. 'orderby',
  17. 'where'
  18. ]
  19. export default {
  20. data() {
  21. return {
  22. loading: false,
  23. listData: this.getone ? {} : [],
  24. paginationInternal: {
  25. current: this.pageCurrent,
  26. size: this.pageSize,
  27. count: 0
  28. },
  29. errorMessage: ''
  30. }
  31. },
  32. created() {
  33. let db = null;
  34. let dbCmd = null;
  35. if(this.collection){
  36. this.db = uniCloud.database();
  37. this.dbCmd = this.db.command;
  38. }
  39. this._isEnded = false
  40. this.$watch(() => {
  41. let al = []
  42. attrs.forEach(key => {
  43. al.push(this[key])
  44. })
  45. return al
  46. }, (newValue, oldValue) => {
  47. this.paginationInternal.pageSize = this.pageSize
  48. let needReset = false
  49. for (let i = 2; i < newValue.length; i++) {
  50. if (newValue[i] != oldValue[i]) {
  51. needReset = true
  52. break
  53. }
  54. }
  55. if (needReset) {
  56. this.clear()
  57. this.reset()
  58. }
  59. if (newValue[0] != oldValue[0]) {
  60. this.paginationInternal.current = this.pageCurrent
  61. }
  62. this._execLoadData()
  63. })
  64. // #ifdef H5
  65. if (process.env.NODE_ENV === 'development') {
  66. this._debugDataList = []
  67. if (!window.unidev) {
  68. window.unidev = {
  69. clientDB: {
  70. data: []
  71. }
  72. }
  73. }
  74. unidev.clientDB.data.push(this._debugDataList)
  75. }
  76. // #endif
  77. // #ifdef MP-TOUTIAO
  78. let changeName
  79. let events = this.$scope.dataset.eventOpts
  80. for (let i = 0; i < events.length; i++) {
  81. let event = events[i]
  82. if (event[0].includes('^load')) {
  83. changeName = event[1][0][0]
  84. }
  85. }
  86. if (changeName) {
  87. let parent = this.$parent
  88. let maxDepth = 16
  89. this._changeDataFunction = null
  90. while (parent && maxDepth > 0) {
  91. let fun = parent[changeName]
  92. if (fun && typeof fun === 'function') {
  93. this._changeDataFunction = fun
  94. maxDepth = 0
  95. break
  96. }
  97. parent = parent.$parent
  98. maxDepth--;
  99. }
  100. }
  101. // #endif
  102. // if (!this.manual) {
  103. // this.loadData()
  104. // }
  105. },
  106. // #ifdef H5
  107. beforeDestroy() {
  108. if (process.env.NODE_ENV === 'development' && window.unidev) {
  109. let cd = this._debugDataList
  110. let dl = unidev.clientDB.data
  111. for (let i = dl.length - 1; i >= 0; i--) {
  112. if (dl[i] === cd) {
  113. dl.splice(i, 1)
  114. break
  115. }
  116. }
  117. }
  118. },
  119. // #endif
  120. methods: {
  121. loadData(args1, args2) {
  122. let callback = null
  123. if (typeof args1 === 'object') {
  124. if (args1.clear) {
  125. this.clear()
  126. this.reset()
  127. }
  128. if (args1.current !== undefined) {
  129. this.paginationInternal.current = args1.current
  130. }
  131. if (typeof args2 === 'function') {
  132. callback = args2
  133. }
  134. } else if (typeof args1 === 'function') {
  135. callback = args1
  136. }
  137. this._execLoadData(callback)
  138. },
  139. loadMore() {
  140. if (this._isEnded) {
  141. return
  142. }
  143. this._execLoadData()
  144. },
  145. refresh() {
  146. this.clear()
  147. this._execLoadData()
  148. },
  149. clear() {
  150. this._isEnded = false
  151. this.listData = []
  152. },
  153. reset() {
  154. this.paginationInternal.current = 1
  155. },
  156. remove(id, {
  157. action,
  158. callback,
  159. confirmTitle,
  160. confirmContent
  161. } = {}) {
  162. if (!id || !id.length) {
  163. return
  164. }
  165. uni.showModal({
  166. title: confirmTitle || '提示',
  167. content: confirmContent || '是否删除该数据',
  168. showCancel: true,
  169. success: (res) => {
  170. if (!res.confirm) {
  171. return
  172. }
  173. this._execRemove(id, action, callback)
  174. }
  175. })
  176. },
  177. _execLoadData(callback) {
  178. if (this.loading) {
  179. return
  180. }
  181. this.loading = true
  182. this.errorMessage = ''
  183. this._getExec().then((res) => {
  184. this.loading = false
  185. const {
  186. data,
  187. count
  188. } = res.result
  189. this._isEnded = data.length < this.pageSize
  190. callback && callback(data, this._isEnded)
  191. this._dispatchEvent(events.load, data)
  192. if (this.getone) {
  193. this.listData = data.length ? data[0] : undefined
  194. } else if (this.pageData === pageMode.add) {
  195. this.listData.push(...data)
  196. if (this.listData.length) {
  197. this.paginationInternal.current++
  198. }
  199. } else if (this.pageData === pageMode.replace) {
  200. this.listData = data
  201. this.paginationInternal.count = count
  202. }
  203. // #ifdef H5
  204. if (process.env.NODE_ENV === 'development') {
  205. this._debugDataList.length = 0
  206. this._debugDataList.push(...JSON.parse(JSON.stringify(this.listData)))
  207. }
  208. // #endif
  209. }).catch((err) => {
  210. this.loading = false
  211. this.errorMessage = err
  212. callback && callback()
  213. this.$emit(events.error, err)
  214. })
  215. },
  216. _getExec() {
  217. let exec = this.db
  218. if (this.action) {
  219. exec = exec.action(this.action)
  220. }
  221. exec = exec.collection(this.collection)
  222. if (!(!this.where || !Object.keys(this.where).length)) {
  223. exec = exec.where(this.where)
  224. }
  225. if (this.field) {
  226. exec = exec.field(this.field)
  227. }
  228. if (this.orderby) {
  229. exec = exec.orderBy(this.orderby)
  230. }
  231. const {
  232. current,
  233. size
  234. } = this.paginationInternal
  235. exec = exec.skip(size * (current - 1)).limit(size).get({
  236. getCount: this.getcount
  237. })
  238. return exec
  239. },
  240. _execRemove(id, action, callback) {
  241. if (!this.collection || !id) {
  242. return
  243. }
  244. const ids = Array.isArray(id) ? id : [id]
  245. if (!ids.length) {
  246. return
  247. }
  248. uni.showLoading({
  249. mask: true
  250. })
  251. let exec = this.db
  252. if (action) {
  253. exec = exec.action(action)
  254. }
  255. exec.collection(this.collection).where({
  256. _id: dbCmd.in(ids)
  257. }).remove().then((res) => {
  258. callback && callback(res.result)
  259. if (this.pageData === pageMode.replace) {
  260. this.refresh()
  261. } else {
  262. this.removeData(ids)
  263. }
  264. }).catch((err) => {
  265. uni.showModal({
  266. content: err.message,
  267. showCancel: false
  268. })
  269. }).finally(() => {
  270. uni.hideLoading()
  271. })
  272. },
  273. removeData(ids) {
  274. let il = ids.slice(0)
  275. let dl = this.listData
  276. for (let i = dl.length - 1; i >= 0; i--) {
  277. let index = il.indexOf(dl[i]._id)
  278. if (index >= 0) {
  279. dl.splice(i, 1)
  280. il.splice(index, 1)
  281. }
  282. }
  283. },
  284. _dispatchEvent(type, data) {
  285. if (this._changeDataFunction) {
  286. this._changeDataFunction(data, this._isEnded)
  287. } else {
  288. this.$emit(type, data, this._isEnded)
  289. }
  290. }
  291. }
  292. }