clientdb.js 6.6 KB

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