index.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. attinfos: {
  16. type: Object
  17. },
  18. /* 上传类型 Logo-品牌logo userImage-用户头像 */
  19. upType: {
  20. type: String
  21. },
  22. /* 上传数量 */
  23. maxCount: {
  24. type: Number,
  25. value: 1
  26. },
  27. /* 未上传图片提示 */
  28. logoTips: {
  29. type: Boolean,
  30. value: false
  31. },
  32. /* 提示文本 */
  33. Tips: {
  34. type: String
  35. },
  36. /* 显示隐藏 */
  37. UploadShow: {
  38. type: Boolean,
  39. value: false
  40. },
  41. /* 文本行高 */
  42. lineHeight: {
  43. type: String,
  44. value: "100rpx"
  45. },
  46. /* 返回图片数据函数 */
  47. imageChange: {
  48. type: Function
  49. }
  50. },
  51. /**
  52. * 组件的初始数据
  53. */
  54. data: {
  55. },
  56. /**
  57. * 组件的方法列表
  58. */
  59. methods: {
  60. /* 文件校验 */
  61. beforeRead(event) {
  62. console.log("文件校验")
  63. const {
  64. file,
  65. callback
  66. } = event.detail;
  67. /* 校验文件大小 */
  68. if (file.size > 10485760) {
  69. wx.showToast({
  70. title: '文件不可超过10Mb',
  71. icon: "none",
  72. duration: 3000
  73. })
  74. return callback(false)
  75. }
  76. /* 校验文件格式 */
  77. const suffix = ['jpg', 'jpeg', 'png', 'gif', 'pdf'],
  78. index = file.url.lastIndexOf("."),
  79. ext = file.url.substr(index + 1);
  80. if (!suffix.some((value) => value == ext)) {
  81. wx.showToast({
  82. title: '错误文件格式',
  83. icon: "none",
  84. duration: 3000
  85. })
  86. return callback(false)
  87. }
  88. callback(true)
  89. },
  90. /* 上传图片 */
  91. afterRead(event) {
  92. // 初始化数据
  93. var that = this;
  94. const {
  95. file
  96. } = event.detail;
  97. //获取文件后缀
  98. var index = file.url.lastIndexOf(".");
  99. var ext = file.url.substr(index + 1);
  100. //文件名称
  101. const timestamp = Date.parse(new Date());;
  102. let data = {};
  103. //不同类型图片数据
  104. if (this.data.upType == 'Logo') {
  105. //logo上传
  106. data = {
  107. "accesstoken": wx.getStorageSync('userData').token,
  108. "classname": "system.system.docManage",
  109. "method": "getFileName",
  110. "content": {
  111. "filename": timestamp,
  112. "filetype": ext,
  113. "ownertable": "tagents",
  114. "ownerid": wx.getStorageSync('userData').tagentsid,
  115. "ftype": "brandlogo"
  116. }
  117. }
  118. } else if (this.data.upType == 'userImage') {
  119. //头像上传
  120. data = {
  121. "accesstoken": wx.getStorageSync('userData').token,
  122. "classname": "system.system.docManage",
  123. "method": "getFileName",
  124. "content": {
  125. "filename": timestamp,
  126. "filetype": ext,
  127. "ownertable": "tenterprise_users",
  128. "ownerid": wx.getStorageSync('userData').userid,
  129. "ftype": "headportrait"
  130. }
  131. }
  132. }
  133. //发送请求
  134. wx.getFileSystemManager().readFile({
  135. filePath: file.url,
  136. success: result => {
  137. //返回临时文件路径
  138. const fileData = result.data
  139. _Http.basic(data).then(res => {
  140. that.uploadFile(res, fileData)
  141. }).catch(err => {})
  142. },
  143. fail: console.error
  144. })
  145. },
  146. /* 上传成功反馈 */
  147. uploadFile(res, data) {
  148. var that = this
  149. wx.request({
  150. url: res.data.obsuploadurl,
  151. method: "PUT",
  152. data: data,
  153. header: {
  154. 'content-type': 'application/octet-stream' // 默认值
  155. },
  156. success() {
  157. _Http.basic({
  158. "accesstoken": wx.getStorageSync('userData').token,
  159. "classname": "system.system.docManage",
  160. "method": "uploadSuccess",
  161. "content": {
  162. "obsfilename": res.data.obsfilename
  163. }
  164. }).then(res => {
  165. console.log(res)
  166. if (res.msg != "成功") return;
  167. let fileList = that.data.fileList;
  168. for (let i = 0; i < res.data.length; i++) {
  169. let arr = {
  170. url: res.data[i].fobsurl,
  171. ownerid: res.data[i].ownerid,
  172. tattachmentid: res.data[i].tattachmentid
  173. }
  174. fileList.push(arr)
  175. };
  176. if (that.data.upType != "userImage") {
  177. //普通返回
  178. that.setData({
  179. fileList
  180. })
  181. } else {
  182. that.dleeteDealWith();
  183. // 需要返回到父组件中 userImage
  184. that.triggerEvent("imageChange", {
  185. fileList
  186. })
  187. }
  188. }).catch(err => {
  189. console.log(err)
  190. })
  191. }
  192. })
  193. },
  194. /* 删除文件 */
  195. imagesDelete(e) {
  196. const that = this;
  197. wx.showModal({
  198. title: '提示',
  199. content: '删除图片不可恢复,是否继续',
  200. success: function (res) {
  201. if (res.confirm) {
  202. const {
  203. index
  204. } = e.detail;
  205. that.dleeteDealWith(index);
  206. }
  207. }
  208. })
  209. },
  210. /* 处理删除 */
  211. dleeteDealWith(index) {
  212. const that = this;
  213. let ownertable = '';
  214. if (that.data.upType == 'Logo') {
  215. //品牌logo
  216. ownertable = "tagents"
  217. } else if (that.data.upType == 'userImage') {
  218. //用户头像
  219. ownertable = "tenterprise_users"
  220. };
  221. let content = {}
  222. if (that.data.upType != "userImage") {
  223. //图片在本页面
  224. content = {
  225. "ownertable": ownertable,
  226. "ownerid": that.data.fileList[index].ownerid,
  227. "tattachmentid": that.data.fileList[index].tattachmentid
  228. }
  229. } else {
  230. //图片在父组件
  231. content = {
  232. "ownertable": ownertable,
  233. "ownerid": that.data.attinfos.ownerid,
  234. "tattachmentid": that.data.attinfos.tattachmentid
  235. }
  236. }
  237. _Http.basic({
  238. "accesstoken": wx.getStorageSync('userData').token,
  239. "classname": "system.system.docManage",
  240. "method": "deleteDoc",
  241. "content": content
  242. }).then(s => {
  243. if (s.msg != '成功') return;
  244. console.log(s)
  245. if (that.data.upType != "userImage") {
  246. let fileList = that.data.fileList;
  247. fileList.splice(index - 1, 1);
  248. that.setData({
  249. fileList
  250. })
  251. } else {
  252. console.log("删除成功")
  253. }
  254. })
  255. },
  256. /* 验证是否上传附件 */
  257. VerifyThere() {
  258. if (this.data.fileList.length < 1) {
  259. return false
  260. }
  261. return true
  262. }
  263. }
  264. })