當(dāng)前位置:
首頁 >
一个小程序的首页对接后端实现用户注册
發(fā)布時(shí)間:2025/5/22
28
豆豆
生活随笔
收集整理的這篇文章主要介紹了
一个小程序的首页对接后端实现用户注册
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一個(gè)小程序的首頁對(duì)接后端實(shí)現(xiàn)用戶注冊(cè) //index.js
//獲取應(yīng)用實(shí)例
const app = getApp()Page({globalData: {appid: 'wx4abc8f71681a89e0',//appid需自己提供secret: '5bf1b1597b219f8a6d77eab59314b885',//secret需自己提供,此處的
},data: {motto: '技術(shù)帶來改變',userInfo: {},openid: '',hasUserInfo: false,canIUse: wx.canIUse('button.open-type.getUserInfo')},//事件處理函數(shù)
bindViewTap: function() {wx.switchTab({url: '../task/task'})},onLoad: function() {if (app.globalData.userInfo) {this.setData({userInfo: app.globalData.userInfo,hasUserInfo: true});} else if (this.data.canIUse) {// 由于 getUserInfo 是網(wǎng)絡(luò)請(qǐng)求,可能會(huì)在 Page.onLoad 之后才返回// 所以此處加入 callback 以防止這種情況app.userInfoReadyCallback = res => {this.setData({userInfo: res.userInfo,hasUserInfo: true})}this.LoadUserSub(); //獲取openid 并且注冊(cè)
} else {// 在沒有 open-type=getUserInfo 版本的兼容處理
wx.getUserInfo({success: res => {app.globalData.userInfo = res.userInfothis.setData({userInfo: res.userInfo,hasUserInfo: true})}});}},getUserInfo: function(e) {app.globalData.userInfo = e.detail.userInfowx.setStorageSync('userInfo', e.detail.userInfo);//存儲(chǔ)userInfothis.setData({userInfo: e.detail.userInfo,hasUserInfo: true})this.register();//注冊(cè)
},LoadUserSub: function () {var that = this;var user = wx.getStorageSync('user') || {};var userInfo = wx.getStorageSync('userInfo') || {};if ((!user.openid || (user.expires_in || Date.now()) < (Date.now() + 600)) && (!userInfo.nickName)) {wx.login({success: function (res) {if (res.code) {var d = that.globalData;//這里存儲(chǔ)了appid、secret、token串 var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + d.appid + '&secret=' + d.secret + '&js_code=' + res.code + '&grant_type=authorization_code';wx.request({url: l,data: {},method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE,
success: function (res) {var obj = {};obj.openid = res.data.openid;obj.expires_in = Date.now() + res.data.expires_in;wx.setStorageSync('user', obj);//存儲(chǔ)openid
that.setData({openid: res.data.openid,hasUser: true})}});} else {console.log('獲取用戶登錄態(tài)失敗!' + res.errMsg)}}});}},register:function(){var user = wx.getStorageSync('user') || {};var userInfo = wx.getStorageSync('userInfo') || {};var l = 'http://123.com/index.php/Home/Wechat/register';//這里的域名改下if (user.openid && userInfo.nickName) {wx.request({url: l,data: {openid: user.openid,nickName: userInfo.nickName,avatarUrl: userInfo.avatarUrl,gender: userInfo.gender,province: userInfo.province,city: userInfo.city,country: userInfo.country},method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE,
success: function (res) {var userid = 0;wx.setStorageSync('userid', res.data);//存儲(chǔ)userid
}});} }})
?
posted on 2019-04-04 10:11?baker95935 閱讀(...) 評(píng)論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/baker95935/p/10653329.html
總結(jié)
以上是生活随笔為你收集整理的一个小程序的首页对接后端实现用户注册的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: phpstudy2018 安装xdebu
- 下一篇: kali渗透-基础篇