把颜色定义迁移到unitool.js; 添加 callBackend 方法,来整合 uniCloud.callFunction 和 uni.request
This commit is contained in:
		
							parent
							
								
									d15c63c716
								
							
						
					
					
						commit
						4e879ad1c7
					
				@ -3,6 +3,14 @@
 | 
				
			|||||||
// #endif
 | 
					// #endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = {
 | 
					module.exports = {
 | 
				
			||||||
 | 
					  RED: 'error',
 | 
				
			||||||
 | 
					  GREEN: 'success',
 | 
				
			||||||
 | 
					  BLUE: 'primary',
 | 
				
			||||||
 | 
					  YELLOW: 'warning',
 | 
				
			||||||
 | 
					  GREY: 'info',
 | 
				
			||||||
 | 
					  BLACK_TOAST: 'default',
 | 
				
			||||||
 | 
					  WHITE_BUTTON: 'default',
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  clog(...message) {
 | 
					  clog(...message) {
 | 
				
			||||||
    console.log(
 | 
					    console.log(
 | 
				
			||||||
      '【【【【【【【【【【',
 | 
					      '【【【【【【【【【【',
 | 
				
			||||||
@ -16,9 +24,7 @@ module.exports = {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  async request({ method = 'POST', url, header = {}, data = {} }) {
 | 
					  async request({ method = 'POST', url, header = {}, data = {} }) {
 | 
				
			||||||
    url = this.makeUrl(url)
 | 
					    url = this.makeUrl(url)
 | 
				
			||||||
    if (uni.getStorageSync('_passtoken')) {
 | 
					    header._passtoken = uni.getStorageSync('_passtoken')
 | 
				
			||||||
      header._passtoken = uni.getStorageSync('_passtoken')
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    if (method === 'GET') {
 | 
					    if (method === 'GET') {
 | 
				
			||||||
      // 如果不是 POST 方法,要额外把参数JSON化
 | 
					      // 如果不是 POST 方法,要额外把参数JSON化
 | 
				
			||||||
      for (let key in data) {
 | 
					      for (let key in data) {
 | 
				
			||||||
@ -32,6 +38,38 @@ module.exports = {
 | 
				
			|||||||
    return [error, response]
 | 
					    return [error, response]
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  async callBackend({ backend = this.BACKEND, method = 'POST', api = 'api', who, act, what = {} }) {
 | 
				
			||||||
 | 
					    console.log('👇 < BackendRequest > ', { who, act, what }, ' < /BackendRequest > 👇')
 | 
				
			||||||
 | 
					    let result = {}
 | 
				
			||||||
 | 
					    if (backend === 'CLOUD') {
 | 
				
			||||||
 | 
					      let { success, header, requestedId, result: resultServer = {} } = await uniCloud.callFunction({
 | 
				
			||||||
 | 
					        name: who,
 | 
				
			||||||
 | 
					        data: {
 | 
				
			||||||
 | 
					          action: act,
 | 
				
			||||||
 | 
					          params: Object.assign(what, { _passtoken: uni.getStorageSync('_passtoken') }),
 | 
				
			||||||
 | 
					          // uniIdToken // uniCloud自动getStorageSync('uni_id_token')并传递为 uniIdToken;也可自行传入 uniIdToken
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					      })
 | 
				
			||||||
 | 
					      result = resultServer
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      if (method === 'GET') {
 | 
				
			||||||
 | 
					        // 如果不是 POST 方法,要额外把参数JSON化
 | 
				
			||||||
 | 
					        for (let key in what) {
 | 
				
			||||||
 | 
					          what[key] = JSON.stringify(what[key])
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      let [error, { data: resultCloud = {}, statusCode, header, errMsg } = {}] = await uni.request({
 | 
				
			||||||
 | 
					        method,
 | 
				
			||||||
 | 
					        url: this.makeUrl(`${api}/${who}/${act}`),
 | 
				
			||||||
 | 
					        header: { _passtoken: uni.getStorageSync('_passtoken') },
 | 
				
			||||||
 | 
					        data: what,
 | 
				
			||||||
 | 
					      })
 | 
				
			||||||
 | 
					      result = resultCloud
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    console.log('👆 < BackendResult > ️', result, '< /BackendResult > 👆')
 | 
				
			||||||
 | 
					    return result
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async pickupFile({
 | 
					  async pickupFile({
 | 
				
			||||||
    mediaType = 'image',
 | 
					    mediaType = 'image',
 | 
				
			||||||
    count = 1,
 | 
					    count = 1,
 | 
				
			||||||
@ -42,6 +80,12 @@ module.exports = {
 | 
				
			|||||||
    formData = {},
 | 
					    formData = {},
 | 
				
			||||||
    name = 'file',
 | 
					    name = 'file',
 | 
				
			||||||
  } = {}) {
 | 
					  } = {}) {
 | 
				
			||||||
 | 
					    if (uni.getStorageSync('_passtoken')) {
 | 
				
			||||||
 | 
					      header._passtoken = uni.getStorageSync('_passtoken')
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      return [{ _ERROR: 'USER_OFFLINE', errMsg: 'offline user cannot upload files' }, null]
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let filePath
 | 
					    let filePath
 | 
				
			||||||
    if (mediaType === 'image') {
 | 
					    if (mediaType === 'image') {
 | 
				
			||||||
      let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType })
 | 
					      let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType })
 | 
				
			||||||
@ -54,12 +98,6 @@ module.exports = {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (filePath) {
 | 
					    if (filePath) {
 | 
				
			||||||
      if (uni.getStorageSync('_passtoken')) {
 | 
					 | 
				
			||||||
        header._passtoken = uni.getStorageSync('_passtoken')
 | 
					 | 
				
			||||||
      } else {
 | 
					 | 
				
			||||||
        return [{ _ERROR: 'USER_OFFLINE', errMsg: 'offline user cannot upload files' }, null]
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      for (let key in formData) {
 | 
					      for (let key in formData) {
 | 
				
			||||||
        // multer 不会自动处理 JSON 数据,必须前后端配合处理
 | 
					        // multer 不会自动处理 JSON 数据,必须前后端配合处理
 | 
				
			||||||
        formData[key] = JSON.stringify(formData[key])
 | 
					        formData[key] = JSON.stringify(formData[key])
 | 
				
			||||||
@ -81,6 +119,12 @@ module.exports = {
 | 
				
			|||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async pickupFile2Cloud({ mediaType = 'image', count = 1, sizeType = ['original', 'compressed'], sourceType = ['album', 'camera'], maxDuration } = {}) {
 | 
					  async pickupFile2Cloud({ mediaType = 'image', count = 1, sizeType = ['original', 'compressed'], sourceType = ['album', 'camera'], maxDuration } = {}) {
 | 
				
			||||||
 | 
					    if (uni.getStorageSync('_passtoken')) {
 | 
				
			||||||
 | 
					      header._passtoken = uni.getStorageSync('_passtoken')
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      return { _state: 'USER_OFFLINE', errMsg: 'offline user cannot upload files' }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let filePath,
 | 
					    let filePath,
 | 
				
			||||||
      cloudPath,
 | 
					      cloudPath,
 | 
				
			||||||
      systemInfo = this.getSystemInfo()
 | 
					      systemInfo = this.getSystemInfo()
 | 
				
			||||||
@ -299,4 +343,23 @@ module.exports = {
 | 
				
			|||||||
    let outputEncoding = output === 'buf' ? undefined : output // (my.OUTPUT_LIST.indexOf(output)>=0?output:my.OUTPUT)  // option.output: 留空=》默认输出hex格式;或者手动指定 'buf', hex', 'latin1' or 'base64'
 | 
					    let outputEncoding = output === 'buf' ? undefined : output // (my.OUTPUT_LIST.indexOf(output)>=0?output:my.OUTPUT)  // option.output: 留空=》默认输出hex格式;或者手动指定 'buf', hex', 'latin1' or 'base64'
 | 
				
			||||||
    return require('crypto').createHash(hasher).update(data, inputEncoding).digest(outputEncoding)
 | 
					    return require('crypto').createHash(hasher).update(data, inputEncoding).digest(outputEncoding)
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  regcode2aiid(code) {
 | 
				
			||||||
 | 
					    if (typeof code === 'string' && /^[a-zA-Z0-9]+$/.test(code)) {
 | 
				
			||||||
 | 
					      const alphabet = 'e5fcdg3hqa4b1n0pij2rstuv67mwx89klyz'
 | 
				
			||||||
 | 
					      const base = 16367
 | 
				
			||||||
 | 
					      code = code.toLowerCase()
 | 
				
			||||||
 | 
					      let len = code.length
 | 
				
			||||||
 | 
					      let num = 0
 | 
				
			||||||
 | 
					      for (let i = 0; i < len; i++) {
 | 
				
			||||||
 | 
					        num += alphabet.indexOf(code[i]) * Math.pow(alphabet.length, i)
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      let aiid = num / (base - alphabet.length) - base
 | 
				
			||||||
 | 
					      if (aiid >= 0 && Number.isInteger(aiid)) {
 | 
				
			||||||
 | 
					        // 允许 aiid===0:当第一个用户(aiid==1)登录时,需要一个系统默认的邀请码。
 | 
				
			||||||
 | 
					        return aiid
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return null
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user