index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. // 图片临时 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. } else if (this.data.upType == 'LogoCover') {
  271. //品牌展示图上传
  272. return {
  273. "accesstoken": wx.getStorageSync('userData').token,
  274. "classname": "system.system.docManage",
  275. "method": "getFileName",
  276. "content": {
  277. "filename": timestamp,
  278. "filetype": ext,
  279. "ownertable": "tagents",
  280. "ownerid": wx.getStorageSync('userData').tagentsid,
  281. "ftype": "brandcover"
  282. }
  283. }
  284. }
  285. },
  286. /* 删除文件 */
  287. imagesDelete(e) {
  288. if (this.data.fisadministrator && this.data.upType != 'SupplyAndDemand') return;
  289. const that = this;
  290. wx.showModal({
  291. title: '提示',
  292. content: '删除图片不可恢复,是否继续',
  293. success: function (res) {
  294. if (res.confirm) {
  295. const {
  296. index
  297. } = e.detail;
  298. that.dleeteDealWith(index);
  299. }
  300. }
  301. })
  302. },
  303. /* 保存退出,修改附件位置 */
  304. saveTheChanges(obj) {
  305. const data = this.data.fileList.filter((value) => value.ownerid == 0);
  306. this.setData({
  307. ExitTheDelete: true
  308. })
  309. for (let i = 0; i < data.length; i++) {
  310. obj.tattachmentid = data[i].tattachmentid;
  311. _Http.basic({
  312. "accesstoken": wx.getStorageSync('userData').token,
  313. "classname": "system.system.docManage",
  314. "method": "changeFilesData",
  315. "content": {
  316. "files": [{
  317. "tattachmentid": data[i].tattachmentid,
  318. "fdocument": data[i].fdocument,
  319. "ownertable": obj.ownertable,
  320. "ownerid": obj.ownerid
  321. }]
  322. }
  323. }).then(res => {
  324. console.log(res)
  325. })
  326. }
  327. },
  328. /* 处理删除 */
  329. dleeteDealWith(index) {
  330. const that = this;
  331. const type = that.data.fileList[index];
  332. _Http.basic({
  333. "accesstoken": wx.getStorageSync('userData').token,
  334. "classname": "system.system.docManage",
  335. "method": "deleteDoc",
  336. "content": {
  337. "ownertable": type.ownertable,
  338. "ownerid": type.ownerid,
  339. "tattachmentid": type.tattachmentid
  340. }
  341. }).then(s => {
  342. if (s.msg != '成功') return;
  343. let fileList = that.data.fileList;
  344. fileList.splice(index, 1);
  345. that.triggerEvent("imageChange", {
  346. fileList
  347. })
  348. that.setData({
  349. fileList
  350. })
  351. console.log("删除成功")
  352. })
  353. },
  354. /* 验证是否上传附件 */
  355. VerifyThere() {
  356. if (this.data.fileList.length < 1) {
  357. return false
  358. }
  359. return true
  360. }
  361. }
  362. })