12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- let baseUrl = ''
- if (process.env.NODE_ENV === 'development') {
- baseUrl = 'http://anvideo.nxmy.com:8011';
- } else {
- baseUrl = 'http://anvideo.nxmy.com:8011';
- }
- function service(options = {}) {
- uni.showLoading({
- title: '加载中'
- });
- options.url = `${baseUrl}${options.url}`;
- // 判断本地是否存在token,如果存在则带上请求头
- let token = uni.getStorageSync('token')
- options.header = {
- 'content-type': 'application/json',
- 'Authorization': `Bearer ${token || false}` // 这里是token(可自行修改)
- };
- // resolved是返回成功数据,rejected返回错误数据
- return new Promise((resolved, rejected) => {
- options.success = (res) => {
- // 如果请求回来的状态码不是200则执行以下操作
- uni.hideLoading();
- if (res.statusCode === 404 || res.statusCode === 500) {
- uni.showToast({
- icon: 'none',
- duration: 3000,
- title: '服务器错误,请稍后再试'
- });
- rejected()
- } else {
- resolved(res)
- }
- // if (res.data.code !== 200) {
- // uni.showToast({
- // icon: 'none',
- // duration: 3000,
- // title: `${res.data.msg}`
- // });
- // rejected(res)
- // } else {
- // resolved(res.data.data)
- // }
- };
- options.fail = (err) => {
- uni.hideLoading();
- uni.showToast({
- icon: 'none',
- duration: 3000,
- title: '服务器错误,请稍后再试'
- });
- rejected(err);
- };
- uni.request(options);
- });
- }
-
- export default service;
|