index.uts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. type SuccessCallback<T> = (res : T | null) => void | null
  2. type FailCallback = (err : any | null) => void | null
  3. type CompleteCallback = () => void | null
  4. export type MixinDatacomPaginationType = {
  5. current : number,
  6. size : number,
  7. count : number
  8. }
  9. export type MixinDatacomGetOptions = {
  10. collection ?: UTSJSONObject,
  11. field ?: string,
  12. orderBy ?: string,
  13. where ?: any,
  14. pageData ?: string,
  15. pageCurrent ?: number,
  16. pageSize ?: number,
  17. getCount ?: boolean,
  18. getTree ?: any,
  19. getTreePath ?: UTSJSONObject,
  20. startWith ?: string,
  21. limitLevel ?: number,
  22. groupBy ?: string,
  23. groupField ?: string,
  24. distinct ?: boolean,
  25. pageIndistinct ?: boolean,
  26. foreignKey ?: string,
  27. loadtime ?: string,
  28. manual ?: boolean
  29. }
  30. export type MixinDatacomEasyGetOptions = {
  31. success ?: SuccessCallback<UniCloudDBGetResult>,
  32. fail ?: FailCallback,
  33. complete ?: CompleteCallback,
  34. }
  35. export const mixinDatacom = defineMixin({
  36. slots: Object as SlotsType<{
  37. default : {
  38. data : Array<UTSJSONObject>,
  39. loading : boolean,
  40. hasMore : boolean,
  41. pagination : MixinDatacomPaginationType,
  42. error : UniCloudError | null
  43. }
  44. }>,
  45. props: {
  46. localdata: {
  47. type: Array as PropType<Array<UTSJSONObject>>,
  48. default: [] as Array<UTSJSONObject>
  49. },
  50. collection: {
  51. type: Object,
  52. default: ''
  53. },
  54. field: {
  55. type: String,
  56. default: ''
  57. },
  58. orderby: {
  59. type: String,
  60. default: ''
  61. },
  62. where: {
  63. type: Object,
  64. default: ''
  65. },
  66. pageData: {
  67. type: String,
  68. default: 'add'
  69. },
  70. pageCurrent: {
  71. type: Number,
  72. default: 1
  73. },
  74. pageSize: {
  75. type: Number,
  76. default: 20
  77. },
  78. getcount: {
  79. type: Boolean,
  80. default: false
  81. },
  82. gettree: {
  83. type: Object,
  84. default: ''
  85. },
  86. gettreepath: {
  87. type: Boolean,
  88. default: false
  89. },
  90. startwith: {
  91. type: String,
  92. default: ''
  93. },
  94. limitlevel: {
  95. type: Number,
  96. default: 10
  97. },
  98. groupby: {
  99. type: String,
  100. default: ''
  101. },
  102. groupField: {
  103. type: String,
  104. default: ''
  105. },
  106. distinct: {
  107. type: Boolean,
  108. default: false
  109. },
  110. pageIndistinct: {
  111. type: Boolean,
  112. default: false
  113. },
  114. foreignKey: {
  115. type: String,
  116. default: ''
  117. },
  118. loadtime: {
  119. type: String,
  120. default: 'auto'
  121. },
  122. manual: {
  123. type: Boolean,
  124. default: false
  125. }
  126. },
  127. data() {
  128. return {
  129. mixinDatacomResData: [] as Array<UTSJSONObject>, // 请求返回的数据,调用 loadData 后会更新
  130. mixinDatacomLoading: false, // 网络请求状态
  131. mixinDatacomHasMore: false, // 是否有更多数据
  132. mixinDatacomPage: {
  133. current: 1,
  134. size: 20,
  135. count: 0,
  136. } as MixinDatacomPaginationType, // 分页信息,详情见 created 生命周期
  137. mixinDatacomError: null as UniCloudError | null, // 请求出错时的错误消息
  138. }
  139. },
  140. created() {
  141. this.mixinDatacomPage.current = this.pageCurrent
  142. this.mixinDatacomPage.size = this.pageSize
  143. const PROPS_NAME = ['', '', 'collection', 'field', 'getcount', 'orderby', 'where', 'groupby', 'groupField', 'distinct']
  144. this.$watch(
  145. () : any => [
  146. this.pageCurrent,
  147. this.pageSize,
  148. this.collection,
  149. this.field,
  150. this.getcount,
  151. this.orderby,
  152. this.where,
  153. this.groupby,
  154. this.groupField,
  155. this.distinct
  156. ],
  157. (newValue : Array<any>, oldValue : Array<any>) => {
  158. this.mixinDatacomPage.size = this.pageSize
  159. if (newValue[0] !== oldValue[0]) {
  160. this.mixinDatacomPage.current = this.pageCurrent
  161. }
  162. let needReset = false
  163. let changed : Array<string> = []
  164. for (let i = 2; i < newValue.length; i++) {
  165. if (newValue[i] !== oldValue[i]) {
  166. needReset = true
  167. changed.push(PROPS_NAME[i])
  168. }
  169. }
  170. this.onMixinDatacomPropsChange(needReset, changed)
  171. }
  172. )
  173. },
  174. methods: {
  175. // props发生变化时被调用,在组件中覆盖此方法
  176. // 非 pageCurrent,pageSize 改变时 needReset=true,需要重置数据
  177. // changed,发生变化的属性名,类型为Array,例如 ['collection', 'action']
  178. onMixinDatacomPropsChange(_ : boolean, changed : Array<string>) {
  179. },
  180. mixinDatacomEasyGet(options ?: MixinDatacomEasyGetOptions) {
  181. if (this.mixinDatacomLoading) {
  182. return
  183. }
  184. this.mixinDatacomLoading = true
  185. this.mixinDatacomError = null
  186. this.mixinDatacomGet(null).then((res : UniCloudDBGetResult) => {
  187. const data = res.data
  188. const count = res.count
  189. if (this.getcount && count != null) {
  190. this.mixinDatacomPage.count = count
  191. }
  192. this.mixinDatacomHasMore = !((count !== null) ? (this.mixinDatacomPage.current * this.mixinDatacomPage.size >= count) : (data.length < this.pageSize))
  193. this.mixinDatacomResData = data
  194. options?.success?.(res)
  195. }).catch((err : any | null) => {
  196. this.mixinDatacomError = err as UniCloudError
  197. options?.fail?.(err)
  198. }).then(() => {
  199. this.mixinDatacomLoading = false
  200. options?.complete?.()
  201. }, () => {
  202. this.mixinDatacomLoading = false
  203. options?.complete?.()
  204. })
  205. },
  206. mixinDatacomGet(options ?: MixinDatacomGetOptions) : Promise<UniCloudDBGetResult> {
  207. let db = uniCloud.databaseForJQL()
  208. let collection = Array.isArray(this.collection) ? db.collection(...(this.collection as Array<any>)) : db.collection(this.collection)
  209. let filter : UniCloudDBFilter | null = null
  210. if (this.foreignKey.length > 0) {
  211. filter = collection.foreignKey(this.foreignKey)
  212. }
  213. const where : any = options?.where ?? this.where
  214. if (typeof where == 'string') {
  215. const whereString = where as string
  216. if (whereString.length > 0) {
  217. filter = (filter != null) ? filter.where(where) : collection.where(where)
  218. }
  219. } else {
  220. filter = (filter != null) ? filter.where(where) : collection.where(where)
  221. }
  222. let query : UniCloudDBQuery | null = null
  223. if (this.field.length > 0) {
  224. query = (filter != null) ? filter.field(this.field) : collection.field(this.field)
  225. }
  226. if (this.groupby.length > 0) {
  227. if (query != null) {
  228. query = query.groupBy(this.groupby)
  229. } else if (filter != null) {
  230. query = filter.groupBy(this.groupby)
  231. }
  232. }
  233. if (this.groupField.length > 0) {
  234. if (query != null) {
  235. query = query.groupField(this.groupField)
  236. } else if (filter != null) {
  237. query = filter.groupField(this.groupField)
  238. }
  239. }
  240. if (this.distinct == true) {
  241. if (query != null) {
  242. query = query.distinct(this.field)
  243. } else if (filter != null) {
  244. query = filter.distinct(this.field)
  245. }
  246. }
  247. if (this.orderby.length > 0) {
  248. if (query != null) {
  249. query = query.orderBy(this.orderby)
  250. } else if (filter != null) {
  251. query = filter.orderBy(this.orderby)
  252. }
  253. }
  254. const size = this.mixinDatacomPage.size
  255. const current = this.mixinDatacomPage.current
  256. if (query != null) {
  257. query = query.skip(size * (current - 1)).limit(size)
  258. } else if (filter != null) {
  259. query = filter.skip(size * (current - 1)).limit(size)
  260. } else {
  261. query = collection.skip(size * (current - 1)).limit(size)
  262. }
  263. const getOptions = {}
  264. const treeOptions = {
  265. limitLevel: this.limitlevel,
  266. startWith: this.startwith
  267. }
  268. const getCount : boolean = options?.getCount ?? this.getcount
  269. if (this.getcount == true) {
  270. getOptions['getCount'] = getCount
  271. }
  272. const getTree : any = options?.getTree ?? this.gettree
  273. if (typeof getTree == 'string') {
  274. const getTreeString = getTree as string
  275. if (getTreeString.length > 0) {
  276. getOptions['getTree'] = treeOptions
  277. }
  278. } else if (typeof getTree == 'object') {
  279. getOptions['getTree'] = treeOptions
  280. } else {
  281. getOptions['getTree'] = getTree
  282. }
  283. const getTreePath = options?.getTreePath ?? this.gettreepath
  284. if (typeof getTreePath == 'string') {
  285. const getTreePathString = getTreePath as string
  286. if (getTreePathString.length > 0) {
  287. getOptions['getTreePath'] = getTreePath
  288. }
  289. } else {
  290. getOptions['getTreePath'] = getTreePath
  291. }
  292. return query.get(getOptions)
  293. }
  294. }
  295. })