diff --git a/app.js b/app.js index e134540..9e0f198 100644 --- a/app.js +++ b/app.js @@ -1,4 +1,5 @@ //app.js +var util = require('utils/util.js'); App({ onLaunch: function () { @@ -10,21 +11,12 @@ App({ key: 'sk', success: function(res) { var sk = res.data; - wx.request({ - url: 'https://xcx.codems.cn/api/user/vaild_sk', - data: { - "sk":sk - }, - method: 'POST', - header: {'Content-Type':'application/x-www-form-urlencoded'}, - success:function(data){ - if(data.data.status == 1){ - that.globalData.sk = sk; - }else{ - that.login(); - return; - } - + util.req('user/vaild_sk', { "sk": sk }, function (data) { + if (data.status == 1) { + that.globalData.sk = sk; + } else { + that.login(); + return; } }) }, @@ -58,23 +50,13 @@ App({ success: function (res) { wx.getUserInfo({ success: function(userinfo){ - wx.request({ - url: 'https://xcx.codems.cn/api/user/login', - data: { - "code":res.code, - "encryptedData":userinfo.encryptedData, - "iv":userinfo.iv - }, - method: 'POST', - header: { - 'Content-type':'application/json' - }, - success:function(data){ - data = data.data; - console.log(data); - that.setUserInfo(data.user); - that.setSk(data.sk); - } + util.req('user/login', { + "code": res.code, + "encryptedData": userinfo.encryptedData, + "iv": userinfo.iv + }, function (data) { + that.setUserInfo(data.user); + that.setSk(data.sk); }) }, fail: function(res) { diff --git a/utils/util.js b/utils/util.js index 0c92321..5b37b90 100644 --- a/utils/util.js +++ b/utils/util.js @@ -14,9 +14,16 @@ function formatNumber(n) { n = n.toString() return n[1] ? n : '0' + n } + + +var rootDocment = 'https://xcx.codems.cn/api/'; -var rootDocment = 'https://xcx.codems.cn/api/'; -function req(url,data,cb){ +//修改成你的appid及appsecret +var AppConf = {'appid':'wx0aa456241abc9e8e', 'appsecret':'76b24dc34ae91aa5fe89b7dc4413e981'}; + +function req(url, data, cb) { + data.appid = AppConf.appid; + data.appsecret = AppConf.appsecret; wx.request({ url: rootDocment + url, data: data, @@ -31,10 +38,12 @@ function req(url,data,cb){ }) } -function getReq(url,data,cb){ +function getReq(url,data,cb){ + data.appid = AppConf.appid; + data.appsecret = AppConf.appsecret; wx.request({ - url: rootDocment + url, - data: data, + url: rootDocment + url, + data: data, method: 'get', header: {'Content-Type':'application/x-www-form-urlencoded'}, success: function(res){