From f699b0699295aea4b5583a62eaa178957326a7b3 Mon Sep 17 00:00:00 2001 From: Abin Date: Mon, 5 Jan 2026 18:09:00 +0800 Subject: [PATCH] =?UTF-8?q?api=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Api/api.js | 408 ++++++++++++++++++++++++++++++++++++++++++++++++++ Api/common.js | 39 +++++ 2 files changed, 447 insertions(+) create mode 100644 Api/api.js create mode 100644 Api/common.js diff --git a/Api/api.js b/Api/api.js new file mode 100644 index 0000000..ccbdf5e --- /dev/null +++ b/Api/api.js @@ -0,0 +1,408 @@ +const baseurl="https://api.yeomai.site" + + +//获取用户的OpenID +function ApiGetUserOpenID() { + return new Promise((resolve, reject) => { + wx.login({ + success: res => { + if (res.code) { + // 发起网络请求 + wx.request({ + url: baseurl + '/API/ApiGetUserOpenID_Yelay.php', + data: { + code: res.code + }, + success: res => { + const openid = res.data; + resolve(openid); // 将获取的openid通过Promise的resolve传递 + }, + fail: () => { + reject(null); // 如果请求失败,传递null给Promise的reject + } + }); + } else { + console.log('换取用户code失败!登录失败!'); + reject(null); // 如果登录失败,传递null给Promise的reject + } + }, + fail: () => { + console.log('获取用户openid失败!登录失败'); + reject(null); // 如果登录失败,传递null给Promise的reject + } + }); + }); + } + +//使用OpenID获取数据库的用户信息,没有信息则插入新用户 +function ApiGetUserDataFromDB(openid) { + return new Promise((resolve, reject) => { + wx.request({ + url: baseurl+'/API/ApiGetOrInsertUserDataFromDB.php', + method: 'GET', // 或 'POST' + data: { + openid: openid + }, + success: res => { + resolve(res.data); // 将获取的结果通过Promise的resolve传递 + }, + fail: err => { + reject(err); // 如果请求失败,传递错误给Promise的reject + } + }); + }); +} + +//使用OpenID获取数据库的用户课程购买信息 +function ApiGetUserCourseDataFromDB(openid) { + return new Promise((resolve, reject) => { + wx.request({ + url: baseurl+'/API/ApiGetUserCourseDataFromDB.php', + method: 'GET', // 或 'POST' + data: { + openid: openid + }, + success: res => { + resolve(res.data); // 将获取的结果通过Promise的resolve传递 + }, + fail: err => { + reject(err); // 如果请求失败,传递错误给Promise的reject + } + }); + }); +} + +function ApiGetWebPagesFromDB(page) { + return new Promise((resolve, reject) => { + wx.request({ + url: baseurl+'/API/ApiGetWebPagesFromDB.php', + method: 'GET', // 或 'POST' + data: { + page: page + }, + success: res => { + resolve(res.data); // 将获取的结果通过Promise的resolve传递 + }, + fail: err => { + reject(err); // 如果请求失败,传递错误给Promise的reject + } + }); + }); + +} + + +function ApiGetWebPageContent(page) { + return new Promise((resolve, reject) => { + wx.request({ + url: baseurl+'/API/ApiGetWebPageContent.php', + method: 'GET', // 或 'POST' + data: { + page: page + }, + success: res => { + resolve(res.data); // 将获取的结果通过Promise的resolve传递 + }, + fail: err => { + reject(err); // 如果请求失败,传递错误给Promise的reject + } + }); + }); + +} + +function ApiGetCourseData(openid, courseid) { + return new Promise((resolve, reject) => { + wx.request({ + url: baseurl+'/API/ApiGetCourseData.php', + method: 'GET', // 或 'POST' + data: { + openid: openid, + courseid: courseid + }, + success: res => { + resolve(res.data); // 将获取的结果通过Promise的resolve传递 + }, + fail: err => { + reject(err); // 如果请求失败,传递错误给Promise的reject + } + }); + }); + +} + +function ApiGetFreeCourseData(index) { + return new Promise((resolve, reject) => { + wx.request({ + url: baseurl+'/API/ApiGetFreeCourseData.php', + method: 'GET', // 或 'POST' + data: { + index: index + }, + success: res => { + resolve(res.data); // 将获取的结果通过Promise的resolve传递 + }, + fail: err => { + reject(err); // 如果请求失败,传递错误给Promise的reject + } + }); + }); + +} + +function ApiLogout(openid) { + return new Promise((resolve, reject) => { + wx.request({ + url: baseurl+'/API/ApiLogout.php', + method: 'GET', // 或 'POST' + data: { + openid: openid + }, + success: res => { + resolve(res.data); // 将获取的结果通过Promise的resolve传递 + }, + fail: err => { + reject(err); // 如果请求失败,传递错误给Promise的reject + } + }); + }); + +} + + +function ApiPasswordCodeValidate(openid, passwordcode) { + return new Promise((resolve, reject) => { + wx.request({ + url: baseurl+'/API/ApiPasswordCodeValidate.php', + method: 'GET', // 或 'POST' + data: { + openid: openid, + PasswordCode:passwordcode + }, + success: res => { + resolve(res.data); // 将获取的结果通过Promise的resolve传递 + }, + fail: err => { + reject(err); // 如果请求失败,传递错误给Promise的reject + } + }); + }); + +} + +function ApiGetIndexCourseList() { + return new Promise((resolve, reject) => { + wx.request({ + url: baseurl+'/API/ApiGetIndexCourseList.php', + method: 'GET', // 或 'POST' + data: { + }, + success: res => { + resolve(res.data); // 将获取的结果通过Promise的resolve传递 + }, + fail: err => { + reject(err); // 如果请求失败,传递错误给Promise的reject + } + }); + }); + +} + +function ApiGetIndexFreeCourseList() { + return new Promise((resolve, reject) => { + wx.request({ + url: baseurl+'/API/ApiGetIndexFreeCourseList.php', + method: 'GET', // 或 'POST' + data: { + }, + success: res => { + resolve(res.data); // 将获取的结果通过Promise的resolve传递 + }, + fail: err => { + reject(err); // 如果请求失败,传递错误给Promise的reject + } + }); + }); + +} + +function ApiGetBannerImg() { + return new Promise((resolve, reject) => { + wx.request({ + url: baseurl+'/API/ApiGetBannerImg.php', + method: 'GET', // 或 'POST' + data: { + }, + success: res => { + resolve(res.data); // 将获取的结果通过Promise的resolve传递 + }, + fail: err => { + reject(err); // 如果请求失败,传递错误给Promise的reject + } + }); + }); + +} + +function ApiAddPasswordCode(openid,courseID,courseName,coursePswdCode,courseEndDate,courseInfo,courseCount) { + return new Promise((resolve, reject) => { + wx.request({ + url: baseurl+'/API/ApiAddPasswordCode.php', + method: 'GET', // 或 'POST' + data: { + openid: openid, + courseID: courseID, + courseName: courseName, + coursePswdCode: coursePswdCode, + courseEndDate: courseEndDate, + courseInfo: courseInfo, + courseCount: courseCount, + }, + header: { + 'content-type': 'application/json' // 设置请求头为JSON格式 + }, + success: res => { + resolve(res.data); // 将获取的结果通过Promise的resolve传递 + }, + fail: err => { + reject(err); // 如果请求失败,传递错误给Promise的reject + } + }); + }); + +} + +function ApiAddPasswordCodeBefor(openid) { + return new Promise((resolve, reject) => { + wx.request({ + url: baseurl+'/API/ApiAddPasswordCodeBefor.php', + method: 'GET', // 或 'POST' + data: { + openid + }, + success: res => { + resolve(res.data); // 将获取的结果通过Promise的resolve传递 + }, + fail: err => { + reject(err); // 如果请求失败,传递错误给Promise的reject + } + }); + }); + +} +function ApiGetPasswordCode(openid) { + return new Promise((resolve, reject) => { + wx.request({ + url: baseurl+'/API/ApiGetPasswordCode.php', + method: 'GET', // 或 'POST' + data: { + openid: openid, + }, + success: res => { + resolve(res.data); // 将获取的结果通过Promise的resolve传递 + }, + fail: err => { + reject(err); // 如果请求失败,传递错误给Promise的reject + } + }); + }); + +} + +function ApiChangePasswordCode(openid,coursePswdCode,courseEndDate,courseInfo,courseCount,oldCoursePswdCode) { + return new Promise((resolve, reject) => { + wx.request({ + url: baseurl+'/API/ApiChangePasswordCode.php', + method: 'GET', // 或 'POST' + data: { + openid: openid, + coursePswdCode: coursePswdCode, + courseEndDate: courseEndDate, + courseInfo: courseInfo, + courseCount: courseCount, + oldCoursePswdCode: oldCoursePswdCode, + }, + success: res => { + resolve(res.data); // 将获取的结果通过Promise的resolve传递 + }, + fail: err => { + reject(err); // 如果请求失败,传递错误给Promise的reject + } + }); + }); + +} + +function ApiDeletePasswordCode(openid,coursePswdCode) { + return new Promise((resolve, reject) => { + wx.request({ + url: baseurl+'/API/ApiDeletePasswordCode.php', + method: 'GET', // 或 'POST' + data: { + openid: openid, + coursePswdCode: coursePswdCode, + }, + success: res => { + resolve(res.data); // 将获取的结果通过Promise的resolve传递 + }, + fail: err => { + reject(err); // 如果请求失败,传递错误给Promise的reject + } + }); + }); + +} + + + module.exports = { + ApiGetUserOpenID, + ApiGetUserDataFromDB, + ApiGetWebPagesFromDB, + ApiGetWebPageContent, + ApiGetUserCourseDataFromDB, + ApiLogout, + ApiGetCourseData, + ApiGetFreeCourseData, + ApiPasswordCodeValidate, + ApiGetIndexCourseList, + ApiGetIndexFreeCourseList, + ApiGetBannerImg, + ApiAddPasswordCode, + ApiAddPasswordCodeBefor, + ApiGetPasswordCode, + ApiChangePasswordCode, + ApiDeletePasswordCode + }; + + +/* +其他页面调用方法是: +首先引入 +const MyApi = require('../../Api/api.js'); + +然后调用,返回值就是result +MyApi.ApiGetUserOpenID() + .then(result => { + // 成功后执行的代码 + console.log(result); + }) + .catch(err => { + // 失败后执行的代码 + console.error("请求失败:"+err); + }); + + + +数据传递方式: +GET请求: 数据通过URL参数传递,可以在URL中看到。 +POST请求: 数据通过请求体(body)传递,不在URL中可见。 + +数据大小限制: +GET请求: 有长度限制,通常在几千个字符之间,因为URL有长度限制。 +POST请求: 没有固定的长度限制,可以传递大量的数据。 + +安全性: +GET请求: 因为数据在URL中可见,不适合传递敏感信息,且容易被拦截。 +POST请求: 数据在请求体中,相对更安全,适用于敏感信息传递。 + +*/ \ No newline at end of file diff --git a/Api/common.js b/Api/common.js new file mode 100644 index 0000000..6cdf5de --- /dev/null +++ b/Api/common.js @@ -0,0 +1,39 @@ +//本文件用于放置常用函数 + +function GetNowData() { + // 获取当前时间 + var currentTime = new Date(); + // 分别获取年、月、日、时、分、秒 + var year = currentTime.getFullYear(); + var month = padZero(currentTime.getMonth() + 1); // 月份是从0开始的,所以要加1 + var day = padZero(currentTime.getDate()); + var hours = padZero(currentTime.getHours()); + var minutes = padZero(currentTime.getMinutes()); + var seconds = padZero(currentTime.getSeconds()); + // 打印输出当前时间 + + return ("当前时间:" + year + "-" + month + "-" + day + "-" + hours + "-" + minutes + "-" + seconds); +} +// 补零函数,不用导出 +function padZero(num) { + return num < 10 ? '0' + num : num; +} + +function multiply(x, y) { + return x * y; +} + +module.exports = { + GetNowData, + multiply +}; + + +/* +其他页面调用方法是: +首先引入 +const common = require('../../api/common.js'); + +然后调用,返回值就是result +common.GetNowData(); + */ \ No newline at end of file