const request = require('request');

function main() {
    const accessToken = cache.get('oauth', 'dingtalk')
    if (accessToken == null) {
        return false
    }
    const unionId = payload.get().query['unionId']

    const req = {
        url: `https://oapi.dingtalk.com/topapi/user/getbyunionid?access_token=${accessToken}`,
        method: 'POST',
        content_type: 'application/json',
        body: JSON.stringify({
            unionid: unionId
        })
    }

    const res = request.parse(req).body.toObject()
    //     {
    //   "errcode":"0",
    //   "errmsg":"ok",
    //   "result":{
    //    "contact_type":"0",
    //    "userid":"zhangsan"
    //   },
    //   "request_id": "zcqi5450rpit"
    // }
    if (res.errcode == 0) {
        return okMsg(res)
    }
    return errMsg(507, `获取用户信息失败,错误代码: ${res.errcode}`)
}