├── .editorconfig ├── .eslintrc ├── .gitignore ├── README.md ├── config ├── dev.js ├── index.js └── prod.js ├── package.json ├── project.config.json ├── src ├── app.js ├── app.scss ├── assets │ └── img │ │ └── xcx.jpg ├── index.html ├── pages │ ├── index │ │ ├── index.js │ │ └── index.scss │ └── page2 │ │ ├── page2.js │ │ └── page2.scss ├── redux │ ├── actions │ │ └── index.js │ ├── reducers │ │ ├── common │ │ │ ├── common.js │ │ │ └── initState.js │ │ └── index.js │ └── store.js ├── servers │ ├── baseUrl.js │ ├── config.js │ ├── http.js │ ├── interceptors.js │ └── servers.js └── utils │ └── common.js ├── yarn-error.log └── yarn.lock /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["taro"], 3 | "rules": { 4 | "no-unused-vars": ["error", { "varsIgnorePattern": "Taro" }], 5 | "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx", ".tsx"] }] 6 | }, 7 | "parser": "babel-eslint" 8 | } 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | .temp/ 3 | .rn_temp/ 4 | node_modules/ 5 | .DS_Store 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 前言 2 | 3 | [Taro](https://taro.aotu.io/) 是一套遵循 [React](https://reactjs.org/) 语法规范的 **多端开发** 解决方案。现如今市面上端的形态多种多样,Web、React-Native、微信小程序等各种端大行其道,当业务要求同时在不同的端都要求有所表现的时候,针对不同的端去编写多套代码的成本显然非常高,这时候只编写一套代码就能够适配到多端的能力就显得极为需要。 4 | 5 | 使用 [Taro](https://taro.aotu.io/),我们可以只书写一套代码,再通过 [Taro](https://taro.aotu.io/) 的编译工具,将源代码分别编译出可以在不同端(微信/百度/支付宝/字节跳动小程序、H5、React-Native 等)运行的代码。 6 | 7 | 该项目基于[Taro](https://taro.aotu.io/),构建了一个demo。 8 | 9 | ## 项目运行 10 | 11 | ```JS 12 | 13 | git clone git@github.com:TigerHee/taro-init.git 14 | 15 | cd taro-init 16 | 17 | // 全局安装taro脚手架 18 | npm install -g @tarojs/cli 19 | 或 20 | yarn global add @tarojs/cli 21 | 22 | // 安装项目依赖 23 | npm install 24 | 或 25 | yarn 26 | 27 | 28 | // 微信小程序 29 | taro build --type weapp --watch 30 | 31 | // 支付宝小程序 32 | taro build --type alipay --watch 33 | 34 | // 百度小程序 35 | taro build --type swan --watch 36 | 37 | // 字节跳动小程序 38 | taro build --type tt --watch 39 | 40 | // H5 41 | taro build --type h5 --watch 42 | 43 | // React Native 44 | taro build --type rn --watch 45 | 46 | // (去掉 --watch 将不会监听文件修改,并会对代码进行压缩打包) 47 | 48 | ``` 49 | ## 项目版本升级 50 | 51 | ```js 52 | // 1. 更新 Taro CLI 工具: 53 | 54 | npm i -g @tarojs/cli@latest 55 | 56 | // 2. 更新项目中 Taro 相关的依赖: 57 | 58 | taro update project 59 | 60 | // 3. 删除原来的node_modules后重新安装依赖(注意): 61 | 62 | cnpm install 63 | 64 | ``` 65 | 66 | ## 目录结构 67 | 68 | 69 | ├── config // Taro配置目录 70 | ├── dist // 小程序编译结果目录 71 | │ ├── dev.js // 开发时配置 72 | │ ├── index.js // 默认配置 73 | │ └── prod.js // 打包时配置 74 | ├── src // 源码目录 75 | │ ├── components // 组件 76 | │ ├── pages // 页面文件目录 77 | │ │ ├── index 78 | │ │ └── page2 79 | │ ├── redux // redux 80 | │ │ ├── actions 81 | │ │ ├── reducers 82 | │ │ └── store 83 | │ ├── servers // 接口请求相关 84 | │ │ ├── api // 请求方法封装改造 85 | │ │ ├── config // BASE_URL获取 86 | │ │ ├── interceptors // 请求拦截器 87 | │ │ └── servers // 请求方法汇总 88 | │ ├── utils // 常用工具类 89 | │ ├── app.js // 入口文件 90 | │ └── index.html 91 | ├── README.md 92 | └── package.json 93 | 94 | ## 线上作品: 95 | 96 | ![线上作品](https://raw.githubusercontent.com/TigerHee/taro-init/master/src/assets/img/xcx.jpg) 97 | -------------------------------------------------------------------------------- /config/dev.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { 3 | NODE_ENV: '"development"' 4 | }, 5 | defineConstants: { 6 | }, 7 | weapp: {}, 8 | h5: {} 9 | } 10 | -------------------------------------------------------------------------------- /config/index.js: -------------------------------------------------------------------------------- 1 | // eslint-disable-next-line import/no-commonjs 2 | const path = require("path"); 3 | 4 | const config = { 5 | projectName: 'tigerHee', 6 | date: '2019-4-1', 7 | designWidth: 750, 8 | deviceRatio: { 9 | '640': 2.34 / 2, 10 | '750': 1, 11 | '828': 1.81 / 2 12 | }, 13 | sourceRoot: 'src', 14 | outputRoot: 'dist', 15 | plugins: { 16 | babel: { 17 | sourceMap: true, 18 | presets: [ 19 | ['env', { 20 | modules: false 21 | }] 22 | ], 23 | plugins: [ 24 | 'transform-decorators-legacy', 25 | 'transform-class-properties', 26 | 'transform-object-rest-spread' 27 | ] 28 | } 29 | }, 30 | defineConstants: { 31 | }, 32 | copy: { 33 | patterns: [ 34 | ], 35 | options: { 36 | } 37 | }, 38 | weapp: { 39 | module: { 40 | postcss: { 41 | autoprefixer: { 42 | enable: true, 43 | config: { 44 | browsers: [ 45 | 'last 3 versions', 46 | 'Android >= 4.1', 47 | 'ios >= 8' 48 | ] 49 | } 50 | }, 51 | pxtransform: { 52 | enable: true, 53 | config: { 54 | 55 | } 56 | }, 57 | url: { 58 | enable: true, 59 | config: { 60 | limit: 10240 // 设定转换尺寸上限 61 | } 62 | }, 63 | cssModules: { 64 | enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true 65 | config: { 66 | namingPattern: 'module', // 转换模式,取值为 global/module 67 | generateScopedName: '[name]__[local]___[hash:base64:5]' 68 | } 69 | } 70 | } 71 | } 72 | }, 73 | h5: { 74 | publicPath: '/', 75 | staticDirectory: 'static', 76 | module: { 77 | postcss: { 78 | autoprefixer: { 79 | enable: true, 80 | config: { 81 | browsers: [ 82 | 'last 3 versions', 83 | 'Android >= 4.1', 84 | 'ios >= 8' 85 | ] 86 | } 87 | }, 88 | cssModules: { 89 | enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true 90 | config: { 91 | namingPattern: 'module', // 转换模式,取值为 global/module 92 | generateScopedName: '[name]__[local]___[hash:base64:5]' 93 | } 94 | } 95 | } 96 | } 97 | }, 98 | alias: { 99 | '@src': path.resolve(__dirname, '..', 'src') 100 | } 101 | } 102 | 103 | module.exports = function (merge) { 104 | if (process.env.NODE_ENV === 'development') { 105 | return merge({}, config, require('./dev')) 106 | } 107 | return merge({}, config, require('./prod')) 108 | } 109 | -------------------------------------------------------------------------------- /config/prod.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { 3 | NODE_ENV: '"production"' 4 | }, 5 | defineConstants: { 6 | }, 7 | weapp: {}, 8 | h5: { 9 | /** 10 | * 如果h5端编译后体积过大,可以使用webpack-bundle-analyzer插件对打包体积进行分析。 11 | * 参考代码如下: 12 | * webpackChain (chain) { 13 | * chain.plugin('analyzer') 14 | * .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin, []) 15 | * } 16 | */ 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tigerHee", 3 | "version": "1.0.0", 4 | "private": true, 5 | "description": "小程序研究学习", 6 | "scripts": { 7 | "build:weapp": "taro build --type weapp", 8 | "build:swan": "taro build --type swan", 9 | "build:alipay": "taro build --type alipay", 10 | "build:tt": "taro build --type tt", 11 | "build:h5": "taro build --type h5", 12 | "build:rn": "taro build --type rn", 13 | "dev:weapp": "npm run build:weapp -- --watch", 14 | "dev:swan": "npm run build:swan -- --watch", 15 | "dev:alipay": "npm run build:alipay -- --watch", 16 | "dev:tt": "npm run build:tt -- --watch", 17 | "dev:h5": "npm run build:h5 -- --watch", 18 | "dev:rn": "npm run build:rn -- --watch" 19 | }, 20 | "author": "", 21 | "license": "MIT", 22 | "dependencies": { 23 | "@tarojs/async-await": "1.2.22", 24 | "@tarojs/components": "1.2.22", 25 | "@tarojs/redux": "1.2.22", 26 | "@tarojs/redux-h5": "1.2.22", 27 | "@tarojs/router": "1.2.22", 28 | "@tarojs/taro": "1.2.22", 29 | "@tarojs/taro-alipay": "1.2.22", 30 | "@tarojs/taro-h5": "1.2.22", 31 | "@tarojs/taro-swan": "1.2.22", 32 | "@tarojs/taro-tt": "1.2.22", 33 | "@tarojs/taro-weapp": "1.2.22", 34 | "nervjs": "^1.3.9", 35 | "nerv-devtools": "^1.3.9", 36 | "redux": "^4.0.0", 37 | "redux-logger": "^3.0.6", 38 | "redux-thunk": "^2.3.0", 39 | "taro-ui": "^2.0.0" 40 | }, 41 | "devDependencies": { 42 | "@types/react": "^16.4.8", 43 | "@types/webpack-env": "^1.13.6", 44 | "@tarojs/plugin-babel": "1.2.22", 45 | "@tarojs/plugin-csso": "1.2.22", 46 | "@tarojs/plugin-sass": "1.2.22", 47 | "@tarojs/plugin-uglifyjs": "1.2.22", 48 | "@tarojs/webpack-runner": "1.2.22", 49 | "babel-plugin-transform-class-properties": "^6.24.1", 50 | "babel-plugin-transform-decorators-legacy": "^1.3.4", 51 | "babel-plugin-transform-jsx-stylesheet": "^0.6.5", 52 | "babel-plugin-transform-object-rest-spread": "^6.26.0", 53 | "babel-preset-env": "^1.6.1", 54 | "babel-eslint": "^8.2.3", 55 | "eslint": "^4.19.1", 56 | "eslint-config-taro": "1.2.22", 57 | "eslint-plugin-react": "^7.8.2", 58 | "eslint-plugin-import": "^2.12.0", 59 | "eslint-plugin-taro": "1.2.22" 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /project.config.json: -------------------------------------------------------------------------------- 1 | { 2 | "miniprogramRoot": "dist/", 3 | "projectname": "tigerHee", 4 | "description": "小程序研究学习", 5 | "appid": "appid", 6 | "setting": { 7 | "urlCheck": true, 8 | "es6": false, 9 | "postcss": false, 10 | "minified": false, 11 | "newFeature": true 12 | }, 13 | "compileType": "miniprogram", 14 | "condition": {} 15 | } -------------------------------------------------------------------------------- /src/app.js: -------------------------------------------------------------------------------- 1 | import '@tarojs/async-await' 2 | import Taro, { Component } from '@tarojs/taro' 3 | import { Provider } from '@tarojs/redux' 4 | import configStore from '@src/redux/store' 5 | 6 | import Index from '@src/pages/index' 7 | import 'taro-ui/dist/style/index.scss' 8 | import './app.scss' 9 | 10 | // 如果需要在 h5 环境中开启 React Devtools 11 | // 取消以下注释: 12 | // if (process.env.NODE_ENV !== 'production' && process.env.TARO_ENV === 'h5') { 13 | // require('nerv-devtools') 14 | // } 15 | const store = configStore() 16 | 17 | class App extends Component { 18 | 19 | config = { 20 | pages: [ 21 | 'pages/index/index', 22 | 'pages/page2/page2', 23 | ], 24 | window: { 25 | backgroundTextStyle: 'light', 26 | navigationBarBackgroundColor: '#ff6100', 27 | navigationBarTitleText: '前端之最', 28 | navigationBarTextStyle: 'white', 29 | navigationStyle: 'default', 30 | backgroundColor: '#ffffff', 31 | } 32 | } 33 | 34 | componentDidMount() { } 35 | 36 | componentDidShow() { } 37 | 38 | componentDidHide() { } 39 | 40 | componentDidCatchError() { } 41 | 42 | // 在 App 类中的 render() 函数没有实际作用 43 | // 请勿修改此函数 44 | render() { 45 | return ( 46 | 47 | 48 | 49 | ) 50 | } 51 | } 52 | 53 | Taro.render(, document.getElementById('app')) 54 | -------------------------------------------------------------------------------- /src/app.scss: -------------------------------------------------------------------------------- 1 | @import "~taro-ui/dist/style/components/button.scss"; 2 | @import "~taro-ui/dist/style/components/loading.scss"; 3 | @import "~taro-ui/dist/style/components/calendar.scss"; -------------------------------------------------------------------------------- /src/assets/img/xcx.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TigerHee/taro-init/f32e8f85811f6795d38456a0463887029c1164a6/src/assets/img/xcx.jpg -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 15 | 16 | 17 |
18 | 19 | 20 | -------------------------------------------------------------------------------- /src/pages/index/index.js: -------------------------------------------------------------------------------- 1 | import Taro, { Component } from '@tarojs/taro' 2 | import { View, Text } from '@tarojs/components' 3 | import { connect } from '@tarojs/redux' 4 | import { update_state } from '@src/redux/actions' 5 | import { AtButton } from 'taro-ui' 6 | import { getIpInfo } from '@src/servers/servers' 7 | 8 | import './index.scss' 9 | 10 | 11 | @connect((state) => ({ 12 | commonData: state.common.commonData 13 | }), (dispatch) => ({ 14 | onUpdateState(val) { 15 | dispatch(update_state(val)) 16 | } 17 | })) 18 | 19 | class Index extends Component { 20 | 21 | config = { 22 | navigationBarTitleText: '首页' 23 | } 24 | 25 | componentWillReceiveProps(nextProps) { 26 | console.log(this.props, nextProps) 27 | } 28 | 29 | componentWillUnmount() { 30 | getIpInfo().then(res=>{ 31 | console.log(res) 32 | }).catch(err=>console.log(err)) 33 | } 34 | 35 | componentDidShow() { } 36 | 37 | componentDidHide() { } 38 | 39 | onShareAppMessage() { 40 | return { 41 | title: 'web前端之最', 42 | path: '/index/index', 43 | imageUrl: 'https://avatars1.githubusercontent.com/u/15956567?s=460&v=4' 44 | } 45 | } 46 | 47 | setNewRedux = () => { 48 | const val = +new Date(); 49 | this.props.onUpdateState({ 50 | commonData: val 51 | }) 52 | } 53 | 54 | goToPage2 = () => { 55 | Taro.navigateTo({ 56 | url: '/pages/page2/page2' 57 | }) 58 | } 59 | 60 | render() { 61 | return ( 62 | 63 | {this.props.commonData} 64 | 65 | update redux 66 | 跳转到page2 67 | 68 | 69 | ) 70 | } 71 | } 72 | 73 | export default Index 74 | -------------------------------------------------------------------------------- /src/pages/index/index.scss: -------------------------------------------------------------------------------- 1 | .index { 2 | &-page2btn { 3 | margin-top: 16px; 4 | } 5 | } -------------------------------------------------------------------------------- /src/pages/page2/page2.js: -------------------------------------------------------------------------------- 1 | import Taro, { Component } from '@tarojs/taro' 2 | import { View } from '@tarojs/components' 3 | import { AtCalendar, AtButton } from "taro-ui" 4 | 5 | import './page2.scss' 6 | 7 | class Page2 extends Component { 8 | 9 | config = { 10 | navigationBarTitleText: 'page2' 11 | } 12 | 13 | onShareAppMessage() { 14 | return { 15 | title: 'web前端之最', 16 | path: '/index/index', 17 | imageUrl: 'https://avatars1.githubusercontent.com/u/15956567?s=460&v=4' 18 | } 19 | } 20 | 21 | goToPage1 = () => { 22 | Taro.navigateTo({ 23 | url: '/pages/index/index' 24 | }) 25 | } 26 | 27 | render() { 28 | return ( 29 | 30 | 31 | 跳转到index 32 | 33 | ) 34 | } 35 | } 36 | 37 | export default Page2 38 | -------------------------------------------------------------------------------- /src/pages/page2/page2.scss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TigerHee/taro-init/f32e8f85811f6795d38456a0463887029c1164a6/src/pages/page2/page2.scss -------------------------------------------------------------------------------- /src/redux/actions/index.js: -------------------------------------------------------------------------------- 1 | 2 | export const update_state = (payload) => { 3 | return { 4 | type: 'UPDATE_STATE', 5 | payload 6 | } 7 | } 8 | 9 | // 异步的action 10 | export function update_state_async() { 11 | return dispatch => { 12 | setTimeout(() => { 13 | dispatch(update_state()) 14 | }, 2000) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/redux/reducers/common/common.js: -------------------------------------------------------------------------------- 1 | import initSate from './initState' 2 | 3 | const common = (state = initSate, action) => { 4 | switch (action.type) { 5 | case 'UPDATE_STATE': 6 | return { ...state, ...action.payload } 7 | default: 8 | return state 9 | } 10 | } 11 | 12 | export default common -------------------------------------------------------------------------------- /src/redux/reducers/common/initState.js: -------------------------------------------------------------------------------- 1 | 2 | const initSate = { 3 | commonData: +new Date(), 4 | } 5 | 6 | export default initSate -------------------------------------------------------------------------------- /src/redux/reducers/index.js: -------------------------------------------------------------------------------- 1 | import { combineReducers } from 'redux' 2 | import common from './common/common' 3 | 4 | export default combineReducers({ 5 | common, 6 | }) -------------------------------------------------------------------------------- /src/redux/store.js: -------------------------------------------------------------------------------- 1 | import { createStore, applyMiddleware, compose } from 'redux' 2 | import thunkMiddleware from 'redux-thunk' 3 | import rootReducer from './reducers' 4 | 5 | const composeEnhancers = 6 | typeof window === 'object' && 7 | window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ? 8 | window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ 9 | // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize... 10 | }) : compose 11 | 12 | const middlewares = [ 13 | thunkMiddleware 14 | ] 15 | 16 | if (process.env.NODE_ENV === 'development') { 17 | middlewares.push(require('redux-logger').createLogger()) 18 | } 19 | 20 | const enhancer = composeEnhancers( 21 | applyMiddleware(...middlewares), 22 | // other store enhancers if any 23 | ) 24 | 25 | export default function configStore () { 26 | const store = createStore(rootReducer, enhancer) 27 | return store 28 | } 29 | -------------------------------------------------------------------------------- /src/servers/baseUrl.js: -------------------------------------------------------------------------------- 1 | const getBaseUrl = (url) => { 2 | let BASE_URL = ''; 3 | if (process.env.NODE_ENV === 'development') { 4 | //开发环境 - 根据请求不同返回不同的BASE_URL 5 | if (url.includes('/api/')) { 6 | BASE_URL = '' 7 | } else if (url.includes('/iatadatabase/')) { 8 | BASE_URL = '' 9 | } 10 | } else { 11 | // 生产环境 12 | if (url.includes('/api/')) { 13 | BASE_URL = '' 14 | } else if (url.includes('/iatadatabase/')) { 15 | BASE_URL = '' 16 | } 17 | } 18 | return BASE_URL 19 | } 20 | 21 | export default getBaseUrl; 22 | -------------------------------------------------------------------------------- /src/servers/config.js: -------------------------------------------------------------------------------- 1 | export const HTTP_STATUS = { 2 | SUCCESS: 200, 3 | CREATED: 201, 4 | ACCEPTED: 202, 5 | CLIENT_ERROR: 400, 6 | AUTHENTICATE: 401, 7 | FORBIDDEN: 403, 8 | NOT_FOUND: 404, 9 | SERVER_ERROR: 500, 10 | BAD_GATEWAY: 502, 11 | SERVICE_UNAVAILABLE: 503, 12 | GATEWAY_TIMEOUT: 504 13 | } -------------------------------------------------------------------------------- /src/servers/http.js: -------------------------------------------------------------------------------- 1 | import Taro from './node_modules/@tarojs/taro' 2 | import getBaseUrl from './baseUrl' 3 | import interceptors from './interceptors' 4 | 5 | interceptors.forEach(i => Taro.addInterceptor(i)) 6 | 7 | class httpRequest { 8 | baseOptions(params, method = "GET") { 9 | let { url, data } = params; 10 | const BASE_URL = getBaseUrl(url); 11 | let contentType = "application/json"; 12 | contentType = params.contentType || contentType; 13 | const option = { 14 | url: BASE_URL + url, 15 | data: data, 16 | method: method, 17 | header: { 18 | 'content-type': contentType, 19 | 'Authorization': Taro.getStorageSync('Authorization') 20 | } 21 | }; 22 | return Taro.request(option); 23 | } 24 | 25 | get(url, data = "") { 26 | let option = { url, data }; 27 | return this.baseOptions(option); 28 | } 29 | 30 | post(url, data, contentType) { 31 | let params = { url, data, contentType }; 32 | return this.baseOptions(params, "POST"); 33 | } 34 | 35 | put(url, data = "") { 36 | let option = { url, data }; 37 | return this.baseOptions(option, "PUT"); 38 | } 39 | 40 | delete(url, data = "") { 41 | let option = { url, data }; 42 | return this.baseOptions(option, "DELETE"); 43 | } 44 | 45 | } 46 | 47 | export default new httpRequest() -------------------------------------------------------------------------------- /src/servers/interceptors.js: -------------------------------------------------------------------------------- 1 | import Taro from "@tarojs/taro"; 2 | import { pageToLogin } from "@src/utils/common"; 3 | import { HTTP_STATUS } from './config' 4 | 5 | const customInterceptor = (chain) => { 6 | 7 | const requestParams = chain.requestParams 8 | 9 | return chain.proceed(requestParams).then(res => { 10 | if (res.statusCode === HTTP_STATUS.NOT_FOUND) { 11 | return Promise.reject("请求资源不存在") 12 | 13 | } else if (res.statusCode === HTTP_STATUS.BAD_GATEWAY) { 14 | return Promise.reject("服务端出现了问题") 15 | 16 | } else if (res.statusCode === HTTP_STATUS.FORBIDDEN) { 17 | Taro.setStorageSync("Authorization", "") 18 | pageToLogin() 19 | // TODO 根据自身业务修改 20 | return Promise.reject("没有权限访问"); 21 | 22 | } else if (res.statusCode === HTTP_STATUS.AUTHENTICATE) { 23 | Taro.setStorageSync("Authorization", "") 24 | pageToLogin() 25 | return Promise.reject("需要鉴权") 26 | 27 | } else if (res.statusCode === HTTP_STATUS.SUCCESS) { 28 | return res.data 29 | 30 | } 31 | }) 32 | } 33 | 34 | const interceptors = [customInterceptor, Taro.interceptors.logInterceptor] 35 | 36 | export default interceptors -------------------------------------------------------------------------------- /src/servers/servers.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable import/prefer-default-export */ 2 | import HTTPREQUEST from "./http"; 3 | 4 | /* 获取IP信息 */ 5 | export function getIpInfo() { 6 | return HTTP.get('https://ipinfo.tianbaows.com'); 7 | } 8 | 9 | export function getResultData(postData) { 10 | return HTTPREQUEST.post('/api/white-screen/search', postData) 11 | } -------------------------------------------------------------------------------- /src/utils/common.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable import/prefer-default-export */ 2 | import Taro from "@tarojs/taro"; 3 | 4 | /*获取当前页url*/ 5 | export const getCurrentPageUrl = () => { 6 | let pages = Taro.getCurrentPages(); 7 | let currentPage = pages[pages.length - 1]; 8 | let url = currentPage.route; 9 | return url; 10 | } 11 | 12 | export const pageToLogin = () => { 13 | let path = getCurrentPageUrl() 14 | if (!path.includes('login')) { 15 | Taro.navigateTo({ 16 | url: "/pages/login/login" 17 | }); 18 | } 19 | } --------------------------------------------------------------------------------