wepy 之用户信息获取与 async 同步
获取用户信息与登录操作
open-type
显示用户信息,
bindgetuserinfo
点击返回获取到的用户信息,
wxpy.login()
登录,
async
对于函数内部有异步操作变同步的声明,
1
| <button type="primary" open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="onGotUserInfo">授权</button>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| async onGotUserInfo(e) { console.log(e); if (e.detail.errMsg == 'getUserInfo:ok') {
let res = await wepy.login(); console.log(res); } }
|
setStorageSync
wxpy 同步保存到本地
1
| wepy.setStorageSync(USER_INFO, e.detail.userInfo)
|
用户授权
1 2
| wepy.getSetting() if ((res.authSetting)['scope.userInfo'])
|
模块化集中管理
在大型项目中通常使用模块化集中管理,便于查询、修改和维护,如:常量、数据请求等。
1 2
| wepy.setStorageSync(USER_INFO, e.detail.userInfo)
|
1 2 3 4 5 6
| import { USER_INFO, USER_SPECIAL_INFO, SYSTEM_INFO } from '@/utils/constant'
|
/utils/constant.js
1 2 3 4 5 6 7
|
export const USER_INFO = 'userInfo' export const USER_SPECIAL_INFO = 'user_special_info' export const SYSTEM_INFO = 'systemInfo'
|
架构思想
安全操作
- 在请求的过程中带上时间戳是一个常用的做法
1 2
| const TIMESTAMP = util.getCurrentTime() data.time = TIMESTAMP
|
- md5 加密
生成一个 32 位的字符串
1 2 3
| const SIGN = md5.hex_md5((TIMESTAMP + API_SECRET_KEY).toLowerCase()) data.sign = SIGN
|