public.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. *@des 公共接口
  3. *@author zhengjinyi
  4. *@date 2020/03/19 14:56:57
  5. *@param registerByPass
  6. */
  7. import requestUrl from '@/services/ajax.env.js'
  8. import request from '@/common/config/caimeiApi.js'
  9. import $reg from '@/common/config/common.js'
  10. /**
  11. * @新分类下的商品列表
  12. * @param:tinyTypeID 三级分类ID
  13. * @param:pageNum 页码
  14. * @param:pageSize 每页显示条数
  15. */
  16. export function searchQueryTinyType(url, params) {
  17. return new Promise(function(resolve, reject) {
  18. request.lodingGet(url, params, res => {
  19. if (res.code == 0) {
  20. resolve(res)
  21. } else {
  22. reject(res)
  23. }
  24. })
  25. })
  26. }
  27. /**
  28. *上传图片
  29. */
  30. export function uploadFileImage() {
  31. return new Promise(function(resolve, reject) {
  32. uni.chooseImage({
  33. count: 1, //默认1
  34. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  35. sourceType: ['album'], //从相册选择
  36. success: (res) => {
  37. const tempFilePaths = res.tempFilePaths
  38. wx.showLoading({ title: '上传中~' })
  39. const uploadTask = uni.uploadFile({
  40. url: requestUrl + '/formData/MultiPictareaddData',
  41. filePath: tempFilePaths[0],
  42. name: 'file',
  43. header: {
  44. 'Content-Type': 'multipart/form-data',
  45. },
  46. formData: {
  47. 'user': 'test'
  48. },
  49. success: function(res) {
  50. wx.hideLoading()
  51. resolve(res)
  52. },
  53. error: function(e) {
  54. wx.hideLoading()
  55. reject(res)
  56. }
  57. })
  58. }
  59. })
  60. })
  61. }
  62. /**
  63. *上传文件
  64. * 限制pdf,doc,docx
  65. */
  66. export function uploadFilePdfDocDocx() {
  67. return new Promise(function(resolve, reject) {
  68. wx.chooseMessageFile({
  69. count: 1,
  70. type: 'file',
  71. success(res) {
  72. // tempFilePath可以作为img标签的src属性显示图片
  73. const tempFilePaths = res.tempFiles
  74. const size = tempFilePaths[0].size //获取图片的大小,单位B
  75. const filename = res.tempFiles[0].name
  76. const newfilename = filename + ''
  77. const url = requestUrl + '/file/upload'
  78. uni.setStorageSync('fileName', filename)
  79. console.log('filename', filename)
  80. //截取
  81. let fixFile = newfilename.substr(newfilename.lastIndexOf('.'))
  82. console.log(fixFile)
  83. //统一转成小写
  84. let lowFixFile = fixFile.toLowerCase()
  85. if (lowFixFile != '.pdf' && lowFixFile != '.doc' && lowFixFile != '.docx') { //限制了文件类型
  86. uni.showToast({
  87. title: '文件格式不正确!',
  88. icon: 'none',
  89. mask: true,
  90. duration: 3000
  91. })
  92. return
  93. }
  94. if (size > 51200000) { //限制了文件的大小50M
  95. uni.showToast({
  96. title: '文件大小不能超过50M',
  97. icon: 'none',
  98. mask: true,
  99. duration: 3000
  100. })
  101. return
  102. }
  103. wx.showLoading({ title: '上传中~' })
  104. wx.uploadFile({
  105. url: url,
  106. filePath: tempFilePaths[0].path,
  107. name: 'file',
  108. header: {
  109. 'Content-Type': 'multipart/form-data',
  110. },
  111. formData: {
  112. 'user': 'test'
  113. },
  114. success: function(res) {
  115. wx.hideLoading()
  116. resolve(res)
  117. },
  118. error: function(err) {
  119. wx.hideLoading()
  120. reject(err)
  121. }
  122. })
  123. }
  124. })
  125. })
  126. }
  127. /**
  128. *上传文件,仅限制PDF文件格式
  129. */
  130. export function uploadFilePdf() {
  131. return new Promise(function(resolve, reject) {
  132. wx.chooseMessageFile({
  133. count: 1,
  134. type: 'file',
  135. success(res) {
  136. // tempFilePath可以作为img标签的src属性显示图片
  137. const tempFilePaths = res.tempFiles
  138. const size = tempFilePaths[0].size //获取图片的大小,单位B
  139. const filename = res.tempFiles[0].name
  140. const newfilename = filename + ''
  141. const url = requestUrl + '/file/upload'
  142. uni.setStorageSync('fileName', filename)
  143. console.log('filename', filename)
  144. //截取
  145. let fixFile = newfilename.substr(newfilename.lastIndexOf('.'))
  146. console.log(fixFile)
  147. //统一转成小写
  148. let lowFixFile = fixFile.toLowerCase()
  149. if (lowFixFile != '.pdf') { //限制了文件类型
  150. uni.showToast({
  151. title: '文件必须为".pdf"格式!',
  152. icon: 'none',
  153. mask: true,
  154. duration: 3000
  155. })
  156. return
  157. }
  158. if (size > 20480000) { //限制了文件的大小20MB
  159. uni.showToast({
  160. title: '文件大小不能超过20M',
  161. icon: 'none',
  162. mask: true,
  163. duration: 3000
  164. })
  165. return
  166. }
  167. wx.showLoading({ title: '上传中~' })
  168. wx.uploadFile({
  169. url: url,
  170. filePath: tempFilePaths[0].path,
  171. name: 'file',
  172. header: {
  173. 'Content-Type': 'multipart/form-data',
  174. },
  175. formData: {
  176. 'user': 'test'
  177. },
  178. success: function(res) {
  179. wx.hideLoading()
  180. resolve(res)
  181. },
  182. error: function(err) {
  183. wx.hideLoading()
  184. reject(err)
  185. }
  186. })
  187. }
  188. })
  189. })
  190. }