index.js 10 KB

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