common.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 selectDictLabelkey(datas, value) {
  56. var actions = [];
  57. var idx=0;
  58. Object.keys(datas).some((key) => {
  59. if (datas[key].dictLabel == ('' + value)) {
  60. idx=key;
  61. actions.push(datas[key].dictValue);
  62. return true;
  63. }
  64. })
  65. var newObj={
  66. actions:actions.join(''),
  67. key:idx
  68. }
  69. return newObj
  70. }
  71. export function selectValueKey(datas, value) {
  72. var actions = [];
  73. var idx=0;
  74. Object.keys(datas).some((key) => {
  75. if (datas[key].dictValue == ('' + value)) {
  76. idx=key;
  77. actions.push(datas[key].dictLabel);
  78. return true;
  79. }
  80. })
  81. var newObj={
  82. actions:actions.join(''),
  83. key:idx
  84. }
  85. return newObj
  86. }
  87. export function selectValue(datas, value) {
  88. var actions = [];
  89. var idx=0;
  90. Object.keys(datas).some((key) => {
  91. if (datas[key].value == ('' + value)) {
  92. actions.push(datas[key].text);
  93. return true;
  94. }
  95. })
  96. return actions.join('')
  97. }
  98. // export function selectValuetext(datas, value) {
  99. // var actions = [];
  100. // var idx=0;
  101. // Object.keys(datas).some((key) => {
  102. // if (datas[key].value == ('' + value)) {
  103. // actions.push(datas[key].text);
  104. // return true;
  105. // }
  106. // })
  107. // return actions.join('')
  108. // }
  109. export function geocodeAddress(address, key) {
  110. return new Promise((resolve, reject) => {
  111. // H5 和 App 平台使用高德地图 JavaScript API
  112. if (uni.getSystemInfoSync().platform === 'h5' || uni.getSystemInfoSync().platform === 'android') {
  113. const url = `https://restapi.amap.com/v3/geocode/geo?address=${encodeURIComponent(address)}&key=${key}`;
  114. uni.request({
  115. url,
  116. success: (res) => {
  117. console.log(res)
  118. if (res.data.status === '1' && res.data.geocodes.length > 0) {
  119. const location = res.data.geocodes[0].location; // 返回格式:经度,纬度
  120. const [longitude, latitude] = location.split(',');
  121. console.log(223)
  122. resolve({ latitude: parseFloat(latitude), longitude: parseFloat(longitude) });
  123. } else {
  124. reject(new Error('未找到地址对应的经纬度'));
  125. }
  126. },
  127. fail: (err) => {
  128. reject(err);
  129. },
  130. });
  131. }
  132. // 小程序平台使用高德地图小程序 SDK
  133. else if (uni.getSystemInfoSync().platform === 'mp-weixin') {
  134. wx.request({
  135. url: `https://restapi.amap.com/v3/geocode/geo?address=${encodeURIComponent(address)}&key=${key}`,
  136. success: (res) => {
  137. if (res.data.status === '1' && res.data.geocodes.length > 0) {
  138. const location = res.data.geocodes[0].location; // 返回格式:经度,纬度
  139. const [longitude, latitude] = location.split(',');
  140. resolve({ latitude: parseFloat(latitude), longitude: parseFloat(longitude) });
  141. } else {
  142. reject(new Error('未找到地址对应的经纬度'));
  143. }
  144. },
  145. fail: (err) => {
  146. reject(err);
  147. },
  148. });
  149. } else {
  150. reject(new Error('不支持的平台'));
  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. }
  179. //上传图片(本地地址识别一张)
  180. export function uploadIdentify(api, filePaths, successUp, failUp, i, length, files, callback) {
  181. const isToken = (config.headers || {}).isToken === false
  182. config.header = config.header || {}
  183. if (getToken() && !isToken) {
  184. config.header['Authorization'] = 'Bearer ' + getToken()
  185. config.header['clientid']=clientid;
  186. }
  187. // get请求映射params参数
  188. if (config.params) {
  189. let url = config.url + '?' + tansParams(config.params)
  190. url = url.slice(0, -1)
  191. config.url = url
  192. }
  193. uni.showLoading({
  194. title: '上传中'
  195. })
  196. var failfile = [];
  197. uni.uploadFile({
  198. timeout: config.timeout || timeout,
  199. url: baseUrl + api, //仅为示例,非真实的接口地址
  200. filePath: filePaths[i],
  201. name: 'file',
  202. header: config.header,
  203. formData: config.formData,
  204. success: function(resp) {
  205. uni.hideLoading();
  206. let result = JSON.parse(resp.data)
  207. const code = result.code || 200
  208. const msg = errorCode[code] || result.msg || errorCode['default']
  209. if (result.code == 200) {
  210. successUp++;
  211. files[i] = result.data;
  212. } else if(result.code==401) {
  213. showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => {
  214. if (res.confirm) {
  215. store.dispatch('LogOut').then(res => {
  216. uni.reLaunch({ url: '/pages/login/login' })
  217. })
  218. }
  219. })
  220. callback('无效的会话,或者会话已过期,请重新登录。');
  221. }else{
  222. failfile = failfile.concat(filePaths[i])
  223. failUp++;
  224. }
  225. },
  226. fail: function(res) {
  227. uni.hideLoading();
  228. failfile = failfile.concat(filePaths[i])
  229. failUp++;
  230. },
  231. complete: function(rsp) {
  232. let rspa = JSON.parse(rsp.data)
  233. // console.log(rsp, filePaths[i])
  234. uni.hideLoading();
  235. i++;
  236. if (i == length) {
  237. if(rspa.code==200){
  238. uni.showToast({
  239. title: '上传成功',
  240. icon: 'none',
  241. duration: 2000
  242. });
  243. }else{
  244. uni.showToast({
  245. title: rspa.msg,
  246. icon: 'none',
  247. duration: 2000
  248. });
  249. }
  250. callback(files);
  251. } else { //递归调用upload函数
  252. uploadIdentify(api, filePaths, successUp, failUp, i, length, files, callback);
  253. }
  254. }
  255. });
  256. }
  257. //上传图片
  258. export function uploadmore(api, filePaths, successUp, failUp, i, length, files, callback) {
  259. const isToken = (config.headers || {}).isToken === false
  260. config.header = config.header || {}
  261. if (getToken() && !isToken) {
  262. config.header['Authorization'] = 'Bearer ' + getToken()
  263. config.header['clientid']=clientid;
  264. }
  265. // get请求映射params参数
  266. if (config.params) {
  267. let url = config.url + '?' + tansParams(config.params)
  268. url = url.slice(0, -1)
  269. config.url = url
  270. }
  271. uni.showLoading({
  272. title: '上传中'
  273. })
  274. var failfile = [];
  275. uni.uploadFile({
  276. timeout: config.timeout || timeout,
  277. url: baseUrl + api, //仅为示例,非真实的接口地址
  278. filePath: filePaths[i],
  279. name: 'file',
  280. header: config.header,
  281. formData: config.formData,
  282. success: function(resp) {
  283. uni.hideLoading();
  284. let result = JSON.parse(resp.data)
  285. const code = result.code || 200
  286. const msg = errorCode[code] || result.msg || errorCode['default']
  287. console.log(result.data,8)
  288. if (result.code == 200) {
  289. successUp++;
  290. files[i] = result.data.fileName;
  291. } else if(result.code==401) {
  292. showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => {
  293. if (res.confirm) {
  294. store.dispatch('LogOut').then(res => {
  295. uni.reLaunch({ url: '/pages/login/login' })
  296. })
  297. }
  298. })
  299. callback('无效的会话,或者会话已过期,请重新登录。');
  300. }else{
  301. failfile = failfile.concat(filePaths[i])
  302. failUp++;
  303. }
  304. },
  305. fail: function(res) {
  306. uni.hideLoading();
  307. failfile = failfile.concat(filePaths[i])
  308. failUp++;
  309. },
  310. complete: function(rsp) {
  311. let rspa = JSON.parse(rsp.data)
  312. uni.hideLoading();
  313. i++;
  314. if (i == length) {
  315. if(rspa.code==200){
  316. uni.showToast({
  317. title: '上传成功',
  318. icon: 'none',
  319. duration: 2000
  320. });
  321. }else{
  322. uni.showToast({
  323. title: rspa.msg,
  324. icon: 'none',
  325. duration: 2000
  326. });
  327. }
  328. callback(files);
  329. } else { //递归调用upload函数
  330. uploadmore(api, filePaths, successUp, failUp, i, length, files, callback);
  331. }
  332. }
  333. });
  334. }