123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- import {
- ApiModel
- } from "../../utils/api";
- const _Http = new ApiModel();
- Component({
- /**
- * 组件的属性列表
- */
- properties: {
- /* 图片列表 */
- fileList: {
- type: Array
- },
- /* 父页面传递图片 */
- attinfos: {
- type: Object
- },
- /* 上传类型 Logo-品牌logo userImage-用户头像 productImage-产品图片*/
- upType: {
- type: String
- },
- /* 上传数量 */
- maxCount: {
- type: Number,
- value: 1
- },
- /* 未上传图片提示 */
- logoTips: {
- type: Boolean,
- value: false
- },
- /* 提示文本 */
- Tips: {
- type: String
- },
- /* 显示隐藏 */
- UploadShow: {
- type: Boolean,
- value: false
- },
- /* 文本行高 */
- lineHeight: {
- type: String,
- value: "100rpx"
- },
- /* 返回图片数据函数 */
- imageChange: {
- type: Function
- },
- /* 产品ID */
- tagents_productid: {
- type: String
- },
- /* 图片尺寸 */
- previewSize: {
- type: String,
- value: "80px"
- },
- /* 是否禁用 */
- fisadministrator: {
- type: Boolean,
- value: false
- }
- },
- /**
- * 组件的初始数据
- */
- data: {
- },
- /**
- * 组件的方法列表
- */
- methods: {
- /* 文件校验 */
- beforeRead(event) {
- console.log("文件校验")
- const {
- file,
- callback
- } = event.detail;
- /* 校验文件大小 */
- if (file.size > 10485760) {
- wx.showToast({
- title: '文件不可超过10Mb',
- icon: "none",
- duration: 3000
- })
- return callback(false)
- }
- /* 校验文件格式 */
- const suffix = ['jpg', 'jpeg', 'png', 'gif', 'pdf'],
- index = file.url.lastIndexOf("."),
- ext = file.url.substr(index + 1);
- if (!suffix.some((value) => value == ext)) {
- wx.showToast({
- title: '错误文件格式',
- icon: "none",
- duration: 3000
- })
- return callback(false)
- }
- callback(true)
- },
- /* 上传图片 */
- afterRead(event) {
- // 初始化数据
- var that = this;
- const {
- file
- } = event.detail;
- //获取文件后缀
- var index = file.url.lastIndexOf(".");
- var ext = file.url.substr(index + 1);
- //文件名称
- const timestamp = Date.parse(new Date());;
- let data = {};
- //不同类型图片数据
- if (this.data.upType == 'Logo') {
- //logo上传
- data = {
- "accesstoken": wx.getStorageSync('userData').token,
- "classname": "system.system.docManage",
- "method": "getFileName",
- "content": {
- "filename": timestamp,
- "filetype": ext,
- "ownertable": "tagents",
- "ownerid": wx.getStorageSync('userData').tagentsid,
- "ftype": "brandlogo"
- }
- }
- } else if (this.data.upType == 'userImage') {
- //头像上传
- data = {
- "accesstoken": wx.getStorageSync('userData').token,
- "classname": "system.system.docManage",
- "method": "getFileName",
- "content": {
- "filename": timestamp,
- "filetype": ext,
- "ownertable": "tenterprise_users",
- "ownerid": wx.getStorageSync('userData').userid,
- "ftype": "headportrait"
- }
- }
- } else if (this.data.upType == 'productImage') {
- //产品图片上传
- data = {
- "accesstoken": wx.getStorageSync('userData').token,
- "classname": "system.system.docManage",
- "method": "getFileName",
- "content": {
- "filename": timestamp,
- "filetype": ext,
- "ownertable": "tagents_product",
- "ownerid": this.data.tagents_productid,
- "ftype": "default"
- }
- }
- }
- //发送请求
- wx.getFileSystemManager().readFile({
- filePath: file.url,
- success: result => {
- //返回临时文件路径
- const fileData = result.data
- _Http.basic(data).then(res => {
- that.uploadFile(res, fileData)
- }).catch(err => {})
- },
- fail: console.error
- })
- },
- /* 上传成功反馈 */
- uploadFile(res, data) {
- var that = this
- wx.request({
- url: res.data.obsuploadurl,
- method: "PUT",
- data: data,
- header: {
- 'content-type': 'application/octet-stream' // 默认值
- },
- success() {
- _Http.basic({
- "accesstoken": wx.getStorageSync('userData').token,
- "classname": "system.system.docManage",
- "method": "uploadSuccess",
- "content": {
- "obsfilename": res.data.obsfilename
- }
- }).then(res => {
- console.log(res)
- if (res.msg != "成功") return;
- let fileList = that.data.fileList;
- for (let i = 0; i < res.data.length; i++) {
- let arr = {
- url: res.data[i].fobsurl,
- ownerid: res.data[i].ownerid,
- tattachmentid: res.data[i].tattachmentid
- }
- fileList.push(arr)
- };
- if (that.data.upType != "userImage") {
- //普通返回
- that.setData({
- fileList
- });
- that.triggerEvent("imageChange", {
- fileList
- })
- } else {
- // 需要返回到父组件中 userImage
- if (that.data.attinfos != null) {
- that.dleeteDealWith();
- }
- that.triggerEvent("imageChange", {
- fileList
- })
- }
- }).catch(err => {
- console.log(err)
- })
- }
- })
- },
- /* 删除文件 */
- imagesDelete(e) {
- if (this.data.fisadministrator) return;
- const that = this;
- wx.showModal({
- title: '提示',
- content: '删除图片不可恢复,是否继续',
- success: function (res) {
- if (res.confirm) {
- const {
- index
- } = e.detail;
- that.dleeteDealWith(index);
- }
- }
- })
- },
- /* 处理删除 */
- dleeteDealWith(index) {
- const that = this;
- let ownertable = '';
- if (that.data.upType == 'Logo') {
- //品牌logo
- ownertable = "tagents"
- } else if (that.data.upType == 'userImage') {
- //用户头像
- ownertable = "tenterprise_users"
- };
- let content = {}
- if (that.data.upType != "userImage") {
- //图片在本页面
- content = {
- "ownertable": ownertable,
- "ownerid": that.data.fileList[index].ownerid,
- "tattachmentid": that.data.fileList[index].tattachmentid
- }
- } else {
- //图片在父组件
- content = {
- "ownertable": ownertable,
- "ownerid": that.data.attinfos.ownerid,
- "tattachmentid": that.data.attinfos.tattachmentid
- }
- }
- _Http.basic({
- "accesstoken": wx.getStorageSync('userData').token,
- "classname": "system.system.docManage",
- "method": "deleteDoc",
- "content": content
- }).then(s => {
- if (s.msg != '成功') return;
- if (that.data.upType != "userImage") {
- let fileList = that.data.fileList;
- fileList.splice(index - 1, 1);
- // 需要返回到父组件中 userImage
- that.triggerEvent("imageChange", {
- fileList
- })
- that.setData({
- fileList
- })
- } else {
- console.log("删除成功")
- }
- console.log("删除成功")
- })
- },
- /* 验证是否上传附件 */
- VerifyThere() {
- if (this.data.fileList.length < 1) {
- return false
- }
- return true
- }
- }
- })
|