微信小程序

文章目录


简介

  • 小程序包含一个描述整体程序的app和多个描述各自页面的page
  • 一个小程序主体部由三个文件组成,必须放在根目录
    • app.js主要是小程序的逻辑
    • app.json是小程序公共配置
    • app.wxss是小程序公共样式表
  • 一个小程序页面由四个文件组成:js、wxml、json和wxss。
  • 其中js(逻辑)和wxml(结构)是必须要有的
  • json页面数据
  • wxss页面样式

index.js

js 复制代码
// index.js
Page({
	//定义数据
	data: {
		msg1: '我是msg1,我在js中',
		userName:'',
		userImage:''
	},

	//自定义方法
	getInfo1() {
		//wx默认对象,获取用户信息
		wx.getUserProfile({
			desc: '获取用户信息',
			//获取信息
			success: (res) => {
				console.log(res.userInfo)
				//赋值
				this.setData({
					userName:res.userInfo.nickName,
					userImage:res.userInfo.avatarUrl
				})
				
			}
		})
	}
})

index.wxml

xml 复制代码
<!--index.wxml-->
<!-- <navigation-bar title="123" back="{{false}}" color="black" background="#FFF" /> -->
<!-- <scroll-view class="scrollarea" scroll-y type="list">
  <view class="container">
    页面内容
  </view>
</scroll-view> -->

<view class="container">
	<view>
		普通信息:{{msg1}}
		<br/>
		用户信息:{{userName}}
		<br/>
		<image src="{{userImage}}" style="width: 100px;height:100px"></image>
	</view>


	<!--type="primary,default,warn" -->
	<button type="primary" bindtap="getInfo1" >
		获取用户信息
	</button>

</view>

demo

index.js

js 复制代码
// index.js
Page({
	//定义数据
	data: {
		msg1: '我是msg1,我在js中',
		userName: '',
		userImage: '',
		//授权码
		code: ''
	},

	//自定义方法
	getInfo() {
		//wx默认对象,获取用户信息
		wx.getUserProfile({
			desc: '获取用户信息',
			//获取信息
			success: (res) => {
				console.log(res.userInfo)
				//赋值
				this.setData({
					userName: res.userInfo.nickName,
					userImage: res.userInfo.avatarUrl
				})

			}
		})
	},
	//登录,获取授权码
	login() {
		wx.login({
			success: (res) => {
				console.log(res.code)
				this.setData({
					code: res.code
				})
			},
		})
	},
	//发送请求
	sendMsg() {
		wx.request({
			url: 'http://localhost:8080/user/shop/status',
			method: 'GET',
			success: (res) => {
				console.log(res.data)
			}
		})
	}
})

index.wxml

xml 复制代码
<!--index.wxml-->
<!-- <navigation-bar title="123" back="{{false}}" color="black" background="#FFF" /> -->
<!-- <scroll-view class="scrollarea" scroll-y type="list">
  <view class="container">
    页面内容
  </view>
</scroll-view> -->

<view class="container">
	<view>
		普通信息:{{msg1}}
		<br/>
		用户信息:{{userName}}
		<br/>
		<image src="{{userImage}}" style="width: 100px;height:100px"></image>
	</view>


	<!--type="primary,default,warn" -->
	<button type="primary" bindtap="getInfo" >
		获取用户信息
	</button>

	----------------------------------------------<br/>
	<view>
		只能用一次的授权码:{{code}}
	</view>
	<button type="warn" bindtap="login" >
		登录
	</button>

	----------------------------------------------<br/>
	<view>
		发送请求:{{code}}
	</view>
	<button type="primary" bindtap="sendMsg" >
		发送请求
	</button>
</view>
相关推荐
赵庆明老师2 小时前
Uniapp微信小程序开发:微信小程序支付功能后台代码
微信小程序·小程序·uni-app
00后程序员张5 小时前
tcpdump 抓包分析,命令、过滤技巧、常见症状定位与移动真机补充方案
网络·测试工具·ios·小程序·uni-app·iphone·tcpdump
会点法律的程序员7 小时前
小程序 地理位置授权怎么搞
前端·小程序·uni-app
FliPPeDround8 小时前
告别 uni-app 启动烦恼:@uni-helper/unh 让开发流程更顺畅
前端·微信小程序·uni-app
2501_9159214311 小时前
iOS 26 电耗监测与优化,耗电问题实战 + 多工具 辅助策略
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_9159214311 小时前
苹果软件混淆与 iOS 应用加固白皮书,IPA 文件加密、反编译防护与无源码混淆方案全解析
android·ios·小程序·https·uni-app·iphone·webview
@二十六11 小时前
微信小程序订阅消息工具封装,兼容一次性订阅和长期订阅
微信小程序·小程序·订阅消息
dcloud_jibinbin12 小时前
【uniapp】体验优化:开源工具集 uni-toolkit 发布
前端·webpack·性能优化·小程序·uni-app·vue
用力的活着13 小时前
uniapp 微信小程序蓝牙接收中文乱码
微信小程序·小程序·uni-app
流***陌13 小时前
陪诊陪护小程序前端功能解析:就医照料的便捷对接与全流程保障
小程序