最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue中uni-app 實(shí)現(xiàn)小程序登錄注冊(cè)功能

 更新時(shí)間:2019年10月12日 15:55:20   作者:houqq  
這篇文章主要介紹了uni-app 實(shí)現(xiàn)小程序登錄注冊(cè)功能,文中給大家介紹了實(shí)現(xiàn)思路,以及vuex和本地緩存的區(qū)別,需要的朋友可以參考下

思路:

1.使用微信的 open-type="getUserInfo" 獲取用戶信息,將用戶信息保存到userinfoDetails對(duì)象中去。

<button v-else type="primary" class="reserve-btn" open-type="getUserInfo" @getuserinfo="getuserinfo">預(yù)約掛號(hào)</button>

2.使用 uni.login() 獲取code,并且把code傳給后臺(tái),后臺(tái)會(huì)返回openid

3.使用vuex和本地緩存保存相關(guān)狀態(tài)值

備注vuex和本地緩存的區(qū)別:

vuex是響應(yīng)式更新,頁(yè)面不刷新數(shù)據(jù)也會(huì)實(shí)時(shí)更新,但是頁(yè)面一刷新,數(shù)據(jù)可能會(huì)失效

本地緩存不會(huì)響應(yīng)式更新,但是一刷新本地緩存就會(huì)更新。所以二者結(jié)合使用,前端小白不知道這個(gè)做法是否科學(xué),

我把調(diào)用登錄注冊(cè)的方法封裝到公共方法里

代碼如下

import store from '@/store'
const app = {
 apiUrl: 'https://hoxxxxxxxxop.com/', //請(qǐng)求的地址
 _getuserinfo(res,ppid) {
 var that = this
 var userinfoDetails = {}
 userinfoDetails = res.detail.userInfo
 uni.getUserInfo({
  provider: 'weixin',
  success: function () {
  uni.login({
  success:function(res){
   uni.showLoading({
   title: '登陸中...',
   mask: false
   });
   uni.request({
   url: that.apiUrl + 'small/id?code=' + res.code,
   success: (res) => {
    console.log(res)
    if (res.data.openid) {
    userinfoDetails.openid = res.data.openid
    userinfoDetails = JSON.parse(JSON.stringify(userinfoDetails).replace(/avatarUrl/g, "headimgurl"));
    userinfoDetails = JSON.parse(JSON.stringify(userinfoDetails).replace(/gender/g, "sex"));
    userinfoDetails = JSON.parse(JSON.stringify(userinfoDetails).replace(/nickName/g, "nickname"));
    delete userinfoDetails.language;
    userinfoDetails.ppid = ppid || ''
    console.log(userinfoDetails)
    uni.setStorageSync('userinfoDetails',userinfoDetails)
    }
    if(res.data.status == 0) {
    that.sendInfo(userinfoDetails) // 用戶還沒注冊(cè)過需調(diào)用此方法
    console.log('我還沒有注冊(cè)')
    } else {
    uni.showToast({
     title: '登錄成功',
     icon: 'success',
     duration: 2000
    })
    store.commit('login', res.data) // vuex的方法,存openid,userinfo,和更改isloginstatus狀態(tài)值
    uni.setStorageSync('StorageloginStatus',true) // 補(bǔ)充本地存儲(chǔ) localStorage解決vuex刷新數(shù)據(jù)不保留
    uni.setStorageSync('Storageopenid',res.data.openid)
    uni.setStorageSync('Storageuserinfo',res.data.userinfo)
    }
    if (res.data.status == 0 && res.data.userinfo == 0) {
    uni.showToast({
     title: '登錄失敗',
     duration: 2000
    })
    }
   }
   })
  }
  })
  }
 });
 },
 sendInfo(userinfoDetails) {
 var that = this
 uni.request({
  url: this.apiUrl + 'sm/vip', //注冊(cè)接口
  data: userinfoDetails,
  method: 'POST',
  success: (res) => {
  if(res.data.userinfo == 1) {
   uni.hideLoading()
   uni.showToast({
   title: '注冊(cè)成功',
   icon: 'success',
   duration: 2000
   })
   store.commit('login', res.data) // vuex的方法,存openid,userinfo,和更改isloginstatus狀態(tài)值
   uni.setStorageSync('StorageloginStatus',true)
   uni.setStorageSync('Storageopenid',res.data.openid)
   uni.setStorageSync('Storageuserinfo',res.data.userinfo)
  } else {
   uni.hideLoading()
   uni.showToast({
   title: res.data.msg,
   duration: 2000
   })
  }
  }
 })
 }
}
export default app;

在index.vue調(diào)用

用Vuex中的isloginStatus和緩存中的StorageloginStatus來(lái)控制是否顯示登錄的按鈕

