使用uni-app开发微信小程序并实现页面间的跳转_通过 vue-cli 创建 uni-app 项目-程序员宅基地

技术标签: 微信小程序  小程序  uni-app  

主要技术

VUE3、pinia、ts、Spring Boot

一、下载需要的开发工具

  • HBuilderX

  • 微信开发者工具

HBuilderX

HBuilderX-高效极客技巧 (dcloud.io)

微信开发者工具

下载 / 开发版更新日志 (qq.com)

二、新建项目

通过vue-cli命令行创建项目

参考:

uni-app官网 (dcloud.net.cn)

2.1全局安装 vue-cli

npm install -g @vue/cli

--查看是否已经安装成功 

vue -version

2.2 创建uni-app

创建以 typescript 开发的工程(模板项目存放于 Github,由于国内网络环境问题,可能下载失败,如果下载失败,你可以直接使用下面的压缩包解压就可以了)

npx degit dcloudio/uni-preset-vue#vite-ts my-vue3-project

uni-app.zip - 蓝奏云

2.3用HB打开项目,安装依赖

 2.4 设置微信开发工具地址

 此处设置你第一步安装微信开发工具的路径,一定不能错

2.5打开微信开发者工具,点击小程序->点击+号创建小程序(注意这步操作只是拿到测试的appid,不需要真的创建,复制号测试appid)

2.6将获得的appid配置到uniapp项目的manifest.json文件中:

 2.7 微信开发者工具的设置,找到安全设置,将服务端口打开

2.8回到HBuilderX 点击运行到微信开发者工具就可以了

 

三、uni-app实现页面间的跳转并传递参数(switchTab、navigateTo)

目标:

3.1 switchTab跳转

设置跳转页面

about.vue

<template>
  <!-- 页面内容 -->
  <view class=".text-area">
	  <text>switchTab接收:{
   {testValue}}</text>
  </view>
</template>

<script lang="ts" setup>
import { onMounted,ref,watch} from 'vue'
import useStore from '../../store/store'
const storeWx=useStore();
const testValue=ref()
onMounted(() => { 
	showValue();
}) 
watch(()=>storeWx.deliver,()=>{
	showValue();
})
const showValue=()=>{
	testValue.value=storeWx.deliver
}
</script>
<style scoped>
	.text-area{
		position: fixed;
		top: 340px; /* 距离顶部10像素 */
		left: 50%;
		transform: translateX(-50%); /* 水平居中 */	
	}
</style>

index.vue

<template>
  <view class="text-area">
    <input
      class="custom-input"
      type="text"
      v-model="inputValue"
      placeholder="请在此输入"
    />
    <view class="button-container">
      <button @click="switchTabtiaozhuan">switchTab跳转到关于页面</button>
	  <button @click="navigaTotiaozhuan">navigaTo跳转到关于页面</button>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { onMounted, ref,watch } from 'vue';
import useStore from '../../store/store'

const storeWx=useStore();
const inputValue = ref('');

onMounted(() => { 
}) 

const switchTabtiaozhuan = () => {
	storeWx.deliver=inputValue,
	uni.switchTab({
	  url: '/pages/about/about'
	});
}

const navigaTotiaozhuan = () => {
	    uni.navigateTo({
	                url: `/pages/log/log?key1=${(inputValue.value)}`,
	                fail: (err) => {
	                    console.log(err)
	                }
	            });
}
</script>
<style scoped>
.text-area {
  position: fixed;
  top: 340px;
  left: 50%;
  transform: translateX(-50%);
  padding-bottom: 16px; /* 为整个.text-area添加底部内边距,使输入框和按钮之间有一定空间 */
}

.custom-input {
  border-color: #3cc51f;
  box-shadow: 0 0 0 2px rgba(60, 197, 31, 0.3);
  width: 300px;
  height: 40px;
}

/* 为按钮添加上外边距 */
.view.button-container {
  margin-top: 16px; /* 添加上外边距,使得按钮与输入框之间有间隔 */
}

/* 如果没有给按钮单独设置类名,可以使用伪类 nth-child 选择器 */
.text-area > view:nth-child(2) {
  margin-top: 16px;
}
</style>

mine.vue

<template>
	<view class="text-area">
	  <text class="title">我的</text>
	</view>
</template>

 进入page.json注册页面

