index.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import BaseTool from '@/utils/tool'
  2. import Msg from '@/utils/message'
  3. export function uploadImageList(tempFilePaths) {
  4. const promiseList = [];
  5. tempFilePaths.forEach((item, index, arr) => {
  6. const promise = new Promise((resolve, reject) => {
  7. uni.uploadFile({
  8. url: BaseTool.UserUtil.getUploadUrl(), // 仅为示例,非真实的接口地址
  9. filePath: item,
  10. name: 'file',
  11. header: {
  12. Authorization: BaseTool.UserUtil.getToken(BaseTool.UserUtil.getUploadUrl())
  13. },
  14. success: uploadFileRes => {
  15. if (uploadFileRes.statusCode === 200) {
  16. resolve(JSON.parse(uploadFileRes.data).data);
  17. } else if (uploadFileRes.statusCode === 401) {
  18. uni.showModal({
  19. content: '登录已过期,请重新登录',
  20. showCancel: false,
  21. success: result => {
  22. uni.reLaunch({ url: '/pages/auth/login/index' });
  23. }
  24. });
  25. return reject(null);
  26. } else {
  27. resolve(null);
  28. }
  29. },
  30. fail: () => {
  31. resolve(null);
  32. }
  33. });
  34. });
  35. promiseList.push(promise);
  36. });
  37. return Promise.all(promiseList);
  38. }
  39. export function uploadWxFileList(tempFileList) {
  40. const promiseList = [];
  41. tempFileList.forEach((item, index, arr) => {
  42. const promise = new Promise((resolve, reject) => {
  43. uni.uploadFile({
  44. url: BaseTool.UserUtil.getUploadUrl() + '/name?fileName=' + item.name, // 仅为示例,非真实的接口地址
  45. filePath: item.path,
  46. name: 'file',
  47. header: {
  48. Authorization: BaseTool.UserUtil.getToken(BaseTool.UserUtil.getUploadUrl())
  49. },
  50. success: uploadFileRes => {
  51. if (uploadFileRes.statusCode === 200) {
  52. resolve(JSON.parse(uploadFileRes.data).data);
  53. } else if (uploadFileRes.statusCode === 401) {
  54. uni.showModal({
  55. content: '登录已过期,请重新登录',
  56. showCancel: false,
  57. success: result => {
  58. uni.reLaunch({ url: '/pages/login/login' });
  59. }
  60. });
  61. return reject(null);
  62. } else {
  63. resolve(null);
  64. }
  65. },
  66. fail: () => {
  67. resolve(null);
  68. }
  69. });
  70. });
  71. promiseList.push(promise);
  72. });
  73. return Promise.all(promiseList);
  74. }
  75. export function uploadImageOpen (params = { type: 1, count: 1 }) {
  76. let isWx = false
  77. // #ifdef MP-WEIXIN
  78. isWx = true
  79. // #endif
  80. if (params.type === 1 || !isWx) {
  81. return new Promise((resolve, reject) => {
  82. uni.chooseImage({
  83. count: params.count,
  84. success: function(chooseImageRes) {
  85. // console.log(JSON.stringify(res.tempFilePaths));
  86. // uni.showToast({ icon: "none", title: '密码与确认密码不一致111' })
  87. const tempFilePaths = chooseImageRes.tempFilePaths
  88. const choseFileList = chooseImageRes.tempFiles
  89. // 校验文件
  90. for (const choseFile of choseFileList) {
  91. const item = choseFile.name
  92. if (BaseTool.Object.isNotBlank(item) && item.length > 3) {
  93. const itemSub = item.substr(item.length - 3)
  94. if (itemSub !== 'PNG' &&
  95. itemSub !== 'png' &&
  96. itemSub !== 'peg' &&
  97. itemSub !== 'PEG' &&
  98. itemSub !== 'jpg' &&
  99. itemSub !== 'JPG' &&
  100. itemSub !== 'GIF' &&
  101. itemSub !== 'gif' &&
  102. itemSub !== 'bmp' &&
  103. itemSub !== 'BMP') {
  104. reject({ code: 501, message: '请上传图片' })
  105. return
  106. }
  107. }
  108. }
  109. Msg.loadingMustClose('正在上传')
  110. try {
  111. uploadImageList(tempFilePaths).then(fileResult => {
  112. const imageListResult = []
  113. if (fileResult && fileResult.length > 0) {
  114. fileResult.forEach(item => {
  115. if (item != null) {
  116. imageListResult.push(item)
  117. }
  118. })
  119. }
  120. resolve(imageListResult)
  121. }, (err) => {
  122. reject(err)
  123. }).catch(err => {
  124. reject(err)
  125. })
  126. .finally(function() {
  127. Msg.hideLoading()
  128. })
  129. } catch (e) {
  130. Msg.hideLoading()
  131. reject({ code: 5502, message: '发生异常', data: e })
  132. }
  133. }
  134. });
  135. })
  136. } else if (params.type === 2 && isWx) {
  137. return new Promise((resolve, reject) => {
  138. wx.chooseMessageFile({
  139. count: params.count,
  140. type: 'image',
  141. success (res) {
  142. // tempFilePath可以作为img标签的src属性显示图片
  143. const tempFilePaths = res.tempFiles
  144. for (const tempFilePath of tempFilePaths) {
  145. const fileName = tempFilePath.name
  146. if (BaseTool.Object.isNotBlank(fileName) && fileName.length > 0) {
  147. const itemSub = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase()
  148. if (itemSub !== 'PNG' &&
  149. itemSub !== 'png' &&
  150. itemSub !== 'peg' &&
  151. itemSub !== 'PEG' &&
  152. itemSub !== 'jpg' &&
  153. itemSub !== 'JPG' &&
  154. itemSub !== 'GIF' &&
  155. itemSub !== 'gif' &&
  156. itemSub !== 'bmp' &&
  157. itemSub !== 'BMP') {
  158. // eslint-disable-next-line prefer-promise-reject-errors
  159. reject({ code: 501, message: '请上传图片' })
  160. return
  161. }
  162. }
  163. }
  164. try {
  165. uploadWxFileList(tempFilePaths).then(fileResult => {
  166. const imageListResult = []
  167. if (fileResult && fileResult.length > 0) {
  168. fileResult.forEach(item => {
  169. if (item != null) {
  170. imageListResult.push(item)
  171. }
  172. })
  173. }
  174. resolve(imageListResult)
  175. }, (err) => {
  176. reject(err)
  177. }).catch(err => {
  178. reject(err)
  179. })
  180. .finally(function() {
  181. Msg.hideLoading()
  182. })
  183. } catch (e) {
  184. Msg.hideLoading()
  185. // eslint-disable-next-line prefer-promise-reject-errors
  186. reject({ code: 5502, message: '发生异常', data: e })
  187. }
  188. }
  189. })
  190. })
  191. }
  192. }