From c4a1aa873a6aa127036e8ad6960b16c204708aa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BD=87?= Date: Mon, 27 Jan 2025 02:09:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=87=86=E5=A4=87=E9=BD=90?= =?UTF-8?q?=E5=85=A8=EF=BC=8C=E5=8D=B3=E5=B0=86=E6=8F=90=E4=BA=A4=E6=95=B0?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/common.js | 31 +++++++- api/request.js | 12 ++- api/user.js | 3 + app.js | 54 ++++++++++++- components/address/index.js | 119 +++++++++++++++++++++++++++- components/address/index.wxml | 29 ++++--- pages/help/address/edit/index.js | 1 - pages/help/address/edit/index.json | 3 +- pages/help/address/index/index.js | 23 ++++-- pages/help/address/index/index.json | 3 +- pages/help/address/index/index.wxml | 33 +++----- pages/help/address/index/index.wxss | 14 ++++ pages/help/community/index.js | 29 ++++--- pages/help/index/index.js | 91 +++++++++++++++++---- pages/help/index/index.wxml | 29 +++++-- pages/help/package/index.js | 66 ++++++++++++--- pages/help/package/index.wxml | 34 +++----- 17 files changed, 454 insertions(+), 120 deletions(-) diff --git a/api/common.js b/api/common.js index d664eed..c5388a5 100644 --- a/api/common.js +++ b/api/common.js @@ -1,9 +1,34 @@ import request from './request' +let app = getApp(); -module.exports = { +export default { community:{ - list(params){ - return request.get('/api/community') + list:()=>request.get('/api/community') + }, + address:{ + list(commityId){ + const data = {}; + if(commityId){ + data.community_id = commityId; + } + return request.get('/api/address',data) + }, + add:(data)=>request.post('/api/address',data), + update:(data)=>request.put(`/api/address/${data.id}`,data), + }, + building:{ + list(communityId){ + if(!app){ + app = getApp(); + } + const data = { + community_id:communityId, + user_id:app.globalData.userInfo.userid + } + return request.get('/api/community/building/list') } + }, + station:{ + list:(community_id)=>request.get('/api/station',{community_id}) } } \ No newline at end of file diff --git a/api/request.js b/api/request.js index 8813c61..a9b4db1 100644 --- a/api/request.js +++ b/api/request.js @@ -26,10 +26,16 @@ const sendRequest = (options)=>{ } export default { - get(url,params){ - sendRequest({url,method:'get',params}); + get(url,data){ + return sendRequest({url,method:'get',data}); }, post(url,data){ - sendRequest({url,method:'post',data}); + return sendRequest({url,method:'post',data}); + }, + put(url,data){ + return sendRequest({url,method:'put',data}); + }, + delete(url,data){ + return sendRequest({url,method:'delete',data}); } } \ No newline at end of file diff --git a/api/user.js b/api/user.js index 48234d9..408b23b 100644 --- a/api/user.js +++ b/api/user.js @@ -18,5 +18,8 @@ export default { }, info(){ return request.get('/api/user/info'); + }, + order:{ + pre:(data)=>request.post('/api/order/pre-order',data) } } \ No newline at end of file diff --git a/app.js b/app.js index 9a4b971..334bf99 100644 --- a/app.js +++ b/app.js @@ -1,4 +1,5 @@ import userApi from './api/user'; +import commonApi from './api/common'; App({ onLaunch() { // 展示本地存储能力 @@ -15,14 +16,61 @@ App({ wx.getStorage({ key:'accessToken', success:(res)=>{ - console.log(res.data); this.globalData.accessToken = res.data; - userApi.info() + if(res.data){ + userApi.info().then((data)=>{ + this.globalData.userInfo = data; + }); + } } }) }, + navToLogin(){ + wx.navigateTo({ + url: '/pages/login/login', + }) + }, globalData: { userInfo: null, - accessToken:null + accessToken:null, + community:{ + _list:[], + lastFetchTime:null, + getList(){ + let lft = this.lastFetchTime; + return new Promise((rs,rj)=>{ + if(!lft&&new Date()-lft>30){ + commonApi.community.list().then((data)=>{ + this.lastFetchTime = new Date(); + this._list = data; + rs(data) + }) + }else{ + rs(this._list); + } + }) + }, + }, + address:{ + _list:[], + lastFetchTime:null, + getList(){ + let lft = this.lastFetchTime; + return new Promise((rs,rj)=>{ + if(!lft&&new Date()-lft>30){ + commonApi.address.list().then((data)=>{ + this.lastFetchTime = new Date(); + this._list = data; + rs(data) + }) + }else{ + rs(this._list); + } + }) + }, + } + }, + getAddressList(){ + } }) diff --git a/components/address/index.js b/components/address/index.js index 128dd2c..f1d0394 100644 --- a/components/address/index.js +++ b/components/address/index.js @@ -1,4 +1,6 @@ -// components/address/index.js +import commonApi from '../../api/common'; +const app = getApp(); + Component({ /** @@ -12,6 +14,10 @@ Component({ deleteText:{ type:String, value:'' + }, + communityId:{ + type:Number, + value:null } }, @@ -19,18 +25,123 @@ Component({ * 组件的初始数据 */ data: { - building:['一栋','二栋','三栋','四栋','五栋'], - }, + buildingList:[], + buildingIndex:0, + communityList:[], + communityIndex:0, + currentEditAddress:{}, + name:'', + gender:'', + phone:'', + community_building_id:'', + address_detail:'' + }, + lifetimes:{ + attached(){ + if(app.globalData.tempAddress){ + this.setData({ + currentEditAddress:app.globalData.tempAddress, + name:app.globalData.tempAddress.name, + gender:app.globalData.tempAddress.gender, + phone:app.globalData.tempAddress.phone, + community_building_id:app.globalData.tempAddress.community_building_id, + address_detail:app.globalData.tempAddress.address_detail + }) + } + commonApi.community.list().then((data)=>{ + let communityIndex; + this.setData({ + communityList:data.items + }); + data.items.map((item,index)=>{ + if(item.id==this.data.currentEditAddress.id){ + communityIndex = index; + } + }) + + //没有找到 id 就是新增,就去找默认的社区进行填充 + if(communityIndex){ + this.getBuildingList(); + }else{ + for(let [index,item] of data.items.entries()){ + if(item.id==this.properties.communityId){ + this.setData({ + communityIndex:index + }); + } + } + this.getBuildingList(); + } + }); + // console.log(app.globalData.tempAddress); + } + }, /** * 组件的方法列表 */ methods: { save(){ - this.triggerEvent('save') + + + this.triggerEvent('save'); + let data = { + community_id:this.data.communityList[this.data.communityIndex].id, + community_building_id:this.data.buildingList[this.data.buildingIndex].id, + address_detail:this.data.address_detail, + name:this.data.name, + gender:this.data.gender, + phone:this.data.phone + } + console.log(data); + if(this.data.currentEditAddress&&this.data.currentEditAddress.id){ + //编辑 + data.id = this.data.currentEditAddress.id; + commonApi.address.update(data); + }else{ + //新增 + commonApi.address.add(data).then((data)=>{ + const pages = getCurrentPages(); + const prePage = pages[pages.length-2]; + prePage.changeAddress(data); + wx.navigateBack({ + success(){ + wx.showToast({ + title: '新增成功', + icon:'success' + }); + } + }); + }); + } + }, + getBuildingList(){ + const current = this.data.communityList[this.data.communityIndex]; + commonApi.building.list(current.id).then((data)=>{ + let buildingIndex = 0; + data.items.map((item,index)=>{ + item.displayText = `${item.community_name} ${item.building_name}`; + if(item.id==this.data.currentEditAddress.id){ + buildingIndex = index; + } + }); + this.setData({ + buildingList:data.items, + buildingIndex + }) + }); }, del(){ this.triggerEvent('delete') + }, + chooseCommonity(event){ + this.setData({ + communityIndex:event.detail.value + }) + }, + buildingChange(event){ + const buildingIndex = event.detail.value; + this.setData({buildingIndex}); } } }) \ No newline at end of file diff --git a/components/address/index.wxml b/components/address/index.wxml index 91aa150..c3566cc 100644 --- a/components/address/index.wxml +++ b/components/address/index.wxml @@ -1,37 +1,42 @@ - - - - + + + + + + - - + + - + - + - + - + diff --git a/pages/help/address/edit/index.js b/pages/help/address/edit/index.js index 388d23f..3f6ea07 100644 --- a/pages/help/address/edit/index.js +++ b/pages/help/address/edit/index.js @@ -29,7 +29,6 @@ Page({ * 生命周期函数--监听页面加载 */ onLoad(options) { - }, /** diff --git a/pages/help/address/edit/index.json b/pages/help/address/edit/index.json index a5410ba..82cadae 100644 --- a/pages/help/address/edit/index.json +++ b/pages/help/address/edit/index.json @@ -1,5 +1,6 @@ { "usingComponents": { "address-editor":"/components/address" - } + }, + "navigationBarTitleText": "编辑地址" } \ No newline at end of file diff --git a/pages/help/address/index/index.js b/pages/help/address/index/index.js index 8517f53..6714677 100644 --- a/pages/help/address/index/index.js +++ b/pages/help/address/index/index.js @@ -1,21 +1,28 @@ -// pages/help/address/index.js +import commonApi from '../../../../api/common'; +const app = getApp(); Page({ /** * 页面的初始数据 */ data: { - items: [ - { name: '1', value: 'cell standard' }, - { name: '2', value: 'cell standard', checked: 'true' }, - ], + addressList:[], + communityId:null }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { - + console.log(options); + this.setData({ + communityId:options.communityId + }) + commonApi.address.list(options.communityId).then((data)=>{ + this.setData({ + addressList:data + }) + }) }, /** @@ -67,7 +74,9 @@ Page({ }, - goToAddressEditor(){ + goToAddressEditor(event){ + const item = event.currentTarget.dataset.item; + app.globalData.tempAddress = item; wx.navigateTo({ url: '/pages/help/address/edit/index', }) diff --git a/pages/help/address/index/index.json b/pages/help/address/index/index.json index a5410ba..8252772 100644 --- a/pages/help/address/index/index.json +++ b/pages/help/address/index/index.json @@ -1,5 +1,6 @@ { "usingComponents": { "address-editor":"/components/address" - } + }, + "navigationBarTitleText": "收件人信息" } \ No newline at end of file diff --git a/pages/help/address/index/index.wxml b/pages/help/address/index/index.wxml index ef3eada..99405ca 100644 --- a/pages/help/address/index/index.wxml +++ b/pages/help/address/index/index.wxml @@ -1,26 +1,19 @@ - + 常用地址 - - - 朝阳时代西锦 3栋2单元1802 - 何灵 13486745777 + + + + {{item.address_detail}} + {{item.name}} {{item.phone}} + + - - - - - 朝阳时代西锦 3栋2单元1802 - 何灵 13486745777 - - - - - - 朝阳时代西锦 3栋2单元1802 - 何灵 13486745777 - - + + + 暂无常用地址 + 使用过的地址会在这里显示 \ No newline at end of file diff --git a/pages/help/address/index/index.wxss b/pages/help/address/index/index.wxss index f3410d0..5c76de8 100644 --- a/pages/help/address/index/index.wxss +++ b/pages/help/address/index/index.wxss @@ -26,4 +26,18 @@ .address-list .item .icon{ width:36rpx;height:36rpx; padding:10rpx; +} + +.list-empty{ + text-align: center; + padding:60rpx 0 30rpx 0; +} +.list-empty .title{ + font-size: 30rpx; + font-weight: 500; +} +.list-empty .sub-title{ + font-size: 26rpx; + color: #A1A1A1; + margin-top:24rpx; } \ No newline at end of file diff --git a/pages/help/community/index.js b/pages/help/community/index.js index 9d19028..9739bf6 100644 --- a/pages/help/community/index.js +++ b/pages/help/community/index.js @@ -1,4 +1,4 @@ -const commonApi = require('../../../api/common'); +import commonApi from '../../../api/common'; Page({ @@ -10,28 +10,31 @@ Page({ communityList:[] }, onSelectItem(event){ + const pages = getCurrentPages(); + const prePage = pages[pages.length-2]; const currentCommunity = event.currentTarget.dataset.item; this.setData({currentCommunity}) - wx.setStorage({ - key:"currentCommunity", - data:currentCommunity - }); + prePage.changeCommunity(currentCommunity); + wx.navigateBack(); }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { - wx.getStorage({ - key:'currentCommunity', - success:(res)=>{ - console.log(res); - this.setData({currentCommunity:res.data}) - } - }) + console.log(options); commonApi.community.list().then((data)=>{ const communityList = data.items; - this.setData({communityList}); + let currentCommunity; + data.items.map((item)=>{ + if(item.id==options.communityId){ + currentCommunity = item; + } + }) + this.setData({ + communityList, + currentCommunity + }); }); }, diff --git a/pages/help/index/index.js b/pages/help/index/index.js index c5fb05f..55d88fe 100644 --- a/pages/help/index/index.js +++ b/pages/help/index/index.js @@ -1,3 +1,5 @@ +const app = getApp(); +import commonApi from '../../../api/common'; Page({ @@ -5,35 +7,92 @@ Page({ * 页面的初始数据 */ data: { - index:0, - array:[1,2,3,4], - communityList:[], - currentCommunity:null + // communityList:[], + isLogin:!!app.globalData.accessToken, + currentAddress:null, + currentCommunity:{ + id:null, + name:'' + }, + package:{ + name:'', + count:0 + } + + // addressList:[], + // addressIndex:0 }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { + this.getAddress(); + wx.getStorage({ + key:'pre-order', + success:(res)=>{ + console.log(res.data); + const name = []; + let count = 0; + res.data.price_request.package.map((item)=>{ + name.push(item.station_name); + count+=item.pickup_codes.split(',').length; + }); + this.setData({ + package:{ + name:name.join('|'), + count:count + } + }) + } + }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { + }, - /** * 生命周期函数--监听页面显示 */ onShow() { - wx.getStorage({ - key:"currentCommunity", - success:(res)=>{ + //这里要要重新过滤当前送达地址,因为可能会重新选择社区 + }, + getAddress(communityId){ + commonApi.address.list(communityId).then((data)=>{ + let currentAddress = data[0]||{}; + data.map((item,index)=>{ + if(item.is_default){ + currentAddress = item; + } + }) + this.setData({ + currentAddress, + }); + if(currentAddress.id){ this.setData({ - currentCommunity:res.data + currentCommunity:{ + id:currentAddress.community_id, + name:currentAddress.community_name + } }) } + }); + }, + changeCommunity(community){ + this.setData({ + currentCommunity:{ + id:community.id, + name:community.name + } + }); + this.getAddress(community.id); + }, + changeAddress(address){ + this.setData({ + currentAddress:address }) }, @@ -73,17 +132,21 @@ Page({ }, goToAddPackage(){ wx.navigateTo({ - url: '/pages/help/package/index', + url: `/pages/help/package/index?communityId=${this.data.currentCommunity.id}`, }) }, goToCommunity(){ wx.navigateTo({ - url: '/pages/help/community/index', + url: `/pages/help/community/index?communityId=${this.data.currentCommunity.id}`, }) }, goToAddress(){ - wx.navigateTo({ - url: '/pages/help/address/index/index', - }) + if(app.globalData.accessToken){ + wx.navigateTo({ + url: `/pages/help/address/index/index?communityId=${this.data.currentCommunity.id}`, + }) + }else{ + app.navToLogin(); + } } }) \ No newline at end of file diff --git a/pages/help/index/index.wxml b/pages/help/index/index.wxml index 6c1ec41..60d41c3 100644 --- a/pages/help/index/index.wxml +++ b/pages/help/index/index.wxml @@ -4,23 +4,40 @@ - {{currentCommunity?currentCommunity.name:'选择小区'}} + + {{ + currentCommunity.id?currentCommunity.name:'请选择小区' + }} + - 朝阳时代西锦12栋1单元2072朝阳时代西锦12栋1单元2072 - 周先生 13888888888 + + + {{currentAddress.community_name}} + {{currentAddress.address_detail}} + + + {{currentAddress.name}} {{currentAddress.phone}} + + + + 请录入配送地址 + - + + {{package.name}} + 共计 {{package.count}} 个包裹 + + 送达地址 - @@ -41,7 +58,7 @@ -