陶杰
2024-10-06 a7e18f3eba7a5bd2008e9aeac9cf4edcc995749a
mixin/mixin.js
@@ -3,7 +3,7 @@
} from 'vuex'
import store from "../store";
import environments from '@/environments'
// import environments from '@/environments'
const mixinsCommon = {
   computed: {
@@ -13,7 +13,7 @@
            return state.hasLogin || false
         },
         selftype: state => {
            return state.type || ''
            return (state.currentInfo || {}).type || ''
         },
         currentInfo: state => {
            return state.currentInfo || {}
@@ -23,6 +23,9 @@
         },
         cache_user: state => {
            return state.currentInfo || {}
         },
         sign: state => {
            return state.sign || {}
         },
         // currentEnId: state=>{
         //    // console.log('currentEnId',state.currentInfo.enId,state.currentInfo)
@@ -43,12 +46,24 @@
            'color': '#fff'
         },
         listApi: '',
         regUserName: '用户-' + this.getRandomName(Math.floor(Math.random() * (6 - 2) + 3)),
         regAvatarUrl: 'https://youzhen123.oss-cn-huhehaote.aliyuncs.com/WechatOwnerProperty/images/mrtx.png',
         // regUserName: '用户-' + this.getRandomName(Math.floor(Math.random() * (6 - 2) + 3)),
         // regAvatarUrl: 'https://youzhen123.oss-cn-huhehaote.aliyuncs.com/WechatOwnerProperty/images/mrtx.png',
         regUserName: '',
         regAvatarUrl: '',
         previewImgSign:false,
      }
   },
   methods: {
      copyTxt(txt) {
         uni.setClipboardData({
            data: txt, //要被复制的内容
            success: () => { //复制成功的回调函数
               uni.showToast({ //提示
                  title: '复制成功'
               })
            }
         })
      },
      onChooseavatar(e) {
         let self = this;
         let {
@@ -63,180 +78,13 @@
      onBindinput(e) {
         this.regUserName = e.detail.value; // 获取微信昵称
      },
      randomAccess(min, max) {
         return Math.floor(Math.random() * (min - max) + max)
      },
      getRandomName(NameLength) {
         let name = ""
         for (let i = 0; i < NameLength; i++) {
            let unicodeNum = ""
            unicodeNum = this.randomAccess(0x4e00, 0x9fa5).toString(16)
            name += this.decodeUnicode(unicodeNum)
         }
         return name
      },
      decodeUnicode(str) {
         //Unicode显示方式是\u4e00
         str = "\\u" + str
         str = str.replace(/\\/g, "%");
         //转换中文
         str = unescape(str);
         //将其他受影响的转换回原来
         str = str.replace(/%/g, "\\");
         return str;
      },
      async onWxCheckSubmit() {
         if (this.regUserName == '微信用户') {
            this.$message.showToast('昵称不合规,不可以叫做:微信用户哈~')
            return
         }
         //保存一下
         //先上传图片
         if (this.regAvatarUrl ===
            'https://youzhen123.oss-cn-huhehaote.aliyuncs.com/WechatOwnerProperty/images/mrtx.png') {
            var picture = this.regAvatarUrl
            //更新接口
            this.$message.showLoading()
            const {
               code
            } = await this.$http.request('post', "/api/current/user/update", {
               data: {
                  ...this.dto,
                  nickName: this.regUserName,
                  picture: picture
               }
            })
            this.$message.hideLoading()
            this.$forceUpdate()
            if (code == 0) {
               this.$refs.popup_info.close()
               this.$message.showToast('设置成功')
               this.currentInfo.picture = picture
               this.currentInfo.nickName = this.regUserName
            } else {
            }
         } else {
            this.$http.upload(this.regAvatarUrl).then(async res => {
               var picture = res.data && res.data.length > 0 && res.data[0].url || ''
               //更新接口
               this.$message.showLoading()
               const {
                  code
               } = await this.$http.request('post', "/api/current/user/update", {
                  data: {
                     ...this.currentInfo,
                     nickName: this.regUserName,
                     picture: picture
                  }
               })
               this.$message.hideLoading()
               this.$forceUpdate()
               if (code == 0) {
                  this.$refs.popup_info.close()
                  this.$message.showToast('设置成功')
                  this.currentInfo.picture = picture
                  this.currentInfo.nickName = this.regUserName
               } else {
               }
            })
         }
      handleContact(e) {
         console.log(e.detail.path)
         console.log(e.detail.query)
      },
      async getUserProfile(e) {
         this.$message.showLoading()
         const res = await wx.getUserProfile({
            desc: '用于完善会员资料',
         });
         console.log(res);
         this.$message.hideLoading()
         this.login_Wx(res.userInfo.avatarUrl, res.userInfo.nickName)
      },
      login_Wx(imgurl, nickname) {
         //获取openId
         // /wx/jscode2session
         let inviter = this.$storage.getItem('inviter') || ''
         let inviterName = this.$storage.getItem('inviterName') || ''
         //有效24小时
         if (inviter) {
            var inviterTime = this.$storage.getItem('inviterTime')
            if (new Date().getTime() > parseInt(inviterTime) + 1000 * 3600 * 24) {
               inviter = ''
               inviterName = ''
               this.$storage.removeItem('inviter')
               this.$storage.removeItem('inviterTime')
               this.$storage.removeItem('inviterName')
            }
         }
         const tmp = this;
         this.$message.showLoading()
         this.openid = this.$storage.getItem('openid');
         this.tel = this.$storage.getItem('tel');
         console.log('info,info,info,info,info,info', this.openid, this.tel);
         if (!!this.openid && !!this.tel) {
            // this.login = true;
         } else {
            // this.login = false;
            console.log('wx.login');
            wx.login({
               success: async res => {
                  if (res.code) {
                     //微信登录成功 已拿到code
                     // tmp.login = true;
                     // tmp.jsCode = res.code; //保存获取到的code
                     console.log('abcd', res.code);
                     // this.$http.request('post',`/api/login/wechat`,{data:{code:tmp.jsCode}})
                     this.$storage.setItem('jsCode', res.code);
                     const res2 = await wx.getUserInfo();
                     console.log('res', res2);
                     this.tcode = res.code || ''
                     // if (nickname == '微信用户' || true) {
                     //    //跳出弹出框,获取具体用户信息
                     //    tmp.$message.hideLoading()
                     //    this.$refs.popup_info.open()
                     // } else {
                     await this.$store.dispatch('loginwx', {
                        code: res.code,
                        imgurl: imgurl,
                        nickname: nickname,
                        inviter: inviter
                     });
                     tmp.$message.hideLoading()
                     this.$forceUpdate()
                     //延迟,如果用户昵称是微信用户的话?
                     setTimeout(() => {
                        if (tmp.currentInfo && tmp.currentInfo.id) {
                           if (tmp.currentInfo.nickName == '微信用户') {
                              tmp.$refs.popup_info && tmp.$refs.popup_info.open()
                           }
                        }
                     }, 500)
                     // }
                  } else {
                     tmp.$message.hideLoading()
                     tmp.$message.showToast('微信登录失败');
                  }
               },
               error: res => {
                  console.log('wx.login error', res);
                  tmp.$message.hideLoading()
                  tmp.$message.showToast('微信授权失败');
               }
            });
         }
      },
      previewImg(url) {
         if (url) {
@@ -255,47 +103,75 @@
         }
         return true
      },
      async refreshList(){
      async refreshList(type = 'get') {
         this.page.current = 1
         await this.getList()
         await this.getList(type)
      },
      async getList() {
      async getList(type = 'get') {
         if (this.listApi) {
            this.$message.showLoading()
            const {
               data
            } = await this.$http.request('get', this.listApi, {
            } = await this.$http.request(type, this.listApi, {
               params: {
                  ...this.query,
                  ...this.page
               },
               data: {
                  ...this.query,
                  ...this.page
               }
            })
            if (data) {
               if (this.page.current == 1) {
                  this.list = data.records || []
               if (data && Array.isArray(data)) {
                  this.list = data || []
                  this.page.total = data.length || 0
               } else {
                  //根据id去重正常
                  var ids = []
                  for (var item of this.list) {
                     ids.push(item.id)
                  }
                  for (var item of data.records) {
                     if (ids.indexOf(item.id) < 0) {
                        this.list.push(item)
                  if (this.page.current === 1) {
                     this.list = data.records || []
                  } else {
                     //根据id去重正常
                     var ids = []
                     var idsMap = {}
                     for (var item of this.list) {
                        ids.push(item.id)
                        if (item.id) {
                           idsMap[item.id] = item
                        }
                     }
                  }
                     var hasnew = false
                     for (var item of data.records) {
                        if (ids.indexOf(item.id) < 0) {
                           this.list.push(item)
                        } else {
                           //最好更新一下
                           idsMap[item.id] = {
                              ...idsMap[item.id],
                              ...item, //覆盖了
                           }
                           hasnew = true
                        }
                     }
                     if (hasnew) {
                        this.$forceUpdate()
                     }
                  }
                  this.page.total = data.total || 0
               }
               this.page.total = data.total || 0
               if (this.getList_after) {
                  this.getList_after()
               }
            }
            this.$message.hideLoading()
         }
      },
      async getMore() {
      async getMore(type = 'get') {
         if (this.page.total > this.page.current * this.page.size) {
            this.page.current += 1
            await this.getList()
            await this.getList(type)
         }
      },
      backHome() {
@@ -323,167 +199,82 @@
      // #ifdef APP || H5
      ,
      async appdownload(url, name = '') {
            await this.$message.confirm('确定下载此文件吗')
            let _this = this
            if (url) {
               // var url2 = environments.httpBaseUri + `/api/download/file?fileName=${name}&filePath=` + url
               // uni.downloadFile({
               //    url: url2,
               //    success: (res) => {
               //       if (res.statusCode === 200) {
               //          _this.$message.showToast('下载成功')
               //       } else {
               //          console.log(res)
               //          _this.$message.showToast('下载失败')
               //       }
               //    },
               //    fail: (res) => {
               //       console.log(res)
               //       _this.$message.showToast('下载失败')
               //    }
               // });
               _this.$message.showLoading()
         await this.$message.confirm('确定下载此文件吗')
         let _this = this
         if (url) {
            // var url2 = environments.httpBaseUri + `/api/download/file?fileName=${name}&filePath=` + url
            // uni.downloadFile({
            //    url: url2,
            //    success: (res) => {
            //       if (res.statusCode === 200) {
            //          _this.$message.showToast('下载成功')
            //       } else {
            //          console.log(res)
            //          _this.$message.showToast('下载失败')
            //       }
            //    },
            //    fail: (res) => {
            //       console.log(res)
            //       _this.$message.showToast('下载失败')
            //    }
            // });
            _this.$message.showLoading()
               uni.downloadFile({
                  url: url, //下载地址接口返回
                  success: (data) => {
                     _this.$message.hideLoading()
                     console.log('success', data)
                     if (data.statusCode === 200) {
                        //文件保存到本地
                        uni.saveFile({
                           tempFilePath: data.tempFilePath, //临时路径
                           success: function(res) {
                              uni.showToast({
                                 icon: 'none',
                                 mask: true,
                                 title: '文件已保存:' + res
                                    .savedFilePath, //保存路径
                                 duration: 2000,
            uni.downloadFile({
               url: url, //下载地址接口返回
               success: (data) => {
                  _this.$message.hideLoading()
                  console.log('success', data)
                  if (data.statusCode === 200) {
                     //文件保存到本地
                     uni.saveFile({
                        tempFilePath: data.tempFilePath, //临时路径
                        success: function(res) {
                           uni.showToast({
                              icon: 'none',
                              mask: true,
                              title: '文件已保存:' + res
                                 .savedFilePath, //保存路径
                              duration: 2000,
                           });
                           setTimeout(() => {
                              //打开文档查看
                              uni.openDocument({
                                 filePath: res.savedFilePath,
                                 success: function(res) {
                                    // console.log('打开文档成功');
                                 }
                              });
                              setTimeout(() => {
                                 //打开文档查看
                                 uni.openDocument({
                                    filePath: res.savedFilePath,
                                    success: function(res) {
                                       // console.log('打开文档成功');
                                    }
                                 });
                              }, 2000)
                           },
                           fail: (err) => {
                           }, 2000)
                        },
                        fail: (err) => {
                              console.log(err);
                              uni.showToast({
                                 icon: 'none',
                                 mask: true,
                                 title: '下载成功保存失败',
                              });
                           },
                        });
                     }
                  },
                  fail: (err) => {
                     _this.$message.hideLoading()
                     console.log(err);
                     uni.showToast({
                        icon: 'none',
                        mask: true,
                        title: '失败请重新下载',
                           console.log(err);
                           uni.showToast({
                              icon: 'none',
                              mask: true,
                              title: '下载成功保存失败',
                           });
                        },
                     });
                  },
               });
            }
         }
         // #endif
         // #ifdef MP
         ,
      async wxdownload(url, name = '') {
         await this.$message.confirm('是否要下载此文件')
         var url2 = environments.httpBaseUri + `/api/download/file?fileName=${name}&filePath=` + url
         let that = this
         that.$message.showLoading()
         // wx.sa
         // wx.downloadFile({
         //    url: url2,
         //    success: res => {
         //       that.$message.showToast('下载成功')
         //    },
         //    fail: res => {
         //       that.$message.showToast('下载失败')
         //    },
         //    complete: res => {
         //       that.$message.hideLoading()
         //    }
         // })
         wx.downloadFile({
            url: url,
            timeout: 120000,
            success(res) {
               that.$message.hideLoading()
               console.log('res', res)
               if (res.statusCode === 200) {
                  const filePath = res.tempFilePath
                  if (filePath.endsWith('.jpg') ||
                     filePath.endsWith('.jpeg') ||
                     filePath.endsWith('.png') ||
                     filePath.endsWith('.jpg')) {
                     wx.saveImageToPhotosAlbum({
                        filePath: filePath,
                        success: function(res) {
                           that.$message.showToast('保存图片成功')
                        },
                        fail: function(res) {
                           that.$message.showToast('打开文档失败,非支持的类型')
                        }
                     })
                  } else if (filePath.endsWith('.mp4') ||
                     filePath.endsWith('.flv') ||
                     filePath.endsWith('.avi') ||
                     filePath.endsWith('.wmv') ||
                     filePath.endsWith('.mpeg')) {
                     wx.saveVideoToPhotosAlbum({
                        filePath: filePath,
                        success: function(res) {
                           that.$message.showToast('保存视频成功')
                        },
                        fail: function(res) {
                           that.$message.showToast('打开文档失败,非支持的类型')
                        }
                     })
                  } else {
                     wx.openDocument({
                        filePath: filePath,
                        showMenu: true, //关键点
                        success: function(res) {
                           console.log('打开文档成功')
                        },
                        fail: function(res) {
                           that.$message.showToast('打开文档失败,非支持的类型')
                        }
                     })
                  }
               } else {
                  that.$message.showToast('下载失败,服务器内部错误')
               },
               fail: (err) => {
                  _this.$message.hideLoading()
               }
            },
            fail: res => {
               that.$message.hideLoading()
               that.$message.showToast('下载失败')
            },
            complete: res => {
            }
         })
                  console.log(err);
                  uni.showToast({
                     icon: 'none',
                     mask: true,
                     title: '失败请重新下载',
                  });
               },
            });
         }
      }
      // #endif
   }
}
export default mixinsCommon