企获客基础班本接口方法等描述

This commit is contained in:
唐明明
2020-11-30 15:23:15 +08:00
parent 0dad32b021
commit f559da991e
20 changed files with 613 additions and 2 deletions

142
README.md
View File

@@ -1,3 +1,141 @@
# AGuestSaas
企获客Saas # 企获客(SAAS)
## 1. 项目目录
***apis***
: 统一的请求工厂处理
|--- interfaces //接口工厂
| |--- auth.js //授权接口模块
| |--- file.js //上传接口
|--- index.js //统一接口路由出口
|--- request.js //请求方法
|--- updateToken.hs //token更新
|--- err.js //错误处理
***static***
: 静态资源目录
|--- imgs //图片
|--- icon //图标
***components***
: 项目组件目录
***pages***
: 页面路由
## 2. 使用apis接口方法
***统一用法*** : 在app.js 中引入api方法使用wx.$方法挂载全局
//app.js
//全局引用apis
import apis from "./apis/index"
onLaunch(){
//挂载api方法
wx.$api = apis
}
//pages.js
function () {
wx.$api...().then(res=>{
//成功回调
}).cathc(err=>{
//失败回调
})
}
***按需引用*** : 按需要引用
//pages.js
import apis from "./apis/index"
function () {
apis...().then(res=>{
//成功回调
}).cathc(err=>{
//失败回调
})
}
***多个接口同时回调*** : 面需多个接口同时返回信息时可使用ES6 Promise All回调方法
```
let api1 = wx.$api.{moduleName}.{apiName}(obj)
api2 = wx.$api.{moduleName}.{apiName}(obj)
api3 = wx.$api.{moduleName}.{apiName}(obj)
Promise.all([api1, api2, api3]).then(res=>{
//请求回调成功
}).cathc(err=>{
//请求回调失败
})
```
## 3. 请求方法描述
> api全局请求方法参数说明
| 请求参数 | 是否必填 | 数据类型 | 数据说明 |
|:-------:|:-------:|:-------:|:---------:|
| method | 否 | String | 请求方式默认为GET |
| url | 是 | String | 接口url地址 |
| data | 否 | Obj | 需要发送的请求数据 |
> 请求方法模块用法
```
import {req} from "../request"
const apiName = (obj) => req({
//请求参数
})
export default({
apiName
})
```
## 4. 上传方法描述
> api全局请求方法参数说明
| 请求参数 | 是否必填 | 数据类型 | 数据说明 |
|:-------:|:-------:|:-------:|:---------:|
| url | 是 | String | 上传接口url地址 |
| key | 是 | String | 文件对应的 key |
| path | 是 | String | 上传文件的临时路径可通过wx.chooseImage获得图片临时路径 |
| data | 否 | Obj | 需要发送的请求数据 |
> 方法模块用法
```
import {upload} from "../request"
const apiName = (fileUrl, keyName, data) => upload({
url : "...", //api上传接口
key : keyName, //上传key
path: fileUrl, //上传路径
data: data //fromData
})
export default({
apiName
})
```
> 接口用法
```
wx.$api.file.uploadImg(fileUrl, keyName, {
//data
}).then(res=>{
//上传成功
}).cathc(err=>{
//上传失败
})
```

62
apis/err.js Normal file
View File

@@ -0,0 +1,62 @@
/**
* 处理错误信息
* @property {Object} errInfo
*/
const errInfo = (obj) =>{
if(obj.status_code == 401){
wx.showModal({
title : "登录提示",
content : "长时间未操作,登录已过期,请重新登录",
confirmColor: "#2d6bf6",
confirmText : "立即登录",
cancelText : "返回首页",
cancelColor : "#aeafb1",
success : res=>{
// 清理客户端登录缓存
wx.removeStorageSync("token")
if(res.confirm){
wx.navigateTo({
url: "/pages/login/login",
})
//转跳到登录页面
}else if(res.cancel){
//考虑是否增加一个跳转首页的操作
wx.redirectTo({
url: "/pages/index/index",
})
}
}
})
}else if(obj.status_code == 422){
wx.showToast({
title: obj.message,
icon : "none"
})
}else if(obj.status_code == 400 || obj.status_code == 0){
wx.showToast({
title: obj.message,
icon : "none"
})
}else if(obj.status_code == 404){
wx.showToast({
title: "接口地址不存在,请联系系统管理员",
icon : "none"
})
}else if(obj.status_code == 500){
wx.showToast({
title: "服务端:" + obj.message,
icon : "none"
})
}else {
wx.showToast({
title: "code:" + obj.status_code + ", msg:" + obj.message,
icon : "none"
})
}
}
module.exports = {
errInfo
}

