addresslist.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { models, db, _ } from '../../utils/cloudbase.js'
  2. Page({
  3. data: {
  4. addresses: [],
  5. souimg: '',
  6. shanchu: '',
  7. xiugaiimg: '',
  8. dingweiimg: '',
  9. keyword: '', // 搜索关键词
  10. },
  11. onShow() {
  12. this.getdatalist()
  13. },
  14. onLoad(options) {
  15. // const title = options.title ? decodeURIComponent(options.title) : '商品列表';
  16. // this.setData({ title });
  17. // wx.setNavigationBarTitle({ title });
  18. const fileIDs = [
  19. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/sou.png',
  20. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/shanchu.png',
  21. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/xiugai.png',
  22. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/dingwei.png'
  23. ];
  24. // 并发下载多个 fileID
  25. Promise.all(
  26. fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  27. ).then(results => {
  28. // 每个 result 对应一个下载结果
  29. const tempFilePaths = results.map(r => r.tempFilePath);
  30. console.log('全部下载成功:', tempFilePaths);
  31. this.setData({
  32. souimg: tempFilePaths[0],
  33. shanchu: tempFilePaths[1],
  34. xiugaiimg: tempFilePaths[2],
  35. dingweiimg: tempFilePaths[3],
  36. });
  37. }).catch(err => {
  38. console.error('有文件下载失败:', err);
  39. });
  40. },
  41. // 模糊搜索
  42. onSearchInput(e) {
  43. const keyword = e.detail.value.trim();
  44. this.setData({ keyword });
  45. this.getdatalist(); // 每次输入更新列表(也可节流优化)
  46. },
  47. // 地址数据
  48. async getdatalist() {
  49. const keyword = this.data.keyword;
  50. // 构建模糊查询
  51. const filter = {
  52. where: {}
  53. };
  54. if (keyword) {
  55. filter.where.$or = [
  56. { province: { $regex_ci: keyword } },
  57. { municipality: { $regex_ci: keyword } },
  58. { district: { $regex_ci: keyword } },
  59. { street: { $regex_ci: keyword } },
  60. { detailed_address: { $regex_ci: keyword } },
  61. { name: { $regex_ci: keyword } },
  62. { phone: { $regex_ci: keyword } },
  63. ];
  64. }
  65. const { data } = await models.adresses.list({
  66. filter,
  67. pageSize: 100, // 分页大小,建议指定,如需设置为其它值,需要和 pageNumber 配合使用,两者同时指定才会生效
  68. pageNumber: 1, // 第几页
  69. getCount: true, // 开启用来获取总数
  70. // envType: pre 体验环境, prod 正式环境
  71. envType: "prod",
  72. });
  73. // 返回查询到的数据列表 records 和 总数 total
  74. console.log(data);
  75. this.setData({
  76. addresses: data.records
  77. })
  78. },
  79. handleEdit(e) {
  80. const index = e.currentTarget.dataset.index;
  81. // 处理编辑逻辑
  82. },
  83. handleDelete(e) {
  84. const index = e.currentTarget.dataset.index;
  85. // 处理删除逻辑
  86. },
  87. // 添加地址
  88. async handleAddAddress() {
  89. // 处理添加地址逻辑
  90. wx.navigateTo({
  91. url: `/subpackages/address/address`
  92. });
  93. // const { data } = await models.adresses.create({
  94. // data: {
  95. // detailed_address: "沈阳国际软件园A区 A08栋 204", // 详细地址
  96. // address: "辽宁省-沈阳市-浑南区-沈本大街-沈阳国际软件园A区 A08栋 204", // 地址
  97. // municipality: "沈阳市", // 市
  98. // default: 1, // 默认
  99. // wx_user_id: "文本", // 用户id
  100. // province: "辽宁省", // 省
  101. // phone: "15376132110", // 手机号
  102. // street: "沈本大街", // 街道
  103. // district: "浑南区", // 区县
  104. // name: "王五", // 收货人
  105. // },
  106. // // envType: pre 体验环境, prod 正式环境
  107. // envType: "prod",
  108. // });
  109. // // 返回创建的数据 id
  110. // console.log(data);
  111. },
  112. // 删除地址
  113. async onDelete(event) {
  114. const _ids = event.currentTarget.dataset.id;
  115. console.log(_ids, '_ids');
  116. const { data } = await models.adresses.delete({
  117. filter: {
  118. where: {
  119. $and: [
  120. {
  121. _id: {
  122. $eq: _ids, // 推荐传入_id数据标识进行操作
  123. },
  124. },
  125. ]
  126. }
  127. },
  128. // envType: pre 体验环境, prod 正式环境
  129. envType: "prod",
  130. });
  131. // 返回删除成功的条数
  132. console.log(data);
  133. // 删除成功后,更新页面数据
  134. if (data.count > 0) {
  135. wx.showToast({
  136. title: '删除成功',
  137. icon: 'success',
  138. });
  139. this.getdatalist(); // 重新获取地址列表
  140. } else {
  141. wx.showToast({
  142. title: '删除失败',
  143. icon: 'none',
  144. });
  145. }
  146. },
  147. // 编辑
  148. onEdit() {
  149. wx.navigateTo({
  150. url: `/subpackages/address/address`
  151. });
  152. }
  153. });