• 小程序登录


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

    首先整理思路:

      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  请求路径的书写。注意此处为``反单引号,切换英文输入法,位置在数字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")

      3.3 post请求和get请求的'content-type'不同,content-type设置错误可能造成后台接受不到数据。

        post:

        realxxx: function (tim){
          var that=this;
          var fordata = {"realAge": "0",
            "filename": tim,
          }
          wx.request({
            url: 'http://xxx', //仅为示例,并非真实的接口地址
            data: fordata,
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
            },
            success: function (res) { },
            fail: function (err) {  }
          })
    
        },

        get:

       sendxxx: function () {
          var that = this;
            wx.request({
              url: 'http://xxx', //仅为示例,并非真实的接口地址
              data: {
             
              },
              method: 'GET',
              header: {
                'content-type': 'application/json' // 默认值
              },
              success: function (res) {
              },
              fail: function (err) {   }
            })
          
     
        }

      3.4 未完待续



  • 相关阅读:
    Swift
    Swift
    POJ2029——Get Many Persimmon Trees
    windows-install-python-and-sphinx(*.rst file)
    【cocos2d-x】尝鲜 Cocos Code IDE(不断更新)
    mysql 删除重复数据sql声明
    开销是有益的:AppCan 至HTML5移动创新和创业精神和健康
    hibernate它5.many2one单向
    Insecure default in Elasticsearch enables remote code execution
    TestNg显示器(一个)-----监听器,类型和配置使用---另外META-INF详细解释
  • 原文地址:https://www.cnblogs.com/dztHome/p/12162719.html
Copyright © 2020-2023  润新知