common.js 4.5 KB

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