{
  "pages": [
	  {
	        "path": "pages/index/index",
	        "style": {
	          "navigationBarTitleText": "uni-app"
	        }
	      },
		  {
		        "path": "pages/home/home",
		        "style": {
		          "navigationBarTitleText": "主页"
		        }
		      },
	      {
	        "path": "pages/mine/mine",
	        "style": {
	          "navigationBarTitleText": "我的"
	        }
	      } ,
		  {
		    "path": "pages/about/about",
		    "style": {
		      "navigationBarTitleText": "关于"
		    }
		  } 
  ],
  "tabBar": {
    "color": "#7A7E83",
    "selectedColor": "#3cc51f",
    "borderStyle": "black",
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页",
        "iconPath": "static/index.png",
        "selectedIconPath": "static/index1.png"
      },
      {
        "pagePath": "pages/about/about",
        "text": "关于",
        "iconPath": "static/about.png",
        "selectedIconPath": "static/about1.png"
      },
	  {
	    "pagePath": "pages/mine/mine",
	    "text": "我的",
	    "iconPath": "static/mine.png",
	    "selectedIconPath": "static/mine1.png"
	  }
    ]
  }
}

3.2 navigateTo跳转

设置页面

index.vue

log.vue

<template>
  <!-- 页面内容 -->
  <view class=".text-area">	
    <text>navigateTo接受到的数据:{
   { testNavigateTo }}</text>
  </view>
</template>

<script lang="ts" setup>
import { onMounted, ref} from 'vue';
import { onLoad } from '@dcloudio/uni-app';

const testNavigateTo = ref('');

onLoad((params) => {
		testNavigateTo.value = params.key1;
  console.log(params.key1);  
});
</script>
<style scoped>
.text-area {
  position: fixed;
  top: 340px; /* 距离顶部10像素 */
  left: 50%;
  transform: translateX(-50%); /* 水平居中 */	
}
</style>
<style scoped>
	.text-area{
		position: fixed;
		top: 340px; /* 距离顶部10像素 */
		left: 50%;
		transform: translateX(-50%); /* 水平居中 */	
	}
</style>

page.json注册

{
  "pages": [
	  {
	        "path": "pages/index/index",
	        "style": {
	          "navigationBarTitleText": "uni-app"
	        }
	      },
	      {
	        "path": "pages/mine/mine",
	        "style": {
	          "navigationBarTitleText": "我的"
	        }
	      } ,
		  {
		    "path": "pages/about/about",
		    "style": {
		      "navigationBarTitleText": "关于"
		    }
		  } ,
		  {
		    "path": "pages/log/log",
		    "style": {
		      "navigationBarTitleText": "日志"
		    }
		  } 
  ],
  "tabBar": {
    "color": "#7A7E83",
    "selectedColor": "#3cc51f",
    "borderStyle": "black",
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页",
        "iconPath": "static/index.png",
        "selectedIconPath": "static/index1.png"
      },
      {
        "pagePath": "pages/about/about",
        "text": "关于",
        "iconPath": "static/about.png",
        "selectedIconPath": "static/about1.png"
      },
	  {
	    "pagePath": "pages/mine/mine",
	    "text": "我的",
	    "iconPath": "static/mine.png",
	    "selectedIconPath": "static/mine1.png"
	  }
    ]
  }
}

四、微信小程序登录

设置Hbuildder微信登录

1 微信模块配置

2  微信小程序配置

再我的页面设置登录逻辑,因为我们使用到了pinia,所以我们先下载一个pinia的插件

问题:pinia可能于vue-demi版本冲突,所以本项目使用2.0.35版本的pinia

npm install [email protected]

将pinia导入项目

import { createSSRApp } from "vue";
import App from "./App.vue";
import * as Pinia from 'pinia';
export function createApp() {
  const app = createSSRApp(App);
  	app.use(Pinia.createPinia());
  return {
    app,
	Pinia, // 此处必须将 Pinia 返回
  };
}

创建pinia仓库


import { createPinia,defineStore } from 'pinia';
const pinia = createPinia()

export default defineStore('storeWx', {
	state: () => {
		return {
			openId: "",
			nickName:"当前状态未登录",
			avatarUrl:"https://fastly.jsdelivr.net/npm/@vant/assets/logo.png",
			deliver:''
		};
	}
});

uni-app的uni.login获取openid,我们可以先判断openid是否已经存在,若不存在,就像后端发送请求获取openid

目标样式:

前端逻辑代码:

mine.vue

