common.js 6.6 KB

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