addresslist.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // import { models, db, _ } from '../../utils/cloudbase.js'
  2. import { getDB, getModels, getCommand, getTempFileURLs } from '../../utils/cloudbase.js'
  3. Page({
  4. data: {
  5. addresses: [],
  6. souimg: '',
  7. shanchu: '',
  8. xiugaiimg: '',
  9. dingweiimg: '',
  10. keyword: '', // 搜索关键词
  11. editId: '', // 当前编辑地址ID
  12. },
  13. onShow() {
  14. const addressInfo = wx.getStorageSync('addressInfo')
  15. if (addressInfo) {
  16. if (this.data.editId) {
  17. // 编辑
  18. console.log('编辑编辑');
  19. this.updateAddress(addressInfo, this.data.editId)
  20. } else {
  21. // 新增
  22. this.dizhi(addressInfo)
  23. }
  24. wx.removeStorageSync('addressInfo')
  25. this.setData({ editId: '' })
  26. }
  27. this.getdatalist()
  28. },
  29. async onLoad(options) {
  30. // const title = options.title ? decodeURIComponent(options.title) : '商品列表';
  31. // this.setData({ title });
  32. // wx.setNavigationBarTitle({ title });
  33. const fileIDs = [
  34. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/sou.png',
  35. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/shanchu.png',
  36. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/xiugai.png',
  37. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/dingwei.png'
  38. ];
  39. const fileList = await getTempFileURLs(fileIDs)
  40. this.setData({
  41. souimg: fileList[0].tempFileURL,
  42. shanchu: fileList[1].tempFileURL,
  43. xiugaiimg: fileList[2].tempFileURL,
  44. dingweiimg: fileList[3].tempFileURL,
  45. })
  46. // // 并发下载多个 fileID
  47. // Promise.all(
  48. // fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  49. // ).then(results => {
  50. // // 每个 result 对应一个下载结果
  51. // const tempFilePaths = results.map(r => r.tempFilePath);
  52. // console.log('全部下载成功:', tempFilePaths);
  53. // this.setData({
  54. // souimg: tempFilePaths[0],
  55. // shanchu: tempFilePaths[1],
  56. // xiugaiimg: tempFilePaths[2],
  57. // dingweiimg: tempFilePaths[3],
  58. // });
  59. // }).catch(err => {
  60. // console.error('有文件下载失败:', err);
  61. // });
  62. },
  63. // 模糊搜索
  64. onSearchInput(e) {
  65. const keyword = e.detail.value.trim();
  66. this.setData({ keyword });
  67. this.getdatalist(); // 每次输入更新列表(也可节流优化)
  68. },
  69. // 地址数据
  70. async getdatalist() {
  71. const models = await getModels()
  72. const keyword = this.data.keyword;
  73. // 构建模糊查询
  74. const filter = {
  75. where: {}
  76. };
  77. if (keyword) {
  78. filter.where.$or = [
  79. { province: { $regex_ci: keyword } },
  80. { municipality: { $regex_ci: keyword } },
  81. { district: { $regex_ci: keyword } },
  82. { street: { $regex_ci: keyword } },
  83. { detailed_address: { $regex_ci: keyword } },
  84. { name: { $regex_ci: keyword } },
  85. { phone: { $regex_ci: keyword } },
  86. ];
  87. }
  88. const { data } = await models.adresses.list({
  89. filter,
  90. pageSize: 100, // 分页大小,建议指定,如需设置为其它值,需要和 pageNumber 配合使用,两者同时指定才会生效
  91. pageNumber: 1, // 第几页
  92. getCount: true, // 开启用来获取总数
  93. // envType: pre 体验环境, prod 正式环境
  94. envType: "prod",
  95. });
  96. // 返回查询到的数据列表 records 和 总数 total
  97. console.log(data);
  98. this.setData({
  99. addresses: data.records
  100. })
  101. },
  102. handleEdit(e) {
  103. const index = e.currentTarget.dataset.index;
  104. // 处理编辑逻辑
  105. },
  106. handleDelete(e) {
  107. const index = e.currentTarget.dataset.index;
  108. // 处理删除逻辑
  109. },
  110. async dizhi(list) {
  111. const models = await getModels()
  112. const isLoggedIn = wx.getStorageSync('userInfo') || null;
  113. console.log(list, isLoggedIn, 'list');
  114. // 解析省市区
  115. const province = list.area?.[0]?.name || '';
  116. const city = list.area?.[1]?.name || '';
  117. const district = list.area?.[2]?.name || '';
  118. const street = list.area?.[3]?.name || ''; // 街道可以是第四项,也可以自定义
  119. const detailed_address = list.address || ''; // 插件返回的详细地址
  120. try {
  121. const { data } = await models.adresses.create({
  122. data: {
  123. detailed_address: detailed_address, // 详细地址
  124. address: `${province}${city}${district}${detailed_address}`, // 拼接完整地址
  125. province: province,
  126. municipality: city,
  127. district: district,
  128. street: street,
  129. phone: list.phone || '', // 手机号
  130. name: list.receiver || '', // 收货人
  131. default: list.default ? 0 : 1, // 默认地址
  132. wx_user_id: isLoggedIn?._id || '', // 用户ID
  133. // label: list.label || '', // 标签
  134. },
  135. envType: "prod",
  136. });
  137. console.log(data);
  138. if (data.id) {
  139. wx.showToast({
  140. title: '新增成功',
  141. icon: 'success',
  142. });
  143. this.getdatalist(); // 重新获取地址列表
  144. } else {
  145. wx.showToast({
  146. title: '新增失败',
  147. icon: 'none',
  148. });
  149. }
  150. } catch (err) {
  151. console.error('新增地址失败', err);
  152. wx.showToast({
  153. title: '新增失败',
  154. icon: 'none',
  155. });
  156. }
  157. },
  158. // 添加地址
  159. async handleAddAddress() {
  160. console.log('添加地址');
  161. wx.navigateTo({
  162. url: 'plugin://address-form/index'
  163. });
  164. },
  165. // 删除地址
  166. async onDelete(event) {
  167. const models = await getModels()
  168. const _ids = event.currentTarget.dataset.id;
  169. console.log(_ids, '_ids');
  170. const { data } = await models.adresses.delete({
  171. filter: {
  172. where: {
  173. $and: [
  174. {
  175. _id: {
  176. $eq: _ids, // 推荐传入_id数据标识进行操作
  177. },
  178. },
  179. ]
  180. }
  181. },
  182. // envType: pre 体验环境, prod 正式环境
  183. envType: "prod",
  184. });
  185. // 返回删除成功的条数
  186. console.log(data);
  187. // 删除成功后,更新页面数据
  188. if (data.count > 0) {
  189. wx.showToast({
  190. title: '删除成功',
  191. icon: 'success',
  192. });
  193. this.getdatalist(); // 重新获取地址列表
  194. } else {
  195. wx.showToast({
  196. title: '删除失败',
  197. icon: 'none',
  198. });
  199. }
  200. },
  201. // 编辑
  202. onEdit(event) {
  203. console.log('编辑地址');
  204. const item = event.currentTarget.dataset.id;
  205. console.log(item, '_ids');
  206. this.setData({ editId: item._id })
  207. // 构建 edit 对象
  208. const editInfo = {
  209. receiver: item.name,
  210. phone: item.phone,
  211. address: item.detailed_address,
  212. area: [
  213. { name: item.province },
  214. { name: item.municipality },
  215. { name: item.district },
  216. { name: item.street }
  217. ],
  218. default: item.default === 0 ? true : false,
  219. label: item.label || ''
  220. };
  221. console.log(editInfo, 'editInfo');
  222. // ⚠️ 直接传 JSON 字符串,不要 encodeURIComponent
  223. wx.navigateTo({
  224. url: `plugin://address-form/index?edit=${JSON.stringify(editInfo)}`
  225. });
  226. },
  227. async updateAddress(list, bianji_id) {
  228. const models = await getModels()
  229. const isLoggedIn = wx.getStorageSync('userInfo') || null;
  230. console.log(list, isLoggedIn, bianji_id, 'list');
  231. // 解析省市区
  232. const province = list.area?.[0]?.name || '';
  233. const city = list.area?.[1]?.name || '';
  234. const district = list.area?.[2]?.name || '';
  235. const street = list.area?.[3]?.name || ''; // 街道可以是第四项,也可以自定义
  236. const detailed_address = list.address || ''; // 插件返回的详细地址
  237. try {
  238. const { data } = await models.adresses.update({
  239. data: {
  240. detailed_address: detailed_address, // 详细地址
  241. address: `${province}${city}${district}${detailed_address}`, // 拼接完整地址
  242. province: province,
  243. municipality: city,
  244. district: district,
  245. street: street,
  246. phone: list.phone || '', // 手机号
  247. name: list.receiver || '', // 收货人
  248. default: list.default ? 0 : 1, // 默认地址
  249. wx_user_id: isLoggedIn?._id || '', // 用户ID
  250. // label: list.label || '', // 标签
  251. },
  252. filter: {
  253. where: {
  254. $and: [
  255. {
  256. _id: {
  257. $eq: bianji_id, // 推荐传入_id数据标识进行操作
  258. },
  259. },
  260. ]
  261. }
  262. },
  263. envType: "prod",
  264. });
  265. console.log(data);
  266. if (data.count > 0) {
  267. wx.showToast({
  268. title: '编辑成功',
  269. icon: 'success',
  270. });
  271. this.getdatalist(); // 重新获取地址列表
  272. } else {
  273. wx.showToast({
  274. title: '编辑失败',
  275. icon: 'none',
  276. });
  277. }
  278. } catch (err) {
  279. console.error('新增地址失败', err);
  280. wx.showToast({
  281. title: '编辑失败',
  282. icon: 'none',
  283. });
  284. }
  285. },
  286. onSelect(e) {
  287. const item = e.currentTarget.dataset.item;
  288. const pages = getCurrentPages();
  289. const prevPage = pages[pages.length - 2]; // 上一页
  290. // 方式一:直接赋值给上一页 data
  291. prevPage.setData({
  292. selectedAddress: item
  293. });
  294. // 返回上一页
  295. wx.navigateBack({
  296. delta: 1
  297. });
  298. }
  299. });