当前位置:   article > 正文

uniapp实现常用功能_uniapp pageorientation

uniapp pageorientation

1.pages.json 

 官网地址:https://uniapp.dcloud.io/collocation/pages?id=%e9%85%8d%e7%bd%ae%e9%a1%b9%e5%88%97%e8%a1%a8

    pages.json是对uniapp进行全局配置,决定了页面文件的路径,窗口样式,原生导航栏,底部原生tabbar等,类似于微信小程序中的app.json的页面管理部分

   globalStyle - 对整个项目的全局配置 - 配置导航栏的信息

   tabBar对底部tab的配置

   pages对单个页面的配置,可以配置是否显示顶部导航栏等

pages.json - 单独配置某个页面的导航栏显示

1.设置基本导航栏

(1)基本配置

  1. pages: [{
  2. "path": "pages/tabbar/tabbar-3/tabbar-3",
  3. "style": {
  4. "titleNView": {
  5. "titleColor": "#ffffff",
  6. "titleText": "详情",
  7. "backgroundColor": "#186ff2"
  8. },
  9. "navigationStyle": "default"
  10. }
  11. }],

(2)动态修改配置

  1. onLoad(){
  2. //设置顶部导航栏的文本文字
  3. uni.setNavigationBarTitle({
  4. title: '消息'
  5. })
  6. //设置导航栏样式
  7. uni.setNavigationBarColor({
  8. frontColor: '#ffffff',
  9. backgroundColor: '#196ff2'
  10. });
  11. }

2.配置文件,设置某个文件不显示顶部导航栏

  1. {
  2. "path": "pages/addressBook/myInfoDetail", //个人通讯录详情
  3. "style": {
  4. "app-plus": {
  5. "titleNView": false
  6. }
  7. }
  8. },

3.导航栏左边部分显示文字或图标

pages的配置:

  1. {
  2. "path": "pages/noticyManage/detail", //通知管理详情
  3. "style": {
  4. "titleNView": {
  5. "titleColor": "#ffffff",
  6. "titleText": "",
  7. "backgroundColor": "#186ff2",
  8. "buttons": [{
  9. "text": "···",
  10. "fontSize": "24",
  11. "redDot": false
  12. }]
  13. }
  14. }
  15. },

页面监听触发

  1. onNavigationBarButtonTap(e) {
  2. console.log(e)
  3. if(e.text == '添加'){
  4. //跳转到添加页面
  5. uni.navigateTo({
  6. url: '/pages/showFoods/addFoods'
  7. })
  8. }
  9. },

types在官网的配置文档 - uni-app官网

可以进行自定义按钮 -

设置导航栏左边部分

