upload.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import { buildDir } from '@/common/file-utils'
  2. import request from '@/common/request'
  3. import { fetchToken as ossToken } from '@/api/sys/config/oss'
  4. import { fetchToken as cosToken } from '@/api/sys/config/cos'
  5. import { fetchDetail as uploadDetail } from '@/api/sys/config/upload'
  6. import { fetchToken as qiuiuToken } from '@/api/sys/config/qiniu'
  7. /**
  8. * 获取网站基本信息
  9. */
  10. function fetchSite() {
  11. return new Promise((resolve, reject) => {
  12. uploadDetail().then(cfg => {
  13. console.log('upload', cfg)
  14. resolve(cfg)
  15. }).catch(err => {
  16. uni.showToast({
  17. title: `获取系统基础设置失败: ${err} `,
  18. icon: 'none',
  19. duration: 5000
  20. });
  21. reject()
  22. })
  23. })
  24. }
  25. /**
  26. * 文件上传入口
  27. * @param file
  28. * @param data
  29. * @returns {Promise<unknown>}
  30. */
  31. async function uploadFile(file) {
  32. // 获取上传设置
  33. let cfg = await fetchSite()
  34. const provider = cfg.provider
  35. uni.showLoading({
  36. title: '上传中..'
  37. })
  38. return new Promise((resolve, reject) => {
  39. // 本地文件上传
  40. if (provider === 'local') {
  41. request.upload('/api/common/file/upload', file).then(res => {
  42. resolve(res.data.url)
  43. uni.hideLoading()
  44. }, err => {
  45. reject(err)
  46. uni.hideLoading()
  47. })
  48. }
  49. // OSS文件上传
  50. if (provider === 'oss') {
  51. ossUpload(file).then(res => {
  52. resolve(res)
  53. uni.hideLoading()
  54. }, err => {
  55. reject(err)
  56. uni.hideLoading()
  57. })
  58. }
  59. // 七牛云文件上传
  60. if (provider === 'qiniu') {
  61. qiniuUpload(file).then(res => {
  62. resolve(res)
  63. uni.hideLoading()
  64. }, err => {
  65. reject(err)
  66. uni.hideLoading()
  67. })
  68. }
  69. // 腾讯云文件上传
  70. if (provider === 'cos') {
  71. cosUpload(file).then(res => {
  72. resolve(res)
  73. uni.hideLoading()
  74. }, err => {
  75. reject(err)
  76. uni.hideLoading()
  77. })
  78. }
  79. }).catch(err=>{
  80. uni.hideLoading()
  81. uni.showToast({
  82. title: `处理上传出现错误: ${err} `,
  83. icon: 'none',
  84. duration: 5000
  85. });
  86. })
  87. }
  88. // OSS上传
  89. function ossUpload(file) {
  90. return new Promise((resolve, reject) => {
  91. // 获得上传令牌
  92. ossToken().then(res => {
  93. const conf = res
  94. const dataObj = {
  95. 'policy': res.policy,
  96. 'OSSAccessKeyId': res.accessId,
  97. 'success_action_status': 200,
  98. 'signature': res.signature,
  99. 'key': buildDir() + getFileExt(file)
  100. }
  101. console.log('调用upload', dataObj)
  102. // 调用
  103. request.upload(conf.host, file, dataObj).then(() => {
  104. // 保存截图
  105. const url = conf.url + dataObj.key
  106. console.log('上传后的地址', url)
  107. resolve(url)
  108. })
  109. }).catch(err => {
  110. uni.showToast({
  111. title: `获取OSS令牌失败: ${err} `,
  112. icon: 'none',
  113. duration: 5000
  114. });
  115. reject(err)
  116. })
  117. })
  118. }
  119. /**
  120. * 腾讯云文件上传
  121. * @param file
  122. * @returns {Promise<unknown>}
  123. */
  124. function cosUpload(file) {
  125. return new Promise((resolve, reject) => {
  126. // 获得上传令牌
  127. cosToken().then(res => {
  128. const conf = res
  129. // 上传后文件目录
  130. const key = buildDir() + getFileExt(file)
  131. // #ifdef MP-WEIXIN
  132. var COS = require('cos-wx-sdk-v5');
  133. const cos = new COS({
  134. SecretId: res.tmpSecretId,
  135. SecretKey: res.tmpSecretKey,
  136. XCosSecurityToken: res.sessionToken
  137. })
  138. cos.postObject({
  139. Bucket: conf.bucket,
  140. Region: conf.region,
  141. Key: key,
  142. FilePath: file.path
  143. }, function(err, data) {
  144. console.log(err || data);
  145. const url = conf.url + key
  146. resolve(url)
  147. });
  148. // #endif
  149. // #ifndef MP-WEIXIN
  150. var COS = require('cos-js-sdk-v5');
  151. const cos = new COS({
  152. SecretId: res.tmpSecretId,
  153. SecretKey: res.tmpSecretKey,
  154. XCosSecurityToken: res.sessionToken
  155. })
  156. cos.putObject({
  157. Bucket: conf.bucket,
  158. Region: conf.region,
  159. Key: key,
  160. Body: file
  161. }, function(err, data) {
  162. console.log(err || data);
  163. const url = conf.url + key
  164. resolve(url)
  165. });
  166. // #endif
  167. }).catch(err => {
  168. uni.showToast({
  169. title: `获取COS上传令牌失败: ${err} `,
  170. icon: 'none',
  171. duration: 5000
  172. });
  173. reject(err)
  174. })
  175. })
  176. }
  177. /**
  178. * 七牛云文件上传
  179. * @param file
  180. * @returns {Promise<unknown>}
  181. */
  182. function qiniuUpload(file) {
  183. return new Promise((resolve, reject) => {
  184. uploadDetail().then(res => {
  185. const conf = JSON.parse(res.data)
  186. // 获得上传Token
  187. qiuiuToken().then(res => {
  188. const dataObj = {
  189. 'token': res.token,
  190. 'key': buildDir() + getFileExt(file)
  191. }
  192. request.upload(conf.endpoint, file, dataObj).then(() => {
  193. // 保存数据
  194. const url = conf.url + dataObj.key
  195. resolve(url)
  196. })
  197. }).catch(err => {
  198. uni.showToast({
  199. title: `获取七牛令牌失败: ${err} `,
  200. icon: 'none',
  201. duration: 5000
  202. });
  203. reject(err)
  204. })
  205. })
  206. })
  207. }
  208. /**
  209. * 获取文件后缀
  210. * @param file
  211. * @returns {string}
  212. */
  213. function getFileExt(file) {
  214. // 取文件名
  215. let name = file.name || file.path || file.tempFilePath
  216. // 获取文件后缀
  217. const ext = name.split('.').pop()
  218. if (ext != null && ext !== '') {
  219. return '.' + ext.toLowerCase()
  220. }
  221. return ''
  222. }
  223. module.exports = {
  224. uploadFile: uploadFile
  225. };