微信小程序-请求封装

//import request from '../../utils/network.js'
export const host = ''; // 域名
let lock =true
export default function request(opt){ 
  return new Promise((resolve,reject)=>{
    if (opt.isLock) {
      if (!lock) return;
      lock = false;
    }
    // 默认开启loading
    if (!opt.isLocking) {
      wx.showLoading({
        title: '加载中...'
      });
    }
    wx.request({
      url: `${host}${opt.url}`,
      method:opt.method||'post',
      data:opt.data||{},
      header:{
        'content-type': opt.hedaer || 'application/json', // 默认值
        'token': wx.getStorageSync('token')
      },
      success:function(res){
        resolve(res)
      },
      fail:function(res){
        reject(res)
      },
      complete:function(res){
        if (opt.isLock) {
          setTimeout(()=>{
            lock=true
          }, 5000)
        }
        if (!opt.isLocking) {
          wx.hideLoading();
        }       
      }
    })
  })
}

//import {authorizeUser} from '../../utils/login.js'
//import {isSession} from '../../utils/login.js'
import request from '/network.js'
export function login(callback) {
  wx.login({
    success(res) {
      let code = res.code
      if (res.code) {
        getUserInfo((res) => { 
          let rawData = res.rawData
          request({
            url: '/api/Auth/Register',
            data: {
              code: code
            }
          }).then(res => {
            let token = res.data.data.token
            wx.setStorageSync('token', token);
            request({
              url: '/api/Auth/Login',
              data: {
                rawData: rawData
              }
            }).then(res => {
              console.log(res)
            })
          }).catch(res=>{
            console.log(res)
          })    
        })
      } else {
        console.log('登录失败!' + res.errMsg)
      }
    }
  })
}


// 用户授权
export function authorizeUser(ev, callback, logFn) {
  wx.getSetting({
    success: function (res) {
      if (res.authSetting['scope.userInfo']) {
        // 用户授权之后将用户信息存入缓存
        const userInfo = ev.detail;
        wx.setStorageSync('userInfo', JSON.stringify(userInfo));
        // 已授权
        callback(true);
        login(() => {
          // 登录成功
          if (logFn) {
            logFn();
          }
        });
      } else {
        callback(false);
        wx.showModal({
          title: '提示',
          content: '请允许授权,否则将会影响小程序的部分功能!',
          success(res1) {

          }
        })
        console.log('用户没有授权')
      }
    },
    fail: function () {
      console.log('失败');
    }
  });
}

// 获取用户信息
export function getUserInfo(callback) {
  const userInfo = wx.getStorageSync('userInfo');
  if (userInfo) {
    callback(JSON.parse(userInfo));
  } else {
    wx.getUserInfo({
      success(res) {
        wx.setStorageSync('userInfo', JSON.stringify(res));
        callback(res);
      }
    })
  }
}

export function isSession(callback, logFn) {
  wx.getSetting({
    success(res) {
      if (res.authSetting['scope.userInfo']) {
        callback(true);
        // 已授权
        const userSession = wx.getStorageSync('token'); // 这里是token
        console.log(userSession)
        // 没有token就登录获取,反之则不需要登录,直接返回回调
        if (!userSession) {
          login(() => {
            if (logFn) {
              logFn();
            }
          });
        } else {
          if (logFn) {
            logFn();
          }
        }
      } else {
        // 未授权
        callback(false);
      }
    },
    fail() {}
  });
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

屋顶上的小喵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值