微信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

View File

@@ -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);
},
}