util.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import api from '@/common/http/'
  2. import {
  3. axios
  4. } from '@/common/http/'
  5. import store from '@/store/index.js'
  6. // 获取用户信息
  7. export const getUserInfo = () => {
  8. return new Promise((resolve, reject) => {
  9. api.get('/wechat/user/info').then((response) => {
  10. const {
  11. data
  12. } = response;
  13. uni.setStorageSync('recycle_mobile_user', data);
  14. resolve(data);
  15. })
  16. .catch((error) => {
  17. reject(error);
  18. });
  19. });
  20. }
  21. const getUUID = function() {
  22. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
  23. return (c === 'x' ? (Math.random() * 16) | 0 : 'r&0x3' | '0x8').toString(16)
  24. })
  25. }
  26. const createName = function(name) {
  27. const date = Date.now()
  28. const uuid = getUUID()
  29. const fileSuffix = name.substring(name.lastIndexOf('.') + 1)
  30. return `${date}${uuid}.${fileSuffix}`
  31. }
  32. // 图片上传
  33. export const uploadImg = async function(file) {
  34. uni.showLoading({
  35. mask: true,
  36. })
  37. //获取阿里oss上传参数
  38. const par = await api.get('/common/oss/config')
  39. .then(res => {
  40. return res.data
  41. }).catch(err => {
  42. uni.hideLoading();
  43. })
  44. const fileKey = par.dir + createName(file.name);
  45. return new Promise((resolve, reject) => {
  46. const uploadTask = uni.uploadFile({
  47. url: par.host,
  48. // header: {
  49. // "Content-Type": 'multipart/form-data',
  50. // },
  51. name: 'file',
  52. formData: {
  53. ...par,
  54. name: file.name,
  55. key: fileKey
  56. },
  57. filePath: file.path,
  58. success(res) {
  59. resolve({
  60. data: {
  61. name: file.name,
  62. url: fileKey
  63. }
  64. })
  65. },
  66. fail(err) {
  67. reject(err)
  68. },
  69. complete(res) {
  70. uni.hideLoading();
  71. }
  72. })
  73. uploadTask.onProgressUpdate((res) => {
  74. console.log('上传进度' + res.progress);
  75. uni.showLoading({
  76. title: `已上传${res.progress}%`
  77. });
  78. // console.log('已经上传的数据长度' + res.totalBytesSent);
  79. // console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
  80. // return res.progress
  81. // 测试条件,取消上传任务。
  82. if (res.progress == 100) {
  83. // uploadTask.abort();
  84. uni.hideLoading();
  85. }
  86. });
  87. })
  88. }
  89. export const getArea = function(str) {
  90. let area = {}
  91. let index11 = 0
  92. let index1 = str.indexOf("省")
  93. if (index1 == -1) {
  94. index11 = str.indexOf("自治区")
  95. if (index11 != -1) {
  96. area.Province = str.substring(0, index11 + 3)
  97. } else {
  98. area.Province = str.substring(0, 0)
  99. }
  100. } else {
  101. area.Province = str.substring(0, index1 + 1)
  102. }
  103. let index2 = str.indexOf("市")
  104. if (index11 == -1) {
  105. area.City = str.substring(index11 + 1, index2 + 1)
  106. } else {
  107. if (index11 == 0) {
  108. area.City = str.substring(index1 + 1, index2 + 1)
  109. } else {
  110. area.City = str.substring(index11 + 3, index2 + 1)
  111. }
  112. }
  113. let index3 = str.lastIndexOf("区")
  114. if (index3 == -1) {
  115. index3 = str.indexOf("县")
  116. area.Country = str.substring(index2 + 1, index3 + 1)
  117. } else {
  118. area.Country = str.substring(index2 + 1, index3 + 1)
  119. }
  120. return area;
  121. }
  122. // 图片上传
  123. export const uploadImgs = async function(file) {
  124. uni.showLoading({
  125. title: '上传中',
  126. mask: true,
  127. });
  128. // 获取oss配置
  129. const par = await axios({
  130. url: '/common/oss/config',
  131. method: 'get',
  132. })
  133. .then((res) => {
  134. return res.data;
  135. })
  136. .catch((err) => {
  137. uni.hideLoading();
  138. });
  139. const fileKey = par.dir + createName(file.name);
  140. return new Promise((resolve, reject) => {
  141. uni.uploadFile({
  142. url: par.host,
  143. // header: {
  144. // "Content-Type": 'multipart/form-data',
  145. // },
  146. name: 'file',
  147. formData: {
  148. ...par,
  149. name: file.name,
  150. key: fileKey,
  151. },
  152. filePath: file.path,
  153. success(res) {
  154. resolve({
  155. url: fileKey,
  156. });
  157. },
  158. fail(err) {
  159. reject(err);
  160. },
  161. complete(res) {
  162. uni.hideLoading();
  163. },
  164. });
  165. });
  166. };
  167. export default {
  168. getUserInfo,
  169. uploadImg,
  170. getArea,
  171. uploadImgs
  172. };