index.js 13 KB

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