Skip to content

Commit

Permalink
封装请求接口,统一管理
Browse files Browse the repository at this point in the history
  • Loading branch information
TeanLee committed Dec 20, 2021
1 parent 843408a commit c32ecdb
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 265 deletions.
97 changes: 97 additions & 0 deletions api/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 小程序开发api接口统一配置
// 如果你的域名是: https://www.baidu.com/cn 那么这里只要填写 cn
let subDomain = '' // 子域名,没有就等于''
const API_BASE_URL = 'http://localhost:8080' // 主域名


const request = (url, method, data, type) => {
let _url = API_BASE_URL + subDomain + url
let ContentType = 'application/json';
if (method === "post" && type !== "json" || method === "delete") ContentType = 'application/x-www-form-urlencoded'
return new Promise((resolve, reject) => {
wx.request({
url: _url,
method: method,
data: data,
header: {
'Content-Type': ContentType
},
success(request) {
resolve(request.data)
},
fail(error) {
reject(error)
},
complete(aaa) {
// 加载完成
}
})
})
}

/**
* 小程序的promise没有finally方法,自己扩展下
*/
Promise.prototype.finally = function (callback) {
var Promise = this.constructor;
return this.then(
function (value) {
Promise.resolve(callback()).then(
function () {
return value;
}
);
},
function (reason) {
Promise.resolve(callback()).then(
function () {
throw reason;
}
);
}
);
}

