微信小程序,访客邀约
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

111 linhas
3.0 KiB

  1. // https://pinia.esm.dev/introduction.html
  2. import { defineStore } from 'pinia'
  3. import {GQLRequest, PinyinHelper, Session} from "../utils";
  4. import Taro from "@tarojs/taro";
  5. import {gql} from "graphql-tag";
  6. import {Ref, ref, UnwrapRef} from "vue";
  7. import {rejects} from "assert";
  8. export interface ContactData {
  9. id?: number
  10. name: string
  11. isVIP: boolean
  12. flexVisit: boolean
  13. isBlock: boolean
  14. nickName: string
  15. avatar: string
  16. phone: string
  17. company: string
  18. nextVisitDate?: string
  19. firstCharPinyin: string
  20. }
  21. export const useContactsStore = defineStore('contacts', () => {
  22. const isContactsLoaded: Ref<UnwrapRef<boolean>> = ref(false)
  23. const allContacts: Ref<UnwrapRef<ContactData[]>> = ref([])
  24. const GQL_QUERY_ALL_CONTACT = gql`
  25. query {
  26. visitors {
  27. id
  28. name
  29. nickname
  30. phone
  31. type
  32. visitorCompany
  33. avatar
  34. isVip
  35. flexVisit
  36. isBlock
  37. }
  38. }
  39. `
  40. function loadContactsFromServer () {
  41. return new Promise((resolve, reject) => {
  42. isContactsLoaded.value = false
  43. const items: ContactData[] = []
  44. return GQLRequest.query(GQL_QUERY_ALL_CONTACT, {})
  45. .then(result => {
  46. if (result.code == 500) {
  47. reject("Network Error")
  48. } else {
  49. console.log(result)
  50. for (let item of result.data.visitors) {
  51. items.push({
  52. id: item.id,
  53. name: item.name,
  54. nickName: item.nickname,
  55. avatar: item.avatar,
  56. company: item.visitorCompany,
  57. phone: item.phone,
  58. flexVisit: item.flexVisit,
  59. isBlock: item.isBlock,
  60. isVIP: item.isVip,
  61. firstCharPinyin: PinyinHelper.getPinyinGroupName(item.name)
  62. } as ContactData)
  63. }
  64. allContacts.value = items.sort((a, b) => a.firstCharPinyin.localeCompare(b.firstCharPinyin));
  65. console.log(allContacts.value)
  66. isContactsLoaded.value = true
  67. resolve(allContacts.value)
  68. }
  69. })
  70. })
  71. }
  72. function getContactById(id: number) {
  73. for(const contact of allContacts.value) {
  74. console.log(contact)
  75. if (contact.id === id) {
  76. return contact
  77. }
  78. }
  79. return undefined
  80. }
  81. function searchContacts(searchQuery: string) {
  82. const searchedContacts: ContactData[] = []
  83. for(const item of allContacts.value) {
  84. if (item.name.match(searchQuery)) {
  85. searchedContacts.push(item)
  86. }
  87. }
  88. return searchedContacts
  89. }
  90. return { allContacts , isContactsLoaded, loadContactsFromServer, searchContacts, getContactById}
  91. })
  92. // You can even use a function (similar to a component setup()) to define a Store for more advanced use cases:
  93. // export const useCounterStore = defineStore('counter', () => {
  94. // const count = ref(0)
  95. //
  96. // function increment() {
  97. // count.value++
  98. // }
  99. //
  100. // return {count, increment}
  101. // })