网友真实露脸自拍10p,成人国产精品秘?久久久按摩,国产精品久久久久久无码不卡,成人免费区一区二区三区

小程序模板網(wǎng)

微信小程序后臺登錄(非微信賬號登錄)

發(fā)布時間:2018-02-10 11:17 所屬欄目:小程序開發(fā)教程


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

廢話不說,直接上代碼

找到app.js在里面寫如下代碼


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"
  },

敲黑板劃重點:上圖中的代碼片段重要的地方就是:“globalData中的 adminUserViewId: "",token: "" ”
這兩個參數(shù)是前端需要存儲的后臺參數(shù),用來標(biāo)記用戶的登錄狀態(tài)的。

然后建一個login文件夾,在login.wxml中寫如下代碼


<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">賬號:</text>
    <input class="login_text" placeholder="請輸入登錄賬號" bindinput="listenerUsernameInput"/>
  </view>
  <view class="login_view">
    <text class="login_lable">密碼:</text>
    <input class="login_text" placeholder="請輸入密碼" password="true" bindinput="listenerPasswordInput"/>
  </view>
  <view>
    <button class="login_button" bindtap="loginAction">登錄</button>
  </view>
</view>
然后建一個login文件夾,在login.wxss中寫如下代碼

.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中寫如下代碼

//login.js
//獲取應(yīng)用實例
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)聽帳號輸入 */
  listenerUsernameInput: function (e) {
    this.data.username = e.detail.value;
  },
  /** 監(jiān)聽密碼輸入 */
  listenerPasswordInput: function (e) {
    this.data.password = e.detail.value;
  },
  // 登錄按鈕點擊事件
  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, //僅為示例,并非真實的接口地址
      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) {
          // 后臺傳遞過來的值
          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存儲到本地
          wx.setStorageSync('adminUserViewId', adminUserViewId);
          wx.setStorageSync('token', token);
          console.log("登錄成功的adminUserViewId:" + adminUserViewId);
          console.log("登錄成功的token:" + token);
          // 切換到首頁
          wx.switchTab({
            url: '/pages/index/index'
          })
        } else {
          that.$wuxToast.show({
            type: 'text',
            timer: 1000,
            color: '#fff',
            text: res.data.msg,
            success: () => console.log('登錄失敗,請稍后重試。' + res.data.msg)
          })
        }
      },
      fail: function () {
        util.hideToast();
        console.log("登錄失敗");
        that.$wuxToast.show({
          type: 'text',
          timer: 1000,
          color: '#fff',
          text: '服務(wù)器君好累 



易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開源 碼云倉庫:starfork
本文地址:http://www.xiuhaier.com/wxmini/doc/course/21934.html 復(fù)制鏈接 如需定制請聯(lián)系易優(yōu)客服咨詢:800182392 點擊咨詢
QQ在線咨詢
AI智能客服 ×