module.exports = {
request,
// 首页接口
// 首页获取分类接口
getCategory: () => request('/category','get'),
// 首页列表接口
getProductList: () => request('/product/list','get'),
// 首页用户登录(获取用户名信息)
login: (data) => request('/user/login','post', data),
// 将商品加入购物车
addToCart: (data) => request('/shopping-cart/add','post', data),
// 减少商品数量
cutCartCount: (data) => request('/shopping-cart/cut','post', data),
// 删除购物车商品
deleteCart: (data) => request('/shopping-cart/delete','delete', data),

// 选择地址页面接口
// 获取当前用户信息
getUserInfo: () => request('/user/info','get'),


// 商品列表页面(点击首页分类跳转的页面)
getProductsByCategoryId: (data) => request('/product/get-by-category-id','get', data),

// 创建地址页面
// 修改收获地址、收件人等信息
setUserInfo: (data) => request('/user/set-info','post', data),
// 获取用户收货地址等信息
getUserInfo: () => request('/user/info','get'),

// 购物车页面
// 获取当前用户购物车的所有商品
getShoppingCart: () => request('/shopping-cart/all','get'),
// 提交订单
addOrder: (data, type) => request('/order/add','post', data, type),


// 订单页面
// 根据订单状态查询所有订单
listOrders: (data) => request('/order/list-detail','get', data),
// 修改订单状态(0:待付款;1:待配送;2:待评价)
updateOrder: (data) => request('/order/update-status','post', data),
}
32 changes: 9 additions & 23 deletions pages/chooseAddress/chooseAddress.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// pages/chooseAddress/chooseAddress.js
const API = require("../../api/main");
Page({

/**
Expand All @@ -20,18 +21,7 @@ Page({
* 生命周期函数--监听页面加载
*/
onLoad: function () {
// wx.request({
// url: 'http://localhost:8080/user/info',
// success: (res) => {
// const { address, phone, receiver} = res.data;
// console.log("address:" + address)
// this.setData({
// address,
// phone,
// receiver
// })
// }
// })

},

/**
Expand All @@ -45,17 +35,13 @@ Page({
* 生命周期函数--监听页面显示
*/
onShow: function () {
wx.request({
url: 'http://localhost:8080/user/info',
success: (res) => {
const { address, phone, receiver} = res.data;
console.log("address:" + address)
this.setData({
address,
phone,
receiver
})
}
API.getUserInfo().then(res => {
const { address, phone, receiver} = res;
this.setData({
address,
phone,
receiver
})
})
},

Expand Down
45 changes: 16 additions & 29 deletions pages/goodsList/goodsList.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const API = require("../../api/main");
const app = getApp();
// pages/goodsList/goodsList.js
Page({
Expand All @@ -10,19 +11,13 @@ Page({
},
onLoad: function (options) {
const selectedId = options.selectedId; // 获取跳转页面传递过来的id
wx.request({
// 获取所有分类的商品信息
url: "http://localhost:8080/product/get-by-category-id",
data: {
categoryId: selectedId
},
success: (res) => {
console.log(res.data)
this.setData({
// app.globalData.goodsSortsChoice从全局变量中获取上一步用户点击的分类是哪一个
goods: res.data
})
}

API.getProductsByCategoryId({
"categoryId": selectedId
}).then(res => {
this.setData({
goods: res
})
})
},
toSort: function() {
Expand All @@ -35,22 +30,14 @@ Page({
this.addToCart(productid);
},
addToCart: function(productId) {
wx.request({
url: "http://localhost:8080/shopping-cart/add",
method: "POST",
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {
"productId": productId
},
success: (res) => {
wx.showToast({
title: '加购成功!', // 标题
icon: 'success',
duration: 1500 // 提示窗停留时间,默认1500ms
})
}
API.addToCart({
"productId": productId
}).then(() => {
wx.showToast({
title: '加购成功!', // 标题
icon: 'success',
duration: 1500 // 提示窗停留时间,默认1500ms
})
})
},

Expand Down
63 changes: 19 additions & 44 deletions pages/index/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//index.js
//获取应用实例
const API = require("../../api/main")
const app = getApp()

Page({
Expand Down Expand Up @@ -50,8 +51,6 @@ Page({
},
linkToList: function(e) {
// 将用户点击的分类保存在全局变量中,用于页面跳转后的商品显示
app.globalData.goodsSortsChoice = e.currentTarget.id;
// console.log(app.globalData.goodsSortsChoice);
wx.navigateTo({
url: "../goodsList/goodsList?selectedId=" + e.currentTarget.id,
})
Expand All @@ -65,35 +64,27 @@ Page({

},
onLoad: function () {
wx.request({
url: "http://localhost:8080/product/list",
success: (res) => {
this.setData({
scrollXList: res.data
})
}
API.getProductList().then(res => {
this.setData({
scrollXList: res
})
})
wx.request({
url: "http://localhost:8080/category",
success: (res) => {
this.setData({
icons: res.data
})
}
API.getCategory().then(res => {
this.setData({
icons: res
})
})
this.setData({
goodsSorts: ["https://gtms03.alicdn.com/tps/i3/TB1gXd1JXXXXXapXpXXvKyzTVXX-520-280.jpg"]
})
},
bindGetUserInfo: function(res) {
console.log("bindGetUserInfo");
var that = this
// getUserProfile
wx.getUserProfile({
desc: '展示用户信息', //不能为空
success(res){
const { nickName } = JSON.parse(res.rawData);
console.log(nickName);
that.postUsername(nickName);
that.setData({
wechat_name:res.userInfo.nickName,
Expand All @@ -114,35 +105,19 @@ Page({
 },
// 向后端发送当前用户名信息
postUsername: function(username) {
wx.request({
url: 'http://localhost:8080/user/login',
method: "POST",
data: {
"username": username
},
// 请求中带参数时,需要指定请求头中的内容,否则后端会识别失败(则默认content-type为application/json)
header: {
'content-type': 'application/x-www-form-urlencoded'
},
API.login({
"username": username
})
},
addToCart: function(productId) {
wx.request({
url: "http://localhost:8080/shopping-cart/add",
method: "POST",
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {
"productId": productId
},
success: (res) => {
wx.showToast({
title: '加购成功!', // 标题
icon: 'success',
duration: 1500 // 提示窗停留时间,默认1500ms
})
}
API.addToCart({
"productId": productId
}).then(() => {
wx.showToast({
title: '加购成功!', // 标题
icon: 'success',
duration: 1500 // 提示窗停留时间,默认1500ms
})
})
},
getUserInfo: function(e) {
Expand Down
58 changes: 23 additions & 35 deletions pages/newAddr/newAddr.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const API = require("../../api/main");
// pages/newAddr/newAddr.js
Page({

Expand All @@ -19,48 +20,35 @@ Page({
console.log("saveInfo", this.data.receiver);
const { address, receiver, phone }= this.data;

wx.request({
url: 'http://localhost:8080/user/set-info',
method: "POST",
data: {
address,
receiver,
phone
},
success: function() {
wx.showToast({
title: "地址保存成功",
icon: 'success',
duration: 2000
API.setUserInfo({
address,
receiver,
phone
}).then(res => {
wx.showToast({
title: "地址保存成功",
icon: 'success',
duration: 2000
})
setTimeout(function(){
wx.navigateBack({
url: "../chooseAddress/chooseAddress"
})
setTimeout(function(){
wx.navigateBack({
url: "../chooseAddress/chooseAddress"
})
},1000);
},
// 请求中带参数时,需要指定请求头中的内容,否则后端会识别失败(则默认content-type为application/json)
header: {
'content-type': 'application/x-www-form-urlencoded'
},
},1000);
})
},

/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
wx.request({
url: 'http://localhost:8080/user/info',
success: (res) => {
const { address, phone, receiver} = res.data;
console.log("address:" + address)
this.setData({
address,
phone,
receiver
})
}
onLoad: function () {
API.getUserInfo().then(res => {
const { address, phone, receiver} = res;
this.setData({
address,
phone,
receiver
})
})
},

Expand Down
Loading

0 comments on commit c32ecdb

Please sign in to comment.