common.js 4.5 KB

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