微信小程序,访客邀约
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

167 lines
4.5 KiB

  1. // https://pinia.esm.dev/introduction.html
  2. import { defineStore } from 'pinia'
  3. import {GQLRequest, PinyinHelper, Session} from "../utils";
  4. import {gql} from "graphql-tag";
  5. import {ContactData} from "./contacts";
  6. import {ref, Ref, UnwrapRef} from "vue";
  7. import Taro from "@tarojs/taro";
  8. import dayjs from "dayjs";
  9. export interface ContactVisitRecord {
  10. id: number
  11. visitorId: number
  12. userId: number
  13. receiverName?: string
  14. receiverPhone?: string
  15. recordStatus?: number
  16. visitDate: string
  17. visitCode?: string
  18. }
  19. export const useContactVisitRecordStore = defineStore('contact-visit-record', () => {
  20. const visitRecord: Ref<UnwrapRef<ContactVisitRecord|null>> = ref(null)
  21. function reset() {
  22. visitRecord.value = null
  23. }
  24. function setContact(contactItem: ContactData) {
  25. contact.value = contactItem
  26. console.log("setContact", contact.value)
  27. }
  28. function setNewAvatar(avatar: string) {
  29. contactNewAvatar.value = avatar
  30. }
  31. const GET_VISIT_RECORD = gql`
  32. query ($visitorId: ID, $limit: Int ) {
  33. visitRecords(visitorId: $visitorId, limit: $limit) {
  34. id
  35. visitorId
  36. userId
  37. visitDate
  38. visitorCode
  39. }
  40. }
  41. `
  42. const CREATE_VISIT_RECORD = gql`
  43. mutation CreateVisitRecord($staffId: ID!, $visitorId: ID!, $visitDate: Date!) {
  44. createVisitRecord( staffId: $staffId, input: {
  45. visitorId: $visitorId,
  46. userId: $staffId,
  47. visitDate: $visitDate,
  48. } ) {
  49. id
  50. visitorId
  51. userId
  52. visitDate
  53. visitorCode
  54. }
  55. }
  56. `
  57. const UPDATE_VISIT_RECORD = gql`
  58. mutation UpdateVisitRecord($staffId: ID!, $visitRecordId: ID!, $visitorId: ID!, $visitDate: Date!) {
  59. updateVisitRecord( staffId: $staffId, input: {
  60. id: $visitRecordId,
  61. visitorId: $visitorId,
  62. userId: $staffId,
  63. visitDate: $visitDate,
  64. } ) {
  65. id
  66. visitorId
  67. userId
  68. visitDate
  69. visitorCode
  70. }
  71. }
  72. `
  73. function getVisitRecord(visitorId: string) {
  74. reset()
  75. const limit = 1
  76. return new Promise((resolve, reject) => {
  77. GQLRequest.query(GET_VISIT_RECORD, { visitorId, limit })
  78. .then(result=> {
  79. if (result.code == 500) {
  80. reject("Network Error")
  81. } else {
  82. console.log("result", result)
  83. if (result.data.visitRecords.length > 0) {
  84. const item = result.data.visitRecords[0]
  85. createData(item)
  86. }
  87. resolve(visitRecord.value)
  88. }
  89. })
  90. .catch(e => {
  91. reject()
  92. })
  93. })
  94. }
  95. function createData(item: any) {
  96. const todayString = dayjs().format("YYYY-MM-DD")
  97. const dateDiff = dayjs(todayString + " 00:00:00").diff(dayjs(item.visitDate, "YYYY-MM-DD"))
  98. if (dateDiff <= 0) {
  99. visitRecord.value = {
  100. id: item.id,
  101. visitorId: item.visitorId,
  102. userId: item.userId,
  103. receiverName: undefined,
  104. receiverPhone: undefined,
  105. recordStatus: undefined,
  106. visitDate: item.visitDate,
  107. visitCode: undefined,
  108. } as ContactVisitRecord
  109. }
  110. }
  111. function save(visitorId: string, date: string) {
  112. return new Promise((resolve, reject) => {
  113. if (visitRecord.value) {
  114. updateVisitRecord(visitRecord.value.id, visitorId, date).then(r => resolve(r))
  115. .catch(e => reject(e))
  116. }
  117. else {
  118. createVisitRecord(visitorId, date).then(r => resolve(r))
  119. .catch(e => reject(e))
  120. }
  121. })
  122. }
  123. function createVisitRecord(visitorId: string, visitDate: string) {
  124. const staffId = Session.get("staff").id
  125. return new Promise((resolve, reject) => {
  126. GQLRequest.query(CREATE_VISIT_RECORD, { staffId, visitorId, visitDate })
  127. .then(r=> {
  128. if (r.code == 500) reject("Network Error")
  129. else {
  130. console.log(r)
  131. createData(r.data.createVisitRecord)
  132. resolve(r)
  133. }
  134. })
  135. .catch(e=> reject(e))
  136. })
  137. }
  138. function updateVisitRecord(visitRecordId: string, visitorId: string, visitDate: string) {
  139. const staffId = Session.get("staff").id
  140. return new Promise((resolve, reject) => {
  141. GQLRequest.query(UPDATE_VISIT_RECORD, { staffId, visitRecordId, visitorId, visitDate })
  142. .then(r=> {
  143. if (r.code == 500) reject("Network Error")
  144. else resolve(r)
  145. })
  146. .catch(e=> reject(e))
  147. })
  148. }
  149. return { visitRecord, getVisitRecord , save , reset}
  150. })