common.js 6.7 KB

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