欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

模仿拼多多小程序自动登录思想

程序员文章站 2022-03-06 13:40:39
...

模仿拼多多小程序自动登录思想

登录页面的js

  1. onShow: function (onshow) {
  2. let that = this
  3. this.setData({
  4. head:wx.getStorageSync('head'),
  5. username:wx.getStorageSync('username')
  6. })
  7. }

APP.JS

  1. onLaunch() {
  2. if(wx.getStorageSync('openid')==false||wx.getStorageSync('username')==false||wx.getStorageSync('head')==false){
  3. wx.login({
  4. success: res => {
  5. wx.request({
  6. url: "https://api.weixin.qq.com/sns/jscode2session",
  7. data: {
  8. 'appid': "自己的appid",
  9. 'secret': "自己的secret",
  10. 'js_code': res.code,
  11. 'grant_type': "authorization_code"
  12. },
  13. method: 'POST',
  14. header: {
  15. "Content-Type": "application/x-www-form-urlencoded"
  16. },
  17. success: function (data) {
  18. wx.request({
  19. url: "http://192.168.199.99/api/autologin.php",
  20. data:{
  21. 'openid':data.data.openid,
  22. },
  23. method: 'POST',
  24. header: { "Content-Type": "application/x-www-form-urlencoded" },
  25. success:res2=>{
  26. res2.data.forEach(element => {
  27. wx.setStorageSync('head', element.head)
  28. wx.setStorageSync('username', element.name)
  29. wx.setStorageSync('openid', element.openid)
  30. });
  31. }
  32. })
  33. },
  34. fail: function (err) {
  35. console.log(err);
  36. }
  37. })
  38. }
  39. })
  40. }
  41. },