欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序 后臺(tái)登錄(非微信賬號(hào))實(shí)例詳解

 更新時(shí)間:2017年03月31日 10:18:59   作者:羅小耳  
這篇文章主要介紹了微信小程序 后臺(tái)登錄(非微信賬號(hào))實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下

微信小程序 后臺(tái)登錄

實(shí)現(xiàn)效果圖:

最近寫(xiě)了一個(gè)工具類(lèi)的小程序,按需求要求不要微信提供的微信賬號(hào)登錄,需要調(diào)取后臺(tái)登錄接口來(lái)登錄。由于小程序大部分都是調(diào)取微信信息登錄,很少有調(diào)用自己后臺(tái)來(lái)登錄的,所以寫(xiě)的時(shí)候各種坑,現(xiàn)在把趟好坑的代碼共享給大家吧?。≒S:如有不妥之處,共勉之。)


廢話不說(shuō),直接上代碼

找到app.js在里面寫(xiě)如下代碼

App({
 onLaunch: function () {
  //調(diào)用API從本地緩存中獲取數(shù)據(jù)
  var logs = wx.getStorageSync('logs') || []
  logs.unshift(Date.now())
  wx.setStorageSync('logs', logs)
 },
 globalData: {
  adminUserViewId: "",
  token: "",
  userInfo: null,
  BaseURL:"http://airb.cakeboss.com.cn"
  // BaseURL:"http://192.168.0.107:8080"
 },

敲黑板劃重點(diǎn):上圖中的代碼片段重要的地方就是:“globalData中的 adminUserViewId: "",token: "" ”

這兩個(gè)參數(shù)是前端需要存儲(chǔ)的后臺(tái)參數(shù),用來(lái)標(biāo)記用戶的登錄狀態(tài)的。

然后建一個(gè)login文件夾,在login.wxml中寫(xiě)如下代碼

<import src="../../components/toast.wxml" />

<!-- is="toast" 匹配組件中的toast提示 如果用dialog的話這就是dialog -->
<template is="toast" data="{{ ...$wux.toast }}" />
<view class="login_container">
 <view class="login_view">
  <text class="login_lable">賬號(hào):</text>
  <input class="login_text" placeholder="請(qǐng)輸入登錄賬號(hào)" bindinput="listenerUsernameInput"/>
 </view>
 <view class="login_view">
  <text class="login_lable">密碼:</text>
  <input class="login_text" placeholder="請(qǐng)輸入密碼" password="true" bindinput="listenerPasswordInput"/>
 </view>
 <view>
  <button class="login_button" bindtap="loginAction">登錄</button>
 </view>
</view>

然后建一個(gè)login文件夾,在login.wxss中寫(xiě)如下代碼

.login_container {
 margin-top: 30px;
}

.login_view {
 width: calc(100% - 40px);
 padding: 0 20px;
 line-height: 45px;
 height: 45px;
 margin-bottom: 20px;
}

.login_text {
 float: left;
 height: 45px;
 line-height: 45px;
 font-size: 12px;
 border: 1px solid rgb(241, 242, 243);
 padding: 0 12px;
 width: calc(100% - 70px);
 border-radius: 4px;
}

.login_lable {
 float: left;
 font-size: 12px;
 width: 40px;
}

.login_button {
 width: 150px;
 background: green;
 color: #fff;
}

在login.js中寫(xiě)如下代碼

//login.js
//獲取應(yīng)用實(shí)例
var app = getApp()
var util = require('../../utils/util.js');

Page({
 data: {
  motto: 'Hello World',
  username: "",
  password: ""
 },
 onLoad(options) {
  // 初始化提示框
  this.$wuxToast = app.wux(this).$wuxToast
 },
 /** 監(jiān)聽(tīng)?zhēng)ぬ?hào)輸入 */
 listenerUsernameInput: function (e) {
  this.data.username = e.detail.value;
 },
 /** 監(jiān)聽(tīng)密碼輸入 */
 listenerPasswordInput: function (e) {
  this.data.password = e.detail.value;
 },
 // 登錄按鈕點(diǎn)擊事件
 loginAction: function () {

  var userName = this.data.username;
  var passwords = this.data.password;
  var that = this;

  if (userName === "") {
   that.$wuxToast.show({
    type: 'text',
    timer: 1000,
    color: '#fff',
    text: "用戶名不能為空!",
    success: () => console.log('用戶名不能為空!')
   })
   return;
  } if (passwords === "") {
   that.$wuxToast.show({
    type: 'text',
    timer: 1000,
    color: '#fff',
    text: "密碼不能為空!",
    success: () => console.log('密碼不能為空!')
   })
   return;
  }

  //加載提示框
  util.showLoading("登錄中...");

  var urlStr = app.globalData.BaseURL + '/api/adminUser/login';
  wx.request({
   method: "POST",
   url: urlStr, //僅為示例,并非真實(shí)的接口地址
   data: util.json2Form({
    username: userName,
    password: passwords
   }),
   header: {
    "Content-Type": "application/x-www-form-urlencoded"
   },
   success: function (res) {
    util.hideToast();
    console.log(res.data);
    var code = res.data.code;
    if (code === 200) {
     // 后臺(tái)傳遞過(guò)來(lái)的值
     var adminUserViewId = res.data.data.adminUserViewId;
     var token = res.data.data.token;
     // 設(shè)置全局變量的值
     app.globalData.adminUserViewId = res.data.data.adminUserViewId;
     app.globalData.token = res.data.data.token;
     // 將token存儲(chǔ)到本地
     wx.setStorageSync('adminUserViewId', adminUserViewId);
     wx.setStorageSync('token', token);
     console.log("登錄成功的adminUserViewId:" + adminUserViewId);
     console.log("登錄成功的token:" + token);
     // 切換到首頁(yè)
     wx.switchTab({
      url: '/pages/index/index'
     })
    } else {
     that.$wuxToast.show({
      type: 'text',
      timer: 1000,
      color: '#fff',
      text: res.data.msg,
      success: () => console.log('登錄失敗,請(qǐng)稍后重試。' + res.data.msg)
     })
    }
   },
   fail: function () {
    util.hideToast();
    console.log("登錄失敗");
    that.$wuxToast.show({
     type: 'text',
     timer: 1000,
     color: '#fff',
     text: '服務(wù)器君好累😫,請(qǐng)稍后重試',
     success: () => console.log('登錄失敗,請(qǐng)稍后重試。')
    })
   }
  })
 }
})

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論