diff --git a/app.js b/app.js index f3df361..fd00f4a 100644 --- a/app.js +++ b/app.js @@ -1,34 +1,29 @@ -//app.js +let config = require('./config/config.js') App({ data: { - appId: '' }, - onLaunch: function() { + onLaunch: function (options) { + var that = this; + that.globalData.sceneAddress = options.scene; // 登录 wx.login({ success: res => { + console.log(res) wx.request({ - url: 'https://ciformall.youlane.cn/C/api/user/login', - header: { - "content-type": "application/json;charset=UTF-8" - }, + url: config.api.login, data: { - appId: "wx649b3be73c1afe47", - code: res.code + appId: config.weapp.AppId, + code: res.code, + sceneAddress: that.globalData.sceneAddress, }, method: 'POST', success: (result)=>{ console.log(result) - this.globalData.openId = result.data.data.openId + that.globalData.token = result.data.data.token; + console.log(that.globalData.token) } }) - // Http.postResquest('/api/user/login', '登录中', { - // appId: "wx649b3be73c1afe47", - // code: result.code - // }, (res) => { - // console.log(res) - // }) } }) // 获取用户信息 @@ -73,16 +68,15 @@ App({ }) }, globalData: { - // 当前用户信息 - userInfo: null, - openId:null, + // token + token: null, + // 渠道 + sceneAddress: null, + // 二维码参数 + scene: null, // 当前商场信息 market: { name: "陕西大悦城" } - }, - getAppid() { - let appId = "wx649b3be73c1afe47" - return appId } }) \ No newline at end of file diff --git a/common/common.js b/common/common.js new file mode 100755 index 0000000..7272fe7 --- /dev/null +++ b/common/common.js @@ -0,0 +1,180 @@ +const Promise = require('../utils/es6-promise.min').Promise +let config = require("../config/config.js"); +var app = getApp(); +const func = { + wxLogin: function (scene) { + return new Promise((resolve, reject) => { + wx.login({ + success: function (res) { + resolve(res) + }, + fail: function (err) { + reject(err) + }, + complete: function (res) { + //console.log("wxLogin complete:" + res) + } + }) + }) + }, + userLogin: function (code, source) { + return new Promise((resolve, reject) => { + wx.request({ + url: config.api.login, + data: { + appId: app.globalData.appId, + code: code, + scene: scene, + sceneAddress: sceneAddress + }, + method: "POST", + success: function (res) { + resolve(res) + }, + fail: function (err) { + reject(err) + }, + complete: function (res) { + //console.log("userLogin complete:" + res) + } + }) + }) + }, + /** + * 授权后获取用户的昵称,unionId等信息 + */ + getUserInfo: function (encryptedData, iv) { + console.log(app.globalData.openId) + return new Promise((resolve, reject) => { + wx.request({ + url: config.api.getUserInfo, + header: { + "content-type": "application/json;charset=UTF-8", + "token": app.globalData.token + }, + data: { + encryptedData: encryptedData, + iv: iv, + }, + method: "POST", + success: function (res) { + resolve(res) + }, + fail: function (err) { + reject(err) + }, + complete: function (res) { + //console.log("getUserInfo complete:" + res) + } + }) + }) + }, + /** + * 授权后获取用户的手机号 + */ + getUserPhone: function (encryptedData, iv) { + return new Promise((resolve, reject) => { + wx.request({ + url: config.api.getUserPhone + "?token=" + app.globalData.token, + data: { + encryptedData: encryptedData, + iv: iv, + }, + header: { + "content-type": "application/json;charset=UTF-8", + "token": app.globalData.token + }, + method: "POST", + success: function (res) { + resolve(res) + }, + fail: function (err) { + reject(err) + }, + complete: function (res) { + //console.log("getUserInfo complete:" + res) + } + }) + }) + }, + /** + * 优惠券查询 + */ + getCouponList: function (pageNum, pageSize) { + return new Promise((resolve, reject) => { + wx.request({ + url: config.api.couponList, + header: { + "content-type": "application/json;charset=UTF-8", + "token": app.globalData.token + }, + method: "Get", + data: { + pageNum: pageNum, + pageSize: pageSize + }, + success: function (res) { + resolve(res) + }, + fail: function (err) { + reject(err) + }, + complete: function (res) { + console.log(res) + } + }) + }) + }, + /** + * 优惠券详情 + */ + getCouponDetail: function (couponId) { + return new Promise((resolve, reject) => { + wx.request({ + url: config.api.couponDetail, + header: { + "content-type": "application/json;charset=UTF-8", + "token": app.globalData.token + }, + method: "Get", + data: { + id: couponId + }, + success: function (res) { + resolve(res) + }, + fail: function (err) { + reject(err) + }, + complete: function (res) { + console.log(res) + } + }) + }) + }, + /** + * 订单下单 + */ + orderSave: function (couponId) { + return new Promise((resolve, reject) => { + wx.request({ + url: config.api.orderSave, + header: { + "token": app.globalData.token + }, + method: "Post", + data: JSON.stringify({"couponId": couponId}), + success: function (res) { + resolve(res) + }, + fail: function (err) { + reject(err) + }, + complete: function (res) { + console.log(res) + } + }) + }) + }, +} +module.exports = func; \ No newline at end of file diff --git a/config/config.js b/config/config.js new file mode 100755 index 0000000..c96a165 --- /dev/null +++ b/config/config.js @@ -0,0 +1,42 @@ +var url = 'https://ciformall.youlane.cn' +//var url = 'http://localhost:8001' +var apiPrefix = url + '/C/'; +var config = { + name: "富茂", + api: { + /** + * 接口用途:login + */ + login: '/api/user/login', + /** + * 授权后获取用户的昵称,unionId等信息 + */ + getUserInfo: '/api/user/getUserInfo', + /** + * 接口用途:授权后获取用户的手机号 + */ + getUserPhone: '/api/user/getUserPhone', + /** + * 优惠券查询 + */ + couponList: '/wxCoupon/list', + /** + * 优惠券详情 + */ + couponDetail: '/wxCoupon/findById', + /** + * 下单 + */ + orderSave: '/wxOrder/save', + }, + + weapp: { + AppId: 'wx649b3be73c1afe47', + } +}; + +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 5b1ef6a..cb36cc9 100644 --- a/pages/coupons/details/index.js +++ b/pages/coupons/details/index.js @@ -1,35 +1,39 @@ -let Http = require('../../../utils/http.js') +let Common = require('../../../common/common.js') var app = getApp() Page({ data: { list:{}, - cuponId:null + couponId:null }, onLoad(e) { console.log(e.id) - Http.getResquest('/wxCoupon/findById','加载中',{ - id: e.id - },(res)=>{ + Common.getCouponDetail(e.id) + .then(res =>{ console.log(res) this.setData({ - list: res.data, - cuponId:e.id + list: res.data.data, + couponId: e.id }) }) }, aaa(){ - Http.postResquest('/wxOrder/save', '加载中', { - couponId: this.data.cuponId, - openId: app.globalData.openId - }, (res) => { + wx.showLoading({ + title: '加载中...', + }) + Common.orderSave(''+this.data.couponId) + .then(res => { + wx.hideLoading(); console.log(res) - if (res.code == 200) { + if (res.data.code == 200) { wx.showToast({ title: '购买成功', }) wx.navigateBack({ delta: 2 }) + setTimeout(() => { + wx.hideToast(); + }, 2000) } }) } diff --git a/pages/index/coupons/index.js b/pages/index/coupons/index.js index 10a431c..25a698c 100644 --- a/pages/index/coupons/index.js +++ b/pages/index/coupons/index.js @@ -1,5 +1,5 @@ // pages/index/sw/index.js - +let Common = require('../../../common/common.js') let Http = require('../../../utils/http.js') Component({ /** @@ -103,13 +103,11 @@ Component({ }, }, ready(){ - Http.getResquest("/wxCoupon/list",'加载中',{ - "pageNum":1, - "pageSize":10 - },(res)=>{ + Common.getCouponList(1, 10) + .then(res=>{ console.log(res) this.setData({ - list:res.data.list + list: res.data.data.list }) }) } diff --git a/pages/login/index.js b/pages/login/index.js index 7e091c6..945aa13 100644 --- a/pages/login/index.js +++ b/pages/login/index.js @@ -3,7 +3,6 @@ let app = getApp() Page({ data: { - appId: app.getAppid() }, onLoad() { diff --git a/utils/es6-promise.min.js b/utils/es6-promise.min.js new file mode 100755 index 0000000..91bfea0 --- /dev/null +++ b/utils/es6-promise.min.js @@ -0,0 +1 @@ +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.ES6Promise=e()}(this,function(){"use strict";function t(t){var e=typeof t;return null!==t&&("object"===e||"function"===e)}function e(t){return"function"==typeof t}function n(t){G=t}function r(t){H=t}function o(){return function(){return process.nextTick(a)}}function i(){return"undefined"!=typeof B?function(){B(a)}:c()}function s(){var t=0,e=new Q(a),n=document.createTextNode("");return e.observe(n,{characterData:!0}),function(){n.data=t=++t%2}}function u(){var t=new MessageChannel;return t.port1.onmessage=a,function(){return t.port2.postMessage(0)}}function c(){var t=setTimeout;return function(){return t(a,1)}}function a(){for(var t=0;t { +const postResquest = (url, token, message, postData, doSuccess, doFail) => { let body={...postData}; - body["openid"] = openId; if (message != "") { wx.showLoading({ title: message, @@ -10,9 +7,10 @@ const postResquest = (url, message, postData, doSuccess, doFail) => { } wx.request({ //项目的真正接口,通过字符串拼接方式实现 - url: host + url, + url: url, header: { - "content-type": "application/json;charset=UTF-8" + "content-type": "application/json;charset=UTF-8", + "token": token }, data: body, method: 'POST', @@ -33,18 +31,18 @@ const postResquest = (url, message, postData, doSuccess, doFail) => { }, }) } -const getResquest = (url, message, getData, doSuccess, doFail) => { +const getResquest = (url, token, message, getData, doSuccess, doFail) => { let body = { ...getData }; - body["openid"] = openId; if (message != "") { wx.showLoading({ title: message, }) } wx.request({ - url: host + url, + url: url, header: { - "content-type": "application/json;charset=UTF-8" + "content-type": "application/json;charset=UTF-8", + "token": token }, data: body, method: 'GET',