From afae4987c6c0b1c030c1870b261186b1add1d6b2 Mon Sep 17 00:00:00 2001 From: "Stormeye.Wu" Date: Thu, 23 Aug 2018 06:30:08 +0800 Subject: [PATCH] =?UTF-8?q?[=E7=94=A8=E6=88=B7=E6=8E=88=E6=9D=83][?= =?UTF-8?q?=E6=96=B0=E5=A2=9E]:=E7=94=A8=E6=88=B7=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E6=98=B5=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 85 ++++------ app.json | 2 + common/common.js | 265 ------------------------------- config/config.js | 36 ++--- pages/coupons/details/index.js | 132 +-------------- pages/coupons/details/index.wxml | 2 +- pages/getphoneInfo/index.js | 32 ++-- pages/getuserinfo/index.js | 29 ++-- pages/index/rushToBuy/index.js | 3 +- utils/HttpBasics.js | 6 +- utils/es6-promise.min.js | 1 - 11 files changed, 83 insertions(+), 510 deletions(-) delete mode 100755 common/common.js delete mode 100755 utils/es6-promise.min.js diff --git a/app.js b/app.js index 1ae0973..eedd271 100644 --- a/app.js +++ b/app.js @@ -1,83 +1,54 @@ let config = require('./config/config.js') const Http = require("./utils/HttpBasics"); App({ - data: { - }, - onLaunch: function (options) { - // var that = this; + data: {}, + onLaunch: function(options) { + var that = this; this.globalData.sceneAddress = options.scene; this.getLocation() this.getUserInfo() // 登录 wx.login({ - success: function(res) { - var code = res.code; - console.log(code); - wx.request({ + success: ({ + code + }) => { + Http.post({ url: config.api.login, data: { appId: config.weapp.AppId, code: code, - screneAddress: options.scene - }, - method: "POST", - success: function (res) { - console.log(res); - that.globalData.token = res.data.token; - console.log(that.globalData.token) - that.globalData.openId = res.data.openId; - //由于这里是网络请求,可能会在 Page.onLoad 之后才返回 - // 所以此处加入 callback 以防止这种情况 - if (that.openIdCallback) { - console.log(res.data.openId); - that.openIdCallback(res.data.openId); - } - wx.request({ - url: config.api.checkUserStatus, - header: { - "token": that.globalData.token - }, - success: function (res) { - console.log(res); - //如果是新用户未获取unionId - if (res.data.code == config.ErrorCode.NICK_NOT_AUTH) { - wx.redirectTo({ - url: '../getuserinfo/index', - }) - } - //如果是跳转到首页 - else if (res.data.code == 200) { - // 老用户跳转首页 - } else { - wx.showToast({ - title: res.data.message, - }) - } - }, - fail: function(res) { - wx.showToast({ - title: res.data.message, + sceneAddress: this.globalData.sceneAddress, + } + }).then(res => { + // console.log("req",res); + this.globalData.token = res.data.token; + Http.setToken(res.data.token) + Http.post({ + url: config.api.checkUserStatus, + data: {} // + }).then( + function(res) { + }, + function(error) { + console.log(error) + if (error.code == 11004) { + // 用户昵称未授权 + wx.redirectTo({ + url: '../getuserinfo/index', }) } }) - } }) } }) - - // setTimeout(() => { - // this.globalData.market={ - // name:"陕西大悦城" - // } - // }, 1000); }, /** * 获取地址位置信息 */ - getLocation: function () { + getLocation: function() { wx.getLocation({ type: 'wgs84', - success: function (res) { + success: function(res) { console.log("getLocation", res); }, fail: error => { @@ -88,7 +59,7 @@ App({ /** * 获取用户信息 */ - getUserInfo: function () { + getUserInfo: function() { // 获取用户信息 wx.getSetting({ success: res => { diff --git a/app.json b/app.json index e943258..690b57c 100644 --- a/app.json +++ b/app.json @@ -1,6 +1,8 @@ { "pages":[ "pages/index/index", + "pages/getuserinfo/index", + "pages/getphoneInfo/index", "pages/market/index", "pages/user/index", "pages/coupons/details/index", diff --git a/common/common.js b/common/common.js deleted file mode 100755 index 385dd1c..0000000 --- a/common/common.js +++ /dev/null @@ -1,265 +0,0 @@ -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) - } - }) - }) - }, - /** - * 授权后获取用户的昵称,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, - 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 - }, - data: { - couponId: couponId, - token: app.globalData.token - }, - method: "POST", - success: function (res) { - resolve(res) - }, - fail: function (err) { - reject(err) - }, - complete: function (res) { - console.log(res) - } - }) - }) - }, - /** - * 支付订单 - */ - payOrderCreate: function (orderId) { - return new Promise((resolve, reject) => { - wx.request({ - url: config.api.payOrderCreate, - header: { - "token": app.globalData.token - }, - data: { - orderId: orderId, - token: app.globalData.token - }, - method: "POST", - success: function (res) { - resolve(res) - }, - fail: function (err) { - reject(err) - }, - complete: function (res) { - console.log(res) - } - }) - }) - }, - /** - * 支付订单更新 - */ - payOrderUpdate: function (orderId, payOrderId, status, reason) { - return new Promise((resolve, reject) => { - wx.request({ - url: config.api.payOrderUpdate, - header: { - "token": app.globalData.token - }, - data: { - payOrderId: payOrderId, - orderId: orderId, - status: status, - reason: reason - }, - method: "POST", - success: function (res) { - resolve(res) - }, - fail: function (err) { - reject(err) - }, - complete: function (res) { - console.log(res) - } - }) - }) - }, - refundCouponOrder: function (couponOrderId) { - return new Promise((resolve, reject) => { - wx.request({ - url: config.api.refundCouponOrder, - header: { - "token": app.globalData.token - }, - data: { - couponOrderId: couponOrderId, - token: app.globalData.token - }, - method: "POST", - success: function (res) { - resolve(res) - }, - fail: function (err) { - reject(err) - }, - complete: function (res) { - console.log(res) - } - }) - }) - }, - refundOrderCreate: function (orderId, payOrderId) { - return new Promise((resolve, reject) => { - wx.request({ - url: config.api.refundOrdercCreate, - header: { - "token": app.globalData.token - }, - data: { - payOrderId: payOrderId, - orderId: orderId, - token: app.globalData.token - }, - method: "POST", - 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 index 53acd30..1f37a20 100755 --- a/config/config.js +++ b/config/config.js @@ -1,26 +1,24 @@ -var url = 'https://ciformall.youlane.cn' -//var url = 'http://7479f9de.ngrok.io' -var apiPrefix = url + '/C'; var config = { name: "富茂", - url: "https://ciformall.youlane.cn/C", + url: "https://ciformall.youlane.cn/C/api", + //url: "http://dac6735a.ngrok.io/C/api", api: { /** * 接口用途:login */ - login: '/api/user/login', + login: '/user/login', /** * 授权后获取用户的昵称,unionId等信息 */ - getUserInfo: '/api/user/getUserInfo', + getUserInfo: '/user/getUserInfo', /** * 接口用途:授权后获取用户的手机号 */ - getUserPhone: '/api/user/getUserPhone', + getUserPhone: '/user/getUserPhone', /** * 检查用户授权状态 */ - checkUserStatus: '/api/user/checkUserStatus', + checkUserStatus: '/user/checkUserStatus', /** * 优惠券查询 */ @@ -29,30 +27,26 @@ var config = { * 优惠券详情 */ couponDetail: '/wxCoupon/findById', + /** + * 业态查询 + */ + couponChannelList: '/wxCouponChannel/list', /** * 下订单 */ - orderSave: '/api/order/save', + orderSave: '/order/save', /** * 支付订单 */ - payOrderCreate: '/api/pay/create', + payOrderCreate: '/pay/create', /** * 订单状态更新 */ - payOrderUpdate: '/api/pay/updatePayOrder', + payOrderUpdate: '/pay/updatePayOrder', /** * 订单列表 */ - orderList: '/api/order/list', - /** - * 退券 - */ - refundCouponOrder: '/api/couponOrder/refund', - /** - * 退款申请 - */ - refundOrdercCreate: '/api/refund/create', + orderList: '/order/list', }, weapp: { @@ -64,7 +58,9 @@ var config = { PHONE_IS_ENCRYPTED: 11006, }, }; +/* 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 b8965d7..6c1c83c 100644 --- a/pages/coupons/details/index.js +++ b/pages/coupons/details/index.js @@ -1,4 +1,4 @@ -let Common = require('../../../common/common.js') +var config = require('../../../config/config.js'); var app = getApp() const Http = require("../../../utils/HttpBasics") Page({ @@ -128,134 +128,4 @@ Page({ }) }, - orderfunc() { - var that = this - /* - var couponOrderId = '189631451703017472'; - var orderId = '189631451640102912'; - var payOrderId = '189631462532710400'; - var status = 1; - var reason = ''; - Common.refundCouponOrder(couponOrderId) - .then(res => { - console.log(res); - if (res.data.code == 200) { - Common.refundOrderCreate(orderId, payOrderId) - .then(res => { - console.log(res); - }) - } else { - wx.showToast({ - title: res.data.message, - duration: 3000 - }) - } - }) - */ - /* - Common.payOrderUpdate(orderId, payOrderId, status, reason) - .then(res =>{ - console.log(res); - }) - */ - - wx.showLoading({ - title: '加载中...', - }) - var couponId = '' + this.data.couponId; - console.log("order coupon: " + couponId) - Common.orderSave(couponId) - .then(res => { - console.log(res) - if (res.data.code == 200) { - var orderId = '' + res.data.data.id; - this.setData({ - orderId: '' + res.data.data.id - }) - if (res.data.data.payment > 0) { - return Common.payOrderCreate(orderId) - } else { - Common.payOrderUpdate(orderId, "0", 1) // 支付成功 - .then(res => { - wx.showToast({ - title: res.data.message, - duration: 3000 - }) - }) - } - } else { - wx.hideLoading(); - wx.showToast({ - title: res.data.message, - duration: 3000 - }) - } - }) - .then(res => { - console.log(res) - var payOrderId = '' + res.data.data.payOrderId; - if (res.data.code == 200) { - wx.requestPayment({ - timeStamp: res.data.data.timeStamp, - nonceStr: res.data.data.nonceStr, - package: res.data.data.package, - signType: 'MD5', - paySign: res.data.data.paySign, - 'success': function (res) { - Common.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功 - console.log(res); - console.log('支付成功'); - wx.showToast({ - title: '购买成功', - duration: 2500 - }) - wx.navigateBack({ - delta: 2 - }) - }, - 'fail': function (res) { - Common.payOrderUpdate(orderId, payOrderId, 2); // 支付失败 - console.log(res) - console.log('支付失败'); - return; - }, - 'complete': function (res) { - console.log(res); - console.log('支付完成'); - var url = that.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; - } - }) - } else { - wx.showModal({ - title: '错误提示', - content: res.data.message - }); - } - }) - wx.hideLoading(); - } }) \ No newline at end of file diff --git a/pages/coupons/details/index.wxml b/pages/coupons/details/index.wxml index ad113a8..4235ba3 100644 --- a/pages/coupons/details/index.wxml +++ b/pages/coupons/details/index.wxml @@ -45,6 +45,6 @@ - + \ No newline at end of file diff --git a/pages/getphoneInfo/index.js b/pages/getphoneInfo/index.js index bd3700b..507757d 100755 --- a/pages/getphoneInfo/index.js +++ b/pages/getphoneInfo/index.js @@ -1,42 +1,42 @@ var config = require('../../config/config.js'); -var Common = require('../../common/common.js'); +const Http = require("../../utils/HttpBasics"); var app = getApp(); Page({ data: { canIUse: wx.canIUse('button.open-type.getPhoneNumber') }, - onLoad: function (options) { + onLoad: function(options) { var that = this; }, - getPhoneNumber: function (e) { + getPhoneNumber: function(e) { var that = this; var iv = e.detail.iv; var encryptedData = e.detail.encryptedData; - Common.getUserPhone(encryptedData, iv) - .then(function (res) { - console.log(res); - /** - * code:0 授权手机号成功 - * code:1 授权手机号失败 - */ - if (res.data.code == 0) { + Http.post({ + url: config.api.getUserPhone, + data: { + encryptedData: encryptedData, + iv: iv, + } + }) + .then(function(res) { + console.log(res); app.globalData.phone = res.data.phone; wx.showToast({ title: res.data.msg, icon: "success", - success: function (res) { + success: function(res) { wx.redirectTo({ url: '../login/login', }) } }) - } - else if (res.data.code == 1) { + }, + function(error) { wx.showToast({ title: '请重新授权手机号', icon: "none", }) - } - }) + }) } }) \ No newline at end of file diff --git a/pages/getuserinfo/index.js b/pages/getuserinfo/index.js index b431aac..37822c9 100755 --- a/pages/getuserinfo/index.js +++ b/pages/getuserinfo/index.js @@ -1,6 +1,6 @@ var config = require('../../config/config.js'); -var Common = require('../../common/common.js'); var app = getApp(); +const Http = require("../../utils/HttpBasics"); Page({ data: { canIUse: wx.canIUse('button.open-type.getUserInfo') @@ -15,26 +15,25 @@ Page({ var encryptedData = e.detail.encryptedData; console.log(iv) console.log(encryptedData) - Common.getUserInfo(encryptedData, iv) - .then(res => { - console.log(res); - /** - * 1:用户拒绝授权 - * 0:用户同意授权 - */ - if(res.data.code == 0){ + Http.post({ + url: config.api.getUserInfo, + data: { + encryptedData: encryptedData, + iv: iv, + } + }) + .then(function(res) { + console.log(res); //返回主页 wx.redirectTo({ url: '../index/index', }) - } - else if(res.data.code == 1){ + }, function(error) { wx.showToast({ title: '请授权个人信息', - icon:"none" + icon: "none" }) } - - }) - } + ) + }, }) \ No newline at end of file diff --git a/pages/index/rushToBuy/index.js b/pages/index/rushToBuy/index.js index 03e4721..7765654 100644 --- a/pages/index/rushToBuy/index.js +++ b/pages/index/rushToBuy/index.js @@ -1,4 +1,5 @@ // pages/index/sw/index.js +let config = require('../../../config/config.js') let Http = require('../../../utils/HttpBasics') Component({ /** @@ -52,7 +53,7 @@ Component({ }, ready() { Http.get({ - url: "/wxCouponChannel/list", data: { + url: config.api.couponChannelList, data: { pageNum: 1, pageSize: 10 } diff --git a/utils/HttpBasics.js b/utils/HttpBasics.js index 33f7a7d..3162421 100644 --- a/utils/HttpBasics.js +++ b/utils/HttpBasics.js @@ -96,17 +96,17 @@ class HttpBasics { if (res.statusCode == 200) { // 服务器code 200 成功 if (res.data.code == 200) { - resolve(res.data.data); + resolve(res.data); } else { wx.hideLoading(); wx.showToast({ title: res.data.message }) - reject(res.data.message); + reject(res.data); } } else { console.error("请求出错:", res); - reject(res.errMsg); + reject(res.data); } } /** 日志 */ diff --git a/utils/es6-promise.min.js b/utils/es6-promise.min.js deleted file mode 100755 index 91bfea0..0000000 --- a/utils/es6-promise.min.js +++ /dev/null @@ -1 +0,0 @@ -!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