interface.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * 通用uni-app网络请求
  3. * 基于 Promise 对象实现更简单的 request 使用方式,支持请求和响应拦截
  4. */
  5. /*
  6. // 开放的接口
  7. import http from './interface'
  8. http.config.baseUrl = "http://localhost:8080/api/"
  9. http.request(url:'user/list',method:'GET').then((res)=>{
  10. console.log(JSON.stringify(res))
  11. })
  12. http.get('user/list').then((res)=>{
  13. console.log(JSON.stringify(res))
  14. })
  15. http.get('user/list', {status: 1}).then((res)=>{
  16. console.log(JSON.stringify(res))
  17. })
  18. http.post('user', {id:1, status: 1}).then((res)=>{
  19. console.log(JSON.stringify(res))
  20. })
  21. http.put('user/1', {status: 2}).then((res)=>{
  22. console.log(JSON.stringify(res))
  23. })
  24. http.delete('user/1').then((res)=>{
  25. console.log(JSON.stringify(res))
  26. })
  27. */
  28. export default {
  29. config: {
  30. // baseUrl: "http://colliery.nxjiewei.com/api",
  31. baseUrl: "http://ningdongyunying.nxjiewei.com:8011/api",
  32. header: {
  33. "Content-Type":"multipart/form-data",
  34. 'Content-Type':'application/json;charset=UTF-8',
  35. 'Content-Type':'application/x-www-form-urlencoded',
  36. 'Authorization' : uni.getStorageSync('token_type') +' '+uni.getStorageSync('Authorization') || {}
  37. },
  38. data: {},
  39. method: "GET",
  40. dataType: "json", /* 如设为json,会对返回的数据做一次 JSON.parse */
  41. responseType: "text",
  42. success() {},
  43. fail() {},
  44. complete() {
  45. // uni.hideLoading()
  46. }
  47. },
  48. interceptor: {
  49. request: null,
  50. response: null
  51. },
  52. request(options) {
  53. if (!options) {
  54. options = {}
  55. }
  56. options.baseUrl = options.baseUrl || this.config.baseUrl
  57. options.dataType = options.dataType || this.config.dataType
  58. options.url = options.baseUrl + options.url
  59. options.data = options.data || {}
  60. options.method = options.method || this.config.method
  61. //TODO 加密数据
  62. //TODO 数据签名
  63. /*
  64. _token = {'token': getStorage(STOREKEY_LOGIN).token || 'undefined'},
  65. _sign = {'sign': sign(JSON.stringify(options.data))}
  66. options.header = Object.assign({}, options.header, _token,_sign)
  67. */
  68. return new Promise((resolve, reject) => {
  69. let _config = null
  70. // uni.showLoading({
  71. // icon:"none",
  72. // title:"加载中...",
  73. // mask:true
  74. // })
  75. options.complete = (response) => {
  76. let statusCode = response.statusCode
  77. // console.log("【" + _config.requestId + "】 状态码:" + JSON.stringify(statusCode))
  78. response.config = _config
  79. if (process.env.NODE_ENV === 'development') {
  80. if (statusCode === 200) {
  81. // uni.hideLoading()
  82. // console.log("【" + _config.requestId + "】 结果:" + JSON.stringify(response.data))
  83. if(response.data.code == 401){
  84. uni.showToast({
  85. icon:"none",
  86. title:"登录失效、请重新登录"
  87. })
  88. setTimeout(function(){
  89. uni.redirectTo({
  90. url:"/pages/login/login"
  91. })
  92. },2000)
  93. }
  94. }else if(statusCode === 500){
  95. uni.hideLoading()
  96. uni.showToast({
  97. icon:"none",
  98. title:"500"
  99. })
  100. // setTimeout(function(){
  101. // uni.navigateTo({
  102. // url:"/pages/login/login"
  103. // })
  104. // },1500)
  105. }
  106. }
  107. if (this.interceptor.response) {
  108. let newResponse = this.interceptor.response(response)
  109. if (newResponse) {
  110. response = newResponse
  111. }
  112. }
  113. // 统一的响应日志记录
  114. // _reslog(response)
  115. if (statusCode === 200) { //成功
  116. resolve(response);
  117. } else {
  118. reject(response)
  119. }
  120. }
  121. _config = Object.assign({}, this.config, options)
  122. _config.requestId = new Date().getTime()
  123. if (this.interceptor.request) {
  124. this.interceptor.request(_config)
  125. }
  126. // 统一的请求日志记录
  127. // _reqlog(_config)
  128. if (process.env.NODE_ENV === 'development') {
  129. // console.log("【" + _config.requestId + "】 地址:" + _config.url)
  130. if (_config.data) {
  131. // console.log("【" + _config.requestId + "】 参数:" + JSON.stringify(_config.data))
  132. }
  133. }
  134. uni.request(_config);
  135. });
  136. },
  137. get(url, data, options) {
  138. if (!options) {
  139. options = {}
  140. }
  141. options.url = url
  142. options.data = data
  143. options.method = 'GET'
  144. return this.request(options)
  145. },
  146. post(url, data, options) {
  147. if (!options) {
  148. options = {}
  149. }
  150. options.url = url
  151. options.data = data
  152. options.method = 'POST'
  153. return this.request(options)
  154. },
  155. put(url, data, options) {
  156. if (!options) {
  157. options = {}
  158. }
  159. options.url = url
  160. options.data = data
  161. options.method = 'PUT'
  162. return this.request(options)
  163. },
  164. delete(url, data, options) {
  165. if (!options) {
  166. options = {}
  167. }
  168. options.url = url
  169. options.data = data
  170. options.method = 'DELETE'
  171. return this.request(options)
  172. }
  173. }
  174. /**
  175. * 请求接口日志记录
  176. */
  177. function _reqlog(req) {
  178. if (process.env.NODE_ENV === 'development') {
  179. console.log("【" + req.requestId + "】 地址:" + req.url)
  180. if (req.data) {
  181. console.log("【" + req.requestId + "】 请求参数:" + JSON.stringify(req.data))
  182. }
  183. }
  184. //TODO 调接口异步写入日志数据库
  185. }
  186. /**
  187. * 响应接口日志记录
  188. */
  189. function _reslog(res) {
  190. let _statusCode = res.statusCode;
  191. if (process.env.NODE_ENV === 'development') {
  192. console.log("【" + res.config.requestId + "】 地址:" + res.config.url)
  193. if (res.config.data) {
  194. console.log("【" + res.config.requestId + "】 请求参数:" + JSON.stringify(res.config.data))
  195. }
  196. console.log("【" + res.config.requestId + "】 响应结果:" + JSON.stringify(res))
  197. }
  198. //TODO 除了接口服务错误外,其他日志调接口异步写入日志数据库
  199. switch(_statusCode){
  200. case 200:
  201. break;
  202. case 401:
  203. break;
  204. case 404:
  205. break;
  206. default:
  207. break;
  208. }
  209. }