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

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

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

思路:

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

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

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

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

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

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

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

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

代碼如下

import store from '@/store'
const app = {
 apiUrl: 'https://hoxxxxxxxxop.com/', //請求的地址
 _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) // 用戶還沒注冊過需調(diào)用此方法
    console.log('我還沒有注冊')
    } else {
    uni.showToast({
     title: '登錄成功',
     icon: 'success',
     duration: 2000
    })
    store.commit('login', res.data) // vuex的方法,存openid,userinfo,和更改isloginstatus狀態(tài)值
    uni.setStorageSync('StorageloginStatus',true) // 補充本地存儲 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', //注冊接口
  data: userinfoDetails,
  method: 'POST',
  success: (res) => {
  if(res.data.userinfo == 1) {
   uni.hideLoading()
   uni.showToast({
   title: '注冊成功',
   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來控制是否顯示登錄的按鈕

<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)注冊或者登錄成功 顯示路由按鈕
  goreserve() {
  console.log('去掛號了')
  }
 }
 }

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 實現(xiàn)小程序登錄注冊功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

最新評論

东乡| 神农架林区| 定州市| 宜宾县| 行唐县| 蓬溪县| 图片| 灌南县| 六盘水市| 长岭县| 桦南县| 同江市| 朔州市| 长治县| 阿勒泰市| 玉田县| 泗阳县| 资阳市| 普格县| 安化县| 广安市| 观塘区| 荃湾区| 开封市| 石柱| 手游| 康乐县| 赞皇县| 济阳县| 安阳市| 日喀则市| 桑植县| 龙海市| 汨罗市| 景谷| 谢通门县| 措勤县| 新竹县| 湾仔区| 绍兴市| 礼泉县|