微信API

This commit is contained in:
2023-01-31 14:58:42 +08:00
parent 13cf902ce2
commit c8a16e91b6
6 changed files with 128 additions and 35 deletions

BIN
image/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

View File

@@ -1,3 +1,4 @@
const app = getApp()
Page({ Page({
data: { data: {
progress: 89, progress: 89,
@@ -17,6 +18,9 @@ Page({
onLoad: function (options) { onLoad: function (options) {
}, },
onShow: function () { onShow: function () {
app.$api.taskList().then(x => {
console.log(x);
})
if (wx.canIUse('hideHomeButton')) { if (wx.canIUse('hideHomeButton')) {
wx.hideHomeButton() wx.hideHomeButton()
} }

View File

@@ -1,11 +1,40 @@
const app = getApp()
Page({ Page({
data: {}, data: {
noLogin: true,
},
onLoad: function (options) { onLoad: function (options) {
let openId = wx.getStorageSync("userId");
if (openId) {
this.setData({
noLogin : false
})
}
}, },
onShow: function () { onShow: function () {
if (wx.canIUse('hideHomeButton')) { if (wx.canIUse('hideHomeButton')) {
wx.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)
});
}
}); });

View File

@@ -1,10 +1,7 @@
<view class="my_look"> <view class="my_look">
<t-cell-group theme="card"> <t-cell-group theme="card">
<t-cell title="浙A88FU7" description="下午好!"> <t-cell wx:if="{{noLogin}}" title="点击登录" description="" bind:tap="loginAcc"/>
<view class="avatar" slot="left-icon"> <t-cell wx:else title="登录成功" description="欢迎使用奎因记账!" image="/image/logo.png"/>
<open-data type="userAvatarUrl" />
</view>
</t-cell>
<t-cell title="预算管理" arrow url="/pages/budget/index" > <t-cell title="预算管理" arrow url="/pages/budget/index" >
<t-icon name="tools" slot="left-icon" /> <t-icon name="tools" slot="left-icon" />
</t-cell> </t-cell>

View File

@@ -1,12 +1,30 @@
import utils from './util'
//testURL //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 //prodURL
// const BASE_URL = 'https://api.hongyutiyu.top'; // const BASE_URL = 'https://www.qnforever.top/wx';
function buildURL(url, needToken) { function buildURL(url, needToken) {
let token = wx.getStorageSync('accessToken'); let userId = wx.getStorageSync('userId');
console.log(url)
if (!needToken) { 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 token ? url + (url.indexOf('?') >= 0 ? '&' : '?') + "access_token=" + token : url
} }
return url + (url.indexOf('?') >= 0 ? '&' : '?'); return url + (url.indexOf('?') >= 0 ? '&' : '?');
@@ -14,16 +32,13 @@ function buildURL(url, needToken) {
export function fetchPost(url, params, needToken, multiple) { export function fetchPost(url, params, needToken, multiple) {
url = buildURL(url, needToken); url = buildURL(url, needToken);
// 如果url不存在返回错误
// 如果url不存在返回错误 TODO if (!url) {
// if (!url) { return new Promise((resolve, reject) => {
// return new Promise((resolve, reject) => { reject();
// reject(); })
// }) }
// }
console.log("网络请求", BASE_URL + url, params); console.log("网络请求", BASE_URL + url, params);
// 如果上传图片 // 如果上传图片
if (multiple) { if (multiple) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@@ -154,12 +169,12 @@ export function fetchPost(url, params, needToken, multiple) {
// get请求 // get请求
export function fetchGet(url, params, needToken) { export function fetchGet(url, params, needToken) {
url = buildURL(url, needToken); url = buildURL(url, needToken);
// 如果url不存在返回错误 TODO // 如果url不存在返回错误
// if (!url) { if (!url) {
// return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// reject(); reject();
// }) })
// } }
console.log("网络请求", BASE_URL + url, params); console.log("网络请求", BASE_URL + url, params);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@@ -223,15 +238,37 @@ export function fetchGet(url, params, needToken) {
// 暴露接口 // 暴露接口
export default { 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) { taskList(params) {
return new Promise(x => { return fetchPost('/test', params, true,false);
let job = {data:{over:true}};
x(job)
console.log(x + '请求成功');
})
// return fetchGet('', params, false);
}, },
} }

View File

@@ -21,7 +21,33 @@ const formatNumber = n => {
return n[1] ? n : `0${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 = { module.exports = {
formatTime, formatTime,
formatDate formatDate,
getToken
} }