diff --git a/image/logo.png b/image/logo.png new file mode 100644 index 0000000..1ad2038 Binary files /dev/null and b/image/logo.png differ diff --git a/pages/index/index.js b/pages/index/index.js index c1972a7..8f349ac 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -1,3 +1,4 @@ +const app = getApp() Page({ data: { progress: 89, @@ -17,6 +18,9 @@ Page({ onLoad: function (options) { }, onShow: function () { + app.$api.taskList().then(x => { + console.log(x); + }) if (wx.canIUse('hideHomeButton')) { wx.hideHomeButton() } diff --git a/pages/myself/index.js b/pages/myself/index.js index bf0dec7..a4344dd 100644 --- a/pages/myself/index.js +++ b/pages/myself/index.js @@ -1,11 +1,40 @@ +const app = getApp() Page({ - data: {}, + data: { + noLogin: true, + }, onLoad: function (options) { - + let openId = wx.getStorageSync("userId"); + if (openId) { + this.setData({ + noLogin : false + }) + } }, onShow: function () { if (wx.canIUse('hideHomeButton')) { wx.hideHomeButton() } }, + loginAcc () { + wx.login({ + success (res) { + if (res.code) { + app.$api.getOpenId(res.code).then(res => { + wx.setStorageSync("openid",res.data.openid) + this.setData({ + noLogin : false + }) + }, err => { + console.log(err); + }) + } else { + console.log('登录失败!' + res.errMsg) + } + } + }) + app.$api.loginAccount(wx.getStorageSync('openid')).then(res => { + wx.setStorageSync('userId',res.data) + }); + } }); diff --git a/pages/myself/index.wxml b/pages/myself/index.wxml index e58f505..32ce56c 100644 --- a/pages/myself/index.wxml +++ b/pages/myself/index.wxml @@ -1,10 +1,7 @@ - - - - - + + diff --git a/utils/api.js b/utils/api.js index 8a9f13c..9aa17e0 100644 --- a/utils/api.js +++ b/utils/api.js @@ -1,12 +1,30 @@ +import utils from './util' //testURL -const BASE_URL = 'http://192.168.73.1:10393/mock/68a4cea5-9a96-4c33-b7b9-0f32269ef34f/test?apipost_id=872f12'; +const BASE_URL = 'http://127.0.0.1:8080/wx'; +const LOGIN_URL = 'https://api.weixin.qq.com/sns/jscode2session'; //prodURL -// const BASE_URL = 'https://api.hongyutiyu.top'; - +// const BASE_URL = 'https://www.qnforever.top/wx'; function buildURL(url, needToken) { - let token = wx.getStorageSync('accessToken'); - - if (!needToken) { + let userId = wx.getStorageSync('userId'); + console.log(url) + if (!userId && url != '/user/login') { + wx.redirectTo({ + url: '../myself/index' + }) + wx.showLoading({ + title: '请先登录', + mask: true, + }) + setTimeout(function () { + wx.hideLoading() + }, 2000) + return; + } + if (url != '/user/login'){ + url = url + (url.indexOf('?') >= 0 ? '&' : '?') + "userId=" + userId + } + if (needToken) { + let token = utils.getToken() return token ? url + (url.indexOf('?') >= 0 ? '&' : '?') + "access_token=" + token : url } return url + (url.indexOf('?') >= 0 ? '&' : '?'); @@ -14,16 +32,13 @@ function buildURL(url, needToken) { export function fetchPost(url, params, needToken, multiple) { url = buildURL(url, needToken); - - // 如果url不存在,返回错误 TODO - // if (!url) { - // return new Promise((resolve, reject) => { - // reject(); - // }) - // } - + // 如果url不存在,返回错误 + if (!url) { + return new Promise((resolve, reject) => { + reject(); + }) + } console.log("网络请求", BASE_URL + url, params); - // 如果上传图片 if (multiple) { return new Promise((resolve, reject) => { @@ -154,12 +169,12 @@ export function fetchPost(url, params, needToken, multiple) { // get请求 export function fetchGet(url, params, needToken) { url = buildURL(url, needToken); - // 如果url不存在,返回错误 TODO - // if (!url) { - // return new Promise((resolve, reject) => { - // reject(); - // }) - // } + // 如果url不存在,返回错误 + if (!url) { + return new Promise((resolve, reject) => { + reject(); + }) + } console.log("网络请求", BASE_URL + url, params); return new Promise((resolve, reject) => { @@ -223,15 +238,37 @@ export function fetchGet(url, params, needToken) { // 暴露接口 export default { + + /** + * 获取用户唯一标识openId + */ + getOpenId(code) { + let param = { + appid:'wxb1f499f0a173865b', + secret:'833eefaf9206337d6c2d643f94baef7b', + js_code: code, + grant_type: 'authorization_code' + }; + //发起网络请求 + return wx.request({ + url: LOGIN_URL, + data: { + appid:'wxb1f499f0a173865b', + secret:'833eefaf9206337d6c2d643f94baef7b', + js_code: res.code, + grant_type: 'authorization_code' + } + }) + }, + + loginAccount(params) { + return fetchPost('/user/login',params,true,false); + }, + /** * 任务清单 */ taskList(params) { - return new Promise(x => { - let job = {data:{over:true}}; - x(job) - console.log(x + '请求成功'); - }) - // return fetchGet('', params, false); + return fetchPost('/test', params, true,false); }, } diff --git a/utils/util.js b/utils/util.js index f7427ad..902618c 100644 --- a/utils/util.js +++ b/utils/util.js @@ -21,7 +21,33 @@ const formatNumber = n => { return n[1] ? n : `0${n}` } +const getToken = () => { + const map = ['E','q','u','i','n','W','A','b','C','d'] + const errKey = ['f','g','h','j','k','l'] + let time = new Date(); + const m = '' + (time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1) + const d = '' + (time.getDate() < 10 ? '0' + time.getDate() : time.getDate()) + const h = '' + (time.getHours() < 10 ? '0' + time.getHours() : time.getHours()) + let min = '' + (time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes()) + const seconds = '' + (time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds()) + min = '20'; + let ident = Math.round(Math.random() * 7) + let beforeStr = map[m[0]] + errKey[Math.round(Math.random() * 5)] + + map[m[1]] + errKey[Math.round(Math.random() * 5)] + + map[d[0]] + errKey[Math.round(Math.random() * 5)] + + map[d[1]] + errKey[Math.round(Math.random() * 5)] + + map[h[0]] + errKey[Math.round(Math.random() * 5)] + + map[h[1]] + errKey[Math.round(Math.random() * 5)] + + map[min[0]] + errKey[Math.round(Math.random() * 5)] + + map[min[1]] + errKey[Math.round(Math.random() * 5)] + + map[seconds[0]] + errKey[Math.round(Math.random() * 5)] + + map[seconds[1]] + errKey[Math.round(Math.random() * 5)] + let finalStr = beforeStr.substring(0,ident) + ident + map[ident] + beforeStr.substring(ident) + return finalStr +} + module.exports = { formatTime, - formatDate + formatDate, + getToken }