index.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <template>
  2. <!--/*
  3. ***HotApp云笔记,基于HotApp小程序统计云后台
  4. ***免费云后台申请地址 https://weixin.hotapp.cn/cloud
  5. ***API 文档地址:https://weixin.hotapp.cn/api
  6. ***小程序技术讨论QQ群:173063969
  7. */-->
  8. <!-- index.wxml -->
  9. <view class="container">
  10. <!-- 写笔记 -->
  11. <view class="col">
  12. <view class="item add_box" @tap="onNewItem">
  13. <image class="add_bg" src="../../../images/add.png" style="width: 120rpx; height: 120rpx"></image>
  14. </view>
  15. <!-- 没有笔记时的提示 -->
  16. <block v-if="items.length < 1">
  17. <view class="tips">
  18. <view class="tips_box">
  19. <image class="tips_icon" src="../../../images/tips_icon.png" style="width: 70rpx; height: 70rpx"></image>
  20. </view>
  21. <view class="tips_txt">点此添加新记事本</view>
  22. </view>
  23. </block>
  24. </view>
  25. <!-- 笔记列表 -->
  26. <block v-for="(item, index) in items" :key="index">
  27. <view class="col" v-if="item.state != 3">
  28. <view :class="'item notepad ' + item.class" :data-key="item.key" @tap="onEditItem">
  29. <view class="content">
  30. <view class="txt">{{ item.value.title }}</view>
  31. </view>
  32. <view class="bottom">
  33. <view class="txt">{{ item.year }} {{ item.month }} {{ item.date }}</view>
  34. </view>
  35. </view>
  36. </view>
  37. </block>
  38. </view>
  39. </template>
  40. <script>
  41. /*
  42. ***HotApp云笔记,基于HotApp小程序统计云后台
  43. ***免费云后台申请地址 https://weixin.hotapp.cn/cloud
  44. ***API 文档地址:https://weixin.hotapp.cn/api
  45. ***小程序技术讨论QQ群:173063969
  46. */
  47. var app = getApp();
  48. export default {
  49. data() {
  50. return {
  51. items: []
  52. };
  53. }
  54. /**
  55. * 首次渲染事件
  56. */,
  57. onShow: function () {
  58. this.setData({
  59. items: []
  60. });
  61. // 获取数据
  62. var that = this;
  63. app.globalData.hotapp.wxlogin(function (res) {
  64. that.onLoadData();
  65. });
  66. },
  67. /**
  68. * 下拉刷新事件, 数据同步
  69. */
  70. onPullDownRefresh: function () {
  71. uni.showToast({
  72. title: '正在同步数据',
  73. icon: 'loading'
  74. });
  75. // 临时变量
  76. var tempData = this.items;
  77. var that = this;
  78. // 先检查版本, 如果和服务器版本不同, 则需要从服务器拉取数据
  79. app.globalData.checkVersion(function (shouldPullData) {
  80. if (shouldPullData) {
  81. var filters = {
  82. prefix: app.globalData.hotapp.getPrefix('item')
  83. };
  84. // 从服务器拉取所有数据
  85. app.globalData.hotapp.searchkey(filters, function (res) {
  86. if (res.ret == 0) {
  87. // 拉取成功, 更新版本号
  88. app.globalData.updateVersion(function (success) {
  89. if (success) {
  90. // 更新版本号之后把本地数据和服务器数据合并去重
  91. tempData = that.syncServerDatatoLocal(tempData, res.data.items);
  92. tempData.forEach(function (item, index, arr) {
  93. arr[index] = app.globalData.formatItem(item);
  94. arr[index].state = 2;
  95. });
  96. // 更新视图数据
  97. that.setData({
  98. items: tempData
  99. });
  100. // 把合并好的数据存缓存
  101. uni.setStorageSync('items', tempData);
  102. that.syncLocalDataToServer(tempData);
  103. }
  104. });
  105. }
  106. });
  107. } else {
  108. // 版本号和服务器相同, 则不需要从服务器上拉取数据, 直接同步数据到服务器
  109. that.syncLocalDataToServer(tempData);
  110. }
  111. });
  112. },
  113. methods: {
  114. /**
  115. * 新增笔记事件
  116. */
  117. onNewItem: function (event) {
  118. uni.navigateTo({
  119. url: '../create/index'
  120. });
  121. },
  122. /**
  123. * 编辑笔记事件
  124. */
  125. onEditItem: function (event) {
  126. uni.navigateTo({
  127. url: '../edit/index?key=' + event.currentTarget.dataset.key
  128. });
  129. },
  130. /**
  131. * 获取数据事件
  132. */
  133. onLoadData: function () {
  134. var that = this;
  135. app.globalData.getItems(function (items) {
  136. that.setData({
  137. items: items
  138. });
  139. });
  140. },
  141. /**
  142. * 将本地数据同步到服务器
  143. */
  144. syncLocalDataToServer: function (data) {
  145. var that = this;
  146. // 遍历所有的数据
  147. data.forEach(function (item, index, items) {
  148. app.globalData.hotapp.replaceOpenIdKey(item.key, function (newKey) {
  149. if (newKey) {
  150. item.key = newKey;
  151. // 如果还有数据没有同步过, 则调用post接口同步到服务器
  152. if (item.state == 1) {
  153. app.globalData.hotapp.post(item.key, item.value, function (res) {
  154. if (res.ret == 0) {
  155. // 同步成功后更新状态, 并存缓存
  156. item.state = 2;
  157. item = app.globalData.formatItem(item);
  158. that.setData({
  159. items: items
  160. });
  161. uni.setStorageSync('items', items);
  162. }
  163. });
  164. }
  165. // 如果数据被删除过, 则调用delete接口从服务器删除数据
  166. if (item.state == 3) {
  167. app.globalData.hotapp.del(item.key, function (res) {
  168. if (res.ret == 0 || res.ret == 103) {
  169. // 服务器的数据删除成功后, 删除本地数据并更新缓存
  170. items.splice(index, 1);
  171. that.setData({
  172. items: items
  173. });
  174. uni.setStorageSync('items', items);
  175. }
  176. });
  177. }
  178. } else {
  179. return;
  180. }
  181. });
  182. });
  183. },
  184. /**
  185. * 将服务器的数据同步到本地
  186. */
  187. syncServerDatatoLocal: function (localData, serverData) {
  188. var that = this;
  189. // 通过hash的性质去重, 服务器数据覆盖本地数据
  190. // 但是要保留本地中状态为已删除的数据
  191. // 删除的逻辑不在这里处理
  192. var localHash = new Array();
  193. localData.forEach(function (item) {
  194. localHash[item.key] = item;
  195. });
  196. var serverHash = new Array();
  197. serverData.forEach(function (item) {
  198. serverHash[item.key] = item;
  199. });
  200. // 先把服务器上有的数据但是本地没有的数据合并
  201. serverData.forEach(function (item) {
  202. var t = localHash[item.key];
  203. // 有新增的数据
  204. if (!t) {
  205. localHash[item.key] = item;
  206. }
  207. // 有相同的key则以服务器端为准
  208. if (t && t.state != 3) {
  209. item.state = 2;
  210. item = app.globalData.formatItem(item);
  211. localHash[item.key] = item;
  212. }
  213. });
  214. // 然后再删除本地同步过的但是服务器上没有的缓存数据(在其它设备上删除过了)
  215. localData.forEach(function (item, index, arr) {
  216. var t = serverHash[item.key];
  217. if (!t && item.state == 2) {
  218. console.log(item);
  219. delete localHash[item.key];
  220. }
  221. });
  222. // 将hash中的数据转换回数组
  223. var result = new Array();
  224. for (var prob in localHash) {
  225. result.push(localHash[prob]);
  226. }
  227. // 按时间排序
  228. result.sort(function (a, b) {
  229. return a.create_time < b.create_time;
  230. });
  231. console.log(result);
  232. return result;
  233. }
  234. }
  235. };
  236. </script>
  237. <style>
  238. @import './index.css';
  239. </style>