Commit d459e3e0 by wong.peiyi

配置信息、异常处理

parent 20b4460d
/*
* @FilePath: /BoneHouse_Business_APP/src/pages/index/index.tsx
* @Author: peii
* @Date: 2021-04-24 22:45:15
* @Vision: 1.0
* @Description: 首页
*
* @Revision:
*
*/
import React, { Component } from 'react' import React, { Component } from 'react'
import { View, Text, TouchableOpacity, Image, ScrollView } from 'react-native' import { View, Text, TouchableOpacity, Image, ScrollView, RefreshControl } from 'react-native'
import slashScreen from 'react-native-splash-screen' import slashScreen from 'react-native-splash-screen'
import { observer, inject } from 'mobx-react' import { observer, inject } from 'mobx-react'
import { IFunction } from 'bonehouse' import { IFunction } from 'bonehouse'
...@@ -11,30 +22,65 @@ import styles from './index.styl' ...@@ -11,30 +22,65 @@ import styles from './index.styl'
type IProps = { type IProps = {
store: { store: {
token: string token: string
getBizFuns: Function
functionIcons: any[] functionIcons: any[]
getBizFuns: Function
setNavigation: Function
}
sysStore: {
getSysProfile: Function
} }
} }
class Index extends Component<IProps> { class Index extends Component<IProps> {
state = {
refreshing: false,
}
constructor(props) {
super(props)
this.onRefresh = this.onRefresh.bind(this)
}
componentDidMount() { componentDidMount() {
slashScreen.hide() slashScreen.hide()
this.props.store.setNavigation(this.props.navigation)
setTimeout(() => { setTimeout(() => {
const { navigation, store } = this.props const { navigation, store } = this.props
!store.token && navigation.navigate('Signin') !store.token && navigation.navigate('Signin')
this.getSysProfile()
}, 0) }, 0)
} }
/**
* @description: 下拉刷新
* @param {*}
* @return {*}
*/
onRefresh() {
this.setState({ refreshing: true })
this.getSysProfile().finally(() => this.setState({ refreshing: false }))
}
async getSysProfile() {
this.props.sysStore.getSysProfile()
}
render() { render() {
const { navigation, store } = this.props const { navigation, store } = this.props
const { refreshing } = this.state
const funs = store.getBizFuns() const funs = store.getBizFuns()
return ( return (
<View style={styles.index}> <View style={styles.index}>
<Header title="骨科智慧仓" back={false} /> <Header title="骨科智慧仓" back={false} />
<ScrollView style={g(styles, 'biz')}> <ScrollView
style={g(styles, 'biz')}
refreshControl={<RefreshControl refreshing={refreshing} onRefresh={this.onRefresh} />}
>
<View style={g(styles, 'biz-view')}> <View style={g(styles, 'biz-view')}>
{funs.map(fun => { {funs.map(fun => {
return ( return (
...@@ -58,4 +104,4 @@ class Index extends Component<IProps> { ...@@ -58,4 +104,4 @@ class Index extends Component<IProps> {
} }
} }
export default inject('store')(observer(Index)) export default inject('store', 'sysStore')(observer(Index))
/*
* @FilePath: /BoneHouse_Business_APP/src/pages/mine/mine.tsx
* @Author: peii
* @Date: 2021-04-24 22:45:15
* @Vision: 1.0
* @Description: 我的页面
*
* @Revision:
*
*/
import React, { Component } from 'react' import React, { Component } from 'react'
import { View, Text, ScrollView, Image, SafeAreaView } from 'react-native' import { View, Text, ScrollView, Image, SafeAreaView } from 'react-native'
import { List } from '@ant-design/react-native' import { List } from '@ant-design/react-native'
......
/*
* @FilePath: /BoneHouse_Business_APP/src/services/request.ts
* @Author: peii
* @Date: 2021-04-24 22:45:15
* @Vision: 1.0
* @Description:
*
* @Revision:
*
*/
// @ts-nocheck // @ts-nocheck
import * as R from 'ramda' import * as R from 'ramda'
import { isBlank } from '../utils/utils' import { isBlank, show, genPid } from '../utils/utils'
import { stringify } from 'querystring' import { stringify } from 'querystring'
import Store from '../stores/store' import Store from '../stores/store'
import container from '../inversify' import container from '../inversify'
...@@ -14,19 +24,34 @@ interface RequestConfig { ...@@ -14,19 +24,34 @@ interface RequestConfig {
headers: any headers: any
} }
let requestQueue = []
export const request = (args: Partial<RequestConfig>) => { export const request = (args: Partial<RequestConfig>) => {
const store = container.get<Store>(TYPES.Store) const store = container.get<Store>(TYPES.Store)
let cancel = null
let isCancel = false
const p = new Promise((resolve, reject) => {
cancel = () => {
isCancel = true
reject({ errorCode: 400, errorMsg: '取消' })
}
return new Promise((resolve, reject) => {
let options: any = { let options: any = {
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
method: R.propOr('get', 'method', args), method: R.propOr('get', 'method', args),
reTries: R.propOr(3, 'reTries', args), reTries: R.propOr(3, 'reTries', args),
needToken: R.propOr(true, 'needToken', args),
...args, ...args,
} }
if (options.needToken) {
options.data.accessToken = store.token
}
args.data = transformObject(args.data, 'toLine')
options = R.cond([ options = R.cond([
[ [
R.propEq('method', 'get'), R.propEq('method', 'get'),
...@@ -42,39 +67,67 @@ export const request = (args: Partial<RequestConfig>) => { ...@@ -42,39 +67,67 @@ export const request = (args: Partial<RequestConfig>) => {
fetch(store.host + options.url, options) fetch(store.host + options.url, options)
.then(res => res.json()) .then(res => res.json())
.then(res => { .then(res => {
if (isCancel) return
console.log('返回结果:', res) console.log('返回结果:', res)
R.ifElse( R.ifElse(
fetchSuccess, fetchSuccess,
() => resolve(transformObject(res, 'toHump')), () => {
removeRequestByPid(pid)
resolve(transformObject(res, 'toHump'))
},
() => { () => {
throw res throw res
}, },
)(res) )(res)
}) })
.catch(e => { .catch(e => {
if (requestTimes <= options.reTries) { if (isCancel) return
if (requestTimes < options.reTries) {
return doRequest() return doRequest()
} }
// TODO: 登录判断 failHandler(e, store, pid)
// failHandler(e)
// throw e
}) })
} }
doRequest() doRequest()
}) })
const pid = genPid()
requestQueue.push({ pid, p, cancel })
return p.catch(e => {
return e
})
} }
const fetchSuccess = R.propEq('error_code', 0) const fetchSuccess = R.propEq('error_code', 0)
const failHandler = async (err: any) => { const removeRequestByPid = pid => {
requestQueue = R.compose(
R.remove(R.__, 1, requestQueue),
R.findIndex(R.propEq('pid', pid)),
)(requestQueue)
}
const failHandler = async (err: any, store: Store, pid) => {
R.ifElse( R.ifElse(
isBlank, isBlank,
() => { () => {
// Message.error(`${err.status} ${err.statusText}`) show(err.status ? `${err.status} ${err.statusText}` : err.message)
}, },
() => { () => {
// Message.error(`${err.code} ${err.msg}`) if (err.error_code === 41006) {
show(err.error_msg)
removeRequestByPid(pid)
R.map(p => {
p.cancel()
}, requestQueue)
requestQueue = []
store.navigation.navigate('Signin')
} else {
show(err.error_msg)
removeRequestByPid(pid)
}
}, },
)(err.code) )(err.error_code)
return err
} }
/*
* @FilePath: /BoneHouse_Business_APP/src/services/service.ts
* @Author: peii
* @Date: 2021-04-24 22:45:15
* @Vision: 1.0
* @Description: 网络服务
*
* @Revision:
*
*/
import { request } from './request' import { request } from './request'
import { injectable } from 'inversify' import { injectable } from 'inversify'
......
import { observable, action, runInAction, computed, decorate, toJS } from 'mobx' /*
* @FilePath: /BoneHouse_Business_APP/src/stores/store.ts
* @Author: peii
* @Date: 2021-04-24 22:45:15
* @Vision: 1.0
* @Description: 一般性store
*
* @Revision:
*
*/
import { observable, action, runInAction, toJS, flow } from 'mobx'
import { persist } from 'mobx-persist' import { persist } from 'mobx-persist'
import { injectable } from 'inversify' import { injectable, inject } from 'inversify'
import { NativeModules } from 'react-native' import { NativeModules } from 'react-native'
import { IFunction } from 'bonehouse' import { IFunction } from 'bonehouse'
import * as R from 'ramda' import * as R from 'ramda'
...@@ -29,6 +39,8 @@ export default class Store { ...@@ -29,6 +39,8 @@ export default class Store {
MOBILE_TRANSFER_APPLICATION: require('../assets/images/trans_order.png'), MOBILE_TRANSFER_APPLICATION: require('../assets/images/trans_order.png'),
} }
@observable navigation = null
/** /**
* 首页功能列表 * 首页功能列表
* @returns IFunction[] * @returns IFunction[]
...@@ -65,6 +77,11 @@ export default class Store { ...@@ -65,6 +77,11 @@ export default class Store {
setFunctions(functions: IFunction[]) { setFunctions(functions: IFunction[]) {
this.functions = functions this.functions = functions
} }
@action
setNavigation(navigation: any) {
this.navigation = navigation
}
} }
runInAction(() => { runInAction(() => {
......
import { observable, action, flow } from 'mobx' /*
* @FilePath: /BoneHouse_Business_APP/src/stores/system.ts
* @Author: peii
* @Date: 2021-04-24 22:45:15
* @Vision: 1.0
* @Description: 系统store
*
* @Revision:
*
*/
import { observable, flow, runInAction } from 'mobx'
import { persist } from 'mobx-persist' import { persist } from 'mobx-persist'
import { injectable } from 'inversify' import { injectable, inject } from 'inversify'
import * as R from 'ramda'
import Service from '../services/service'
import { TYPES } from '../inversify/types'
import { toBinaryNumber } from '../utils/utils'
@injectable() @injectable()
export default class System { export default class System {
@inject(TYPES.Service)
private service!: Service
@persist @observable sysProfiles = {
// APP借货仓库显隐及是否必填
OBS_MOBILE_BOR_WARE_REQUIRED: '00',
// APP消耗是否显示部门权限
OBS_MOBILE_CONSU_DEPARTMENT_DISPLAY: '00',
// APP消耗是否显示医生权限
OBS_MOBILE_CONSU_DOCTOR_DISPLAY: '00',
// APP消耗是否显示跟台员权限
OBS_MOBILE_CONSU_FOLLOW_DISPLAY: '00',
// APP下单是否显示部门权限
OBS_MOBILE_DEPARTMENT_DISPLAY: '00',
// APP下单是否显示业务经理权限
OBS_MOBILE_BM_DISPLAY: '00',
// APP下单是否显示跟台员权限
OBS_MOBILE_SUR_FOLLOWER_DISPLAY: '00',
// APP下单是否显示送货员权限
OBS_MOBILE_DELIVERYMAN_DISPLAY: '00',
// APP器械消耗展示价格权限
OBS_MOBILE_EQU_CON_DISPLAY_PRICE: 'N',
}
/**
* @description: 获取系统配置
* @param {*} function
* @return {*}
*/
getSysProfile = flow(function* (this: System) {
const getProfiles = async (key: any) => {
const res = (await this.service.getSysProfile({ profileCode: key })) as any
runInAction(() => {
// @ts-ignore
this.sysProfiles[key] = isNaN(res.data.profileValue)
? res.data.profileValue
: toBinaryNumber(res.data.profileValue)
})
}
R.compose(R.map(getProfiles), R.keys)(this.sysProfiles)
})
} }
/*
* @FilePath: /BoneHouse_Business_APP/src/utils/transform.ts
* @Author: peii
* @Date: 2021-04-24 22:45:15
* @Vision: 1.0
* @Description:
*
* @Revision:
*
*/
import * as R from 'ramda' import * as R from 'ramda'
import { isBlank } from './utils' import { isBlank } from './utils'
...@@ -39,7 +49,7 @@ export function transformObject(obj: ITransObj, type: ITransformTypes = 'toHump' ...@@ -39,7 +49,7 @@ export function transformObject(obj: ITransObj, type: ITransformTypes = 'toHump'
R.map((key: string) => { R.map((key: string) => {
const dKey = fn.call(null, key) const dKey = fn.call(null, key)
if (R.type(item[key]) === 'Array' || R.type(item[key]) === 'Object') { if (R.type(item[key]) === 'Array' || R.type(item[key]) === 'Object') {
desc[dKey] = isBlank(item[key]) ? item[key] : transformObject(item[key]) desc[dKey] = isBlank(item[key]) ? item[key] : transformObject(item[key], type)
} else { } else {
desc[dKey] = item[key] desc[dKey] = item[key]
} }
......
/*
* @FilePath: /BoneHouse_Business_APP/src/utils/utils.ts
* @Author: peii
* @Date: 2021-04-24 22:45:15
* @Vision: 1.0
* @Description:
*
* @Revision:
*
*/
// @ts-nocheck // @ts-nocheck
import * as R from 'ramda' import * as R from 'ramda'
import { Dimensions, Platform } from 'react-native' import { Dimensions, Platform } from 'react-native'
...@@ -107,3 +117,23 @@ export const isIphoneX = (): boolean => { ...@@ -107,3 +117,23 @@ export const isIphoneX = (): boolean => {
(screenH === XS_MAX_HEIGHT && screenW === XS_MAX_WIDTH)) (screenH === XS_MAX_HEIGHT && screenW === XS_MAX_WIDTH))
) )
} }
/**
* 生成随机ID
* @returns
*/
export const genPid = (): string => {
return new Date().getTime() + Math.random() * 1000
}
/**
* @description: 转成二进制字符串
* @param {number} n
* @return {string}
*/
export const toBinaryNumber = (n: number | string, len = 2): string => {
if (isNaN(n)) return n
let binN = parseInt(n).toString(2)
while (R.length(binN) < len) binN = '0' + binN
return binN
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment