微信小程序,访客邀约
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

114 行
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. type
  31. visitorCompany
  32. avatar
  33. phone
  34. }
  35. }
  36. `
  37. function loadContactsFromServer () {
  38. return new Promise((resolve, reject) => {
  39. isContactsLoaded.value = false
  40. const items: ContactData[] = []
  41. return GQLRequest.query(GQL_QUERY_ALL_CONTACT, {})
  42. .then(result => {
  43. if (result.code == 500) {
  44. reject("Network Error")
  45. } else {
  46. // console.log(result)
  47. for (let item of result.data.visitors) {
  48. items.push({
  49. id: item.id,
  50. name: item.name,
  51. nickName: item.nickname,
  52. avatar: item.avatar,
  53. company: item.visitorCompany,
  54. phone: item.phone,
  55. flexVisit: "",
  56. isBlock: "",
  57. isVIP: "",
  58. firstCharPinyin: PinyinHelper.getPinyinGroupName(item.name)
  59. } as ContactData)
  60. }
  61. allContacts.value = items.sort((a, b) => a.firstCharPinyin.localeCompare(b.firstCharPinyin));
  62. console.log(allContacts.value)
  63. isContactsLoaded.value = true
  64. resolve(allContacts.value)
  65. }
  66. })
  67. })
  68. }
  69. function searchContacts(searchQuery: string) {
  70. const searchedContacts: ContactData[] = []
  71. for(const item of allContacts.value) {
  72. if (item.name.match(searchQuery)) {
  73. searchedContacts.push(item)
  74. }
  75. }
  76. return searchedContacts
  77. }
  78. function getEmptyContactData(): ContactData {
  79. return {
  80. id: undefined,
  81. name: "",
  82. isVIP: false,
  83. flexVisit: false,
  84. isBlock: false,
  85. nickName: "",
  86. avatar: "",
  87. phone: "",
  88. company: "",
  89. nextVisitDate: undefined,
  90. firstCharPinyin: ""
  91. }
  92. }
  93. return { allContacts , isContactsLoaded, loadContactsFromServer, searchContacts, getEmptyContactData}
  94. })
  95. // You can even use a function (similar to a component setup()) to define a Store for more advanced use cases:
  96. // export const useCounterStore = defineStore('counter', () => {
  97. // const count = ref(0)
  98. //
  99. // function increment() {
  100. // count.value++
  101. // }
  102. //
  103. // return {count, increment}
  104. // })