diff --git a/app.js b/app.js index dc32f94..7e809a7 100644 --- a/app.js +++ b/app.js @@ -1,34 +1,47 @@ let config = require('./config/config.js') - +const Http = require("./utils/HttpBasics"); App({ data: { }, onLaunch: function (options) { - var that = this; - that.globalData.sceneAddress = options.scene; + // var that = this; + this.globalData.sceneAddress = options.scene; + this.getLocation() + this.getUserInfo() // 登录 wx.login({ - success: res => { - console.log(res) - wx.request({ + success: ({ code }) => { + Http.post({ url: config.api.login, data: { appId: config.weapp.AppId, - code: res.code, - sceneAddress: that.globalData.sceneAddress, - }, - method: 'POST', - success: (result)=>{ - console.log(result) - that.globalData.token = result.data.data.token; - console.log(that.globalData.token) - that.getUserInfo() - that.getLocation() + code: code, + sceneAddress: this.globalData.sceneAddress, } + }).then(res => { + // console.log("req",res); + this.globalData.token = res.token; + Http.setToken(res.token) }) + // wx.request({ + // url: config.api.login, + // data: { + // appId: config.weapp.AppId, + // code: res.code, + // sceneAddress: that.globalData.sceneAddress, + // }, + // method: 'POST', + // success: (result) => { + // that.globalData.token = result.data.data.token; + // console.log(that.globalData.token) + // that.getUserInfo() + // that.getLocation() + // Http.setToken(result.data.data.token) + // } + // }) } }) - + // setTimeout(() => { // this.globalData.market={ // name:"陕西大悦城" @@ -38,10 +51,10 @@ App({ /** * 获取地址位置信息 */ - getLocation: function() { + getLocation: function () { wx.getLocation({ type: 'wgs84', - success: function(res) { + success: function (res) { console.log("getLocation", res); }, fail: error => { @@ -52,7 +65,7 @@ App({ /** * 获取用户信息 */ - getUserInfo: function() { + getUserInfo: function () { // 获取用户信息 wx.getSetting({ success: res => { diff --git a/config/config.js b/config/config.js index 9fc3692..8532f5f 100755 --- a/config/config.js +++ b/config/config.js @@ -1,9 +1,10 @@ -var url = 'https://ciformall.youlane.cn' -//var url = 'http://a8b668aa.ngrok.io' -//var url = 'http://localhost:8001' -var apiPrefix = url + '/C'; +// var url = 'https://ciformall.youlane.cn' +// //var url = 'http://a8b668aa.ngrok.io' +// //var url = 'http://localhost:8001' +// var apiPrefix = url + '/C'; var config = { name: "富茂", + url: "https://ciformall.youlane.cn/C", api: { /** * 接口用途:login @@ -55,9 +56,7 @@ var config = { AppId: 'wx8eb8275b78db4ede', } }; - -for (var key in config.api) { - config.api[key] = apiPrefix + config.api[key]; -} - +// for (var key in config.api) { +// config.api[key] = apiPrefix + config.api[key]; +// } module.exports = config; \ No newline at end of file diff --git a/pages/coupons/details/index.js b/pages/coupons/details/index.js index 1e23ff8..b09297d 100644 --- a/pages/coupons/details/index.js +++ b/pages/coupons/details/index.js @@ -1,22 +1,128 @@ let Common = require('../../../common/common.js') var app = getApp() +const Http = require("../../../utils/HttpBasics") Page({ data: { - list:{}, - couponId:null, - orderId:'' + list: {}, + couponId: null, + orderId: '' }, onLoad(e) { console.log(e.id) - Common.getCouponDetail(e.id) - .then(res =>{ - console.log(res) + Http.get({ + url: Http.config.api.couponDetail, + data: { + id: e.id + } + }).then(res => { this.setData({ - list: res.data.data, + list: res, couponId: e.id }) }) }, + /** + * 支付订单更新 + */ + payOrderUpdate: (orderId, payOrderId, status, reason) => { + // 支付成功 + return Http.post({ + url: Http.config.api.payOrderUpdate, data: { + payOrderId: payOrderId, + orderId: orderId, + status: status, + reason: reason + } + }) + }, + newOrderfunc() { + wx.showLoading({ + title: '加载中...', + }) + Http.post({ + url: Http.config.api.orderSave, + data: { + couponId: this.data.couponId + } + }).then(res => { + const orderId = '' + res.id; + this.setData({ + orderId: orderId + }) + if (res.payment > 0) { + return Http.post({ url: Http.config.api.payOrderCreate, data: { orderId: orderId } }) + } else { + this.payOrderUpdate(orderId, "0", 1) // 支付成功 + .then(res => { + wx.showToast({ + title: "支付状态更新成功", + duration: 3000 + }) + }) + } + }).then(res => { + console.log(res) + if (res) { + var payOrderId = '' + res.payOrderId; + wx.hideLoading(); + wx.requestPayment({ + timeStamp: res.timeStamp, + nonceStr: res.nonceStr, + package: res.package, + signType: 'MD5', + paySign: res.paySign, + 'success': (res) => { + this.payOrderUpdate(this.data.orderId, payOrderId, 1)// 支付成功 + console.log(res); + console.log('支付成功'); + wx.showToast({ + title: '购买成功', + duration: 2500 + }) + wx.navigateBack({ + delta: 2 + }) + }, + 'fail': (res) => { + this.payOrderUpdate(this.data.orderId, payOrderId, 2)// 支付失败 + console.log(res) + console.log('支付失败'); + return; + }, + 'complete': (res) => { + console.log(res); + console.log('支付完成'); + var url = this.data.url; + console.log('get url', url) + if (res.errMsg == 'requestPayment:ok') { + wx.showModal({ + title: '提示', + content: '支付成功' + }); + if (url) { + setTimeout(function () { + wx.redirectTo({ + url: '/pages' + url + }); + }, 2000) + } else { + setTimeout(() => { + wx.navigateBack() + }, 2000) + } + } else { + wx.showModal({ + title: '错误提示', + content: res.errMsg + }); + } + return; + } + }) + } + + }) + }, orderfunc() { var that = this /* @@ -49,7 +155,7 @@ Page({ */ wx.showLoading({ - title: '加载中...', + title: '加载中...', }) var couponId = '' + this.data.couponId; console.log("order coupon: " + couponId) @@ -65,12 +171,12 @@ Page({ return Common.payOrderCreate(orderId) } else { Common.payOrderUpdate(orderId, "0", 1) // 支付成功 - .then(res =>{ - wx.showToast({ - title: res.data.message, - duration: 3000 + .then(res => { + wx.showToast({ + title: res.data.message, + duration: 3000 + }) }) - }) } } else { wx.hideLoading(); @@ -90,7 +196,7 @@ Page({ package: res.data.data.package, signType: 'MD5', paySign: res.data.data.paySign, - 'success': function(res) { + 'success': function (res) { Common.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功 console.log(res); console.log('支付成功'); @@ -102,13 +208,13 @@ Page({ delta: 2 }) }, - 'fail': function(res) { + 'fail': function (res) { Common.payOrderUpdate(orderId, payOrderId, 2); // 支付失败 console.log(res) console.log('支付失败'); return; }, - 'complete': function(res) { + 'complete': function (res) { console.log(res); console.log('支付完成'); var url = that.data.url; @@ -132,7 +238,7 @@ Page({ } else { wx.showModal({ title: '错误提示', - content: res.errMsg + content: res.errMsg }); } return; diff --git a/pages/coupons/details/index.wxml b/pages/coupons/details/index.wxml index 5f1f323..4a88ddd 100644 --- a/pages/coupons/details/index.wxml +++ b/pages/coupons/details/index.wxml @@ -42,5 +42,5 @@ 每桌限用2张 - + \ No newline at end of file diff --git a/pages/index/coupons/index.js b/pages/index/coupons/index.js index 25a698c..c40fe3e 100644 --- a/pages/index/coupons/index.js +++ b/pages/index/coupons/index.js @@ -1,6 +1,5 @@ // pages/index/sw/index.js -let Common = require('../../../common/common.js') -let Http = require('../../../utils/http.js') +let Http = require('../../../utils/HttpBasics') Component({ /** * 组件的属性列表 @@ -41,46 +40,7 @@ Component({ }, ], list: [ - { - img: "http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg", - name: "海鲜焗饭", - distance: "12.5km", - address: "西单大悦城2F-012A", - oPrice: "59.00", - pPrice: "1.00", - }, - { - img: "http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg", - name: "海鲜焗饭", - distance: "12.5km", - address: "西单大悦城2F-012A", - oPrice: "59.00", - pPrice: "1.00", - }, - { - img: "http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg", - name: "海鲜焗饭", - distance: "12.5km", - address: "西单大悦城2F-012A", - oPrice: "59.00", - pPrice: "1.00", - }, - { - img: "http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg", - name: "海鲜焗饭", - distance: "12.5km", - address: "西单大悦城2F-012A", - oPrice: "59.00", - pPrice: "1.00", - }, - { - img: "http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg", - name: "海鲜焗饭", - distance: "12.5km", - address: "西单大悦城2F-012A", - oPrice: "59.00", - pPrice: "1.00", - }, + ], current: '1', current_scroll: '1', @@ -102,12 +62,16 @@ Component({ }); }, }, - ready(){ - Common.getCouponList(1, 10) - .then(res=>{ - console.log(res) + ready() { + Http.get({ + url: Http.config.api.couponList, + data: { + pageNum: 1, + pageSize: 10 + } + }).then(res => { this.setData({ - list: res.data.data.list + list: res.list }) }) } diff --git a/pages/index/index.js b/pages/index/index.js index 635993d..9464f0d 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -1,9 +1,9 @@ -//index.js +const Http = require("../../utils/HttpBasics") //获取应用实例 const app = getApp() Page({ data: { - market:app.globalData.market, + market: app.globalData.market, imgUrls: [ 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg', 'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg', @@ -17,4 +17,10 @@ Page({ swiperCurrent: e.detail.current }) }, + /** + * 生命周期函数--监听页面初次渲染完成 + */ + onReady: function () { + + }, }) diff --git a/pages/order/index/index.js b/pages/order/index/index.js index d7ed390..a2be8eb 100644 --- a/pages/order/index/index.js +++ b/pages/order/index/index.js @@ -1,4 +1,4 @@ -let Http = require('../../../utils/http.js') +let Http = require('../../../utils/HttpBasics') let app = getApp() Page({ @@ -27,15 +27,21 @@ Page({ }); }, getList(key) { - Http.getResquest('/api/order/list', app.globalData.token, '加载中', { - pageNum: 1, - pageSize: 10, - orderStatus: key - }, (res) => { + wx.showLoading({ + title: 'loading...', + }) + Http.get({ + url: '/api/order/list', + data: { + pageNum: 1, + pageSize: 10, + orderStatus: key + } + }).then((res) => { + wx.hideLoading(); this.setData({ - list: res.data.list + list: res.list }) - console.log(res) }) }, handleChange({ detail }) { diff --git a/utils/HttpBasics.js b/utils/HttpBasics.js new file mode 100644 index 0000000..33f7a7d --- /dev/null +++ b/utils/HttpBasics.js @@ -0,0 +1,116 @@ +const config = require('../config/config.js') +class HttpBasics { + constructor(address) { + if (address) { + this.address = address; + } + } + /** + * 配置 + */ + config = config; + /** + * 请求路径前缀 + */ + address = config.url + /** + * 请求头 + */ + headers = { + "Content-Type": "application/json;charset=UTF-8", + "token": "" + }; + /** + * 设置token + * @param {*} token + */ + setToken(token) { + this.headers.token = token; + } + /** + * 获取数据 + * @param {url,data,headers} param0 + */ + get({ url, data, headers }) { + headers = { ...this.headers, ...headers }; + url = `${this.address}${url}`; + // wx.showLoading({ + // title: 'loading...', + // }) + return new Promise((resolve, reject) => { + wx.request({ + url: url, + header: headers, + data: data, + method: "Get", + success: (res) => { + // wx.hideLoading(); + this.responseMap(res, resolve, reject) + }, + fail: (err) => { + // wx.hideLoading(); + reject(err) + }, + complete: (res) => { + } + }) + }) + } + /** + * 提交数据 + * @param {url,data,headers} param0 + */ + post({ url, data, headers }) { + headers = { ...this.headers, ...headers }; + url = `${this.address}${url}`; + // wx.showLoading({ + // title: 'loading...', + // }) + return new Promise((resolve, reject) => { + wx.request({ + url: url, + header: headers, + data: data, + method: "POST", + success: (res) => { + // wx.hideLoading(); + this.responseMap(res, resolve, reject) + }, + fail: (err) => { + // wx.hideLoading(); + reject(err) + }, + complete: (res) => { + } + }) + }) + } + /** + * 过滤 请求信息 + * @param {*} res + * @param {*} resolve + * @param {*} reject + */ + responseMap=(res, resolve, reject)=> { + // 网络状态码200 + if (res.statusCode == 200) { + // 服务器code 200 成功 + if (res.data.code == 200) { + resolve(res.data.data); + } else { + wx.hideLoading(); + wx.showToast({ + title: res.data.message + }) + reject(res.data.message); + } + } else { + console.error("请求出错:", res); + reject(res.errMsg); + } + } + /** 日志 */ + log(url, body, headers) { + } +} +module.exports = new HttpBasics(); \ No newline at end of file