Promise的优化方法

公共请求的promsie的封装

export function getJseKey ({value = '' } = {}) {
  return new Promise((resolve, reject) => {
    getEncryptKey().then(res => {
      let pubKey = res.data && res.data.pub_key
      const jse = new JSEncrypt()
      jse.setPublicKey(`-----BEGIN PUBLIC KEY-----` +
      pubKey +
      `-----END PUBLIC KEY-----`)
      let key = jse.encrypt(value)
      resolve(key)
    }).catch(err => {
      reject(err)
    })
  })
}

原先代码

---- 公共请求的promise的封装 前
getEncryptKey().then(res => {
      return res.data.pub_key
  }).then(pubKey => {
      // 通过公钥加密密码
      let { key } = this.setKey({ pubKey, value: this.data.password })
----  
      // 请求登录接口
      let params = {
        account: this.data.phoneNum,
        password: key
      }
      return signInByPassowrd(params)
     }).then(res => {
      token = res.token
      isNewUser = res.data.is_new_user
      userModel = res.data
      wx.setStorageSync('token', token)
      // isNewUser = true
      return utils.getWxcode()
    }).then(res => {
      // 获取openid
      return fetchOpenId({ js_code: res.code })
    }).then(res => {
      userModel.openid = res.data.openid
      if (isNewUser) {
        // 临时存储
        app.globalData.token = token
        app.globalData.userModel = userModel
        callback(token, isNewUser)
      } else {
        wx.setStorageSync('userModel', userModel)
        callback(token, isNewUser)
      }
    }).catch(err => {
      console.log("登录接口-报错", err)
      wx.hideLoading()
      wx.showToast({
        title: err && err.msg ? err.msg : '登录接口出错',
        icon: 'none',
        duration: 2000
      })
    })

现在的代码

-----  公共请求的promise的封装 后
getJseKey({ value: this.data.password }).then(passwordKey => {
      // 请求登录接口
      let params = {
        account: this.data.phoneNum,
        password: passwordKey
      }
-----
      return signInByPassowrd(params)
    }).then(res => {
      token = res.token
      isNewUser = res.data.is_new_user
      userModel = res.data
      wx.setStorageSync('token', token)
      // isNewUser = true
      return utils.getWxcode()
    }).then(res => {
      // 获取openid
      return fetchOpenId({ js_code: res.code })
    }).then(res => {
      userModel.openid = res.data.openid
      if (isNewUser) {
        // 临时存储
        app.globalData.token = token
        app.globalData.userModel = userModel
        callback(token, isNewUser)
      } else {
        wx.setStorageSync('userModel', userModel)
        callback(token, isNewUser)
      }
    }).catch(err => {
      console.log("登录接口-报错", err)
      wx.hideLoading()
      wx.showToast({
        title: err && err.msg ? err.msg : '登录接口出错',
        icon: 'none',
        duration: 2000
      })
    })
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,040评论 3 119
  • 设计模式概述 在学习面向对象七大设计原则时需要注意以下几点:a) 高内聚、低耦合和单一职能的“冲突”实际上,这两者...
    彦帧阅读 9,153评论 0 14
  • 中秋,中国一个最最传统的节日,寄托着无数远方游子那颗思乡的心,尽管我已在离家乡不远的城市里安家落户,有了新的...
    游仔阅读 1,638评论 0 0
  • 昨天猫叔讲到找准自己的细分领域,而今天的分享就是细分领域里的牛人是如何炼成的,我最大的感触就是问题背后是机...
    遥遥仲夏阅读 3,931评论 0 0
  • 聪明不代表智慧。聪明是一种生存的能力,它多数是得益于一种遗传;而智慧,是一种生存境界,它更多的是后天的一种修为。 ...
    Kayan阿嘉阅读 3,868评论 0 21