index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. import {
  2. ApiModel
  3. } from "../../utils/api";
  4. const _Http = new ApiModel();
  5. Component({
  6. /**
  7. * 组件的属性列表
  8. */
  9. properties: {
  10. /* 图片列表 */
  11. fileList: {
  12. type: Array
  13. },
  14. /* 父页面传递图片 */
  15. attinfos: {
  16. type: Object
  17. },
  18. /* 上传类型 Logo-品牌logo userImage-用户头像 productImage-产品图片 SupplyAndDemand-供需*/
  19. upType: {
  20. type: String
  21. },
  22. /* 上传数量 */
  23. maxCount: {
  24. type: Number,
  25. value: 1
  26. },
  27. /* 未上传图片提示 */
  28. logoTips: {
  29. type: Boolean,
  30. value: false
  31. },
  32. /* 提示文本 */
  33. Tips: {
  34. type: String
  35. },
  36. /* 显示隐藏 */
  37. UploadShow: {
  38. type: Boolean,
  39. value: false
  40. },
  41. /* 文本行高 */
  42. lineHeight: {
  43. type: String,
  44. value: "100rpx"
  45. },
  46. /* 返回图片数据函数 */
  47. imageChange: {
  48. type: Function
  49. },
  50. /* 产品ID */
  51. tagents_productid: {
  52. type: String
  53. },
  54. /* 图片尺寸 */
  55. previewSize: {
  56. type: String,
  57. value: "80px"
  58. },
  59. /* 是否禁用 */
  60. fisadministrator: {
  61. type: Boolean,
  62. value: false
  63. },
  64. tsupplyanddemand: {
  65. type: Number
  66. }
  67. },
  68. /**
  69. * 组件的初始数据
  70. */
  71. data: {
  72. },
  73. /**
  74. * 组件的方法列表
  75. */
  76. methods: {
  77. /* 文件校验 */
  78. beforeRead(event) {
  79. console.log("文件校验")
  80. const {
  81. file,
  82. callback
  83. } = event.detail;
  84. /* 校验文件大小 */
  85. if (file.size > 10485760) {
  86. wx.showToast({
  87. title: '文件不可超过10Mb',
  88. icon: "none",
  89. duration: 3000
  90. })
  91. return callback(false)
  92. }
  93. /* 校验文件格式 */
  94. const suffix = ['jpg', 'jpeg', 'png', 'gif', 'pdf'],
  95. index = file.url.lastIndexOf("."),
  96. ext = file.url.substr(index + 1);
  97. if (!suffix.some((value) => value == ext)) {
  98. wx.showToast({
  99. title: '错误文件格式',
  100. icon: "none",
  101. duration: 3000
  102. })
  103. return callback(false)
  104. }
  105. callback(true)
  106. },
  107. /* 上传图片 */
  108. afterRead(event) {
  109. // 初始化数据
  110. var that = this;
  111. const {
  112. file
  113. } = event.detail;
  114. //获取文件后缀
  115. var index = file.url.lastIndexOf(".");
  116. var ext = file.url.substr(index + 1);
  117. //文件名称
  118. const timestamp = Date.parse(new Date());;
  119. let data = {};
  120. //不同类型图片数据
  121. if (this.data.upType == 'Logo') {
  122. //logo上传
  123. data = {
  124. "accesstoken": wx.getStorageSync('userData').token,
  125. "classname": "system.system.docManage",
  126. "method": "getFileName",
  127. "content": {
  128. "filename": timestamp,
  129. "filetype": ext,
  130. "ownertable": "tagents",
  131. "ownerid": wx.getStorageSync('userData').tagentsid,
  132. "ftype": "brandlogo"
  133. }
  134. }
  135. } else if (this.data.upType == 'userImage') {
  136. //头像上传
  137. data = {
  138. "accesstoken": wx.getStorageSync('userData').token,
  139. "classname": "system.system.docManage",
  140. "method": "getFileName",
  141. "content": {
  142. "filename": timestamp,
  143. "filetype": ext,
  144. "ownertable": "tenterprise_users",
  145. "ownerid": wx.getStorageSync('userData').userid,
  146. "ftype": "headportrait"
  147. }
  148. }
  149. } else if (this.data.upType == 'productImage') {
  150. //产品图片上传
  151. data = {
  152. "accesstoken": wx.getStorageSync('userData').token,
  153. "classname": "system.system.docManage",
  154. "method": "getFileName",
  155. "content": {
  156. "filename": timestamp,
  157. "filetype": ext,
  158. "ownertable": "tagents_product",
  159. "ownerid": this.data.tagents_productid,
  160. "ftype": "default"
  161. }
  162. }
  163. } else if (this.data.upType == "SupplyAndDemand") {
  164. //供需附件上传
  165. data = {
  166. "accesstoken": wx.getStorageSync('userData').token,
  167. "classname": "system.system.docManage",
  168. "method": "getFileName",
  169. "content": {
  170. "filename": timestamp,
  171. "filetype": ext,
  172. "ownertable": "tsupplyanddemand",
  173. "ownerid": this.data.tsupplyanddemand,
  174. "ftype": "default"
  175. }
  176. }
  177. }
  178. //发送请求
  179. wx.getFileSystemManager().readFile({
  180. filePath: file.url,
  181. success: result => {
  182. //返回临时文件路径
  183. const fileData = result.data
  184. _Http.basic(data).then(res => {
  185. that.uploadFile(res, fileData)
  186. }).catch(err => {})
  187. },
  188. fail: console.error
  189. })
  190. },
  191. /* 上传成功反馈 */
  192. uploadFile(res, data) {
  193. var that = this
  194. wx.request({
  195. url: res.data.obsuploadurl,
  196. method: "PUT",
  197. data: data,
  198. header: {
  199. 'content-type': 'application/octet-stream' // 默认值
  200. },
  201. success() {
  202. _Http.basic({
  203. "accesstoken": wx.getStorageSync('userData').token,
  204. "classname": "system.system.docManage",
  205. "method": "uploadSuccess",
  206. "content": {
  207. "obsfilename": res.data.obsfilename
  208. }
  209. }).then(res => {
  210. console.log(res)
  211. if (res.msg != "成功") return;
  212. let fileList = that.data.fileList;
  213. for (let i = 0; i < res.data.length; i++) {
  214. let arr = {
  215. url: res.data[i].fobsurl,
  216. ownerid: res.data[i].ownerid,
  217. tattachmentid: res.data[i].tattachmentid
  218. }
  219. fileList.push(arr)
  220. };
  221. if (that.data.upType != "userImage") {
  222. //普通返回
  223. that.setData({
  224. fileList
  225. });
  226. that.triggerEvent("imageChange", {
  227. fileList
  228. })
  229. } else {
  230. // 需要返回到父组件中 userImage
  231. if (that.data.attinfos != null) {
  232. that.dleeteDealWith();
  233. }
  234. that.triggerEvent("imageChange", {
  235. fileList
  236. })
  237. }
  238. }).catch(err => {
  239. console.log(err)
  240. })
  241. }
  242. })
  243. },
  244. /* 删除文件 */
  245. imagesDelete(e) {
  246. if (this.data.fisadministrator && this.data.upType != 'SupplyAndDemand') return;
  247. const that = this;
  248. wx.showModal({
  249. title: '提示',
  250. content: '删除图片不可恢复,是否继续',
  251. success: function (res) {
  252. if (res.confirm) {
  253. const {
  254. index
  255. } = e.detail;
  256. that.dleeteDealWith(index);
  257. }
  258. }
  259. })
  260. },
  261. /* 处理删除 */
  262. dleeteDealWith(index) {
  263. const that = this;
  264. let ownertable = '';
  265. if (that.data.upType == 'Logo') {
  266. //品牌logo
  267. ownertable = "tagents"
  268. } else if (that.data.upType == 'userImage') {
  269. //用户头像
  270. ownertable = "tenterprise_users"
  271. } else if (that.data.upType == "SupplyAndDemand") {
  272. ownertable = "tsupplyanddemand"
  273. };
  274. let content = {}
  275. if (that.data.upType != "userImage") {
  276. //图片在本页面
  277. content = {
  278. "ownertable": ownertable,
  279. "ownerid": that.data.fileList[index].ownerid,
  280. "tattachmentid": that.data.fileList[index].tattachmentid
  281. }
  282. } else {
  283. //图片在父组件
  284. content = {
  285. "ownertable": ownertable,
  286. "ownerid": that.data.attinfos.ownerid,
  287. "tattachmentid": that.data.attinfos.tattachmentid
  288. }
  289. }
  290. _Http.basic({
  291. "accesstoken": wx.getStorageSync('userData').token,
  292. "classname": "system.system.docManage",
  293. "method": "deleteDoc",
  294. "content": content
  295. }).then(s => {
  296. if (s.msg != '成功') return;
  297. if (that.data.upType != "userImage") {
  298. let fileList = that.data.fileList;
  299. fileList.splice(index - 1, 1);
  300. // 需要返回到父组件中 userImage
  301. that.triggerEvent("imageChange", {
  302. fileList
  303. })
  304. that.setData({
  305. fileList
  306. })
  307. } else {
  308. console.log("删除成功")
  309. }
  310. console.log("删除成功")
  311. })
  312. },
  313. /* 验证是否上传附件 */
  314. VerifyThere() {
  315. if (this.data.fileList.length < 1) {
  316. return false
  317. }
  318. return true
  319. }
  320. }
  321. })