index.js 9.9 KB

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