13
apis/index.js Normal file
View File

@@ -0,0 +1,13 @@
/*
* 接口路由
*/
import file from "./interfaces/file"
import auth from "./interfaces/auth"
export default{
file,
auth
}

13
apis/interfaces/auth.js Normal file
View File

@@ -0,0 +1,13 @@
/*
* 授权
*/
import {req} from "../request"
const authPhone = data => req({url: "auth/mini", method: "POST", data: data}) //登录
const authInfo = data => req({url: "auth/mini/info", method: "POST", data: data}) //完善用户信息
export default({
authPhone,
authInfo
})

12
apis/interfaces/file.js Normal file
View File

@@ -0,0 +1,12 @@
/*
* 公用接口
*/
import {upload} from "../request"
const uploadImg = (imgPaht, data) => upload({url: "storage", key: "image", path: imgPaht, data: data}) //上传图片
export default({
uploadImg
})

111
apis/request.js Normal file
View File

@@ -0,0 +1,111 @@
import {errInfo} from './err'
import {updToken} from './updateToken'
// 请求方式配置
const api = "https://mi-org.cnskl.com/api/"
const header = {
"Accept": "application/json"
}
/**
* 请求
* @property {Object} req
*/
const req = (obj) => {
// header
header.Authorization = wx.getStorageSync("token") || ""
// 处理请求信息
return new Promise((resolve, reject) => {
wx.showLoading({
title: "加载中..",
mask: true
})
wx.request({
url : api + obj.url,
header : header,
method : obj.method || 'GET',
data : obj.data || {},
success : res => {
wx.hideLoading();
// 更新token
if (res.header.Authorization) updToken(res.header.Authorization)
// 处理信息
if (res.data.status_code == 200) {
resolve(res.data.data)
} else {
if (res.data.status_code == 401 || res.data.status_code == 400) {
reject({
login : false,
codeBeen: false
})
}
errInfo(res.data)
}
},
fail: err => {
wx.showToast({
title : err.errMsg,
icon : "none"
})
reject(err)
}
})
})
}
/**
* 上传
* @property {Object} upload
*/
const upload = (obj) => {
// header
header.Authorization = wx.getStorageSync("token") || ""
// 处理上传信息
return new Promise((resolve, reject) => {
wx.showLoading({
title: "上传中..",
mask: true
})
wx.uploadFile({
url : api + obj.url,
header : header,
name : obj.key || "",
filePath: obj.path || "",
formData: obj.data || {},
success : res=>{
wx.hideLoading();
// 处理返回值
let jsonData = JSON.parse(res.data)
// 更新token
if (res.header.Authorization) updToken(res.header.Authorization)
// 处理信息
if (jsonData.status_code == 200) {
resolve(jsonData.data)
} else {
if (jsonData.status_code == 401) {
reject({
login: false
})
}
errInfo(jsonData)
}
},
fail : err=>{
wx.showToast({
title : err.errMsg,
icon : "none"
})
reject(err)
}
})
})
}
module.exports = {
req,
upload
}

17
apis/updateToken.js Normal file
View File

@@ -0,0 +1,17 @@
/**
* 更新token
* @property {String} updToken
*/
const updToken = (token) =>{
// 更新全局存储器
getApp().globalData.token = token
// 更新客户端登录缓存
wx.setStorageSync('token', token)
}
module.exports = {
updToken
}

0
app.js Normal file
View File

13
app.json Normal file
View File

@@ -0,0 +1,13 @@
{
"pages": [
"pages/shortVideo/shortVideo"
],
"window": {
"navigationBarBackgroundColor": "#ffffff",
"navigationBarTextStyle": "black",
"navigationBarTitleText": "小程序",
"backgroundColor": "#eeeeee",
"backgroundTextStyle": "light",
"enablePullDownRefresh": false
}
}

2
app.wxss Normal file
View File

@@ -0,0 +1,2 @@

View File

