TypeScript: miniProgram 获取用户信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <!--index.wxml--> < view class="container"> < view class="userinfo"> < block wx:if="{{canIUseOpenData}}"> < view class="userinfo-avatar" bindtap="bindViewTap"> < open-data type="userAvatarUrl"></ open-data > </ view > < open-data type="userNickName"></ open-data > </ block > < block wx:elif="{{!hasUserInfo}}"> < button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 获取头像昵称 </ button > < button wx:elif="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </ button > < view wx:else> 请使用1.4.4及以上版本基础库 </ view > </ block > < block wx:else> < image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></ image > < text class="userinfo-nickname">{{userInfo.nickName}}</ text > </ block > </ view > < view class="usermotto"> < text class="user-motto">{{motto}}</ text > </ view > < button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">获取手机号</ button > < text class="phone">{{phone}}</ text > </ view > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | // index.ts // 获取应用实例 const app = getApp<IAppOption>() Page({ data: { motto: 'Hello World,涂聚文' , userInfo: {}, hasUserInfo: false , canIUse: wx.canIUse( 'button.open-type.getUserInfo' ), canIUseGetUserProfile: false , canIUseOpenData: wx.canIUse( 'open-data.type.userAvatarUrl' ) && wx.canIUse( 'open-data.type.userNickName' ) // 如需尝试获取用户信息可改为false }, // 事件处理函数 bindViewTap() { wx.navigateTo({ url: '../logs/logs' , }) }, onLoad() { // @ts-ignore if (wx.getUserProfile) { this .setData({ canIUseGetUserProfile: true }) } }, getUserProfile() { // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗 wx.getUserProfile({ desc: '展示用户信息' , // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 success: (res) => { console.log(res) this .setData({ userInfo: res.userInfo, hasUserInfo: true }) } }) }, getUserInfo(e: any) { // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息 console.log(e) this .setData({ userInfo: e.detail.userInfo, hasUserInfo: true }) }, getPhoneNumber(e:any) { console.log(e.detail.encryptedData); //数据 解密了才可以得到数据 //console.log(e.detail.encryptData); console.log(e.detail.iv); console.log( "开始" ); var crypto = require( '../../utils/crypto.js' ); console.log(crypto.encrypted( "福田区" )) //加密 console.log(crypto.decrypted( "m6pCKPCLF+3mtABJqGC13g==" )) //解密 //console.log(crypto.decrypted(e.detail.iv)) wx.cloud.callFunction( { name: 'getPhone' , data:{ cloudID:e.detail.cloudID } } ).then(es=>{ console.log( "读取:" ,es.result); console.log(es.result?.link); // this.setData({phone:'手机号码是:'+ es.result.list[0].data.getPhoneNumber}) }). catch (err=>{console.log( 'erro' ,err)}) } }) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | //crypto.js //https://github.com/gwjjeff/cryptojs var CryptoJS = require( 'crypto-js.js' ); // 密钥 16 位 var key = 'skdidhjdksle2345' ; // 初始向量 initial vector 16 位 var iv = 'skdidhjdksle2345' ; // key 和 iv 可以一致 key = CryptoJS.enc.Utf8.parse(key); iv = CryptoJS.enc.Utf8.parse(iv); //加密 function encrypted(param) { var encrypted = CryptoJS.AES.encrypt(param, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); encrypted = encrypted.toString(); return encrypted; } //解密 function decrypted(param) { var decrypted = CryptoJS.AES.decrypt(param, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); decrypted = CryptoJS.enc.Utf8.stringify(decrypted); return decrypted; } module.exports = { encrypted: encrypted, decrypted: decrypted, } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Ajax&JavaScript
标签:
miniProgram
, typescript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?