index.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. } else {
  205. that.dleeteDealWith();
  206. // 需要返回到父组件中 userImage
  207. that.triggerEvent("imageChange", {
  208. fileList
  209. })
  210. }
  211. }).catch(err => {
  212. console.log(err)
  213. })
  214. }
  215. })
  216. },
  217. /* 删除文件 */
  218. imagesDelete(e) {
  219. const that = this;
  220. wx.showModal({
  221. title: '提示',
  222. content: '删除图片不可恢复,是否继续',
  223. success: function (res) {
  224. if (res.confirm) {
  225. const {
  226. index
  227. } = e.detail;
  228. that.dleeteDealWith(index);
  229. }
  230. }
  231. })
  232. },
  233. /* 处理删除 */
  234. dleeteDealWith(index) {
  235. const that = this;
  236. let ownertable = '';
  237. if (that.data.upType == 'Logo') {
  238. //品牌logo
  239. ownertable = "tagents"
  240. } else if (that.data.upType == 'userImage') {
  241. //用户头像
  242. ownertable = "tenterprise_users"
  243. };
  244. let content = {}
  245. if (that.data.upType != "userImage") {
  246. //图片在本页面
  247. content = {
  248. "ownertable": ownertable,
  249. "ownerid": that.data.fileList[index].ownerid,
  250. "tattachmentid": that.data.fileList[index].tattachmentid
  251. }
  252. } else {
  253. //图片在父组件
  254. content = {
  255. "ownertable": ownertable,
  256. "ownerid": that.data.attinfos.ownerid,
  257. "tattachmentid": that.data.attinfos.tattachmentid
  258. }
  259. }
  260. _Http.basic({
  261. "accesstoken": wx.getStorageSync('userData').token,
  262. "classname": "system.system.docManage",
  263. "method": "deleteDoc",
  264. "content": content
  265. }).then(s => {
  266. if (s.msg != '成功') return;
  267. console.log(s)
  268. if (that.data.upType != "userImage") {
  269. let fileList = that.data.fileList;
  270. fileList.splice(index - 1, 1);
  271. that.setData({
  272. fileList
  273. })
  274. } else {
  275. console.log("删除成功")
  276. }
  277. })
  278. },
  279. /* 验证是否上传附件 */
  280. VerifyThere() {
  281. if (this.data.fileList.length < 1) {
  282. return false
  283. }
  284. return true
  285. }
  286. }
  287. })