1. config.js
    2. class Config{
    3. constructor() {}
    4. }
    5. /*
    6. * 1 表示正式环境
    7. * 2 表示测试环境
    8. * 3 表示本地环境
    9. */
    10. Config.official = 1;
    11. if (Config.official == 1) {
    12. Config.domain = 'http://xxxxx'; //正式
    13. }
    14. if (Config.official == 2) {
    15. Config.domain = "http://xxxxxxx"; //测试
    16. }
    17. if(Config.official == 3) {
    18. Config.domain = "http://xxxxxxx"; //本地测试
    19. }
    20. export {Config};
    1. import {
    2. Config
    3. } from './config.js';
    4. class Base {
    5. constructor() {
    6. "use strict"
    7. this.domain = Config.domain;
    8. }
    9. /*
    10. * 1. 封装 uniapp 的请求接口
    11. * 带Token请求--promise封装
    12. * @Author wangqiang
    13. * @DateTime 2019-07-02
    14. * return 返回promise
    15. * 使用方式:
    16. * let opts = {
    17. * url: '/api/device/add',
    18. * method: 'post'
    19. * },
    20. * let data = {
    21. * name:'zs',
    22. * age:10
    23. * }
    24. * this.httpTokenRequest(opts,data);
    25. */
    26. httpTokenRequest(opts, data = {}) {
    27. let token = "";
    28. uni.getStorage({
    29. key: 'user_token',
    30. success: function(result) {
    31. token = result.data
    32. }
    33. });
    34. if (!opts.method) {
    35. opts.method = 'GET';
    36. }
    37. let httpDefaultOpts = {
    38. url: this.domain + opts.url,
    39. data: data,
    40. method: opts.method.toUpperCase(),
    41. header: opts.method.toUpperCase() == 'POST' ? {
    42. from: 'web',
    43. 'Token': token,
    44. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    45. } : {
    46. from: 'web',
    47. 'Token': token,
    48. "Accept": "application/json",
    49. "Content-Type": "application/json; charset=UTF-8"
    50. },
    51. dataType: 'json',
    52. }
    53. let promise = new Promise((resolve, reject) => {
    54. uni.request(httpDefaultOpts)
    55. .then(
    56. res => {
    57. if ((res[1])) {
    58. // 判断以2(2XX)开头的状态码为正确
    59. let _code = res[1].statusCode.toString();
    60. let _startChar = _code.charAt(0);
    61. if (_startChar == '2') {
    62. resolve(res[1].data)
    63. } else {
    64. this._processError(res[1]); //错误处理函数
    65. }
    66. }
    67. }
    68. )
    69. .catch(error => reject(error))
    70. })
    71. return promise;
    72. }
    73. /**
    74. * 2. 封装上传文件接口
    75. * @Author wangqiang
    76. * @DateTime 2019-07-02
    77. * @param {[type]} params [description]
    78. * @return 返回promise对象
    79. */
    80. uploadFileAsync(opts) {
    81. let token = "";
    82. uni.getStorage({
    83. key: 'user_token',
    84. success: function(res) {
    85. token = res.data
    86. }
    87. });
    88. let httpDefaultOpts = {
    89. url: this.domain + opts.url,
    90. header: {
    91. from: 'web',
    92. token: token,
    93. },
    94. filePath: opts.filePath,
    95. name: 'files'
    96. }
    97. let promise = new Promise((resolve, reject) => {
    98. uni.uploadFile(httpDefaultOpts).then(
    99. res => {
    100. resolve(res[1].data)
    101. }
    102. ).catch(
    103. response => {
    104. reject(response)
    105. }
    106. )
    107. })
    108. return promise;
    109. }
    110. /**
    111. * 3. 封装错误异常处理接口
    112. * @Author wangqiang
    113. * @DateTime 2019-07-02
    114. * @param {[type]} params [description]
    115. */
    116. _processError(params) {
    117. if(params.statusCode==404){
    118. uni.showToast({title: '页面不存在',icon: 'none'})
    119. return uni.navigateBack({delta:1})
    120. }
    121. let token_code= params.data.error_code ? params.data.error_code : ''
    122. var title = params.data.msg.toString();
    123. let _token = uni.getStorageSync('user_token');
    124. //判断token是否过期、Token是否为空
    125. if(token_code === 5007 || _token == "" || _token == null || _token === undefined){
    126. uni.showToast({
    127. title: '离线时间过长,请重新登录!',
    128. icon: 'none'
    129. });
    130. return uni.navigateTo({url: '/pages/login/login'})
    131. }
    132. if (title != 'JWT验证未通过') {
    133. if (title.indexOf("Token")<0){
    134. uni.showToast({
    135. title: title,
    136. icon: 'none'
    137. });
    138. } else {
    139. uni.showToast({
    140. title: "离线时间过长,请重新登录",
    141. icon: 'none'
    142. });
    143. return uni.navigateTo({url: '/pages/login/login'})
    144. }
    145. }else{
    146. uni.showToast({
    147. title: title,
    148. icon: 'none'
    149. });
    150. }
    151. }
    152. /**
    153. * 4.判断是否登录(根据token判断)
    154. * @Author wangqiang
    155. * @DateTime 2019-07-02
    156. * @return 返回布尔值
    157. */
    158. judgeIsLogined() {
    159. let user_token = uni.getStorageSync('user_token');
    160. if (user_token == "" || user_token == null || user_token === undefined) {
    161. uni.navigateTo({
    162. url: '../login/login'
    163. })
    164. return false;
    165. } else {
    166. return true;
    167. }
    168. }
    169. }
    170. export {
    171. Base
    172. };