diff --git a/config/dev.js b/config/dev.js index 270666a..24d3f9c 100644 --- a/config/dev.js +++ b/config/dev.js @@ -1,6 +1,6 @@ module.exports = { env: { - NODE_ENV: 'development', + NODE_ENV: '"development"', }, defineConstants: {}, weapp: {}, diff --git a/config/prod.js b/config/prod.js index 86e8e4a..1886bd4 100644 --- a/config/prod.js +++ b/config/prod.js @@ -1,6 +1,6 @@ module.exports = { env: { - NODE_ENV: 'production', + NODE_ENV: '"production"', }, defineConstants: {}, weapp: {}, diff --git a/src/service/api.ts b/src/service/api.ts index b5318c0..e158ad9 100644 --- a/src/service/api.ts +++ b/src/service/api.ts @@ -1,5 +1,6 @@ import apiDef from '~/constants/api'; -import http from './http'; +// import http from './http'; +import http from './mock/http'; export const login = () => http.get({ url: apiDef.login }); diff --git a/src/service/http.ts b/src/service/http.ts index ba96f6a..177e748 100644 --- a/src/service/http.ts +++ b/src/service/http.ts @@ -1,6 +1,6 @@ import Taro from '@tarojs/taro'; -export interface FetchOption { +export interface IFetchOption { /** * 请求资源地址 */ @@ -28,16 +28,16 @@ export interface FetchOption { showErrorToast?: boolean; } -export interface ResponseData { +export interface IResponse { /** * 后台定义的状态码 */ - errorCode: number; + code: number; /** * 后台定义的 message */ - errorMessage: string; + message: string; /** * 实际返回数据 @@ -53,7 +53,7 @@ const CODE_SUCCESS = 200; Taro.addInterceptor(Taro.interceptors.logInterceptor); -async function fetch(option: FetchOption) { +async function fetch(option: IFetchOption) { const { url, data, header, method, showErrorToast = true } = option; // 这里可以根据业务需求对 header 进行改造 @@ -62,27 +62,27 @@ async function fetch(option: FetchOption) { * 根据后台实现做相应的修改, 小程序只要成功接收到服务器返回, 无论 statusCode 是多少 * 都会成功返回, 不会抛出错误, 所以需要根据后台实际返回的状态码来判断请求是否成功 */ - return Taro.request({ + return Taro.request({ url, method, header, data, }) .then(res => { - const { errorCode } = res.data; + const { code } = res.data; - if (errorCode !== CODE_SUCCESS) { + if (code !== CODE_SUCCESS) { return Promise.reject(res.data); } return res.data; }) .catch(err => { - console.error(err.error || err.errorMessage || '请求异常'); + console.error(err.error || err.message || '请求异常'); if (showErrorToast) { Taro.showToast({ - title: err.error || err.errorMessage || '请求异常', + title: err.error || err.message || '请求异常', icon: 'none', duration: 2000, }); @@ -94,10 +94,10 @@ async function fetch(option: FetchOption) { } export default { - get(option: FetchOption) { + get(option: IFetchOption) { return fetch({ ...option, method: 'GET' }); }, - post(option: FetchOption) { + post(option: IFetchOption) { return fetch({ ...option, method: 'POST' }); }, }; diff --git a/src/service/mock/data.ts b/src/service/mock/data.ts index e0d8b47..de80cf1 100644 --- a/src/service/mock/data.ts +++ b/src/service/mock/data.ts @@ -1,24 +1,24 @@ import apiDef from '~/constants/api'; -import { ResponseData } from '../http'; +import { IResponse } from '../http'; export interface ResponseFunc { - (payload: any): ResponseData; + (payload: any): IResponse; } -type MockData = Record; +type MockData = Record; const mockData: MockData = { [apiDef.login]: { - errorCode: 200, - errorMessage: 'success', + code: 200, + message: 'success', data: { name: 'dpyzo0o', age: 100, }, }, [apiDef.test]: () => ({ - errorCode: 200, - errorMessage: 'success', + code: 200, + message: 'success', data: { greeting: 'hello taro-toolkit', }, diff --git a/src/service/mock/http.ts b/src/service/mock/http.ts index fc4ed79..e5b5525 100644 --- a/src/service/mock/http.ts +++ b/src/service/mock/http.ts @@ -1,8 +1,8 @@ import apiDef from '~/constants/api'; -import data, { ResponseFunc } from './data'; -import { FetchOption, ResponseData } from '../http'; +import data from './data'; +import { IFetchOption } from '../http'; -function fetch(option: FetchOption) { +function fetch(option: IFetchOption) { const { url, data: payload } = option; const delay = Math.floor(Math.random() * 1000); @@ -13,23 +13,22 @@ function fetch(option: FetchOption) { return new Promise((resolve, reject) => { const key = Object.keys(apiDef).find(k => apiDef[k] === url) as string; + const result = data[apiDef[key]]; + setTimeout(() => { - const res = - typeof data[key] === 'function' - ? (data[key] as ResponseFunc)(payload) - : (data[key] as ResponseData); + const res = typeof result === 'function' ? result(payload) : result; console.group('%c RECEIVE', 'color: #428df5'); console.log('Url ->', url); console.log('Results -> ', res); console.groupEnd(); - if (res.errorCode === 200) { + if (res.code === 200) { resolve(res); } else { reject({ - errorCode: res.errorCode, - errorMessage: res.errorMessage, + code: res.code, + message: res.message, }); } }, delay); @@ -37,10 +36,10 @@ function fetch(option: FetchOption) { } export default { - get(option: FetchOption) { + get(option: IFetchOption) { return fetch(option); }, - post(option: FetchOption) { + post(option: IFetchOption) { return fetch(option); }, };