index.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. /* 上传类型 */
  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. /**
  34. * 组件的初始数据
  35. */
  36. data: {
  37. },
  38. /**
  39. * 组件的方法列表
  40. */
  41. methods: {
  42. /* 文件校验 */
  43. beforeRead(event) {
  44. console.log("文件校验")
  45. const {
  46. file,
  47. callback
  48. } = event.detail;
  49. /* 校验文件大小 */
  50. if (file.size > 10485760) {
  51. wx.showToast({
  52. title: '文件不可超过10Mb',
  53. icon: "none",
  54. duration: 3000
  55. })
  56. return callback(false)
  57. }
  58. /* 校验文件格式 */
  59. const suffix = ['jpg', 'jpeg', 'png', 'gif', 'pdf'],
  60. index = file.url.lastIndexOf("."),
  61. ext = file.url.substr(index + 1);
  62. if (!suffix.some((value) => value == ext)) {
  63. wx.showToast({
  64. title: '错误文件格式',
  65. icon: "none",
  66. duration: 3000
  67. })
  68. return callback(false)
  69. }
  70. callback(true)
  71. },
  72. /* 上传图片 */
  73. afterRead(event) {
  74. // 初始化数据
  75. var that = this;
  76. const {
  77. file
  78. } = event.detail;
  79. //获取文件后缀
  80. var index = file.url.lastIndexOf(".");
  81. var ext = file.url.substr(index + 1);
  82. //文件名称
  83. const timestamp = Date.parse(new Date());;
  84. let data = {};
  85. //不同类型图片数据
  86. if (this.data.upType == 'Logo') {
  87. data = {
  88. "accesstoken": wx.getStorageSync('userData').token,
  89. "classname": "system.system.docManage",
  90. "method": "getFileName",
  91. "content": {
  92. "filename": timestamp,
  93. "filetype": ext,
  94. "ownertable": "tagents",
  95. "ownerid": wx.getStorageSync('userData').tagentsid,
  96. "ftype": "brandlogo"
  97. }
  98. }
  99. }
  100. //发送请求
  101. wx.getFileSystemManager().readFile({
  102. filePath: file.url,
  103. success: result => {
  104. //返回临时文件路径
  105. const fileData = result.data
  106. _Http.basic(data).then(res => {
  107. that.uploadFile(res, fileData)
  108. }).catch(err => {
  109. })
  110. },
  111. fail: console.error
  112. })
  113. },
  114. /* 上传成功反馈 */
  115. uploadFile(res, data) {
  116. var that = this
  117. wx.request({
  118. url: res.data.obsuploadurl,
  119. method: "PUT",
  120. data: data,
  121. header: {
  122. 'content-type': 'application/octet-stream' // 默认值
  123. },
  124. success() {
  125. _Http.basic({
  126. "accesstoken": wx.getStorageSync('userData').token,
  127. "classname": "system.system.docManage",
  128. "method": "uploadSuccess",
  129. "content": {
  130. "obsfilename": res.data.obsfilename
  131. }
  132. }).then(res => {
  133. console.log(res)
  134. if (res.msg != "成功") return;
  135. let fileList = that.data.fileList;
  136. for (let i = 0; i < res.data.length; i++) {
  137. let arr = {
  138. url: res.data[i].fobsurl,
  139. ownerid: res.data[i].ownerid,
  140. tattachmentid: res.data[i].tattachmentid
  141. }
  142. fileList.push(arr)
  143. };
  144. that.setData({
  145. fileList
  146. })
  147. }).catch(err => {
  148. console.log(err)
  149. })
  150. }
  151. })
  152. },
  153. /* 删除文件 */
  154. imagesDelete(e) {
  155. const that = this;
  156. wx.showModal({
  157. title: '提示',
  158. content: '删除图片不可恢复,是否继续',
  159. success: function (res) {
  160. if (res.confirm) {
  161. const {
  162. index
  163. } = e.detail;
  164. let ownertable = '';
  165. if (that.data.upType == 'Logo') {
  166. //品牌logo
  167. ownertable = "tagents"
  168. };
  169. _Http.basic({
  170. "accesstoken": wx.getStorageSync('userData').token,
  171. "classname": "system.system.docManage",
  172. "method": "deleteDoc",
  173. "content": {
  174. "ownertable": ownertable,
  175. "ownerid": that.data.fileList[index].ownerid,
  176. "tattachmentid": that.data.fileList[index].tattachmentid
  177. }
  178. }).then(s => {
  179. if (s.msg != '成功') return;
  180. console.log(s)
  181. let fileList = that.data.fileList;
  182. fileList.splice(index - 1, 1);
  183. that.setData({
  184. fileList
  185. })
  186. })
  187. } else {
  188. return
  189. }
  190. }
  191. })
  192. },
  193. /* 验证是否上传附件 */
  194. VerifyThere() {
  195. if (this.data.fileList.length < 1) {
  196. return false
  197. }
  198. return true
  199. }
  200. }
  201. })