-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.jsx
More file actions
244 lines (205 loc) · 7.4 KB
/
Copy pathindex.jsx
File metadata and controls
244 lines (205 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import React, { Fragment } from 'react';
import DocumentTitle from 'react-document-title';
import { Spin } from 'antd';
import { Toast, WhiteSpace, Button } from 'antd-mobile';
import Error from 'components/WarningError';
import FloatingIcon from 'components/FloatingIcon';
import { isObject, isFunction, isString } from 'feewee/utils';
import { getQueryStringWrapper, getDisplayName, checkOptionsByKeys, getValueByGivenPathFromObject } from 'utils';
import { loadWxJsSDK } from './api';
import { MP_ENV, WX_H5_ENV } from 'utils/api';
const ERROR_WITH_REQUEST_MSG = '请求错误,请检查'
const DEFAULT_TITLE = 'DEFAULT_TITLE'
/**
* @author vuchan<givingwu@gmail.com>
* @description h5页面配置的UrlOpts HOC
*/
export default ({
urlOptions = {}, //`object` url 中默认的 params: object
pageTitle = DEFAULT_TITLE,
titleKeyPath = 'title',
showLoading = undefined,
requestMethod, //`func` 请求回调
transformData, //`func` 自定义转义data函数
requestLoadingText, //`string` 请求时候提示的文字内容
requestErrorContent = ERROR_WITH_REQUEST_MSG, // `string | node` 请求错误的warning/error文字
responseWholeDown = true, //`bool` 整体解构下传,还是以一个 `response` 为 `props` 下传
checkUrlOptions = false, //`bool` 是否强制检查 options
checkRequiredKeys = [],
checkRequiredErrMsg = '',
showFloatingIconInMiniProgram = true,
showShareIconInMiniProgram = false,
}) => WrappedComponent => {
if (showLoading === undefined) {
if (isFunction(requestMethod)) {
showLoading = true;
}
}
return class withURLOpts extends React.PureComponent {
static displayName = `withURLOpts(${getDisplayName(WrappedComponent)})`
state = {
loading: showLoading,
}
// instance properties
options = null
// flag to only execute `checkUrlOptions` once
checkedUrlOptions = false
componentDidMount = async () => {
if (checkUrlOptions) {
if (this.checkOptions()) {
this.executeRequest()
} else {
this.setState({
loading: false,
ERROR_MSG: checkRequiredErrMsg,
})
}
} else {
this.executeRequest()
}
const env = await loadWxJsSDK((platform) => {
this.isMiniProgram = platform === MP_ENV;
this.isWeXinBrowser = platform === WX_H5_ENV;
this.setState({
isMiniProgram: this.isMiniProgram,
isWeXinBrowser: this.isWeXinBrowser,
})
return platform;
});
this.isMiniProgram = env === MP_ENV;
this.isWeXinBrowser = env === WX_H5_ENV;
}
executeRequest = () => {
const ctx = WrappedComponent;
const { onRequestSuccess, onRequestFailed } = ctx;
const config = { hideLoading: true } // 因为界面上已经有了首屏渲染的loading effects
/* console.log('ctx, onRequestSuccess, onRequestFailed====================================');
console.log(ctx, onRequestSuccess, onRequestFailed);
console.log('===================================='); */
if (requestMethod) {
requestMethod({ params: { ...urlOptions, ...this.options }, ...config } || config)
.then((data) => {
data = transformData ? transformData(data) : data;
if (!data || !isObject(data)) {
console.warn(`Are u sure u have returned a data and it is an object ${data}, right?`);
}
this.setState((prevState) => {
let state = {};
if (responseWholeDown) {
state = { ...prevState, ...data, loading: false }
} else {
state = { response: data, loading: false }
}
return state;
}, () => {
isFunction(onRequestSuccess) && onRequestSuccess(responseWholeDown ? this.state : this.state.response).bind(ctx)
})
})
.catch(error => {
const { message } = error;
console.error(error);
Toast.fail(message, 3, () => {
this.setState({
loading: false,
requestError: true,
ERROR_MSG: message || ERROR_WITH_REQUEST_MSG,
}, () => {
isFunction(onRequestFailed) && onRequestFailed(this.state.ERROR_MSG).bind(ctx)
})
}, false);
})
}
}
checkOptions = () => {
if (!this.options) {
this.options = getQueryStringWrapper();
console.info('url options', this.options)
};
if (this.options === null) return false;
if (checkRequiredKeys.length) return checkOptionsByKeys(this.options, checkRequiredKeys);
}
getTitle = () => {
if (titleKeyPath) {
try {
const title = getValueByGivenPathFromObject(this, titleKeyPath)
if (title && isString(title) && title.trim()) {
pageTitle = title.trim()
}
} catch(e) {
console.error(e)
}
}
return pageTitle;
}
getFloatingIcons = () => {
if (!showFloatingIconInMiniProgram) return;
const homeIconOption = {
type: 'home',
page: '/pages/newCar/Index/index',
title: '返回主页',
onClick() {
const wx = window.wx;
if (wx && isObject(wx) && wx.miniProgram) {
wx.miniProgram.switchTab({
url: homeIconOption.page
})
}
}
}
const icons = [homeIconOption]
if (showShareIconInMiniProgram) {
const shareIconOpt = {
type: 'share-alt',
page: `/pages/common/SharePic/index?from=activity&url=${encodeURIComponent(window.location.href)}`,
title: '返回页面',
onClick() {
const wx = window.wx;
if (wx && isObject(wx) && wx.miniProgram) {
wx.miniProgram.navigateTo({
url: shareIconOpt.page
})
}
}
}
icons.push(shareIconOpt)
}
return icons;
}
render() {
const { isMiniProgram, loading, ERROR_MSG, requestError } = this.state
// Firstly, checkUrlOptions
if (checkUrlOptions && !this.checkedUrlOptions) {
this.checkedUrlOptions = true;
if (!this.checkOptions()) {
return <DocumentTitle title={checkRequiredErrMsg}><Error>{checkRequiredErrMsg}</Error></DocumentTitle>
}
}
// Secondly, Show Loading Effect
if (loading) {
return (
<DocumentTitle title={"正在加载中..."}>
<Spin style={{ paddingTop: 100 }} spinning={loading} tip={requestLoadingText}><WhiteSpace size="lg" /></Spin>
</DocumentTitle>
)
}
if (ERROR_MSG) {
return (
<DocumentTitle title={ERROR_MSG}>
<Error>
{ ERROR_MSG || requestErrorContent }
{ requestError && <Button style={{ marginTop: 20 }} type="ghost" onClick={() => window.location.reload()}>点击刷新</Button> }
</Error>
</DocumentTitle>
)
}
return (
<DocumentTitle title={this.getTitle()}>
<Fragment>
{ isMiniProgram && <FloatingIcon icons={this.getFloatingIcons()} /> }
<WrappedComponent style={{ maxWidth: '100vw', width: '100%' }} ref={node => this.element = node} options={this.options || {}} {...this.state} />
</Fragment>
</DocumentTitle>
)
}
};
}