common.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. export function geocodeAddress(address, key) {
  93. return new Promise((resolve, reject) => {
  94. // H5 和 App 平台使用高德地图 JavaScript API
  95. if (uni.getSystemInfoSync().platform === 'h5' || uni.getSystemInfoSync().platform === 'android') {
  96. const url = `https://restapi.amap.com/v3/geocode/geo?address=${encodeURIComponent(address)}&key=${key}`;
  97. uni.request({
  98. url,
  99. success: (res) => {
  100. console.log(res)
  101. if (res.data.status === '1' && res.data.geocodes.length > 0) {
  102. const location = res.data.geocodes[0].location; // 返回格式:经度,纬度
  103. const [longitude, latitude] = location.split(',');
  104. console.log(223)
  105. resolve({ latitude: parseFloat(latitude), longitude: parseFloat(longitude) });
  106. } else {
  107. reject(new Error('未找到地址对应的经纬度'));
  108. }
  109. },
  110. fail: (err) => {
  111. reject(err);
  112. },
  113. });
  114. }
  115. // 小程序平台使用高德地图小程序 SDK
  116. else if (uni.getSystemInfoSync().platform === 'mp-weixin') {
  117. wx.request({
  118. url: `https://restapi.amap.com/v3/geocode/geo?address=${encodeURIComponent(address)}&key=${key}`,
  119. success: (res) => {
  120. if (res.data.status === '1' && res.data.geocodes.length > 0) {
  121. const location = res.data.geocodes[0].location; // 返回格式:经度,纬度
  122. const [longitude, latitude] = location.split(',');
  123. resolve({ latitude: parseFloat(latitude), longitude: parseFloat(longitude) });
  124. } else {
  125. reject(new Error('未找到地址对应的经纬度'));
  126. }
  127. },
  128. fail: (err) => {
  129. reject(err);
  130. },
  131. });
  132. } else {
  133. reject(new Error('不支持的平台'));
  134. }
  135. });
  136. }
  137. /**
  138. * 参数处理
  139. * @param params 参数
  140. */
  141. export function tansParams(params) {
  142. let result = ''
  143. for (const propName of Object.keys(params)) {
  144. const value = params[propName]
  145. var part = encodeURIComponent(propName) + "="
  146. if (value !== null && value !== "" && typeof (value) !== "undefined") {
  147. if (typeof value === 'object') {
  148. for (const key of Object.keys(value)) {
  149. if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
  150. let params = propName + '[' + key + ']'
  151. var subPart = encodeURIComponent(params) + "="
  152. result += subPart + encodeURIComponent(value[key]) + "&"
  153. }
  154. }
  155. } else {
  156. result += part + encodeURIComponent(value) + "&"
  157. }
  158. }
  159. }
  160. return result
  161. }
  162. //上传图片(本地地址识别一张)
  163. export function uploadIdentify(api, filePaths, successUp, failUp, i, length, files, callback) {
  164. const isToken = (config.headers || {}).isToken === false
  165. config.header = config.header || {}
  166. if (getToken() && !isToken) {
  167. config.header['Authorization'] = 'Bearer ' + getToken()
  168. }
  169. // get请求映射params参数
  170. if (config.params) {
  171. let url = config.url + '?' + tansParams(config.params)
  172. url = url.slice(0, -1)
  173. config.url = url
  174. }
  175. uni.showLoading({
  176. title: '上传中'
  177. })
  178. var failfile = [];
  179. uni.uploadFile({
  180. timeout: config.timeout || timeout,
  181. url: baseUrl + api, //仅为示例,非真实的接口地址
  182. filePath: filePaths[i],
  183. name: 'file',
  184. header: config.header,
  185. formData: config.formData,
  186. success: function(resp) {
  187. uni.hideLoading();
  188. let result = JSON.parse(resp.data)
  189. const code = result.code || 200
  190. const msg = errorCode[code] || result.msg || errorCode['default']
  191. // console.log(result.fileName,8)
  192. if (result.code == 200) {
  193. successUp++;
  194. files[i] = result;
  195. } else if(result.code==401) {
  196. showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => {
  197. if (res.confirm) {
  198. store.dispatch('LogOut').then(res => {
  199. uni.reLaunch({ url: '/pages/login/login' })
  200. })
  201. }
  202. })
  203. callback('无效的会话,或者会话已过期,请重新登录。');
  204. }else{
  205. failfile = failfile.concat(filePaths[i])
  206. failUp++;
  207. }
  208. },
  209. fail: function(res) {
  210. uni.hideLoading();
  211. failfile = failfile.concat(filePaths[i])
  212. failUp++;
  213. },
  214. complete: function(rsp) {
  215. // console.log(rsp, filePaths[i])
  216. uni.hideLoading();
  217. i++;
  218. if (i == length) {
  219. uni.showToast({
  220. title: '上传成功',
  221. icon: 'none',
  222. duration: 2000
  223. });
  224. callback(files);
  225. } else { //递归调用upload函数
  226. uploadIdentify(api, filePaths, successUp, failUp, i, length, files, callback);
  227. }
  228. }
  229. });
  230. }
  231. //上传图片
  232. export function uploadmore(api, filePaths, successUp, failUp, i, length, files, callback) {
  233. const isToken = (config.headers || {}).isToken === false
  234. config.header = config.header || {}
  235. if (getToken() && !isToken) {
  236. config.header['Authorization'] = 'Bearer ' + getToken()
  237. }
  238. // get请求映射params参数
  239. if (config.params) {
  240. let url = config.url + '?' + tansParams(config.params)
  241. url = url.slice(0, -1)
  242. config.url = url
  243. }
  244. uni.showLoading({
  245. title: '上传中'
  246. })
  247. var failfile = [];
  248. uni.uploadFile({
  249. timeout: config.timeout || timeout,
  250. url: baseUrl + api, //仅为示例,非真实的接口地址
  251. filePath: filePaths[i],
  252. name: 'file',
  253. header: config.header,
  254. formData: config.formData,
  255. success: function(resp) {
  256. uni.hideLoading();
  257. let result = JSON.parse(resp.data)
  258. const code = result.code || 200
  259. const msg = errorCode[code] || result.msg || errorCode['default']
  260. // console.log(result.fileName,8)
  261. if (result.code == 200) {
  262. successUp++;
  263. files[i] = result.fileName;
  264. } else if(result.code==401) {
  265. showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => {
  266. if (res.confirm) {
  267. store.dispatch('LogOut').then(res => {
  268. uni.reLaunch({ url: '/pages/login/login' })
  269. })
  270. }
  271. })
  272. callback('无效的会话,或者会话已过期,请重新登录。');
  273. }else{
  274. failfile = failfile.concat(filePaths[i])
  275. failUp++;
  276. }
  277. },
  278. fail: function(res) {
  279. uni.hideLoading();
  280. failfile = failfile.concat(filePaths[i])
  281. failUp++;
  282. },
  283. complete: function(rsp) {
  284. uni.hideLoading();
  285. i++;
  286. if (i == length) {
  287. uni.showToast({
  288. title: '上传成功',
  289. icon: 'none',
  290. duration: 2000
  291. });
  292. callback(files);
  293. } else { //递归调用upload函数
  294. uploadmore(api, filePaths, successUp, failUp, i, length, files, callback);
  295. }
  296. }
  297. });
  298. }