码迷,mamicode.com
首页 > 微信 > 详细

小程序登录

时间:2020-01-07 17:49:12      阅读:2103      评论:0      收藏:0      [点我收藏+]

标签:style   pass   lse   ken   func   stat   示例   icon   研究   

小程序新手上路磕磕碰碰终于把登录给弄出来了

首先整理思路:

  1.登录 并 保存cookis

  3.替换其他请求的head

 

实现:

1.登录

 formSubmit: function (e) {
    wx.showLoading({
      title: ‘登录中...‘,
    })
    console.log(e);
    this.setData({ disabled: true });
    wx.request({
      url: ‘http://127.0.0.1:9999/mytest/login‘, //仅为示例,并非真实的接口地址
      data: {
        username: e.detail.value.no,
        password: e.detail.value.pwd
      },
      header: {
        ‘content-type‘: ‘application/json‘, // 默认值
      
      },
      success: function (res) {
        console.log(res);
        if (res.statusCode == 200) {
          if (res.data.error == true) {
            wx.showToast({
              title: res.data.msg,
              icon: ‘none‘,
              duration: 2000
            })
            
          } else {
            wx.setStorageSync(‘student‘, res.data.data);
            wx.showToast({
              title: res.data.msg,
              icon: ‘success‘,
              duration: 2000
            })
       //缓存JSESSIONID wx.setStorage({ key:
"token", data: res.cookies[0], }) wx.navigateTo({ url: ‘../packageSell/packageSell‘ , }) } } else { wx.showToast({ title: ‘服务器出现错误‘, icon: ‘none‘, duration: 2000 }) } } }) },

2.替换其他请求head

goPackageSellPage: function (e) {
    wx.showLoading({
      title: ‘请求中...‘,
    })
    console.log(e);
    this.setData({ disabled: true });

    var header;
    header = {
      ‘content-type‘: ‘application/json‘,
      ‘cookie‘: wx.getStorageSync("token")//读取缓存JSESSIONID,实现权限认证
    };

    wx.request({
      url: ‘http://127.0.0.1:9999/mytest/user/dataxx‘, //仅为示例,并非真实的接口地址
      data: {
      
      },
      header: header,
      success: function (res) {
        console.log(res);
        if (res.statusCode == 200) {
          if (res.data.error == true) {
            wx.showToast({
              title: res.data.msg,
              icon: ‘none‘,
              duration: 2000
            })

          } else {
            wx.setStorageSync(‘student‘, res.data.data);
            wx.showToast({
              title: res.data.msg,
              icon: ‘success‘,
              duration: 2000
            })
            wx.navigateTo({
              url: ‘../index/index‘,
            })
          }
        } else {
          wx.showToast({
            title: ‘服务器出现错误‘,
            icon: ‘none‘,
            duration: 2000
          })
        }
      }
    })
  },

3.意外收获

本次研究学到几点小程序代码小技巧

  3.1  请求路径的书写

//新建param.js保存
export const rootUrl2 = ‘http://127.0.0.1/‘
export const rootUrl = `${rootUrl2}mytest`;

//有路径的界面js文件头部
import {rootUrl} from ‘../../utils/params‘

//路径书写
 url: `${rootUrl}/user/dataxx`,
 url:`${rootUrl}/images/upload/bg.jpg` ,

 

  3.2 缓存的写入与读取

//写入  
wx.setStorage({
              key: "xxx",
              data:"message",
            })

//读取
wx.getStorageSync("xxx")

小程序登录

标签:style   pass   lse   ken   func   stat   示例   icon   研究   

原文地址:https://www.cnblogs.com/dztHome/p/12162719.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!