index.obj.js 3.6 KB
Newer Older
DCloud_JSON's avatar
3.4.40  
DCloud_JSON 已提交
1
const httpApi = require('./httpApi.js')
DCloud_JSON's avatar
DCloud_JSON 已提交
2 3 4 5 6 7 8
const conversation = require('./conversation.js')
const msg = require('./msg.js')
const push = require('./push.js')
const friend = require('./friend.js')
const group = require('./group.js')
const filtered_conversation = require('./filtered-conversation.js')
const tools = require('./tools.js')
DCloud_JSON's avatar
3.4.40  
DCloud_JSON 已提交
9 10 11 12 13 14 15 16 17
const modules = Object.assign(
  conversation,
  msg,
  push,
  friend,
  group,
  filtered_conversation,
  tools
)
DCloud_JSON's avatar
DCloud_JSON 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

module.exports = {
  async _before() {
    // 记录请求时间
    this.requestStartTime = Date.now()
    
    // 获取客户端信息
    this.clientInfo = this.getClientInfo()
    
    // 调用扩展插件的初始化接口
    const { invokeExts } = require('uni-im-ext')
    await invokeExts('ext-init',this.clientInfo)

    // 定义uni-id公共模块对象
    const uniIdCommon = require('uni-id-common')
    this.uniIdCommon = uniIdCommon.createInstance({
      clientInfo: this.clientInfo
    })
DCloud_JSON's avatar
3.4.40  
DCloud_JSON 已提交
36
    
DCloud_JSON's avatar
DCloud_JSON 已提交
37
    // 除非特定方法允许未登录用户调用,其它都需要验证用户的身份
DCloud_JSON's avatar
3.4.40  
DCloud_JSON 已提交
38
    const allowedMethodsWithouLogin = ["httpApi"]
DCloud_JSON's avatar
DCloud_JSON 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51
    if (!allowedMethodsWithouLogin.includes(this.getMethodName())) {
      if (this.getClientInfo().source == 'function') {
        // 云函数互调时,免校验 token 直接使用传来的用户 id
        this.current_uid = this.getParams()[1]
        this.current_user_role = []
      } else {
        // 客户端调用时验证 uni-id token
        let res = await this.uniIdCommon.checkToken(this.clientInfo.uniIdToken)
        // console.log('checkToken res',res);
        if (res.errCode) {
          // 如果token校验出错,则抛出错误
          throw res
        }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
52 53 54 55 56 57 58 59 60 61 62
        
        // 用于临时禁用IM的用户,临时方案用于突发情况。当用户二次登录时,role会自动带disable_im
        const tmpBlackUid = []
        if (res.role.includes('disable_im') || tmpBlackUid.includes(res.uid)) {
          // 如果用户被禁用IM,则抛出错误
          throw {
            errSubject: 'uni-im-co',
            errCode: 'USER_DISABLED_IM',
            errMsg: '你的账号,已被禁止使用uni-im'
          }
        }
DCloud_JSON's avatar
DCloud_JSON 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

        // token 有效,取出 id 和角色
        this.current_uid = res.uid
        this.current_user_role = res.role
      }
    }

    // 为方便云对象内部调用 sendPushMsg() 方法,把它挂在云对象实例上
    this.sendPushMsg = push.sendPushMsg

    // 提供一个方法,用于添加并发任务
    this._promises = []
    this.addPromise = (promise) => {
      this._promises.push(promise)
    }
  },
DCloud_JSON's avatar
3.4.40  
DCloud_JSON 已提交
79
  
DCloud_JSON's avatar
DCloud_JSON 已提交
80 81
  async _after(error, result) {
    // 请求完成时间
DCloud_JSON's avatar
DCloud_JSON 已提交
82
    // console.error('请求完成时间', Date.now() - this.requestStartTime, 'ms')
DCloud_JSON's avatar
DCloud_JSON 已提交
83 84
    
    if (error) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
85 86 87 88 89 90
      console.error( '云对象_after发现错误' + error.message,JSON.stringify({error,result}) );
      console.error({
        "getMethodName":this.getMethodName(),
        "getParams":this.getParams(),
        "getClientInfo":this.getClientInfo()
      });
91
      console.error(error.stack)
DCloud_JSON's avatar
DCloud_JSON 已提交
92 93 94 95
      if (error.errCode && error.errMsg) {
        // 符合响应体规范的错误,直接返回
        return error
      } else {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
96 97 98 99 100
        return {
          errSubject: "uni-im-co",
          errCode: "unicloud throw error",
          errMsg: error.message
        }
DCloud_JSON's avatar
DCloud_JSON 已提交
101 102 103 104 105 106 107
      }
    }

    // 如果有并发任务,则等它们都执行完再返回
    if (this._promises.length > 0) {
      await Promise.all(this._promises)
    }
DCloud_JSON's avatar
DCloud_JSON 已提交
108 109
    
    // console.error('云函数结束时间', Date.now() - this.requestStartTime, 'ms')
DCloud_JSON's avatar
DCloud_JSON 已提交
110 111 112

    return result || { errCode: 0 }
  },
DCloud_JSON's avatar
3.4.40  
DCloud_JSON 已提交
113 114 115 116
  ...modules,
  httpApi(){
    return httpApi.call(this,modules)
  }
DCloud_JSON's avatar
DCloud_JSON 已提交
117
}