common.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import store from '@/store'
  2. import config from '@/config'
  3. import { getToken } from '@/utils/auth'
  4. import errorCode from '@/utils/errorCode'
  5. let timeout = 10000
  6. const baseUrl = config.baseUrl
  7. const clientid = config.Clientid
  8. /**
  9. * 显示消息提示框
  10. * @param content 提示的标题
  11. */
  12. export function toast(content) {
  13. uni.showToast({
  14. icon: 'none',
  15. title: content
  16. })
  17. }
  18. /**
  19. * 显示模态弹窗
  20. * @param content 提示的标题
  21. */
  22. export function showConfirm(content) {
  23. return new Promise((resolve, reject) => {
  24. uni.showModal({
  25. title: '提示',
  26. content: content,
  27. cancelText: '取消',
  28. confirmText: '确定',
  29. success: function(res) {
  30. resolve(res)
  31. }
  32. })
  33. })
  34. }
  35. // 字典值匹配
  36. export function selectDictLabel(datas, value) {
  37. var actions = [];
  38. Object.keys(datas).some((key) => {
  39. if (datas[key].dictLabel == ('' + value)) {
  40. actions.push(datas[key].dictValue);
  41. return true;
  42. }
  43. })
  44. return actions.join('');
  45. }
  46. // 字典值匹配
  47. export function selectDictValue(datas, value) {
  48. var actions = [];
  49. Object.keys(datas).some((key) => {
  50. if (datas[key].dictValue == ('' + value)) {
  51. actions.push(datas[key].dictLabel);
  52. return true;
  53. }
  54. })
  55. return actions.join('');
  56. }
  57. // 日期格式
  58. export function format (fmt) {
  59. var o = {
  60. "M+": this.getMonth() + 1, //月份
  61. "d+": this.getDate(), //日
  62. "h+": this.getHours(), //小时
  63. "m+": this.getMinutes(), //分
  64. "s+": this.getSeconds(), //秒
  65. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  66. "S": this.getMilliseconds() //毫秒
  67. };
  68. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  69. for (var k in o)
  70. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  71. return fmt;
  72. }
  73. // 上传
  74. //上传图片
  75. export function uploadmore(api, filePaths, successUp, failUp, i, length, files, callback) {
  76. const isToken = (config.headers || {}).isToken === false
  77. config.header = config.header || {}
  78. if (getToken() && !isToken) {
  79. config.header['Authorization'] = 'Bearer ' + getToken();
  80. config.header['clientid']=clientid;
  81. }
  82. // get请求映射params参数
  83. if (config.params) {
  84. let url = config.url + '?' + tansParams(config.params)
  85. url = url.slice(0, -1)
  86. config.url = url
  87. }
  88. uni.showLoading({
  89. title: '上传中'
  90. })
  91. var failfile = [];
  92. uni.uploadFile({
  93. timeout: config.timeout || timeout,
  94. url: baseUrl + api, //仅为示例,非真实的接口地址
  95. filePath: filePaths[i],
  96. name: 'file',
  97. header: config.header,
  98. formData: config.formData,
  99. success: function(resp) {
  100. uni.hideLoading();
  101. let result = JSON.parse(resp.data)
  102. const code = result.code || 200
  103. const msg = errorCode[code] || result.msg || errorCode['default']
  104. // console.log(result.fileName,8)
  105. if (result.code == 200) {
  106. successUp++;
  107. files[i] = result.fileName;
  108. } else if(result.code==401) {
  109. showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => {
  110. if (res.confirm) {
  111. store.dispatch('LogOut').then(res => {
  112. uni.reLaunch({ url: '/pages/login/login' })
  113. })
  114. }
  115. })
  116. callback('无效的会话,或者会话已过期,请重新登录。');
  117. }else{
  118. failfile = failfile.concat(filePaths[i])
  119. failUp++;
  120. }
  121. },
  122. fail: function(res) {
  123. uni.hideLoading();
  124. failfile = failfile.concat(filePaths[i])
  125. failUp++;
  126. },
  127. complete: function(rsp) {
  128. // console.log(rsp, filePaths[i])
  129. uni.hideLoading();
  130. i++;
  131. if (i == length) {
  132. // uni.showToast({
  133. // title: '总共' + successUp + '张上传成功,' + failUp + '张上传失败!',
  134. // icon: 'none',
  135. // duration: 2000
  136. // });
  137. callback(files);
  138. } else { //递归调用upload函数
  139. uploadmore(api, filePaths, successUp, failUp, i, length, files, callback);
  140. }
  141. }
  142. });
  143. }
  144. /**
  145. * 参数处理
  146. * @param params 参数
  147. */
  148. export function tansParams(params) {
  149. let result = ''
  150. for (const propName of Object.keys(params)) {
  151. const value = params[propName]
  152. var part = encodeURIComponent(propName) + "="
  153. if (value !== null && value !== "" && typeof (value) !== "undefined") {
  154. if (typeof value === 'object') {
  155. for (const key of Object.keys(value)) {
  156. if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
  157. let params = propName + '[' + key + ']'
  158. var subPart = encodeURIComponent(params) + "="
  159. result += subPart + encodeURIComponent(value[key]) + "&"
  160. }
  161. }
  162. } else {
  163. result += part + encodeURIComponent(value) + "&"
  164. }
  165. }
  166. }
  167. return result
  168. }