微信小程序

文章目录


简介

  • 小程序包含一个描述整体程序的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>
相关推荐
数据皮皮侠3 小时前
最新上市公司业绩说明会文本数据(2017.02-2025.08)
大数据·数据库·人工智能·笔记·物联网·小程序·区块链
不如摸鱼去19 小时前
Trae 辅助下的 uni-app 跨端小程序工程化开发实践分享
微信小程序·小程序·uni-app·aigc·ai编程
意会21 小时前
微信闪照小程序实现
前端·css·微信小程序
小白_ysf1 天前
uniapp 开发微信小程序,获取经纬度并且转化详细地址(单独封装版本)
微信小程序·uni-app
weixin_lynhgworld1 天前
剧本杀小程序系统开发:构建剧本杀社交新生态
小程序
说私域1 天前
基于定制开发开源 AI 智能名片 S2B2C 商城小程序的热点与人工下发策略研究
人工智能·小程序
weixin_lynhgworld1 天前
陪诊小程序系统开发:让就医不再是一件难事
小程序
是一碗螺丝粉2 天前
拯救你的app/小程序审核!一套完美避开审核封禁的URL黑名单机制
前端·javascript·微信小程序
weixin_lynhgworld2 天前
盲盒抽谷机小程序系统开发:从0到1的完整方法论
小程序
weixin_lynhgworld2 天前
短剧小程序系统开发:赋能创作者,推动短剧艺术创新发展
小程序