From 49b589c3695d239f79ee41510f75b964f51551d5 Mon Sep 17 00:00:00 2001 From: xjw123456 Date: Thu, 30 Nov 2023 14:12:09 +0800 Subject: [PATCH] =?UTF-8?q?//=20=E6=95=B4=E5=90=88=E8=AF=B7=E6=B1=82=20?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E9=A6=96=E9=A1=B5=E4=B8=8B=E6=8B=89=E5=88=B7?= =?UTF-8?q?=E6=96=B0=E5=92=8C=E4=B8=8A=E6=8B=89=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/api.js | 27 +++++++++++++ common/axios.js | 59 +++++++++++++++++++---------- common/functions.js | 48 ++++++++++++++++++++++++ main.js | 8 ++++ pages.json | 6 ++- pages/index/index.vue | 102 +++++++++++++++++++++++++++++++++++++++----------- 6 files changed, 207 insertions(+), 43 deletions(-) create mode 100644 common/api.js create mode 100644 common/functions.js diff --git a/common/api.js b/common/api.js new file mode 100644 index 0000000..c864650 --- /dev/null +++ b/common/api.js @@ -0,0 +1,27 @@ +import request from './axios'; + + +// 获取首页轮播图 +export const getBannerList = (params, succCallback, errorCallback) => { + return request('/job-dev/house/applet/banner',params,'GET', succCallback,errorCallback) +}; + +// 获取地区 +export const getAddressList = (params, succCallback, errorCallback) => { + return request('/job-dev/house/applet/indexListPage', params,'GET', succCallback,errorCallback) +}; + +// 首页获取分类 +export const getClassList = (params, succCallback, errorCallback) => { + return request('/job-dev/house/applet/indexClass', params,'GET', succCallback,errorCallback) +}; + +// 首页获取好房源 +export const getHotList = (params, succCallback, errorCallback) => { + return request('/job-dev/house/applet/indexHotList', params,'GET', succCallback,errorCallback) +}; + +// 请求接口获得后台列表数据信息 = 推荐房源 +export const getHouseListPage = (params, succCallback, errorCallback) => { + return request('/job-dev/house/applet/indexListPage', params,'GET', succCallback,errorCallback) +}; \ No newline at end of file diff --git a/common/axios.js b/common/axios.js index d32bcbe..6114fac 100644 --- a/common/axios.js +++ b/common/axios.js @@ -1,5 +1,6 @@ import config from "./config"; import tokenUtil from "./token"; +import functions from './functions.js'; const doCommand = function (response, var0, var1) { //console.log(response.code, response.code*1 === 200, response.success, @@ -24,26 +25,44 @@ const doCommand = function (response, var0, var1) { const request = function (uri, param, method, var0, var1) { console.log("==> " + config + uri); - uni.request({ - url: config + uri, - method: method, - dataType: "json", - data: param, - header: {'x-auth': tokenUtil.token()}, - success: function (res) { - let response = res.data; - console.log("<== ", response); - doCommand(response, var0, var1); - }, - complete(res) { - //let response = res.data; - //console.log("<== ", response); - //doCommand(response, var0, var1); - }, - fail(err) { - console.log("Error:", err); - }, - }); + // 是否要检测有token + + return new Promise((resolve,reject) => { + uni.request({ + url: config + uri, + method: method, + dataType: "json", + data: param, + header: {'x-auth': tokenUtil.token()}, + success: function (res) { + let response = res.data; + console.log("<== ", response); + if(response.code == 200) { + // 成功 执行成功回调 + resolve(response); + }else if (response.code == 401){ + // 登录过期 提示并跳转至登录页面 + }else if(response.code == 500) { + // 报错提示 是否展示 + // functions.showToast(response.message) + resolve(response); + }else { + // 其他情况 + reject(response); + } + // doCommand(response, var0, var1); + }, + complete(res) { + //let response = res.data; + //console.log("<== ", response); + //doCommand(response, var0, var1); + }, + fail(err) { + functions.showToast('数据加载异常!'); + console.log("Error:", err); + }, + }); + }) }; export default request; diff --git a/common/functions.js b/common/functions.js new file mode 100644 index 0000000..1d695fd --- /dev/null +++ b/common/functions.js @@ -0,0 +1,48 @@ + +function showLoading(title= '加载中...'){ + uni.showLoading({ + title: title, + duration: 10000, + mask: true + }) +} + + function hideLoading(){ + uni.hideLoading(); + } + + + function showToast (title) { + uni.showToast({ + title:title, + icon:"none", + duration: 2000, + mask: false + }) + } + + function showToastSuccess(title){ + uni.showToast({ + title: title, + icon: "success", + duration: 2000, + mask:false + }) + } + + function isExist(value) { + if (value !== null && value !== undefined && value !== "undefined" && value !== "null" && value.length > 0) { + return true; + } else { + return false; + } + } + + export default { + showToast: showToast, + showLoading: showLoading, + hideLoading: hideLoading, + showToastSuccess: showToastSuccess, + isExist: isExist + } + \ No newline at end of file diff --git a/main.js b/main.js index 93635f2..065334f 100644 --- a/main.js +++ b/main.js @@ -9,6 +9,14 @@ Vue.prototype.$tokenUtil = tokenUtil; Vue.prototype.$token = tokenUtil.token; Vue.prototype.$user = tokenUtil.user; +/* 导入接口 */ +let api = require('./common/api'); +Vue.prototype.$api = api; + +// 方法 +import functions from './common/functions.js' +Vue.prototype.$functions = functions + Vue.mixin({ methods: { setData: function(obj) { diff --git a/pages.json b/pages.json index b401ace..d36f5a4 100644 --- a/pages.json +++ b/pages.json @@ -1,6 +1,10 @@ { "pages": [{ - "path": "pages/index/index" + "path": "pages/index/index", + "style": { + "navigationBarTitleText": "", + "enablePullDownRefresh": true + } },{ "path": "pages/index/ihome" },{ diff --git a/pages/index/index.vue b/pages/index/index.vue index ea5c73c..a8a3f59 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -108,7 +108,12 @@ addressList:[], classList:[], hotList:[], - list:[] + list:[], + lock: true, + size: 20, + page: 1, + total: null, + is_pull: 1, // 1是下拉刷新 2是上拉加载 } }, onLoad() { @@ -118,6 +123,17 @@ this.getHotList(); this.getHouseListPage(); }, + // 下拉刷新首页推荐房源 + onPullDownRefresh() { + this.is_pull = 1; + this.getHouseListPage() + }, + // 上拉加载 + onReachBottom() { + this.is_pull = 2; + // 写下限制重复发 以及最后一页 + this.getHouseListPage() + }, // 隐藏微信h5的标题栏 onReady() { @@ -126,41 +142,83 @@ //获取轮播图 getBannerList(){ - this.$request('/job-dev/house/applet/banner',{},'GET', (res) => { - this.bannerList = res; - console.log("111111") - console.log(res[0].image) - }) + // this.getBannerList() + this.$api.getBannerList().then(res => { + this.bannerList = res.result; + }).catch(err => { + console.log(err,"错误信息") + }); + // this.$request('/job-dev/house/applet/banner',{},'GET', (res) => { + // this.bannerList = res; + // console.log("111111") + // console.log(res[0].image) + // }) }, //获取地区 getAddressList(){ - this.$request('/job-dev/house/applet/indexListPage',{},'GET', (res) => { - this.addressList = res.records; - console.log(res.records) - }) + this.$api.getAddressList().then(res => { + this.addressList = res.result; + }).catch(err => { + console.log(err,"错误信息") + }); + // this.$request('/job-dev/house/applet/indexListPage',{},'GET', (res) => { + // this.addressList = res.records; + // console.log(res.records) + // }) }, //获取分类 getClassList(){ - this.$request('/job-dev/house/applet/indexClass',{},'GET', (res) => { - this.classList = res.records; - console.log(res.records) - }) + this.$api.getClassList().then(res => { + this.classList = res.result; + }).catch(err => { + console.log(err,"错误信息") + }); + // this.$request('/job-dev/house/applet/indexClass',{},'GET', (res) => { + // this.classList = res.records; + // console.log(res.records) + // }) }, //获取好房源 getHotList(){ - this.$request('/job-dev/house/applet/indexHotList',{},'GET', (res) => { - this.hotList = res.records; - console.log(res.records) - }) + this.$api.getHotList().then(res => { + this.hotList = res.result.records; + }).catch(err => { + console.log(err,"错误信息") + }); + // this.$request('/job-dev/house/applet/indexHotList',{},'GET', (res) => { + // this.hotList = res.records; + // console.log(res.records) + // }) }, //请求接口获得后台列表数据信息 = 推荐房源 getHouseListPage() { - this.$request('/job-dev/house/applet/indexListPage',{},'GET', (res) => { - this.list = res.records; - console.log(res.records) - }) + // 区分上拉加载和下拉刷新 is_pull 下拉 1 上拉 2 + const is_pull = this.is_pull; + this.$api.getHouseListPage().then(res => { + this.list = res.result; + if(is_pull === 1) { + // 下拉 + this.page = 1; + this.list = res.result.records; + }else { + // 加载 + this.page +=1; + this.list = this.list.concat(res.result.records); + } + }).catch(err => { + console.log(err,"错误信息") + }); + // this.$request('/job-dev/house/applet/indexListPage',{},'GET', (res) => { + // this.list = res.records; + // console.log(res.records) + // }) } + + // 下拉刷新 首页推荐房源分页下拉加载,上拉刷新。如果加载完了提示 没有更多了 + + + // 上拉加载 } }