common.js 8.6 KB

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