微信小程序记录小技巧[持续更新]

授权

授权是最基本的需求,官方获取用户信息接口调整

使用 button 组件,并将 open-type 指定为 getUserInfo 类型,获取用户基本信息
html:

<button open-type="getUserInfo">授权</button>

这样效果是获得一个授权的弹窗,但是和之前的是大不一样
授权弹窗
js:

wx.getUserInfo({
	success: (res) => {
		console.log("成功",res);
	},
	fail:res=>{
		console.log("失败",res)
	}
});

效果
成功示例
失败示例
至于 wx.authorize,我是这样写的,致死也没有触发

if (res.authSetting["scope.userInfo"] == undefined) {
	wx.authorize({
		scope: "scope.userInfo",
		success: (res) => {
			console.log("首次授权成功", res);
		},
		fail:res=>{
			console.log("首次授权失败",res)
		}
	});
}

啊不对,是致死也没有成功触发,失败是这样
首次授权失败
至于为什么写 ==undefined,是因为在一次都没有授权的情况下是这样的,很干净,此时是 undefined
没有授权的设置页面
而授权过但是后来关闭了是这样的,这时是有授权信息的,只是关闭了,此时是 false
授权过的设置页面
授权这方面除了用 button,也可以使用 wx.openSetting(),打开小程序授权页面,让用户自行打开授权信息,我这里是这样写的,但是出 bug 了,不会打开还授权失败,这里就没图了。。。

wx.openSetting({
	success: (res) => {
		console.log("授权成功");
	},
	fail: (res) => {
		console.log("授权失败");
	},
});

动画

和 H5 有点差距
第一种:官方文档
wxml:

直接在元素上加上 animation="{{动画名}}"
例如
<view animation="{{GoTop}}"></view>

js:

data:{
	GoTop:“”
}
this.GoTop = wx.createAnimation({ //创建动画
	duration: 1000, //动画持续时长
}).translateY(-1000).step() //动画
this.setData({
	GoTop: this.GoTop.export()
})

第二种:官方文档

获取当天日期

var util = require("../../utils/utils");
var TIME = util.formatTime(new Date());
console.log(TIME)

绑定的事件带有传递参数

wxml:
<view bindtap="test" data-name="{{666}}">点击</view>
js:
test:function(event){
	console.log(event)
}
输出结果里有currentTarget里有dataset里有name:"666"可使用

当然也可以传递动态参数

wxml:
<view bindtap="test" data-name="{{arr}}">点击</view>
js:
data:{
	arr=[1,2,3,4,5,6]
}
test:function(event){
	console.log(event)
}
输出结果里有currentTarget里有dataset里有name:[1,2,3,4,5,6]可使用

按钮功能阻止冒泡

wxml:
<button  catchtap="click1"></button>
catch 用来阻止冒泡,bind不可阻止冒泡

使用动画 更多属性看官网

wxml:
<view animation=" animation">
js:
data:{
	animation:""
}
this.animation = wx.createAnimation({
	duration: 1000,//持续时间
	delay:100//延时
}).translate(100).step()

获取元素信息

js:
wx.createSelectorQuery().select(".||#||标签").boundingClientRect(res =>
	console.log(res)
).exec();

❀❀❀❀❀❀ 完结散花 ❀❀❀❀❀❀

Written ❤️ sywdebug.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值