当前位置:   article > 正文

uni-app笔记_uniapp window.open

uniapp window.open

1.uni-app打开外链

  1. // #ifdef APP-PLUS
  2.   plus.runtime.openURL(item.link);
  3. // #endif
  4. // #ifdef H5
  5.   window.open( item.link )
  6. // #endif
  7. // #ifdef MP-WEIXIN
  8. uni.navigateTo({
  9. url:"/pages/home/linkOthers?url=" + encodeURIComponent(info.news_link)
  10. })
  11. // #endif

微信小程序打开外链

  1. let url = 'https://mp.weixin.qq.com/s/QESLxUHF30Qbq-Fx_qvEHw';
  2. uni.navigateTo({
  3. url:"/pages/home/linkOthers?url=" + encodeURIComponent(url)
  4. })

linkOthers页面

  1. <template>
  2. <view>
  3. <view>
  4. <!-- url为要跳转外链的地址-->
  5. <web-view :src="url">
  6. </web-view>
  7. </view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. url:""
  15. }
  16. },
  17. onLoad(val) {
  18. this.url = decodeURIComponent(val.url);
  19. // 设置当前的title 如果外链中有的话将被覆盖
  20. if(this.isNotEmpty(val.title)){
  21. this.setTitle(val.title);
  22. }
  23. },
  24. methods: {
  25. isNotEmpty(obj) {
  26. if (typeof obj == undefined || obj == null || obj == "" || obj == "undefined" || obj.length == 0) {
  27. return false;
  28. } else {
  29. return true;
  30. }
  31. },
  32. // 设置title
  33. setTitle(title) {
  34. uni.setNavigationBarTitle({
  35. title: title
  36. })
  37. },
  38. }
  39. }
  40. </script>

