index.js 12 KB

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