server-date.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. const util = require('../../../../util/util.js')
  2. // 参考文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-client-api/database/db.serverDate.html
  3. const app = getApp()
  4. const collection = 'serverDate'
  5. Page({
  6. onShareAppMessage() {
  7. return {
  8. title: '服务端时间',
  9. path: 'page/cloud/pages/server-date/server-date'
  10. }
  11. },
  12. data: {
  13. openid: '',
  14. loading: false,
  15. clientDate: null,
  16. serverDate: null,
  17. clientDateFormatted: '',
  18. serverDateFormatted: '',
  19. delta: 0
  20. },
  21. onLoad() {
  22. if (app.globalData.openid) {
  23. this.setData({
  24. openid: app.globalData.openid
  25. })
  26. } else {
  27. wx.showLoading({
  28. title: '正在初始化...'
  29. })
  30. app.getUserOpenIdViaCloud()
  31. .then(openid => {
  32. this.setData({
  33. openid
  34. })
  35. wx.hideLoading()
  36. return openid
  37. }).catch(err => {
  38. console.error(err)
  39. wx.hideLoading()
  40. wx.showToast({
  41. icon: 'none',
  42. title: '初始化失败,请检查网络'
  43. })
  44. })
  45. }
  46. },
  47. showError() {
  48. wx.showToast({
  49. icon: 'none',
  50. title: '插入失败'
  51. })
  52. },
  53. completeTask() {
  54. this.setData({
  55. loading: false
  56. })
  57. },
  58. // 如果已有记录则更新,否则插入
  59. insertOrUpdateData(existedData, data) {
  60. const db = wx.cloud.database()
  61. if (existedData._id) {
  62. db.collection(collection).doc(existedData._id).update({data})
  63. .then(res => {
  64. this.setCompletedData(existedData._id)
  65. return res
  66. })
  67. .catch(err => {
  68. this.showError()
  69. console.error('[数据库] [更新记录] 失败:', err)
  70. this.completeTask()
  71. })
  72. } else {
  73. db.collection(collection).add({data})
  74. .then(res => {
  75. this.setCompletedData(res._id)
  76. return res
  77. })
  78. .catch(err => {
  79. this.showError()
  80. console.error('[数据库] [新增记录] 失败:', err)
  81. this.completeTask()
  82. })
  83. }
  84. },
  85. // 查询已插入/更新的数据中记录的服务端时间
  86. setCompletedData(id) {
  87. const db = wx.cloud.database()
  88. db.collection(collection).doc(id).get()
  89. .then(res => {
  90. this.setData({
  91. delta: Math.abs(res.data.time - this.data.clientDate), // 大致的时间差
  92. serverDate: res.data.time, // 服务端时间
  93. clientDateFormatted: util.formatDateTime(this.data.clientDate, true),
  94. serverDateFormatted: util.formatDateTime(res.data.time, true)
  95. })
  96. wx.showToast({
  97. title: '插入成功',
  98. })
  99. this.completeTask()
  100. return res
  101. })
  102. .catch(err => {
  103. this.showError()
  104. console.error('[数据库] [查询记录] 失败:', err)
  105. this.completeTask()
  106. })
  107. },
  108. insertData() {
  109. const db = wx.cloud.database()
  110. const data = {
  111. time: db.serverDate()
  112. }
  113. this.setData({
  114. loading: true
  115. })
  116. db.collection(collection).where({
  117. _openid: this.data.openid
  118. }).get()
  119. .then(res => {
  120. this.setData({
  121. clientDate: new Date() // 客户端时间
  122. })
  123. console.log('[数据库] [查询记录] 成功: ', res)
  124. const resFirstData = res.data[0] || {}
  125. this.insertOrUpdateData(resFirstData, data)
  126. return res
  127. })
  128. .catch(err => {
  129. this.showError()
  130. console.error('[数据库] [查询记录] 失败:', err)
  131. this.completeTask()
  132. })
  133. },
  134. })