<template>
	<view>
		<view>
			<image class="avatar-background" :src="storeWx.avatarUrl" style="position:absolute; z-index:-1; top:0; left:0; width:100%; height:40%; object-fit:cover;"></image><br>
			<text class="nickname">用户昵称:{
   {storeWx.nickName}}</text>
			<a href="storeWx.nickName"></a>
		</view><br>
		
		
		<button class="button-style" @click="wxLogin" v-if="storeWx.openId==''">微信登录</button>
	</view>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import useStore from '../../store/store';
  const storeWx = useStore();
	onMounted(() => {
		
	})
	const wxLogin = () => {
						uni.login({
						  provider: 'weixin',
						  success: function (loginRes) {
						    const code = loginRes.code;
						    uni.request({
						      url: "http://localhost:8080/api/weixin/access_token",
						      method: "GET", 
						      data: {
						        code: code,
						        // 其他可能需要的参数,如appid、secret等
						      },
						      success: (response) => {
						        console.log(response);
								storeWx.openId=response.data.openid,
						        console.log("向后端请求成功");
								 // 获取用户信息
								    uni.getUserInfo({
								      provider: 'weixin',
								      success: function (infoRes) {
										  storeWx.nickName=infoRes.userInfo.nickName,
										  storeWx.avatarUrl=infoRes.userInfo.avatarUrl,
										  //登录后隐藏按钮
										  console.log(infoRes)
								        // console.log('用户昵称为:' + infoRes.userInfo.avatarUrl);
								      }
								    });
						      },
						    });
						  },
						}); 
	}
</script>

<style scoped>

.nickname {
  position: fixed;
  top: 280px; /* 距离顶部10像素 */
  left: 50%;
  transform: translateX(-50%); /* 水平居中 */
  /* 其他文本样式 */
}
.button-style{
	position: fixed;
	top: 340px; /* 距离顶部10像素 */
	left: 50%;
	transform: translateX(-50%); /* 水平居中 */
}
</style>

后端逻辑代码:

@RestController
@RequestMapping("/api/weixin")
public class WeChatController {

    private final  String appid ="wx84e44fcb3a08e24c";
    private final  String secret ="1e6d37e46955fa63577d55534a384e46";
    @GetMapping("/access_token")
    public JSONObject getOpenId(@RequestParam String code){

        String url="https://api.weixin.qq.com/sns/jscode2session?appid="
                +appid+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code";

        String s = HttpUtil.get(url);
        JSONObject entries = JSONUtil.parseObj(s);
        return entries;
    }
}

要注意的依赖JSONUtil: 

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.16</version>
        </dependency>
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_74007708/article/details/137136169

智能推荐

51单片机的中断系统_51单片机中断篇-程序员宅基地