@@ -0,0 +1,43 @@
/**
* Web唐明明
* 一个梦想做木雕手艺人的程序员
* explain: userInfoLayer
*/
Component({
/**
* 组件的属性列表
*/
properties: {
showLayer: {
type : Boolean,
value : false
}
},
/**
* 组件的方法列表
*/
methods: {
userInfo(info){
if(info.detail.errMsg == "getUserInfo:ok"){
wx.$api.auth.authInfo({
nickname: info.detail.userInfo.nickName,
avatar : info.detail.userInfo.avatarUrl
}).then(()=>{
this.triggerEvent("updateinfo", true)
this.setData({
showLayer: false
})
})
}else{
wx.showToast({
title: '拒绝了授权',
icon : 'none'
})
this.triggerEvent("updateinfo", false)
}
}
}
})

View File

@@ -0,0 +1,4 @@
{
"component": true,
"usingComponents": {}
}

View File

@@ -0,0 +1,14 @@
<block wx:if="{{showLayer}}">
<view class="layer-back active"></view>
<view class="layer-content">
<view class="layer-content-block">
<image class="layer-content-img" src="./userInfoLayer_img.png" mode="widthFix"></image>
<view class="layer-content-mian">
<view class="layer-content-title">完善信息</view>
<view class="layer-content-text">获取您的公开信息(头像,昵称等),完善平台信息</view>
<button class="layer-content-btn" size="default" open-type="getUserInfo" bindgetuserinfo="userInfo">授权微信</button>
</view>
</view>
</view>
</block>

View File

@@ -0,0 +1,70 @@
/**
* Web唐明明
* 一个梦想做木雕手艺人的程序员
*/
.layer-back,
.layer-content{
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.layer-back{
z-index: 999;
background-color: rgba(0, 0, 0, .3);
}
.layer-content{
z-index: 1000;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-box-pack: center;
text-align: center;
padding-bottom: 100rpx;
box-sizing: border-box;
}
.layer-content-block{
background-color: white;
width: 500rpx;
display: inline-block;
border-radius: 8rpx;
box-sizing: border-box;
overflow: hidden;
}
.layer-content-img{
width: 100%;
vertical-align: top;
}
.layer-content-mian{
padding: 40rpx;
}
.layer-content-title{
font-weight: bold;
font-size: 38rpx;
line-height: 90rpx;
}
.layer-content-btn[size="default"]{
background-color: #0c0047;
color: white;
width: 100%;
height: 90rpx;
line-height: 90rpx;
border-radius: 0;
font-size: 34rpx;
padding: 0;
}
.layer-content-text{
padding-bottom: 40rpx;
font-size: 30rpx;
color: #afafaf;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

View File

@@ -0,0 +1,21 @@
import apis from "../../apis/index"
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
apis.auth.authInfo().then(res=>{
console.log(res)
})
}
})

View File

@@ -0,0 +1,3 @@
{
"usingComponents": {}
}

View File

@@ -0,0 +1,2 @@
<!--pages/shortVideo/shortVideo.wxml-->
<text>pages/shortVideo/shortVideo.wxml</text>

View File

@@ -0,0 +1 @@
/* pages/shortVideo/shortVideo.wxss */

72
project.config.json Normal file
View File

@@ -0,0 +1,72 @@
{
"description": "项目配置文件",
"packOptions": {
"ignore": []
},
"setting": {
"urlCheck": true,
"es6": true,
"enhance": false,
"postcss": true,
"preloadBackgroundData": false,
"minified": true,
"newFeature": false,
"coverView": true,
"nodeModules": false,
"autoAudits": false,
"showShadowRootInWxmlPanel": true,
"scopeDataCheck": false,
"uglifyFileName": false,
"checkInvalidKey": true,
"checkSiteMap": true,
"uploadWithSourceMap": true,
"compileHotReLoad": false,
"useMultiFrameRuntime": false,
"useApiHook": true,
"babelSetting": {
"ignore": [],
"disablePlugins": [],
"outputPath": ""
},
"enableEngineNative": false,
"bundle": false,
"useIsolateContext": true,
"useCompilerModule": true,
"userConfirmedUseCompilerModuleSwitch": false,
"userConfirmedBundleSwitch": false,
"packNpmManually": false,
"packNpmRelationList": [],
"minifyWXSS": true
},
"compileType": "miniprogram",
"libVersion": "2.14.1",
"appid": "wxd931d03dfe955254",
"projectname": "miniprogram-1",
"debugOptions": {
"hidedInDevtools": []
},
"scripts": {},
"isGameTourist": false,
"simulatorType": "wechat",
"simulatorPluginLibVersion": {},
"condition": {
"search": {
"list": []
},
"conversation": {
"list": []
},
"game": {
"list": []
},
"plugin": {
"list": []
},
"gamePlugin": {
"list": []
},
"miniprogram": {
"list": []
}
}
}