index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. /* 校验文件大小 */
  108. if (file.size > 10485760) {
  109. wx.showToast({
  110. title: '文件不可超过10Mb',
  111. icon: "none",
  112. duration: 3000
  113. })
  114. return callback(false)
  115. }
  116. /* 校验文件格式 */
  117. const suffix = ['jpg', 'jpeg', 'png', 'gif', 'pdf'],
  118. index = file.url.lastIndexOf("."),
  119. ext = file.url.substr(index + 1);
  120. if (!suffix.some((value) => value == ext)) {
  121. wx.showToast({
  122. title: '错误文件格式',
  123. icon: "none",
  124. duration: 3000
  125. })
  126. return callback(false)
  127. }
  128. callback(true)
  129. },
  130. /* 上传图片 */
  131. afterRead(event) {
  132. // 初始化数据
  133. let that = this,
  134. data = this.requestType(event.detail.file);
  135. //发送请求
  136. wx.getFileSystemManager().readFile({
  137. filePath: event.detail.file.url,
  138. success: result => {
  139. //返回临时文件路径
  140. const fileData = result.data
  141. _Http.basic(data).then(res => {
  142. that.uploadFile(res, fileData)
  143. }).catch(err => {})
  144. },
  145. fail: console.error
  146. })
  147. },
  148. /* 上传成功反馈 */
  149. uploadFile(res, data) {
  150. var that = this
  151. wx.request({
  152. url: res.data.obsuploadurl,
  153. method: "PUT",
  154. data: data,
  155. header: {
  156. 'content-type': 'application/octet-stream' // 默认值
  157. },
  158. success() {
  159. _Http.basic({
  160. "accesstoken": wx.getStorageSync('userData').token,
  161. "classname": "system.system.docManage",
  162. "method": "uploadSuccess",
  163. "content": {
  164. "obsfilename": res.data.obsfilename
  165. }
  166. }).then(res => {
  167. console.log(res)
  168. if (res.msg != "成功") return;
  169. let fileList = that.data.fileList;
  170. for (let i = 0; i < res.data.length; i++) {
  171. let arr = {
  172. url: res.data[i].fobsurl,
  173. ownerid: res.data[i].ownerid,
  174. tattachmentid: res.data[i].tattachmentid,
  175. ownertable: res.data[i].ownertable,
  176. fdocument: res.data[i].fdocument,
  177. }
  178. fileList.push(arr)
  179. };
  180. // 用户头像 先删除 在修改
  181. if (that.data.upType == "userImage" && fileList.length >= 2) {
  182. that.dleeteDealWith(0);
  183. }
  184. that.setData({
  185. fileList
  186. });
  187. /* 返回数据 */
  188. that.triggerEvent("imageChange", {
  189. fileList
  190. })
  191. }).catch(err => {
  192. console.log(err)
  193. })
  194. }
  195. })
  196. },
  197. /* 请求类型 */
  198. requestType(file) {
  199. //获取文件后缀
  200. var index = file.url.lastIndexOf(".");
  201. var ext = file.url.substr(index + 1);
  202. //文件名称
  203. const timestamp = Date.parse(new Date());;
  204. //不同类型图片数据
  205. if (this.data.upType == 'Logo') {
  206. //logo上传
  207. return {
  208. "accesstoken": wx.getStorageSync('userData').token,
  209. "classname": "system.system.docManage",
  210. "method": "getFileName",
  211. "content": {
  212. "filename": timestamp,
  213. "filetype": ext,
  214. "ownertable": "tagents",
  215. "ownerid": wx.getStorageSync('userData').tagentsid,
  216. "ftype": "brandlogo"
  217. }
  218. }
  219. } else if (this.data.upType == 'userImage') {
  220. //头像上传
  221. return {
  222. "accesstoken": wx.getStorageSync('userData').token,
  223. "classname": "system.system.docManage",
  224. "method": "getFileName",
  225. "content": {
  226. "filename": timestamp,
  227. "filetype": ext,
  228. "ownertable": "tenterprise_users",
  229. "ownerid": wx.getStorageSync('userData').userid,
  230. "ftype": "headportrait"
  231. }
  232. }
  233. } else if (this.data.upType == 'productImage') {
  234. //产品图片上传
  235. return {
  236. "accesstoken": wx.getStorageSync('userData').token,
  237. "classname": "system.system.docManage",
  238. "method": "getFileName",
  239. "content": {
  240. "filename": timestamp,
  241. "filetype": ext,
  242. "ownertable": this.data.forTheTimeId,
  243. "ownerid": 0,
  244. "ftype": "default"
  245. }
  246. }
  247. } else if (this.data.upType == "SupplyAndDemand") {
  248. //供需附件上传
  249. return {
  250. "accesstoken": wx.getStorageSync('userData').token,
  251. "classname": "system.system.docManage",
  252. "method": "getFileName",
  253. "content": {
  254. "filename": timestamp,
  255. "filetype": ext,
  256. "ownertable": this.data.forTheTimeId,
  257. "ownerid": 0,
  258. "ftype": "default"
  259. }
  260. }
  261. }
  262. },
  263. /* 删除文件 */
  264. imagesDelete(e) {
  265. if (this.data.fisadministrator && this.data.upType != 'SupplyAndDemand') return;
  266. const that = this;
  267. wx.showModal({
  268. title: '提示',
  269. content: '删除图片不可恢复,是否继续',
  270. success: function (res) {
  271. if (res.confirm) {
  272. const {
  273. index
  274. } = e.detail;
  275. that.dleeteDealWith(index);
  276. }
  277. }
  278. })
  279. },
  280. /* 保存退出,修改附件位置 */
  281. saveTheChanges(obj) {
  282. const data = this.data.fileList.filter((value) => value.ownerid == 0);
  283. this.setData({
  284. ExitTheDelete: true
  285. })
  286. for (let i = 0; i < data.length; i++) {
  287. obj.tattachmentid = data[i].tattachmentid;
  288. _Http.basic({
  289. "accesstoken": wx.getStorageSync('userData').token,
  290. "classname": "system.system.docManage",
  291. "method": "changeFilesData",
  292. "content": {
  293. "files": [{
  294. "tattachmentid": data[i].tattachmentid,
  295. "fdocument": data[i].fdocument,
  296. "ownertable": obj.ownertable,
  297. "ownerid": obj.ownerid
  298. }]
  299. }
  300. }).then(res => {
  301. console.log(res)
  302. })
  303. }
  304. },
  305. /* 处理删除 */
  306. dleeteDealWith(index) {
  307. const that = this;
  308. const type = that.data.fileList[index];
  309. _Http.basic({
  310. "accesstoken": wx.getStorageSync('userData').token,
  311. "classname": "system.system.docManage",
  312. "method": "deleteDoc",
  313. "content": {
  314. "ownertable": type.ownertable,
  315. "ownerid": type.ownerid,
  316. "tattachmentid": type.tattachmentid
  317. }
  318. }).then(s => {
  319. if (s.msg != '成功') return;
  320. let fileList = that.data.fileList;
  321. fileList.splice(index, 1);
  322. that.triggerEvent("imageChange", {
  323. fileList
  324. })
  325. that.setData({
  326. fileList
  327. })
  328. console.log("删除成功")
  329. })
  330. },
  331. /* 验证是否上传附件 */
  332. VerifyThere() {
  333. if (this.data.fileList.length < 1) {
  334. return false
  335. }
  336. return true
  337. }
  338. }
  339. })