下面是一个例子,基本满足普通项目需求的配置

  1. {
  2. "pages": [{ //页面路径和窗口的表现
  3. "path": "pages/component/index", //配置路径
  4. "style": {
  5. "navigationBarTitleText": "组件"
  6. }
  7. }, {
  8. "path": "pages/API/index",
  9. "style": {
  10. "navigationBarTitleText": "接口"
  11. }
  12. }, {
  13. "path": "pages/component/view/index",
  14. "style": {
  15. "navigationBarTitleText": "view"
  16. }
  17. }],
  18. "condition": { //模式配置,仅开发期间生效
  19. "current": 0, //当前激活的模式(list 的索引项)
  20. "list": [{
  21. "name": "test", //模式名称
  22. "path": "pages/component/view/index" //启动页面,必选
  23. }]
  24. },
  25. "globalStyle": { //页面的默认样式
  26. "navigationBarTextStyle": "black", //导航栏标题颜色及状态栏前景色(white/black)
  27. "navigationBarTitleText": "演示", //导航栏文字内容
  28. "navigationBarBackgroundColor": "#F8F8F8", //导航栏背景色
  29. "backgroundColor": "#F8F8F8", //下拉显示窗口背景色
  30. "usingComponents":{ //引用小程序组件
  31. "collapse-tree-item":"/components/collapse-tree-item"
  32. },
  33. "renderingMode": "seperated", // 仅微信小程序,webrtc 无法正常时尝试强制关闭同层渲染
  34. "pageOrientation": "portrait"//横屏配置,全局屏幕旋转设置(仅 APP/微信/QQ小程序),支持 auto / portrait / landscape
  35. },
  36. "tabBar": { //底部tab的表现
  37. "color": "#7A7E83",
  38. "selectedColor": "#3cc51f",
  39. "borderStyle": "black",
  40. "backgroundColor": "#ffffff",
  41. "height": "50px",
  42. "fontSize": "10px",
  43. "iconWidth": "24px",
  44. "spacing": "3px",
  45. "list": [{
  46. "pagePath": "pages/component/index",
  47. "iconPath": "static/image/icon_component.png",
  48. "selectedIconPath": "static/image/icon_component_HL.png",
  49. "text": "组件"
  50. }, {
  51. "pagePath": "pages/API/index",
  52. "iconPath": "static/image/icon_API.png",
  53. "selectedIconPath": "static/image/icon_API_HL.png",
  54. "text": "接口"
  55. }],
  56. "midButton": {
  57. "width": "80px",
  58. "height": "50px",
  59. "text": "文字",
  60. "iconPath": "static/image/midButton_iconPath.png",
  61. "iconWidth": "24px",
  62. "backgroundImage": "static/image/midButton_backgroundImage.png"
  63. }
  64. },
  65. "easycom": { //组件自动引入规则
  66. "autoscan": true, //是否自动扫描组件
  67. "custom": {//自定义扫描规则
  68. "uni-(.*)": "@/components/uni-$1.vue"
  69. }
  70. }
  71. }

2.跳转,传参与接收

  1.链接跳转

   官网 - uni-app官网

    传参 地址?参数名=参数值&参数名=参数值

 <navigator url="navigate/navigate?title=navigate" hover-class="navigator-hover"> <button type="default">跳转到新页面</button> </navigator>

   属性:open-type 可以选择跳转方式

  2.点击等事件触发跳转

  官网 - uni-app官网

  注意:跳转的路径为非tabbar的页面路径

  1. uni.navigateTo({
  2. url: 'test?id=1&name=uniapp'
  3. });

 3.接收

   在生命周期中接收

  1. onLoad: function (option) { //option为object类型,会序列化上个页面传递的参数
  2. console.log(option.id); //打印出上个页面传递的参数。
  3. console.log(option.name); //打印出上个页面传递的参数。
  4. }

3.解决跨域问题

跨域问题和vue的解决利用代理一样,只是uniapp配置的地方不同

  1. "h5" : {
  2. "optimization" : {
  3. "treeShaking" : {
  4. "enable" : false
  5. }
  6. },
  7. "devServer" : {
  8. "disableHostCheck" : true,
  9. "port" : "8081",
  10. "proxy" : {
  11. "" : {
  12. // "target" : "http://20.4.5.35", //生产环境
  13. "target" : "http://192.168.1.107:8083",
  14. // "target" : "http://211.92.134.56:8282",
  15. "changeOrigin" : true
  16. }
  17. }
  18. },
  19. "domain" : "www.test.test"
  20. }

4.利用原生实现上传附件

uniapp的上传_哆来A梦没有口袋的博客-CSDN博客

5.富文本上传视频等和回显

富文本其实uniapp也是有自己的组件,但是值得悲伤的是,uniapp自带的组件在app是不支持视频上传和解析(rich-text)的(uni-app官网),其实也有很多现有的封装好的组件不满足需求

因此这里借用了wangEdtior进行上传,利用原生代码进行解析回显

wangEditor上传视频在h5是可以的,在app需要修改源码支持,这里就友情提示,可以根据上传图片进行修改上传视频

上传的案例

  1. <template>
  2. <view class="addOrEditMsg_wagePolicy" :editorHtml="editorHtml" :change:editorHtml="wagePolicyNew.changeHtml">
  3. <view>
  4. <u-form :model="form" ref="uForm">
  5. <u-form-item label="标题" prop="title" required>
  6. <u-input v-model="form.title" placeholder="请输入标题" />
  7. </u-form-item>
  8. <u-form-item label="正文内容" prop="policy_content" required class="hasTextarea form_flex_column">
  9. <view class="editH" id="editH">
  10. <!-- <jinEdit placeholder="请输入内容并保存" ref="editFrom" @editOk="editOk" :html="initHtml"></jinEdit> -->
  11. </view>
  12. </u-form-item>
  13. <u-form-item label="附件" prop="attachment_code" class="form_flex_column">
  14. <view class="test">
  15. <view class="wrap">
  16. <view class="pre-box">
  17. <view class="pre-item" v-for="(item, index) in lists" :key="index">
  18. <view><text class="textOver">{{item.substr(item.lastIndexOf('\\') + 1)}}</text>
  19. <image src="../../static/img/icon/false.png" @click="delfoc(index)"></image>
  20. </view>
  21. </view>
  22. </view>
  23. <view class="uploadFiles">
  24. <UploadFile @uploaded='uploaded'></UploadFile>
  25. </view>
  26. </view>
  27. </view>
  28. </u-form-item>
  29. </u-form>
  30. <view class="form_button flex_a">
  31. <button @click="wagePolicyNew.submitToComp">提交</button>
  32. </view>
  33. </view>
  34. </view>
  35. </template>
  36. <script>
  37. import {
  38. Config
  39. } from '@/utils/config.js';
  40. const config = new Config();
  41. let prefix = config.getImgPdfUrl();
  42. // import jinEdit from '@/components/jin-edit/jin-edit.vue';
  43. import {
  44. UploadApi
  45. } from '@/api/upload.js';
  46. import {
  47. WagePolicyApi
  48. } from '@/api/wagePolicy.js';
  49. import UploadFile from '@/pages/commonComponents/uploadFile.vue'
  50. const uploadApi = new UploadApi();
  51. const wagePolicyApi = new WagePolicyApi();
  52. export default {
  53. components: {
  54. UploadFile // jinEdit
  55. },
  56. data() {
  57. return {
  58. showUploadList: false, //展示下载列表
  59. lists: [], //附件列表
  60. initHtml: '',
  61. editorHtml: '',
  62. fileList: [],
  63. // attachmentList: [], //附件列表
  64. form: {
  65. title: "", //标题
  66. "policy_content": "", //正文内容
  67. attachment_code: {
  68. attachment: []
  69. }, //附近
  70. sys_org_code: '', //部门id
  71. create_by: '', //创建人
  72. create_name: "", //创建人姓名
  73. },
  74. attachmentLimitType: ['zip', 'docx', 'doc', 'pdf', 'xlsx', 'xls','jpg', 'png', 'jpeg'], //文档限制类型
  75. rules: {
  76. title: [{
  77. required: true,
  78. message: '请输入标题',
  79. // 可以单个或者同时写两个触发验证方式
  80. trigger: ['change', 'blur'],
  81. }]
  82. },
  83. }
  84. },
  85. onLoad(params) {
  86. this.form.sys_org_code = this.vuex_org_code;
  87. this.form.create_by = this.vuex_userName;
  88. this.form.create_name = this.vuex_realname;
  89. this.flag = params.flag; //1为新增,2为编辑
  90. this.addOpt = params.addOpt;
  91. //新增
  92. if (params.flag == 1) {
  93. uni.setNavigationBarTitle({
  94. title: '新增'
  95. })
  96. } else {
  97. //编辑
  98. uni.setNavigationBarTitle({
  99. title: '编辑'
  100. })
  101. this.id = params.id;
  102. this.getDetail(params.id)
  103. }
  104. },
  105. mounted() {
  106. this.$refs.uForm.setRules(this.rules);
  107. },
  108. methods: {
  109. //上传显示
  110. uploaded(url) {
  111. this.lists.push(url)
  112. },
  113. //获取细节详情
  114. async getDetail(id) {
  115. try {
  116. let res = await wagePolicyApi.queryWagePolicyDetail(id);
  117. console.log(res)
  118. this.editorHtml = this.initHtml = res.policy_content.replace(/\http:.*?\upload/g, `${prefix}/upload`);
  119. this.form.policy_content = res.policy_content.replace(/\http:.*?\upload/g, `${prefix}/upload`);
  120. this.form.title = res.title;
  121. //回显pdf
  122. if (res.attachment_code) {
  123. let files = res.attachment_code.split(',')
  124. if (files.length > 0) {
  125. this.lists = files;
  126. }
  127. }
  128. } catch (err) {
  129. console.log(err)
  130. throw(e)
  131. }
  132. },
  133. //提交表单
  134. submit(html) {
  135. this.$refs.uForm.validate(async valid => {
  136. if (valid) {
  137. //对正文内容进行判断
  138. this.form.policy_content = html;
  139. if (!this.form.policy_content) {
  140. uni.showToast({
  141. title: '请输入正文内容',
  142. icon: 'none'
  143. });
  144. return
  145. }
  146. this.form.attachment_code= this.lists.join(',');
  147. this.toApproval();
  148. } else {
  149. console.log('验证失败');
  150. }
  151. });
  152. },
  153. delfoc(index) {
  154. // 删除文档
  155. this.lists.splice(index, 1);
  156. },
  157. async toApproval() {
  158. console.log('真实提交')
  159. try {
  160. let res, title
  161. if (this.flag == 1) {
  162. console.log(this.form, 'xxx');
  163. res = await wagePolicyApi.addApprovalData(this.form);
  164. title = '提交成功'
  165. } else {
  166. this.form['id'] = this.id;
  167. res = await wagePolicyApi.editApprovalData(this.form);
  168. title = '修改成功'
  169. }
  170. uni.showToast({
  171. title,
  172. duration: 2000
  173. })
  174. //跳转到列表页面
  175. setTimeout(() => {
  176. //跳转到列表页面
  177. uni.navigateTo({
  178. url: `/pages/wagePolicy/index?addOpt=${this.addOpt}`
  179. })
  180. }, 2000)
  181. } catch (e) {
  182. throw (e)
  183. }
  184. }
  185. }
  186. }
  187. </script>
  188. <script module="wagePolicyNew" lang="renderjs">
  189. import wangEditor from '@/utils/wangEditor.js';
  190. import {
  191. UploadApi
  192. } from '@/api/upload.js';
  193. const uploadApi = new UploadApi();
  194. import {
  195. Config
  196. } from '@/utils/config.js';
  197. const config = new Config();
  198. let prefix = config.getImgPdfUrl();
  199. let editor;
  200. export default {
  201. beforeDestroy(){
  202. editor.destroy();
  203. },
  204. mounted() {
  205. editor = new wangEditor('#editH');
  206. editor.config.showFullScreen = false;
  207. editor.config.showLinkImg = false
  208. editor.config.excludeMenus = [
  209. 'indent',
  210. 'code'
  211. ]
  212. //编辑设置
  213. if (this.flag !== 1) {
  214. //设置初始化
  215. editor.txt.html(this.initHtml);
  216. }
  217. editor.create();
  218. //绑定wangEditor的插入图片和视频
  219. editor.config.customUploadImg = async (resultFiles, insertImgFn) => {
  220. // resultFiles 是 input 中选中的文件列表
  221. // insertImgFn 是获取图片 url 后,插入到编辑器的方法
  222. // 上传图片,返回结果,将图片插入到编辑器中
  223. try {
  224. let res = await uploadApi.uploadFileByJquery(resultFiles[0], 1);
  225. console.log(res)
  226. insertImgFn(prefix + res)
  227. } catch (e) {
  228. // uni.showToast({
  229. // title: '上传图片失败',
  230. // icon: 'none'
  231. // });
  232. throw (e)
  233. }
  234. };
  235. //上传视频
  236. editor.config.customUploadFile = async (file, i) => {
  237. console.log(file, i);
  238. console.log(URL.createObjectURL(file[0]), 'xx')
  239. try {
  240. let res = await uploadApi.uploadFileByJquery(file[0], 2);
  241. console.log(res, 'res返回数据');
  242. // 做上传
  243. // 返回src
  244. i(editor, prefix + res)
  245. document.getElementsByClassName('w-e-panel-container')[0].style.diplay = none;
  246. } catch (e) {
  247. throw (e)
  248. }
  249. }
  250. },
  251. methods:{
  252. submitToComp() {
  253. this.$ownerInstance.callMethod('submit', editor.txt.html())
  254. },
  255. changeHtml(newValue, oldValue, ownerVm, vm) {
  256. editor.txt.html(newValue)
  257. }
  258. }
  259. }
  260. </script>
  261. <style lang="scss" scoped>
  262. @import 'uview-ui/libs/css/style.components.scss';
  263. .addOrEditMsg_wagePolicy {
  264. background: #f5f6f8;
  265. .form_flex_column /deep/ .u-form-item__body {
  266. display: flex;
  267. flex-direction: column !important;
  268. }
  269. .form_step {
  270. width: 100%;
  271. display: flex;
  272. flex-direction: row;
  273. justify-content: space-between;
  274. align-items: center;
  275. height: 150rpx;
  276. .form_step_left {
  277. display: flex;
  278. flex-direction: row;
  279. align-items: center;
  280. .form_dot {
  281. width: 15rpx;
  282. height: 15rpx;
  283. border-radius: 50%;
  284. background: #dadada;
  285. margin-right: 34rpx;
  286. }
  287. .form_text {
  288. display: flex;
  289. margin-right: 34rpx;
  290. flex-direction: column;
  291. justify-content: space-around;
  292. align-items: center;
  293. height: 68rpx;
  294. line-height: 0;
  295. text:last-child {
  296. color: #a9a9a9;
  297. }
  298. }
  299. }
  300. .form_step_right {
  301. width: 60%;
  302. height: 100%;
  303. // display: flex;
  304. flex-direction: row;
  305. // align-items: center;
  306. .form_step_right_show {
  307. padding-top: 15rpx;
  308. max-width: 80%;
  309. float: right;
  310. /deep/ .uni-scroll-view-content {
  311. display: flex !important;
  312. flex-direction: row;
  313. // justify-content: flex-end;
  314. }
  315. .form_step_right_showImg {
  316. display: flex !important;
  317. flex-direction: row;
  318. flex-wrap: nowrap;
  319. .form_step_right_icon {
  320. line-height: 75rpx;
  321. color: #999;
  322. margin: 0 10rpx;
  323. }
  324. &>view {
  325. position: relative;
  326. display: flex;
  327. flex-direction: column;
  328. align-items: center;
  329. justify-content: center;
  330. image {
  331. width: 70rpx;
  332. height: 70rpx;
  333. border-radius: 50%;
  334. }
  335. text {
  336. margin-top: -15rpx;
  337. color: #999;
  338. }
  339. .form_step_people_del {
  340. position: absolute;
  341. top: 5rpx;
  342. right: 0;
  343. width: 23rpx;
  344. height: 23rpx;
  345. font-size: 15rpx;
  346. border-radius: 50%;
  347. line-height: 15rpx;
  348. text-align: center;
  349. color: white;
  350. background: black;
  351. }
  352. }
  353. }
  354. }
  355. .form_step_right_add {
  356. float: right;
  357. margin-top: 15rpx;
  358. width: 70rpx;
  359. height: 70rpx;
  360. border-radius: 50%;
  361. margin-left: 10rpx;
  362. text-align: center;
  363. line-height: 65rpx;
  364. font-size: 45rpx;
  365. font-weight: 600;
  366. color: #01a4e8;
  367. border: 1px dashed #01a4e8;
  368. }
  369. }
  370. }
  371. .form_button {
  372. width: 100%;
  373. height: 120rpx;
  374. background: white;
  375. button {
  376. width: 480rpx;
  377. height: 85rpx;
  378. background: #0383fb;
  379. border: none !important;
  380. border-radius: 10rpx;
  381. color: white;
  382. }
  383. }
  384. .hasTextarea /deep/ .u-form-item__body {
  385. .u-form-item--right__content {
  386. height: 880rpx;
  387. }
  388. .u-form-item--left__content {
  389. flex: 0 0 138rpx;
  390. }
  391. .uni-input-placeholder {
  392. top: 0 !important;
  393. }
  394. .u-input__input {
  395. max-height: 170rpx !important;
  396. }
  397. .u-input {
  398. text-align: left !important;
  399. height: 170rpx;
  400. // margin-right: 15rpx;
  401. // text-indent: 22rpx;
  402. }
  403. }
  404. .u-form-item {
  405. margin-bottom: 14rpx;
  406. font-size: 32rpx !important;
  407. background: white;
  408. padding: 15rpx 30rpx;
  409. border: none;
  410. line-height: none;
  411. /deep/.u-form-item--left__content .u-form-item--left__content--required {
  412. color: #fa3534;
  413. left: 100% !important;
  414. }
  415. /deep/ .u-form-item--left {
  416. width: auto !important;
  417. flex: none !important;
  418. margin-right: 36rpx;
  419. }
  420. /deep/ .u-input {
  421. text-align: right !important;
  422. }
  423. // &:first-child /deep/ .uni-input-placeholder {
  424. // left: -38rpx !important;
  425. // }
  426. /deep/ .uicon-arrow-down-fill:before {
  427. position: absolute;
  428. top: -28rpx;
  429. font-size: 30rpx;
  430. content: '>' !important;
  431. }
  432. }
  433. .editH {
  434. width: 100%;
  435. height: 800rpx;
  436. max-width: 700rpx;
  437. z-index: 0;
  438. // overflow-y: scroll;
  439. /deep/ .w-e-toolbar .w-e-droplist {
  440. left: -45px !important;
  441. }
  442. }
  443. //弹出层样式
  444. .toCommomApproval_modal {
  445. .content {
  446. // padding: 0 50rpx;
  447. .content_top_button {
  448. padding: 25rpx 50rpx;
  449. border-bottom: 1rpx solid #F5F7F9;
  450. font-weight: 500;
  451. }
  452. .content_main {
  453. padding: 0 50rpx;
  454. color: rgb(96, 98, 102);
  455. .unitType {
  456. padding: 25rpx 0;
  457. border-bottom: 1rpx solid #F5F7F9;
  458. }
  459. }
  460. }
  461. }
  462. .test {
  463. width: 700rpx;
  464. .wrap {
  465. padding: 24rpx;
  466. width: 100%;
  467. .slot-btn {
  468. width: 140rpx;
  469. height: 140rpx;
  470. display: flex;
  471. justify-content: center;
  472. // align-items: center;
  473. line-height: 130rpx;
  474. font-size: 60rpx;
  475. background: rgb(244, 245, 246);
  476. border-radius: 10rpx;
  477. }
  478. .slot-btn__hover {
  479. background-color: rgb(235, 236, 238);
  480. }
  481. .pre-box {
  482. display: flex;
  483. align-items: center;
  484. justify-content: space-between;
  485. flex-wrap: wrap;
  486. }
  487. .pre-item {
  488. width: 100%;
  489. // border-radius: 10rpx;
  490. // height: 140rpx;
  491. overflow: hidden;
  492. position: relative;
  493. &>view {
  494. display: flex;
  495. flex-direction: row;
  496. justify-content: space-between;
  497. align-items: center;
  498. font-size: 30rpx;
  499. text {
  500. width: 90%;
  501. }
  502. image {
  503. width: 30rpx;
  504. height: 30rpx;
  505. }
  506. }
  507. }
  508. }
  509. }
  510. }
  511. </style>

视频图片等富文本的回显 - 同样是利用renderjs,原生解析,与上传附件类似

  1. <template>
  2. <view :id="randomID" :richHtml="richHtml" :change:richHtml="richTextSelf.changeHtml" :randomID="randomID" :change:randomID="richTextSelf.changeRandomID">
  3. </view>
  4. </template>
  5. <script>
  6. export default {
  7. name: 'rich-text-self',
  8. props:{
  9. id: '',
  10. html: '',
  11. },
  12. data() {
  13. return {
  14. randomID: '',
  15. richHtml: ''
  16. };
  17. },
  18. mounted() {
  19. this.randomID = 'richtextSelf' + parseInt(Math.random() * 10000)
  20. this.richHtml = this.html
  21. },
  22. created() {
  23. }
  24. }
  25. </script>
  26. <script module="richTextSelf" lang="renderjs">
  27. var container
  28. var html
  29. export default {
  30. mounted() {},
  31. methods: {
  32. changeHtml(newValue, oldValue, ownerVm, vm) {
  33. html = newValue
  34. var div = document.createElement("DIV");
  35. div.className = 'rich-text-self'
  36. var reg = new RegExp('<video', 'g');
  37. html = html.replace(reg, '<video style="width:100%;"')
  38. div.innerHTML = "'" + html + "'"
  39. var videos = document.getElementsByTagName('video', div)
  40. if(videos && videos.length > 0){
  41. videos.forEach(video => {
  42. video.style.width = '100%'
  43. })
  44. }
  45. var regImg = new RegExp('<img', 'g');
  46. html = html.replace(regImg, '<img style="width:100%;"')
  47. div.innerHTML = "'" + html + "'"
  48. // var videos = document.getElementsByTagName('video', div)
  49. // if(videos && videos.length > 0){
  50. // videos.forEach(video => {
  51. // video.style.width = '100%'
  52. // })
  53. // }
  54. container.appendChild(div)
  55. },
  56. changeRandomID(newValue, oldValue, ownerVm, vm) {
  57. container = document.getElementById(newValue)
  58. }
  59. }
  60. }
  61. </script>

6.实现下拉刷新

pages里面配置

  1. {
  2. "path": "pages/commonComponents/DynamicForm/index", //公共表单首页
  3. "style": {
  4. "enablePullDownRefresh": true, //可以下来刷新
  5. "titleNView": {
  6. "titleColor": "#ffffff",
  7. "titleText": "",
  8. "backgroundColor": "#186ff2"
  9. },
  10. "navigationStyle": "default",
  11. "pullToRefresh": { //下来刷新样式
  12. "support": true,
  13. "style": "default",
  14. "offset": "70px",
  15. "color": "#007AFF",
  16. "contentdown": {
  17. "caption": ""//下拉可刷新自定义文本
  18. }
  19. }
  20. }
  21. },

页面进行监听

  1. onPullDownRefresh(){
  2. // console.log('onpull')
  3. if(this.TabCur == 0){
  4. uni.stopPullDownRefresh()
  5. return
  6. }
  7. this.$nextTick(() => {
  8. this.$refs.approvalDate.getList();
  9. setTimeout(() => {
  10. uni.stopPullDownRefresh()
  11. })
  12. })
  13. },

如果还是不能实现,可以看看最后的那个文章,scroll-view和原生下拉刷新不能混动哦

7.实现分页(下拉下一页)

实现下拉下一页,主要就是下面这个监听回调-一旦到了底部,就page+1就行了

下面是实现得代码

  1. //触底继续下拉刷新
  2. onReachBottom() {
  3. this.getList();
  4. },
  5. methods:{
  6. //获取列表
  7. async getList() {
  8. //是否为最后一页
  9. if (!this.isLastPage) {
  10. this.loading = true
  11. // console.log(this.policyParams, 'xxxx')
  12. let res;
  13. if (this.isAuth) {
  14. res = await policyApi.qryQList(this.policyParams)
  15. } else {
  16. res = await policyApi.qryList(this.policyParams);
  17. }
  18. this.loading = false;
  19. if (res.list && res.list.length > 0) {
  20. this.favourable = this.favourable.concat(res.list);
  21. this.policyParams.pageNum = res.nextPage;
  22. this.isLastPage = res.isLastPage;
  23. }
  24. }
  25. }
  26. }

8.查看图片和富文本

主要是利用自带得两个api-uni.previewImage,uni.openDocument

  1. export function searchPdf(url) {
  2. if (url.includes('jpeg') || url.includes('jpg') || url.includes('png')) {
  3. uni.previewImage({
  4. urls: [url],
  5. fail: () => {
  6. console.log("预览图片失败", JSON.stringify(err))
  7. uni.showToast({
  8. title: '预览图片失败',
  9. icon: 'none'
  10. });
  11. },
  12. success(index, tapIndex) {
  13. console.log("预览图片成功", index, tapIndex)
  14. }
  15. });
  16. return
  17. } else {
  18. uni.showLoading({
  19. icon: 'loading',
  20. title: '正在打开,请等待'
  21. });
  22. uni.downloadFile({
  23. url: url,
  24. fail: () => {
  25. uni.hideLoading()
  26. uni.showToast({
  27. title: '预览文件失败',
  28. icon: 'none'
  29. });
  30. },
  31. success: function(res) {
  32. console.log(res, 'res')
  33. var filePath = res.tempFilePath;
  34. console.log(filePath, 'folefasa',filePath)
  35. uni.openDocument({
  36. filePath: filePath,
  37. success: function(res) {
  38. console.log("预览文件成功", res)
  39. uni.hideLoading();
  40. },
  41. fail(err) {
  42. console.log("预览文件失败", err)
  43. uni.hideLoading();
  44. uni.showToast({
  45. title: '预览文件失败',
  46. icon: 'none'
  47. });
  48. }
  49. });
  50. },
  51. })
  52. }
  53. }

9.微信授权登录

超详细的uniapp实现微信登录功能_哆来A梦没有口袋的博客-CSDN博客_uniapp 微信登录

10.APP检测是否是最新版本号

检查最新版本号,一般是在App.vue的onLauch生命周期,获取版本号并检测是否是最新版本号吗,提示用户是否需要更新

  1. onLaunch: function() {
  2. let that = this
  3. //#ifdef APP-PLUS
  4. plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
  5. // 获取 app的version
  6. let appversion = wgtinfo.version;
  7. // 调取接口查询版本号
  8. let url = 'http://222.180.163.242:9769'
  9. uni.request({
  10. url: `${url}/tsAdmin/wxApp/checkAppUpdate`,
  11. method: 'GET',
  12. success: (result) => {
  13. if (result.data.code == 'rest.success') {
  14. let info = result.data.result;
  15. // 版本号不一致
  16. if (info.versionText !== appversion) {
  17. uni.showModal({ //提醒用户更新
  18. title: "更新提示",
  19. content: "已有新版本,请确认是否更新",
  20. success: (res) => {
  21. //点击确认
  22. if (res.confirm) {
  23. //浏览器打开下载地址
  24. plus.runtime.openURL(`${url}/tsAdmin/wxApp/downloadApp?url=${info.path}`)
  25. }
  26. }
  27. })
  28. }
  29. }
  30. },
  31. fail:(err)=> {
  32. console.log(err,'err')
  33. }
  34. })
  35. });
  36. //#endif

11.分享

分享是常见的app的功能

官网对于分享的讲解很详细

uni-app官网

这里讲一下其他的分享 - 微信小程序分享

微信的button加一个open-type="share",立即会实现分享功能,什么也不用写

  1. <button class="toshare-button" open-type="share">
  2. 去分享
  3. </button>

这样子的分享,会分享当前页面,有微信的默认样子

点击会自动进入分享页面

但是其实有时候我们分享出去的页面,想跳转的并不是当前点击页面,应该怎么做

 可以监听点击分享

  1. onShareAppMessage(res) {
  2. return {
  3. title: '分享注册就领现金红包,提现不设限,最高200元',
  4. path: '/share/open'
  5. }
  6. }

这个时候在点击分享链接进入的就是path - share/open啦

12.H5中使用高德地图

uniapp使用H5高德地图_哆来A梦没有口袋的博客-CSDN博客

13. H5中高德地图实现uni.chooseLcation

H5实现高德地图的uni.chooseLocation_哆来A梦没有口袋的博客-CSDN博客

当然,uniapp还有一个小坑在里面。uniapp的那些坑_哆来A梦没有口袋的博客-CSDN博客_uniapp的坑有哪些 ,这篇博客写了在项目中遇到的一些问题

文章持续更新中

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

闽ICP备14008679号