common.js 4.5 KB

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