2.附件预览(目前只有QQ浏览器能可以在线预览)

  1. openfjs(item){
  2. // #ifdef APP-PLUS
  3. this.itemurl=this.ImgUrl+item.filePath;
  4. uni.downloadFile({
  5. url: this.itemurl,
  6. success: function (res) {
  7. if(res.statusCode==404){
  8. uni.showToast({
  9. icon:'none',
  10. title:'文件不存在,或已经被移除!'
  11. })
  12. }else{
  13. let filePath = res.tempFilePath;
  14. uni.openDocument({
  15. filePath: filePath,
  16. showMenu: true,
  17. success: function (res) {
  18. console.log('打开文档成功');
  19. }
  20. });
  21. }
  22. }
  23. });
  24. // #endif
  25. //#ifndef APP-PLUS
  26. let browser =this.getBrowserInfo();
  27. if(browser=='QQ'){
  28. let filePath= item.filePath
  29. let index= filePath.lastIndexOf(".");
  30. //获取后缀
  31. let ext = filePath.substr(index+1);
  32. if(ext=='jpg'||ext=='png'||ext=='jpeg'){
  33. this.isimg=true;
  34. }else{
  35. this.isimg=false
  36. }
  37. this.modalName = 'RadioModal'
  38. this.itemurl=this.ImgUrl+item.filePath;
  39. }else{
  40. window.open(item.name,"_blank")
  41. }
  42. //#endif
  43. },

 判断是什么浏览器的方法

  1. getBrowserInfo(){
  2. var ua = navigator.userAgent.toLocaleLowerCase();
  3. var browserType=null;
  4. if (ua.match(/msie/) != null || ua.match(/trident/) != null) {
  5. browserType = "IE";
  6. browserVersion = ua.match(/msie ([\d.]+)/) != null ? ua.match(/msie ([\d.]+)/)[1]
  7. : ua.match(/rv:([\d.]+)/)[1];
  8. } else if (ua.match(/firefox/) != null) {
  9. browserType = "火狐";
  10. }else if (ua.match(/ubrowser/) != null) {
  11. browserType = "UC";
  12. }else if (ua.match(/opera/) != null) {
  13. browserType = "欧朋";
  14. } else if (ua.match(/bidubrowser/) != null) {
  15. browserType = "百度";
  16. }else if (ua.match(/metasr/) != null) {
  17. browserType = "搜狗";
  18. }else if (ua.match(/tencenttraveler/) != null || ua.match(/qqbrowse/) != null) {
  19. browserType = "QQ";
  20. }else if (ua.match(/maxthon/) != null) {
  21. browserType = "遨游";
  22. }else if (ua.match(/chrome/) != null) {
  23. browserType = "chrome";
  24. }else if (ua.match(/safari/) != null) {
  25. browserType = "Safari";
  26. }
  27. return browserType;
  28. },

 完整代码(根据我们项目的需求写的,如有不对,请指教)

  1. <template>
  2. <view class="neiye animated bounceInRight" :style="{padding:`${0.4 * fontSize}px ${ 0.4 *fontSize}px`}">
  3. <!-- 右上角图片 -->
  4. <view class="left_top_img_box" :style="{width:`${3.8 * fontSize}px`,height:`${1.6 * fontSize}px`}">
  5. <image src="../../assets/other/top_tu.png" alt="" :style="{width:`${3.8 * fontSize}px`,height:`${1.6 * fontSize}px`}">
  6. </view>
  7. <view class="container" :style="{'padding-top':`${ 1 * fontSize }px`}">
  8. <view class="news_title" :style="{width:`${17 * fontSize}px`,'padding-left':`${0.4 * fontSize}px`,height:`${0.8 * fontSize}px`, 'font-size':`${ 0.32 * fontSize }px`,'line-height':`${ 0.8 * fontSize }px`}">
  9. {{ info.newsTitle }}
  10. </view>
  11. <view class="news_content" :style="{padding:`${ 0.2 * fontSize }px`}">
  12. <!-- <rich-text :nodes="info.newsContent"></rich-text> -->
  13. <!-- <u-parse :content="info.newsContent" ></u-parse> -->
  14. <div v-html="info.newsContent" style="width: 50%;"></div>
  15. </view>
  16. <view class="fujian" v-if="info.fileArr&&info.fileArr.length>0" :style="{padding:`${0.2 * fontSize}px ${ 0.2 *fontSize}px `}">
  17. <!-- <view :style="{'font-size':`${ 0.2 * fontSize }px`}">附件:</view> -->
  18. <view class="fujian_box">
  19. <view class="fujiancs" :style="{'font-size':`${ 0.15 * fontSize }px`}" v-for="(item, index) in info.fileArr" :key="index" @click="openfjs( item )">
  20. <view class="fuj_box">
  21. <image :style="{width:`${0.25 * fontSize}px`,height:`${0.25 * fontSize}px`}" src="../../static/work/pdf.png" v-if="item.fileType=='pdf'"></image>
  22. <image :style="{width:`${0.25 * fontSize}px`,height:`${0.25 * fontSize}px`}" src="../../static/work/word.png" v-if="item.fileType=='doc'||item.fileType=='docx'"></image>
  23. <image :style="{width:`${0.25 * fontSize}px`,height:`${0.25* fontSize}px`}" src="../../static/work/xls.png" v-if="item.fileType=='xls'||item.fileType=='xlsx'"></image>
  24. <image :style="{width:`${0.25 * fontSize}px`,height:`${0.25 * fontSize}px`}" src="../../static/work/png.png" v-if="item.fileType=='png'"></image>
  25. </view>
  26. <view class="tittles">{{item.fileName}}</view>
  27. </view>
  28. </view>
  29. </view>
  30. </view>
  31. <image src="../../assets/other/back.png" :style="{position: 'fixed','z-index': 10000, width:`${0.92 * fontSize}px`, height:`${0.92 * fontSize}px`, right:`${0.2 * fontSize}px`,bottom:`${0.2 * fontSize}px`, }" mode="" @click="goback"></image>
  32. <view class="cu-modal" :class="modalName=='RadioModal'?'show':''" @tap="hideModal" v-if="itemurl!=''">
  33. <view class="cu-dialog" @tap.stop="" :style="{width:`${10* fontSize}px`,height:`${7 * fontSize}px`}">
  34. <image :src="itemurl" :style="{width:`${10* fontSize}px`,height:`${7 * fontSize}px`}" v-if="isimg"></image>
  35. <iframe :src="itemurl" width="100%" height="100%" border="0" v-else></iframe>
  36. </view>
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. import uParse from '@/components/u-parse/u-parse.vue'
  42. import DeptName from "../../components/common/DeptName"
  43. import TimeLine from "../../components/zzsh/TimeLine"
  44. import ListBox from "../../components/zzsh/ListBox"
  45. import { $ImgUrl } from "../../config.js"
  46. export default {
  47. components: {
  48. DeptName,TimeLine, ListBox,uParse,
  49. },
  50. onLoad( e ) {
  51. if (e.cateId && e.cateId != 'undefined' ) {
  52. this.cateId = e.cateId
  53. }
  54. this.newsId = e.newsId
  55. this.getInfo()
  56. },
  57. data() {
  58. return {
  59. info: {},
  60. cateId: "",
  61. newsId: "",
  62. ImgUrl: $ImgUrl,
  63. modalName: null,
  64. itemurl:'',
  65. isimg:false,
  66. }
  67. },
  68. computed: {
  69. deptId () {
  70. return this.$store.getters.deptId
  71. },
  72. fontSize () {
  73. return this.$store.getters.fontSize
  74. }
  75. },
  76. methods: {
  77. // 获取新闻详情
  78. async getInfo () {
  79. let { newsId, cateId } = this
  80. let data
  81. if (cateId) {
  82. data = { newsId: Number(newsId), cateId }
  83. } else {
  84. data = { newsId: Number(newsId) }
  85. }
  86. let res = await this.$axios("/lencon/dp/news/newsDetail", data )
  87. if ( res.data.code == 1 ) {
  88. this.info = res.data.obj
  89. let reg = /\/uploadImgs/g
  90. let rega = /<a/g
  91. let regs = /<img/g
  92. this.info.newsContent = this.info.newsContent.replace( reg, `${this.ImgUrl}/uploadImgs`)
  93. .replace( rega, "<a href='#' " ).replace( regs, "<img style='max-width:100%' ")
  94. }
  95. },
  96. getBrowserInfo(){
  97. var ua = navigator.userAgent.toLocaleLowerCase();
  98. var browserType=null;
  99. if (ua.match(/msie/) != null || ua.match(/trident/) != null) {
  100. browserType = "IE";
  101. browserVersion = ua.match(/msie ([\d.]+)/) != null ? ua.match(/msie ([\d.]+)/)[1] : ua.match(/rv:([\d.]+)/)[1];
  102. } else if (ua.match(/firefox/) != null) {
  103. browserType = "火狐";
  104. }else if (ua.match(/ubrowser/) != null) {
  105. browserType = "UC";
  106. }else if (ua.match(/opera/) != null) {
  107. browserType = "欧朋";
  108. } else if (ua.match(/bidubrowser/) != null) {
  109. browserType = "百度";
  110. }else if (ua.match(/metasr/) != null) {
  111. browserType = "搜狗";
  112. }else if (ua.match(/tencenttraveler/) != null || ua.match(/qqbrowse/) != null) {
  113. browserType = "QQ";
  114. }else if (ua.match(/maxthon/) != null) {
  115. browserType = "遨游";
  116. }else if (ua.match(/chrome/) != null) {
  117. browserType = "chrome";
  118. }else if (ua.match(/safari/) != null) {
  119. browserType = "Safari";
  120. }
  121. return browserType;
  122. },
  123. openfjs(item){
  124. // #ifdef APP-PLUS
  125. this.itemurl=this.ImgUrl+item.filePath;
  126. uni.downloadFile({
  127. url: this.itemurl,
  128. success: function (res) {
  129. if(res.statusCode==404){
  130. uni.showToast({
  131. icon:'none',
  132. title:'文件不存在,或已经被移除!'
  133. })
  134. }else{
  135. let filePath = res.tempFilePath;
  136. uni.openDocument({
  137. filePath: filePath,
  138. showMenu: true,
  139. success: function (res) {
  140. console.log('打开文档成功');
  141. }
  142. });
  143. }
  144. }
  145. });
  146. // #endif
  147. //#ifndef APP-PLUS
  148. let browser =this.getBrowserInfo();
  149. if(browser=='QQ'){
  150. let filePath= item.filePath
  151. let index= filePath.lastIndexOf(".");
  152. //获取后缀
  153. let ext = filePath.substr(index+1);
  154. if(ext=='jpg'||ext=='png'||ext=='jpeg'){
  155. this.isimg=true;
  156. }else{
  157. this.isimg=false
  158. }
  159. this.modalName = 'RadioModal'
  160. this.itemurl=this.ImgUrl+item.filePath;
  161. }else{
  162. window.open(item.name,"_blank")
  163. }
  164. //#endif
  165. },
  166. showModal(e) {
  167. this.modalName = e.currentTarget.dataset.target
  168. },
  169. hideModal(e) {
  170. this.modalName = null
  171. },
  172. goback () {
  173. uni.navigateBack({
  174. })
  175. },
  176. }
  177. }
  178. </script>
  179. <style lang="scss" scoped>
  180. @import "../../assets/set.scss";
  181. .neiye {
  182. width: 100vw;
  183. height: 100vh;
  184. background: url(../../assets/other/neiye_bg.jpg) 0 0 no-repeat;
  185. background-size: 100% 100%;
  186. position: relative;
  187. left: 0;
  188. top: 0;
  189. padding: 0.6rem 0.8rem;
  190. .left_top_img_box {
  191. z-index: 10;
  192. width: 148rpx;
  193. height: 62rpx;
  194. position: absolute;
  195. left: 0;
  196. top: 0;
  197. image {
  198. width: 148rpx;
  199. height: 62rpx;
  200. }
  201. }
  202. .container {
  203. width: 100%;
  204. height: 100%;
  205. background: url(../../assets/other/neiye_xq_bg.jpg) 0 0 no-repeat;
  206. background-size: 100% 100%;
  207. padding: 30rpx;
  208. overflow-y: scroll;
  209. &::-webkit-scrollbar {
  210. display: none;
  211. }
  212. .news_title {
  213. // width: 630rpx;
  214. // height: 30rpx;
  215. // line-height: 100%;
  216. text-align: center;
  217. background: linear-gradient( to right, #d30000 80%, #ffffff);
  218. //background: linear-gradient( to right, #373BAC 80%, #ffffff);
  219. color: #FFFFFF;
  220. // padding-left: 20rpx;
  221. // font-size: 13rpx;
  222. margin: 0 auto;
  223. position: relative;
  224. left: 0;
  225. top: 0;
  226. &::before {
  227. content: "";
  228. width: 3px;
  229. height: 100%;
  230. background-color: #d30000;
  231. position: absolute;
  232. left: -1%;
  233. top: 0;
  234. }
  235. }
  236. .fujian{
  237. color: #606060;
  238. font-weight: bold;
  239. display: flex;
  240. align-items: center;
  241. border-top: 1px solid #d1d1d1;
  242. // width: 60%;
  243. .fujian_box{
  244. // display: flex;
  245. // flex-wrap: wrap;
  246. width: 88%;
  247. .fujiancs{
  248. padding: 10px;
  249. border-radius: 4px;
  250. display: flex;
  251. .fuj_box{
  252. text-align: center;
  253. margin-right: 10px;
  254. }
  255. .tittles{
  256. cursor: pointer;
  257. }
  258. }
  259. .fujiances:hover{
  260. color: #C90A10;
  261. }
  262. .fujiances{
  263. cursor: pointer;
  264. border-radius: 4px;
  265. margin: 10px 10px 10px 0;
  266. .fuj_box{
  267. text-align: center;
  268. }
  269. }
  270. .fujiancs:hover{
  271. color: #C90A10;
  272. }
  273. }
  274. }
  275. .fujianimg{
  276. display: flex;
  277. flex-wrap: wrap;
  278. width: 100%;
  279. .img_ingo{
  280. width: 12%;
  281. margin: 1% 1%;
  282. image{
  283. height: 100%;
  284. width: 100%;
  285. }
  286. }
  287. }
  288. .news_content {
  289. width: 100%;
  290. display: flex;
  291. justify-content: center;
  292. // font-size: 10rpx;
  293. // margin-top: 30rpx;
  294. }
  295. }
  296. .bottomImg {
  297. position: absolute;
  298. left: 0;
  299. bottom: 0;
  300. width: 100%;
  301. height: 40rpx;
  302. z-index: 2;
  303. }
  304. }
  305. </style>

3.组装新数组对象

  1. let jsondata = res.data.data
  2. let keys=Object.keys(jsondata)
  3. for (let i = 0; i < keys.length; i++) {
  4. let savename={};
  5. let vals = keys[i];
  6. savename['name'] = vals;
  7. let valus =jsondata[vals];
  8. savename['value'] = valus;
  9. _this.PieA.series[0].data.push(savename)
  10. }

4.更换键名

  1. this.LanMu = lanMu.map(function (item) {
  2. return{
  3. "newsCateId":item.newsCateId,
  4. "name":item.newsCateName
  5. }
  6. })

5.微信小程序跳转公众号配置

1.配置微信公众平台

 2.页面增加代码配置

  1. <!-- #ifdef MP-WEIXIN -->
  2. <official-account></official-account>
  3. <!-- #endif -->

6.判断当前时间是否到达活动时间

  1. if(new Date().getTime() < new Date('2021-02-23 12:20:20').getTime()){
  2. return uni.showToast({
  3. icon:'none',
  4. title:'活动还未开始,暂不能查看排行榜!'
  5. })
  6. }

7.图片懒加载(插件)

Image组件增强,增强占位、失败图,全端兼容,(兼容H5、APP、小程序) - DCloud 插件市场

8.图片裁剪工具

图片裁剪工具 - DCloud 插件市场

9.js设置下载文件名称

  1. downFile(url, fileName){
  2. const xhr = new XMLHttpRequest();
  3. xhr.open("GET", url, true);
  4. xhr.responseType = "blob";
  5. xhr.onload = () => {
  6. if (xhr.status === 200) {
  7. let link = document.createElement("a");
  8. let body = document.querySelector("body");
  9. link.href = window.URL.createObjectURL(xhr.response);
  10. link.download = fileName;
  11. link.click();
  12. }
  13. };
  14. xhr.send();
  15. },
  16. this.downFile(openurl,val);

10.页面跳转params传参 

  1. uni.navigateTo({
  2. url:'/pages/a/a',
  3. success(res) {
  4. res.eventChannel.emit('change',{name:'张三'})
  5. console.log(res)
  6. }
  7. })
  8. //接收页面
  9. onload(){
  10. const even = this.getOpenerEventChannel()
  11. let _this = this
  12. even.on('change', function(data) {
  13. console.log(data.name)
  14. _this.idssss = data.name
  15. })
  16. },
  17. }

