common.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 uploadVideo(api, filePaths, 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,
  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. if (result.code == 200) {
  149. files[0] = result.data;
  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. }
  162. },
  163. fail: function(res) {
  164. uni.hideLoading();
  165. failfile = failfile.concat(filePaths[i])
  166. },
  167. complete: function(rsp) {
  168. // console.log(rsp, filePaths[i])
  169. uni.hideLoading();
  170. // i++;
  171. // if (i == length) {
  172. // uni.showToast({
  173. // title: '上传成功',
  174. // icon: 'none',
  175. // duration: 2000
  176. // });
  177. callback(files);
  178. // } else { //递归调用upload函数
  179. // uploadIdentify(api, filePaths, successUp, failUp, i, length, files, callback);
  180. // }
  181. }
  182. });
  183. }
  184. //上传图片(本地地址识别一张)
  185. export function uploadIdentify(api, filePaths, successUp, failUp, i, length, files, callback) {
  186. const isToken = (config.headers || {}).isToken === false
  187. config.header = config.header || {}
  188. if (getToken() && !isToken) {
  189. config.header['Authorization'] = 'Bearer ' + getToken()
  190. config.header['clientid']=clientid;
  191. }
  192. // get请求映射params参数
  193. if (config.params) {
  194. let url = config.url + '?' + tansParams(config.params)
  195. url = url.slice(0, -1)
  196. config.url = url
  197. }
  198. uni.showLoading({
  199. title: '上传中'
  200. })
  201. var failfile = [];
  202. uni.uploadFile({
  203. timeout: config.timeout || timeout,
  204. url: baseUrl + api, //仅为示例,非真实的接口地址
  205. filePath: filePaths[i],
  206. name: 'file',
  207. header: config.header,
  208. formData: config.formData,
  209. success: function(resp) {
  210. uni.hideLoading();
  211. let result = JSON.parse(resp.data)
  212. const code = result.code || 200
  213. const msg = errorCode[code] || result.msg || errorCode['default']
  214. if (result.code == 200) {
  215. successUp++;
  216. files[i] = result.data;
  217. } else if(result.code==401) {
  218. showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => {
  219. if (res.confirm) {
  220. store.dispatch('LogOut').then(res => {
  221. uni.reLaunch({ url: '/pages/login/login' })
  222. })
  223. }
  224. })
  225. callback('无效的会话,或者会话已过期,请重新登录。');
  226. }else{
  227. failfile = failfile.concat(filePaths[i])
  228. failUp++;
  229. }
  230. },
  231. fail: function(res) {
  232. uni.hideLoading();
  233. failfile = failfile.concat(filePaths[i])
  234. failUp++;
  235. },
  236. complete: function(rsp) {
  237. // console.log(rsp, filePaths[i])
  238. uni.hideLoading();
  239. i++;
  240. if (i == length) {
  241. uni.showToast({
  242. title: '上传成功',
  243. icon: 'none',
  244. duration: 2000
  245. });
  246. callback(files);
  247. } else { //递归调用upload函数
  248. uploadIdentify(api, filePaths, successUp, failUp, i, length, files, callback);
  249. }
  250. }
  251. });
  252. }
  253. //上传图片
  254. export function uploadmore(api, filePaths, successUp, failUp, i, length, files, callback) {
  255. const isToken = (config.headers || {}).isToken === false
  256. config.header = config.header || {}
  257. if (getToken() && !isToken) {
  258. config.header['Authorization'] = 'Bearer ' + getToken()
  259. config.header['clientid']=clientid;
  260. }
  261. // get请求映射params参数
  262. if (config.params) {
  263. let url = config.url + '?' + tansParams(config.params)
  264. url = url.slice(0, -1)
  265. config.url = url
  266. }
  267. uni.showLoading({
  268. title: '上传中'
  269. })
  270. var failfile = [];
  271. uni.uploadFile({
  272. timeout: config.timeout || timeout,
  273. url: baseUrl + api, //仅为示例,非真实的接口地址
  274. filePath: filePaths[i],
  275. name: 'file',
  276. header: config.header,
  277. formData: config.formData,
  278. success: function(resp) {
  279. uni.hideLoading();
  280. let result = JSON.parse(resp.data)
  281. const code = result.code || 200
  282. const msg = errorCode[code] || result.msg || errorCode['default']
  283. // console.log(result.fileName,8)
  284. if (result.code == 200) {
  285. successUp++;
  286. files[i] = result.data.fileName;
  287. } else if(result.code==401) {
  288. showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => {
  289. if (res.confirm) {
  290. store.dispatch('LogOut').then(res => {
  291. uni.reLaunch({ url: '/pages/login/login' })
  292. })
  293. }
  294. })
  295. callback('无效的会话,或者会话已过期,请重新登录。');
  296. }else{
  297. failfile = failfile.concat(filePaths[i])
  298. failUp++;
  299. }
  300. },
  301. fail: function(res) {
  302. uni.hideLoading();
  303. failfile = failfile.concat(filePaths[i])
  304. failUp++;
  305. },
  306. complete: function(rsp) {
  307. uni.hideLoading();
  308. i++;
  309. if (i == length) {
  310. uni.showToast({
  311. title: '上传成功',
  312. icon: 'none',
  313. duration: 2000
  314. });
  315. callback(files);
  316. } else { //递归调用upload函数
  317. uploadmore(api, filePaths, successUp, failUp, i, length, files, callback);
  318. }
  319. }
  320. });
  321. }