Skip to content

Instantly share code, notes, and snippets.

@abearxiong
Last active May 4, 2019 13:41
Show Gist options
  • Save abearxiong/97c84d21be8be0993a9474aa48f2d748 to your computer and use it in GitHub Desktop.
Save abearxiong/97c84d21be8be0993a9474aa48f2d748 to your computer and use it in GitHub Desktop.
tencent

腾讯的api网关,使用记录

  1. 新建服务,创建API
  2. 使用无服务函数scf

无服务函数创建函数

  1. 函数代码
'use strict'
/**************************************************
公有云 - API网关触发云函数
***************************************************/

const nums = "0123456789", upperCases = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", lowerCases = "abcdefghijklmnopqrstuvwxyz", characters = "!@#$%^&*()_+~{}", allCharacters = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~�" //没有空格
var GetRandomNum = async (Min,Max)=>
{   
    var Range = Max - Min;   
    var Rand = Math.random();   
    return(Min + Math.round(Rand * Range));   
}   
var printFromTo = async (arg1,arg2)=>{
    let from = arg1 || '0'
    let length = arg2 || 10
    let value = ''
    for(let i = 0;i < length; i++){
        let char2ascii = from.charCodeAt()
        let ascii2char = String.fromCharCode(char2ascii+i)
        value = value + ascii2char
    }
    console.log(value)
}
//printFromTo('!',94)
var getPwd= async (length, strs) =>{
    console.log("长度:",length,"字符串:",strs)
    let res = ''
    for(let i = 0;i<length;i++){
        let random = await GetRandomNum(0,strs.length-1)
        res += strs[random]
    }
    return res
}
var generatePassword = async(length,isNum,isUpperCase,isLowerCase,isCharacter,isAll) => {
    let strs = ''
    let flag = 0 // 如果参数为空的情况下执行
    if(isNaN(length)){
      length = 8
    }
    length = length || 8
    if(isNum){
        strs = strs + nums
        flag = 1
    }
    if(isUpperCase){
        strs = strs + upperCases
        flag = 1
    }
    if(isLowerCase){
        strs = strs + lowerCases
        flag = 1
    }
    if(isCharacter){
        strs = strs + characters
        flag = 1
    }
    if(isAll){
        strs = allCharacters
        flag = 1
    }
    if(!flag){
        strs = nums + lowerCases
    }
    return await getPwd(length,strs)
}
exports.main_handler = async (event, context, callback) => {
  let query = event.queryStringParameters
  console.log(query)
  let length = query.plength?query.plength : 8
  let isNum = query.num == 1?1:0
  let isUpperCase = query.uppercase == 1?1:0
  let isLowerCase = query.lowercase == 1?1:0
  let isCharacter = query.character == 1?1:0
  let isAll = query.all == 1?1:0
  //let pd = await generatePassword(8,1,0,0,1,0)
  console.log("isAll",isAll)
  let pd = await generatePassword(length,isNum,isUpperCase,isLowerCase,isCharacter,isAll)
  const body = pd
  return {
    isBase64: false,
    statusCode: 200,
    headers: { 'Content-Type': 'text', 'Access-Control-Allow-Origin': '*' },
    body: body
  }
}
  1. 绑定函数到服务
  2. 发布服务,可以访问
  3. 使用JDK

本地化使用sdk,需要修改version,默认无效的日期格式;nodejs 耽搁了我一会的时间;npm i 引入只需要 require('./src');其他复制SDK即可

后面的是API SKD,腾讯云自动创建的。无需看

# API网关SDK使用指南

- API 更新日期: 2019-05-04 17:39:35
- SDK 创建日期: 2019-05-04T20:15:15.188+08:00

Password - JavaScript client for password

##目录
* 一、SDK简介
* 二、SDK安装
* 三、本SDK的API信息

## 一、SDK简介
本SDK是API网关根据您自定义的API接口自动生成的 Javascript 调用代码,通过使用SDK,您可以更方便地访问腾讯云提供的API网关服务。下面向您介绍如何使用API网关的SDK。

主要文件的层级结构如下:

* src/ 目录
  * api/ 目录   `包含用户自定义的所有API接口`
  * ApiClient.js
  * index.js
* docs/ 目录      `存放API接口文档`
* README.md       `本SDK使用指南`
* swagger.json    `包含所有API定义的swagger文件,您可以用此文件在`[这里](https://editor.swagger.io//?_ga=2.13932151.1152137434.1516190014-1544097500.1514542032#/)`调试您的API`



## 二、SDK安装

### For [Node.js](https://nodejs.org/)

#### npm

To publish the library as a [npm](https://www.npmjs.com/),
please follow the procedure in ["Publishing npm packages"](https://docs.npmjs.com/getting-started/publishing-npm-packages).

Then install it via:

```shell
npm install password --save
Local development

To use the library locally without publishing to a remote npm registry, first install the dependencies by changing into the directory containing package.json (and this README). Let's call this JAVASCRIPT_CLIENT_DIR. Then run:

npm install

Next, link it globally in npm with the following, also from JAVASCRIPT_CLIENT_DIR:

npm link

Finally, switch to the directory you want to use your password from, and run:

npm link /path/to/<JAVASCRIPT_CLIENT_DIR>

You should now be able to require('password') in javascript files from the directory you ran the last command above from.

git

If the library is hosted at a git repository, e.g. https://github.com/GIT_USER_ID/GIT_REPO_ID then install it via:

    npm install GIT_USER_ID/GIT_REPO_ID --save

For browser

The library also works in the browser environment via npm and browserify. After following the above steps with Node.js and installing browserify with npm install -g browserify, perform the following (assuming main.js is your entry file, that's to say your javascript file where you actually use this library):

browserify main.js > bundle.js

Then include bundle.js in the HTML pages.

Webpack Configuration

Using Webpack you may encounter the following error: "Module not found: Error: Cannot resolve module", most certainly you should disable AMD loader. Add/merge the following section to your webpack config:

module: {
  rules: [
    {
      parser: {
        amd: false
      }
    }
  ]
}

Getting Started

Please follow the installation instruction and execute the following JS code:

var Password = require('password');

var api = new Password.PasswordApi()

var opts = { 
  'all': 0, // {Number} 所有
  'character': 0, // {Number} 字符
  'lowercase': 0, // {Number} 是否小写字母
  'num': 1, // {Number} 是否数字
  'plength': 8, // {Number} 长度
  'uppercase': 0 // {Number} 是否大写字母
};

var callback = function(error, data, response) {
  if (error) {
    console.error(error);
  } else {
    console.log('API called successfully. Returned data: ' + data);
  }
};
api.getApiPassword(opts, callback);

三、本SDK的API信息

password generate

  • 默认域名: service-bccvblr9-1252152609.ap-beijing.apigateway.myqcloud.com/release

All URIs are relative to http://service-bccvblr9-1252152609.ap-beijing.apigateway.myqcloud.com/release

Class API名称 HTTP 请求方法与路径 描述
Password.PasswordApi getApiPassword GET /api/password

更多详细信息请查看docs下的API文档

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment