uni-app获取小程序带参

拼搏现实的明天。 2023-03-02 05:17 163阅读 0赞

背景

继上一篇我们使用springboot生成带参数的小程序码之后,我们就需要在小程序里面接收对应的scene参数,在uni-app里面怎么接收小程序scene参数,网上很多攻略讲不清楚,在哪里调用,怎么调用也没讲,这里特地捋了一下思路。

这里DEMO是以一个证书查询系统来弄。微信扫码带参的二维码/小程序码,自动跳转到小程序,并且识别和填充参数到界面上,这里是填充证书编号certNumber=xxxx

核心代码" class="reference-link">关于怎么生成可以看Java/SpringBoot获取小程序带参二维码并保存到本地 在这里插入图片描述 核心代码

for uni-app , by HBuildx

  1. <script>
  2. export default {
  3. onLoad (query) {
  4. //by zhengkai.blog.csdn.net
  5. // scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
  6. const scene = decodeURIComponent(query.scene);
  7. //类型为string,所以只能判断不叫"undefined"
  8. //console.log(typeof(scene));
  9. if(scene!="undefined"&&scene.length>4){
  10. console.log(scene);
  11. uni.showModal({
  12. content: '证书编码:'+scene+',点击查询查看详情',
  13. showCancel: false
  14. });
  15. this.certNumber=scene;
  16. }else{
  17. console.log("没有启动参数");
  18. }
  19. },
  20. data() {
  21. return {
  22. //这个参数用于接收和存储传过来的scene
  23. certNumber:'',
  24. }
  25. },
  26. methods: {
  27. //......
  28. }
  29. }
  30. </script>

完整代码

我是简单的单页面应用,直接往index加就完事。如果非单页面,建议存储起来供其他页面调用。
在这里插入图片描述
部分代码做了处理

  1. <template>
  2. <view class="container">
  3. <view v-if="show1">
  4. <image src="../../static/xxxx.png" mode="widthFix"></image>
  5. <br><br>
  6. <label v-text="label" style="font-size: large;padding-top: 140px;"></label>
  7. <input type="text" class="flex" @focus="onBlur()" v-model="certNumber" placeholder="请输入证书编号,格式EGAG-SAL0*-2020****"/>
  8. <button type="default" @click="onSearch()">查询</button>
  9. <br>
  10. </view>
  11. <view v-if="show2">
  12. <image src="../../static/xxxxx.jpg" mode="widthFix"></image><br>
  13. <text>证 书 编 号:{ { certInfo.certNumber}}</text><br>
  14. <text>公 司 名 称:{ { certInfo.certCompany}}</text><br>
  15. <text>有 效 时 间:{ { certInfo.certValidateStart}} 至 { { certInfo.certValidateEnd}}</text><br><br><br><br><br><br>
  16. <button type="default" @click="onBack()">返回</button>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. onLoad (query) {
  23. //by zhengkai.blog.csdn.net
  24. // scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
  25. const scene = decodeURIComponent(query.scene);
  26. //类型为string,所以只能判断不叫"undefined"
  27. //console.log(typeof(scene));
  28. if(scene!="undefined"&&scene.length>4){
  29. console.log(scene);
  30. uni.showModal({
  31. content: '证书编码:'+scene+',点击查询查看详情',
  32. showCancel: false
  33. });
  34. this.certNumber=scene;
  35. }else{
  36. console.log("没有启动参数");
  37. }
  38. },
  39. data() {
  40. return {
  41. label:"证书编号:",
  42. certNumber:'',
  43. show1: true,
  44. show2: false,
  45. certInfo:{
  46. certId: 0, certNumber: "EGAG-SAL03-******", certCompany: "******有限公司", certAddress: "******", certLevel: "叁",certService: "***",
  47. certValidateEnd: "2022-10-12",certValidateStart: "2020-10-12"
  48. }
  49. }
  50. },
  51. methods: {
  52. onSearch: function(e) {
  53. let that=this;
  54. //console.log("click:"+this.certCompanyNumber);
  55. if(this.certNumber.length>11){
  56. new Promise((resolve, reject) => {
  57. uni.request({
  58. url: "https://xxxxxx/certInfo?certNumber="+that.certNumber.trim(),
  59. /* data: {"cert_number"},*/
  60. header: {
  61. "Access-Control-Allow-Origin": "*"
  62. },
  63. method:"POST",
  64. success: (res) => {
  65. resolve(res);
  66. console.log(res.data);
  67. if(res.data===null||res.data.length<1){
  68. uni.showModal({
  69. content: '请输入有效证书编码',
  70. showCancel: false
  71. });
  72. }else{
  73. that.certInfo=res.data;
  74. that.show2=true;that.show1=false;
  75. }
  76. }
  77. });
  78. })
  79. }else{
  80. uni.showModal({
  81. content: '请输入正确的证书编码',
  82. showCancel: false
  83. });
  84. }
  85. },
  86. onBlur:function(e) {
  87. //console.log("blur");
  88. if(this.certNumber.length<1){
  89. this.certNumber="EGAG-SAL03-";
  90. }
  91. },
  92. onBack:function(e) {
  93. this.show1=true;this.show2=false;
  94. },
  95. }
  96. }
  97. </script>
  98. <style>
  99. .container {
  100. padding: 20px;
  101. font-size: 14px;
  102. line-height: 24px;
  103. }
  104. </style>

效果预览

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,163人围观)

还没有评论,来说两句吧...

相关阅读