[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
小程序授权登陆流程:
1、当用户进入微信小程序时,首先我们先判断用户是否授权过此小程序
wx.getSetting({//wx.getSetting方法获取用户的当前设置(查看是否授权) sucsess:res=>{//调用成功的回调函数
if (res.authSetting['scope.userInfo'])
{//如果res.authSetting['scope.userInfo']有值时,代表已授权 }else{//否则就是没有授权 } } })
2、如果没有授权,我们通过一个按钮来实现授权登录
//在微信小程序开发文档中,我们可以知道当button组件的open-type="getUserInfo"
并结合bindgetuserinfo事件,可以获取到用户信息(是否授权) <button open-type="getUserInfo"
bindgetuserinfo="getuserinfo">授权</button>
3、通过bindgetuserinfo事件,我们可以获取到个人的信息、加密偏移数据、加密用户信息(e.detail获取)
getuserinfo(e){ console.log(e.detail);//可以获取到个人的信息、加密偏移数据、加密用户信息 }
4、用户可以授权登录,也可以取消授权
用户取消授权
//当用户取消授权时,我们可以做一个提示 wx.showModal({//显示模态框 title: '警告', content:
'您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!', showCancel: false, confirmText: '返回授权',
success: function (res) { // 用户没有授权成功,不需要改变 isHide 的值 if (res.confirm) {
console.log('用户点击了“返回授权”'); } } })
用户授权了
wx.login({//登录 success: (res) => {//成功的回调 //
console.log(res.code);//获取临时登录凭证code if (res.code) {//当有临时登录凭证code码时,我们请求登录接口
//请求登录接口 } } })
5、根据登录接口返回的code码,判断用户是否时新用户
如果不是新用户,我们就直接保存下token(服务器返回的token);如果是新用户,我们就要先注册,在登录
//根据登录返回的code码,我们在需要注册的里面,请求注册接口,并配置好参数、请求方式等
//例如:登录接口code码返回10000时,代表未注册,返回0时,代表注册过 if (data.code == 10000) { //注册请求接口 }
else if (data.code == 0) { wx.setStorage({//保存token data: token, key: 'token'
}) }
6、当用户注册成功后,在调登录接口,保存token。在有些页面需要使用token
7、在步骤1中,当我们授权过时,我们要看token是否存在
8、当token存在时,我们直接执行逻辑代码
9、当token不存在时,我们就需要登录,登录后判断返回的code码,在根据code码判断用户是否是新用户。最后保存token
注::wx.login()方法,在成功回调可以获取临时登录凭证(code),需要先获取,在调用。多次调用,在每次调用前都要获取。临时登录凭证 code
只能使用一次