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. 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. // console.log(rsp, filePaths[i])
  217. uni.hideLoading();
  218. i++;
  219. if (i == length) {
  220. uni.showToast({
  221. title: '上传成功',
  222. icon: 'none',
  223. duration: 2000
  224. });
  225. callback(files);
  226. } else { //递归调用upload函数
  227. uploadIdentify(api, filePaths, successUp, failUp, i, length, files, callback);
  228. }
  229. }
  230. });
  231. }
  232. //上传图片
  233. export function uploadmore(api, filePaths, successUp, failUp, i, length, files, callback) {
  234. const isToken = (config.headers || {}).isToken === false
  235. config.header = config.header || {}
  236. if (getToken() && !isToken) {
  237. config.header['Authorization'] = 'Bearer ' + getToken()
  238. config.header['clientid']=clientid;
  239. }
  240. // get请求映射params参数
  241. if (config.params) {
  242. let url = config.url + '?' + tansParams(config.params)
  243. url = url.slice(0, -1)
  244. config.url = url
  245. }
  246. uni.showLoading({
  247. title: '上传中'
  248. })
  249. var failfile = [];
  250. uni.uploadFile({
  251. timeout: config.timeout || timeout,
  252. url: baseUrl + api, //仅为示例,非真实的接口地址
  253. filePath: filePaths[i],
  254. name: 'file',
  255. header: config.header,
  256. formData: config.formData,
  257. success: function(resp) {
  258. uni.hideLoading();
  259. let result = JSON.parse(resp.data)
  260. const code = result.code || 200
  261. const msg = errorCode[code] || result.msg || errorCode['default']
  262. console.log(result.data,8)
  263. if (result.code == 200) {
  264. successUp++;
  265. files[i] = result.data.fileName;
  266. } else if(result.code==401) {
  267. showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => {
  268. if (res.confirm) {
  269. store.dispatch('LogOut').then(res => {
  270. uni.reLaunch({ url: '/pages/login/login' })
  271. })
  272. }
  273. })
  274. callback('无效的会话,或者会话已过期,请重新登录。');
  275. }else{
  276. failfile = failfile.concat(filePaths[i])
  277. failUp++;
  278. }
  279. },
  280. fail: function(res) {
  281. uni.hideLoading();
  282. failfile = failfile.concat(filePaths[i])
  283. failUp++;
  284. },
  285. complete: function(rsp) {
  286. uni.hideLoading();
  287. i++;
  288. if (i == length) {
  289. uni.showToast({
  290. title: '上传成功',
  291. icon: 'none',
  292. duration: 2000
  293. });
  294. callback(files);
  295. } else { //递归调用upload函数
  296. uploadmore(api, filePaths, successUp, failUp, i, length, files, callback);
  297. }
  298. }
  299. });
  300. }