<button v-if="isloginStatus || StorageloginStatus" type="primary" class="reserve-btn" @click="goreserve">跳轉(zhuǎn)</button>
<button v-else type="primary" class="reserve-btn" open-type="getUserInfo" @getuserinfo="getuserinfo">登錄</button>
import app from '../../common/config.js'
export default {
 data() {
  return {
  ppid: "",
  StorageloginStatus: false
  }
 },
 computed: mapState({
  isloginStatus: state => state.isloginStatus,
 }),
 onLoad(option) {
  this.ppid = this.scene_decode(option.scene).ppid //封裝的scene_decode() 方法
  this.StorageloginStatus = uni.getStorageSync('StorageloginStatus')
 },
 methods: {
  // 獲取用戶信息
  getuserinfo(res) {
  app._getuserinfo(res,this.ppid) // 封裝好的方法 res是微信返回的用戶信息,ppid是二維碼攜帶的參數(shù) 
  },
  // 當(dāng)注冊(cè)或者登錄成功 顯示路由按鈕
  goreserve() {
  console.log('去掛號(hào)了')
  }
 }
 }

vuex

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
 state: {
 isloginStatus: false,
 userinfo: "", // 狀態(tài)值
 openid: "", 
 userinfoDetails: {} ,// 頭像姓名城市等。。。
 ppid: ""
 },
 mutations: {
 login(state,res) {
  state.isloginStatus = true,
  state.userinfo = res.userinfo, // 如果userinfo==1 --->已登錄
  state.openid = res.openid 
 },
 saveUserinfoDetails(state,res) {
  state.userinfoDetails = res
 },
 savePPid(state,id) {
  stage.ppid = id // 存ppid
 }
 },
})
export default store

總結(jié)

以上所述是小編給大家介紹的vue中uni-app 實(shí)現(xiàn)小程序登錄注冊(cè)功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

  • 教你如何使用VUE組件創(chuàng)建SpreadJS自定義單元格

    教你如何使用VUE組件創(chuàng)建SpreadJS自定義單元格

    這篇文章主要介紹了使用VUE組件創(chuàng)建SpreadJS自定義單元格的方法,通常我們使用組件的方式是,在實(shí)例化Vue對(duì)象之前,通過Vue.component方法來(lái)注冊(cè)全局的組件,文中通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-01-01
  • vue下的@change事件的實(shí)現(xiàn)

    vue下的@change事件的實(shí)現(xiàn)

    這篇文章主要介紹了vue下的@change事件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • 使用vux實(shí)現(xiàn)上拉刷新功能遇到的坑

    使用vux實(shí)現(xiàn)上拉刷新功能遇到的坑

    最近公司在研發(fā)app,選擇了基于Vue框架的vux組件庫(kù),現(xiàn)總結(jié)在實(shí)現(xiàn)上拉刷新功能遇到的坑,感興趣的朋友參考下吧
    2018-02-02
  • Vue實(shí)現(xiàn)驗(yàn)證碼功能

    Vue實(shí)現(xiàn)驗(yàn)證碼功能

    這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)驗(yàn)證碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • 聊聊Vue中provide/inject的應(yīng)用詳解

    聊聊Vue中provide/inject的應(yīng)用詳解

    這篇文章主要介紹了聊聊Vue中provide/inject的應(yīng)用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Vue實(shí)現(xiàn)登錄以及登出詳解

    Vue實(shí)現(xiàn)登錄以及登出詳解

    本篇文章主要介紹了vue實(shí)現(xiàn)登陸登出的實(shí)現(xiàn)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2021-09-09
  • Vue配置文件vue.config.js配置前端代理方式

    Vue配置文件vue.config.js配置前端代理方式

    這篇文章主要介紹了Vue配置文件vue.config.js配置前端代理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • element中el-autocomplete的常見用法示例

    element中el-autocomplete的常見用法示例

    這篇文章主要給大家介紹了關(guān)于element中el-autocomplete的常見用法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用element具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2023-03-03
  • Vue 實(shí)現(xiàn)點(diǎn)擊空白處隱藏某節(jié)點(diǎn)的三種方式(指令、普通、遮罩)

    Vue 實(shí)現(xiàn)點(diǎn)擊空白處隱藏某節(jié)點(diǎn)的三種方式(指令、普通、遮罩)

    最近小編接到這樣的需求:彈出框(或Popover)在 show 后,點(diǎn)擊空白處可以將其 hide。針對(duì)這個(gè)需求,小編整理了三種實(shí)現(xiàn)方式,如果大家對(duì)vue 點(diǎn)擊空白隱藏節(jié)點(diǎn)問題感興趣的朋友跟隨小編一起看看吧
    2019-10-10
  • vue 使用lodash實(shí)現(xiàn)對(duì)象數(shù)組深拷貝操作

    vue 使用lodash實(shí)現(xiàn)對(duì)象數(shù)組深拷貝操作

    這篇文章主要介紹了vue 使用lodash實(shí)現(xiàn)對(duì)象數(shù)組深拷貝操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2020-09-09

最新評(píng)論

荥经县| 盐池县| 万州区| 同江市| 杨浦区| 页游| 济源市| 新民市| 青铜峡市| 广德县| 昆明市| 丹凤县| 屏山县| 仁化县| 泸溪县| 浦城县| 金川县| 牙克石市| 和政县| 平安县| 晴隆县| 苍南县| 巫山县| 瓮安县| 双流县| 渝北区| 东阳市| 中牟县| 广水市| 册亨县| 阿拉善左旗| 体育| 梁平县| 平凉市| 宾阳县| 娄底市| 平阳县| 定陶县| 玛曲县| 甘德县| 大余县|