common.js 8.8 KB

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