index.js 12 KB

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