index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. import {
  2. ApiModel
  3. } from "../../utils/api";
  4. const _Http = new ApiModel();
  5. Component({
  6. /**
  7. * 组件的属性列表
  8. */
  9. properties: {
  10. /* 附件类型 */
  11. filesType: {
  12. type: String,
  13. value: "image"
  14. },
  15. /* 图片列表 */
  16. fileList: {
  17. type: Array
  18. },
  19. /* 上传类型 Logo-品牌logo userImage-用户头像 productImage-产品图片 SupplyAndDemand-供需 LogoCover-产品展示图 annunciate-公告类型*/
  20. upType: {
  21. type: String
  22. },
  23. /* 上传数量 */
  24. maxCount: {
  25. type: Number,
  26. value: 1
  27. },
  28. /* 未上传图片提示 */
  29. logoTips: {
  30. type: Boolean,
  31. value: false
  32. },
  33. /* 提示文本 */
  34. Tips: {
  35. type: String
  36. },
  37. /* 显示隐藏 */
  38. UploadShow: {
  39. type: Number,
  40. value: 2 // 1 是登录样式 2 是产品上传图片样式
  41. },
  42. /* 文本行高 */
  43. lineHeight: {
  44. type: String,
  45. value: "100rpx"
  46. },
  47. /* 返回图片数据函数 */
  48. imageChange: {
  49. type: Function
  50. },
  51. /* 图片尺寸 */
  52. previewSize: {
  53. type: String,
  54. value: "80px"
  55. },
  56. /* 是否禁用 */
  57. fisadministrator: {
  58. type: Boolean,
  59. value: false
  60. },
  61. /* 供需产品id */
  62. tsupplyanddemand: {
  63. type: Number
  64. }
  65. },
  66. /* 生命周期 */
  67. lifetimes: {
  68. attached: function () {
  69. // 图片临时 id = 数字类型的 userid + 去掉前五位的时间戳
  70. const forTheTimeId = parseInt(wx.getStorageSync('userData').userid + (Date.parse(new Date()).toString().slice(5)));
  71. this.setData({
  72. forTheTimeId
  73. })
  74. },
  75. detached: function () {
  76. // 在组件实例被从页面节点树移除时执行
  77. if (this.data.ExitTheDelete) return;
  78. const data = this.data.fileList.filter((value) => value.ownerid == 0);
  79. for (let i = 0; i < data.length; i++) {
  80. _Http.basic({
  81. "accesstoken": wx.getStorageSync('userData').token,
  82. "classname": "system.system.docManage",
  83. "method": "deleteDoc",
  84. "content": {
  85. "ownertable": data[i].ownertable,
  86. "ownerid": data[i].ownerid,
  87. "tattachmentid": data[i].tattachmentid
  88. }
  89. }).then((res) => {
  90. if (res.msg != "成功") return;
  91. console.log("附件删除成功")
  92. })
  93. };
  94. },
  95. },
  96. /**
  97. * 组件的初始数据
  98. */
  99. data: {
  100. ExitTheDelete: false,
  101. },
  102. /**
  103. * 组件的方法列表
  104. */
  105. methods: {
  106. /* 文件校验 */
  107. beforeRead(event) {
  108. const {
  109. file,
  110. callback
  111. } = event.detail;
  112. for (let i = 0; i < file.length; i++) {
  113. /* 校验文件大小 */
  114. if (file[i].size > 10485760) {
  115. wx.showToast({
  116. title: '文件体积大于10Mb',
  117. icon: "none",
  118. duration: 3000
  119. })
  120. return callback(false)
  121. }
  122. /* 校验文件格式 */
  123. if (this.data.filesType == 'image') {
  124. const suffix = ['jpg', 'jpeg', 'png', 'gif', 'pdf'],
  125. index = file[i].url.lastIndexOf("."),
  126. ext = file[i].url.substr(index + 1);
  127. if (!suffix.some((value) => value == ext)) {
  128. wx.showToast({
  129. title: '错误文件格式',
  130. icon: "none",
  131. duration: 3000
  132. })
  133. return callback(false)
  134. }
  135. }
  136. callback(true)
  137. }
  138. },
  139. /* 上传图片 */
  140. afterRead(event) {
  141. for (let i = 0; i < event.detail.file.length; i++) {
  142. // 初始化数据
  143. let that = this,
  144. data = this.requestType(event.detail.file[i]);
  145. //发送请求
  146. wx.getFileSystemManager().readFile({
  147. filePath: event.detail.file[i].url,
  148. success: result => {
  149. //返回临时文件路径
  150. const fileData = result.data
  151. _Http.basic(data).then(res => {
  152. console.log(res)
  153. if (res.msg != "成功") return wx.showToast({
  154. title: res.data,
  155. icon: "none"
  156. })
  157. that.uploadFile(res, fileData)
  158. }).catch(err => {})
  159. },
  160. fail: console.error
  161. })
  162. }
  163. },
  164. /* 上传成功反馈 */
  165. uploadFile(res, data) {
  166. var that = this
  167. wx.request({
  168. url: res.data.obsuploadurl,
  169. method: "PUT",
  170. data: data,
  171. header: {
  172. 'content-type': 'application/octet-stream' // 默认值
  173. },
  174. success() {
  175. _Http.basic({
  176. "accesstoken": wx.getStorageSync('userData').token,
  177. "classname": "system.system.docManage",
  178. "method": "uploadSuccess",
  179. "content": {
  180. "obsfilename": res.data.obsfilename
  181. }
  182. }).then(res => {
  183. console.log(res)
  184. if (res.msg != "成功") return;
  185. let fileList = that.data.fileList;
  186. for (let i = 0; i < res.data.length; i++) {
  187. let arr = {
  188. url: res.data[i].fobsurl,
  189. ownerid: res.data[i].ownerid,
  190. tattachmentid: res.data[i].tattachmentid,
  191. ownertable: res.data[i].ownertable,
  192. fdocument: res.data[i].fdocument,
  193. }
  194. fileList.push(arr)
  195. };
  196. // 用户头像 先删除 在修改
  197. if (that.data.upType == "userImage" && fileList.length >= 2) {
  198. that.dleeteDealWith(0);
  199. }
  200. that.setData({
  201. fileList
  202. });
  203. /* 返回数据 */
  204. that.triggerEvent("imageChange", {
  205. fileList
  206. })
  207. }).catch(err => {
  208. console.log(err)
  209. })
  210. }
  211. })
  212. },
  213. /* 请求类型 */
  214. requestType(file) {
  215. //获取文件后缀
  216. var index = file.url.lastIndexOf(".");
  217. var ext = file.url.substr(index + 1);
  218. //文件名称
  219. const timestamp = Date.parse(new Date());;
  220. //不同类型图片数据
  221. if (this.data.upType == 'Logo') {
  222. //logo上传
  223. return {
  224. "accesstoken": wx.getStorageSync('userData').token,
  225. "classname": "system.system.docManage",
  226. "method": "getFileName",
  227. "content": {
  228. "filename": timestamp,
  229. "filetype": ext,
  230. "ownertable": "tagents",
  231. "ownerid": wx.getStorageSync('userData').tagentsid,
  232. "ftype": "brandlogo"
  233. }
  234. }
  235. } else if (this.data.upType == 'userImage') {
  236. //头像上传
  237. return {
  238. "accesstoken": wx.getStorageSync('userData').token,
  239. "classname": "system.system.docManage",
  240. "method": "getFileName",
  241. "content": {
  242. "filename": timestamp,
  243. "filetype": ext,
  244. "ownertable": "tenterprise_users",
  245. "ownerid": wx.getStorageSync('userData').userid,
  246. "ftype": "headportrait"
  247. }
  248. }
  249. } else if (this.data.upType == 'productImage') {
  250. //产品图片上传
  251. return {
  252. "accesstoken": wx.getStorageSync('userData').token,
  253. "classname": "system.system.docManage",
  254. "method": "getFileName",
  255. "content": {
  256. "filename": timestamp,
  257. "filetype": ext,
  258. "ownertable": this.data.forTheTimeId,
  259. "ownerid": 0,
  260. "ftype": "default"
  261. }
  262. }
  263. } else if (this.data.upType == "SupplyAndDemand") {
  264. //供需附件上传
  265. return {
  266. "accesstoken": wx.getStorageSync('userData').token,
  267. "classname": "system.system.docManage",
  268. "method": "getFileName",
  269. "content": {
  270. "filename": timestamp,
  271. "filetype": ext,
  272. "ownertable": this.data.forTheTimeId,
  273. "ownerid": 0,
  274. "ftype": "default"
  275. }
  276. }
  277. } else if (this.data.upType == 'LogoCover') {
  278. //品牌展示图上传
  279. return {
  280. "accesstoken": wx.getStorageSync('userData').token,
  281. "classname": "system.system.docManage",
  282. "method": "getFileName",
  283. "content": {
  284. "filename": timestamp,
  285. "filetype": ext,
  286. "ownertable": "tagents",
  287. "ownerid": wx.getStorageSync('userData').tagentsid,
  288. "ftype": "brandcover"
  289. }
  290. }
  291. } else if (this.data.upType == 'annunciate') {
  292. let type = (this.data.filesType == 'file') ? "default" : "cover";
  293. console.log(type)
  294. //通告封面上传
  295. return {
  296. "accesstoken": wx.getStorageSync('userData').token,
  297. "classname": "system.system.docManage",
  298. "method": "getFileName",
  299. "content": {
  300. "filename": timestamp,
  301. "filetype": ext,
  302. "ownertable": this.data.forTheTimeId,
  303. "ownerid": 0,
  304. "ftype": type
  305. }
  306. }
  307. }
  308. },
  309. /* 删除文件 */
  310. imagesDelete(e) {
  311. if (this.data.fisadministrator && this.data.upType != 'SupplyAndDemand') return;
  312. const that = this;
  313. wx.showModal({
  314. title: '提示',
  315. content: '删除后不可恢复,是否继续',
  316. success: function (res) {
  317. if (res.confirm) {
  318. const {
  319. index
  320. } = e.detail;
  321. that.dleeteDealWith(index);
  322. }
  323. }
  324. })
  325. },
  326. /* 保存退出,修改附件位置 */
  327. saveTheChanges(obj) {
  328. const data = this.data.fileList.filter((value) => value.ownerid == 0);
  329. this.setData({
  330. ExitTheDelete: true
  331. })
  332. for (let i = 0; i < data.length; i++) {
  333. obj.tattachmentid = data[i].tattachmentid;
  334. _Http.basic({
  335. "accesstoken": wx.getStorageSync('userData').token,
  336. "classname": "system.system.docManage",
  337. "method": "changeFilesData",
  338. "content": {
  339. "files": [{
  340. "tattachmentid": data[i].tattachmentid,
  341. "fdocument": data[i].fdocument,
  342. "ownertable": obj.ownertable,
  343. "ownerid": obj.ownerid
  344. }]
  345. }
  346. }).then(res => {
  347. console.log("修改附件", res)
  348. })
  349. }
  350. },
  351. /* 处理删除 */
  352. dleeteDealWith(index) {
  353. const that = this;
  354. const type = that.data.fileList[index];
  355. _Http.basic({
  356. "accesstoken": wx.getStorageSync('userData').token,
  357. "classname": "system.system.docManage",
  358. "method": "deleteDoc",
  359. "content": {
  360. "ownertable": type.ownertable,
  361. "ownerid": type.ownerid,
  362. "tattachmentid": type.tattachmentid
  363. }
  364. }).then(s => {
  365. console.log(s)
  366. if (s.msg != '成功') return wx.showToast({
  367. title: "删除失败,请稍后再试!",
  368. icon: "none"
  369. })
  370. let fileList = that.data.fileList;
  371. fileList.splice(index, 1);
  372. that.triggerEvent("imageChange", {
  373. fileList
  374. })
  375. that.setData({
  376. fileList
  377. })
  378. console.log("删除成功")
  379. })
  380. },
  381. /* 验证是否上传附件 */
  382. VerifyThere() {
  383. if (this.data.fileList.length < 1) {
  384. return false
  385. }
  386. return true
  387. }
  388. }
  389. })