common.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * 显示消息提示框
  3. * @param content 提示的标题
  4. */
  5. export function toast(content) {
  6. uni.showToast({
  7. icon: 'none',
  8. title: content
  9. })
  10. }
  11. /**
  12. * 显示模态弹窗
  13. * @param content 提示的标题
  14. */
  15. export function showConfirm(content) {
  16. return new Promise((resolve, reject) => {
  17. uni.showModal({
  18. title: '提示',
  19. content: content,
  20. cancelText: '取消',
  21. confirmText: '确定',
  22. success: function(res) {
  23. resolve(res)
  24. }
  25. })
  26. })
  27. }
  28. /**
  29. * 参数处理
  30. * @param params 参数
  31. */
  32. export function tansParams(params) {
  33. let result = ''
  34. for (const propName of Object.keys(params)) {
  35. const value = params[propName]
  36. var part = encodeURIComponent(propName) + "="
  37. if (value !== null && value !== "" && typeof (value) !== "undefined") {
  38. if (typeof value === 'object') {
  39. for (const key of Object.keys(value)) {
  40. if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
  41. let params = propName + '[' + key + ']'
  42. var subPart = encodeURIComponent(params) + "="
  43. result += subPart + encodeURIComponent(value[key]) + "&"
  44. }
  45. }
  46. } else {
  47. result += part + encodeURIComponent(value) + "&"
  48. }
  49. }
  50. }
  51. return result
  52. }
  53. //上传图片(本地地址识别一张)
  54. export function uploadIdentify(api, filePaths, successUp, failUp, i, length, files, callback) {
  55. const isToken = (config.headers || {}).isToken === false
  56. config.header = config.header || {}
  57. if (getToken() && !isToken) {
  58. config.header['Authorization'] = 'Bearer ' + getToken()
  59. }
  60. // get请求映射params参数
  61. if (config.params) {
  62. let url = config.url + '?' + tansParams(config.params)
  63. url = url.slice(0, -1)
  64. config.url = url
  65. }
  66. uni.showLoading({
  67. title: '上传中'
  68. })
  69. var failfile = [];
  70. uni.uploadFile({
  71. timeout: config.timeout || timeout,
  72. url: baseUrl + api, //仅为示例,非真实的接口地址
  73. filePath: filePaths[i],
  74. name: 'file',
  75. header: config.header,
  76. formData: config.formData,
  77. success: function(resp) {
  78. uni.hideLoading();
  79. let result = JSON.parse(resp.data)
  80. const code = result.code || 200
  81. const msg = errorCode[code] || result.msg || errorCode['default']
  82. // console.log(result.fileName,8)
  83. if (result.code == 200) {
  84. successUp++;
  85. files[i] = result;
  86. } else if(result.code==401) {
  87. showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => {
  88. if (res.confirm) {
  89. store.dispatch('LogOut').then(res => {
  90. uni.reLaunch({ url: '/pages/login/login' })
  91. })
  92. }
  93. })
  94. callback('无效的会话,或者会话已过期,请重新登录。');
  95. }else{
  96. failfile = failfile.concat(filePaths[i])
  97. failUp++;
  98. }
  99. },
  100. fail: function(res) {
  101. uni.hideLoading();
  102. failfile = failfile.concat(filePaths[i])
  103. failUp++;
  104. },
  105. complete: function(rsp) {
  106. // console.log(rsp, filePaths[i])
  107. uni.hideLoading();
  108. i++;
  109. if (i == length) {
  110. uni.showToast({
  111. title: '上传成功',
  112. icon: 'none',
  113. duration: 2000
  114. });
  115. callback(files);
  116. } else { //递归调用upload函数
  117. uploadIdentify(api, filePaths, successUp, failUp, i, length, files, callback);
  118. }
  119. }
  120. });
  121. }
  122. //上传图片
  123. export function uploadmore(api, filePaths, successUp, failUp, i, length, files, callback) {
  124. const isToken = (config.headers || {}).isToken === false
  125. config.header = config.header || {}
  126. if (getToken() && !isToken) {
  127. config.header['Authorization'] = 'Bearer ' + getToken()
  128. }
  129. // get请求映射params参数
  130. if (config.params) {
  131. let url = config.url + '?' + tansParams(config.params)
  132. url = url.slice(0, -1)
  133. config.url = url
  134. }
  135. uni.showLoading({
  136. title: '上传中'
  137. })
  138. var failfile = [];
  139. uni.uploadFile({
  140. timeout: config.timeout || timeout,
  141. url: baseUrl + api, //仅为示例,非真实的接口地址
  142. filePath: filePaths[i],
  143. name: 'file',
  144. header: config.header,
  145. formData: config.formData,
  146. success: function(resp) {
  147. uni.hideLoading();
  148. let result = JSON.parse(resp.data)
  149. const code = result.code || 200
  150. const msg = errorCode[code] || result.msg || errorCode['default']
  151. // console.log(result.fileName,8)
  152. if (result.code == 200) {
  153. successUp++;
  154. files[i] = result.fileName;
  155. } else if(result.code==401) {
  156. showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => {
  157. if (res.confirm) {
  158. store.dispatch('LogOut').then(res => {
  159. uni.reLaunch({ url: '/pages/login/login' })
  160. })
  161. }
  162. })
  163. callback('无效的会话,或者会话已过期,请重新登录。');
  164. }else{
  165. failfile = failfile.concat(filePaths[i])
  166. failUp++;
  167. }
  168. },
  169. fail: function(res) {
  170. uni.hideLoading();
  171. failfile = failfile.concat(filePaths[i])
  172. failUp++;
  173. },
  174. complete: function(rsp) {
  175. console.log(rsp, filePaths[i])
  176. uni.hideLoading();
  177. i++;
  178. if (i == length) {
  179. uni.showToast({
  180. title: '上传成功',
  181. icon: 'none',
  182. duration: 2000
  183. });
  184. callback(files);
  185. } else { //递归调用upload函数
  186. uploadmore(api, filePaths, successUp, failUp, i, length, files, callback);
  187. }
  188. }
  189. });
  190. }