文章浏览阅读3.3k次,点赞7次,收藏39次。CPU 执行现行程序的过程中,出现某些急需处理的异常情况或特殊请求,CPU暂时中止现行程序,而转去对异常情况或特殊请求进行处理,处理完毕后再返回现行程序断点处,继续执行原程序。void 函数名(void) interrupt n using m {中断函数内容 //尽量精简 }编译器会把该函数转化为中断函数,表示中断源编号为n,中断源对应一个中断入口地址,而中断入口地址的内容为跳转指令,转入本函数。using m用于指定本函数内部使用的工作寄存器组,m取值为0~3。该修饰符可省略,由编译器自动分配。_51单片机中断篇

oracle项目经验求职,网络工程师简历中的项目经验怎么写-程序员宅基地

文章浏览阅读396次。项目经验(案例一)项目时间:2009-10 - 2009-12项目名称:中驰别克信息化管理整改完善项目描述:项目介绍一,建立中驰别克硬件档案(PC,服务器,网络设备,办公设备等)二,建立中驰别克软件档案(每台PC安装的软件,财务,HR,OA,专用系统等)三,能过建立的档案对中驰别克信息化办公环境优化(合理使用ADSL宽带资源,对域进行调整,对文件服务器进行优化,对共享打印机进行调整)四,优化完成后..._网络工程师项目经历

LVS四层负载均衡集群-程序员宅基地

文章浏览阅读1k次,点赞31次,收藏30次。LVS:Linux Virtual Server,负载调度器,内核集成, 阿里的四层SLB(Server Load Balance)是基于LVS+keepalived实现。NATTUNDR优点端口转换WAN性能最好缺点性能瓶颈服务器支持隧道模式不支持跨网段真实服务器要求anyTunneling支持网络private(私网)LAN/WAN(私网/公网)LAN(私网)真实服务器数量High (100)High (100)真实服务器网关lvs内网地址。

「技术综述」一文道尽传统图像降噪方法_噪声很大的图片可以降噪吗-程序员宅基地

文章浏览阅读899次。https://www.toutiao.com/a6713171323893318151/作者 | 黄小邪/言有三编辑 | 黄小邪/言有三图像预处理算法的好坏直接关系到后续图像处理的效果,如图像分割、目标识别、边缘提取等,为了获取高质量的数字图像,很多时候都需要对图像进行降噪处理,尽可能的保持原始信息完整性(即主要特征)的同时,又能够去除信号中无用的信息。并且,降噪还引出了一..._噪声很大的图片可以降噪吗

Effective Java 【对于所有对象都通用的方法】第13条 谨慎地覆盖clone_为继承设计类有两种选择,但无论选择其中的-程序员宅基地

文章浏览阅读152次。目录谨慎地覆盖cloneCloneable接口并没有包含任何方法,那么它到底有什么作用呢?Object类中的clone()方法如何重写好一个clone()方法1.对于数组类型我可以采用clone()方法的递归2.如果对象是非数组,建议提供拷贝构造器(copy constructor)或者拷贝工厂(copy factory)3.如果为线程安全的类重写clone()方法4.如果为需要被继承的类重写clone()方法总结谨慎地覆盖cloneCloneable接口地目的是作为对象的一个mixin接口(详见第20_为继承设计类有两种选择,但无论选择其中的

毕业设计 基于协同过滤的电影推荐系统-程序员宅基地

文章浏览阅读958次,点赞21次,收藏24次。今天学长向大家分享一个毕业设计项目基于协同过滤的电影推荐系统项目运行效果:项目获取:https://gitee.com/assistant-a/project-sharing21世纪是信息化时代,随着信息技术和网络技术的发展,信息化已经渗透到人们日常生活的各个方面,人们可以随时随地浏览到海量信息,但是这些大量信息千差万别,需要费事费力的筛选、甄别自己喜欢或者感兴趣的数据。对网络电影服务来说,需要用到优秀的协同过滤推荐功能去辅助整个系统。系统基于Python技术,使用UML建模,采用Django框架组合进行设

随便推点

你想要的10G SFP+光模块大全都在这里-程序员宅基地

文章浏览阅读614次。10G SFP+光模块被广泛应用于10G以太网中,在下一代移动网络、固定接入网、城域网、以及数据中心等领域非常常见。下面易天光通信(ETU-LINK)就为大家一一盘点下10G SFP+光模块都有哪些吧。一、10G SFP+双纤光模块10G SFP+双纤光模块是一种常规的光模块,有两个LC光纤接口,传输距离最远可达100公里,常用的10G SFP+双纤光模块有10G SFP+ SR、10G SFP+ LR,其中10G SFP+ SR的传输距离为300米,10G SFP+ LR的传输距离为10公里。_10g sfp+

计算机毕业设计Node.js+Vue基于Web美食网站设计(程序+源码+LW+部署)_基于vue美食网站源码-程序员宅基地

文章浏览阅读239次。该项目含有源码、文档、程序、数据库、配套开发软件、软件安装教程。欢迎交流项目运行环境配置:项目技术:Express框架 + Node.js+ Vue 等等组成,B/S模式 +Vscode管理+前后端分离等等。环境需要1.运行环境:最好是Nodejs最新版,我们在这个版本上开发的。其他版本理论上也可以。2.开发环境:Vscode或HbuilderX都可以。推荐HbuilderX;3.mysql环境:建议是用5.7版本均可4.硬件环境:windows 7/8/10 1G内存以上;_基于vue美食网站源码

oldwain随便写@hexun-程序员宅基地

文章浏览阅读62次。oldwain随便写@hexun链接:http://oldwain.blog.hexun.com/ ...

渗透测试-SQL注入-SQLMap工具_sqlmap拖库-程序员宅基地

文章浏览阅读843次,点赞16次,收藏22次。用这个工具扫描其它网站时,要注意法律问题,同时也比较慢,所以我们以之前写的登录页面为例子扫描。_sqlmap拖库

origin三图合一_神教程:Origin也能玩转图片拼接组合排版-程序员宅基地

文章浏览阅读1.5w次,点赞5次,收藏38次。Origin也能玩转图片的拼接组合排版谭编(华南师范大学学报编辑部,广州 510631)通常,我们利用Origin软件能非常快捷地绘制出一张单独的绘图。但是,我们在论文的撰写过程中,经常需要将多种科学实验图片(电镜图、示意图、曲线图等)组合在一张图片中。大多数人都是采用PPT、Adobe Illustrator、CorelDraw等软件对多种不同类型的图进行拼接的。那么,利用Origin软件能否实..._origin怎么把三个图做到一张图上

51单片机智能电风扇控制系统proteus仿真设计( 仿真+程序+原理图+报告+讲解视频)_电风扇模拟控制系统设计-程序员宅基地

文章浏览阅读4.2k次,点赞4次,收藏51次。51单片机智能电风扇控制系统仿真设计( proteus仿真+程序+原理图+报告+讲解视频)仿真图proteus7.8及以上 程序编译器:keil 4/keil 5 编程语言:C语言 设计编号:S0042。_电风扇模拟控制系统设计