11.uni-app配置项

1.ios  input框弹起时页面往上挤问题

  1. "style": {
  2. //手机软键盘升起不让其将页面头部上推
  3. "app-plus": {
  4. "softinputMode": "adjustResize"
  5. }
  6. }

2.打包后报打包版本不一致 增加 compatible

  1. "app-plus" : {
  2. "compatible" : {
  3. "ignoreVersion" : true
  4. }
  5. }

3.第一次打开时提示app-plus增加

  1. "privacy" : {
  2. "prompt" : "template",
  3. "template" : {
  4. "title" : "温馨提示",
  5. "message" : "欢迎使用App,在你使用时,需要连接数据网络或者WIFI,产生的流量请咨询当地运营商。非常重视你的隐私保护和个人信息保护。在使用App服务前,请认真阅读<a href='http://lcxzcgl.hnoa.cn/h5/pagesPublic/introduce/introduce?type=1065'>《用户服务协议》</a>及<a href='http://lcxzcgl.hnoa.cn/h5/pagesPublic/introduce/introduce?type=1066'>《隐私政策》</a>,全部条款。你同意并接受全部条款后开始使用我们的服务<br/>",
  6. "buttonAccept" : "同意并继续", //继续下一步,进入首页
  7. "buttonRefuse" : "不同意" //退出下载
  8. }
  9. },

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号