var APIBase = require("../../api.base");
var system = require("../../../system");
var settings = require("../../../../config/settings");
class AccessAuthAPI extends APIBase {
   constructor() {
      super();
      this.appS = system.getObject("service.common.appSve");
      this.apitradeSvr = system.getObject("service.common.apitradeSve");
      this.authUtils = system.getObject("util.businessManager.authUtils");
      this.userSve = system.getObject("service.auth.userSve");
   }
   /*查询注册用户的信息sys*/
   async queryRegUser(pobj, query, req){
        var appid=req.app.id;
        var userList=    await this.userSve.queryUsersByAppId(appid);
        return system.getResult(userList);
   }
   /**
    * 发送短信
    * @param {*} pobj {mobile为要发送的手机号，tmplCode为发送短信编码，需在阿里开通，signName为短信头描述信息}
    * @param {*} query 
    * @param {*} req 
    */
   async fetchVCode(pobj, query, req) {
      var appkey = req.app.appkey;
      var mobile = pobj.mobile;
      var tmplCode = pobj.tmplCode;
      var signName = pobj.signName;
      //var msg=pobj.msg;

      if (!mobile) {
         return system.getResult(null, "电话号码不能为空.");
      }
      // if(!msg){
      //    return system.getResult(null,"发送的内容不能为空.");
      // }
      if (!tmplCode) {
         return system.getResult(null, "模版编码不能为空.");
      }
      if (!signName) {
         return system.getResult(null, "短信签名不能为空.");
      }
      if (tmplCode && tmplCode.indexOf("SMS_") < 0) {
         return system.getResult(null, "模版编码有误");
      }
      var vcodeResult = await this.cacheManager["VCodeCache"].cache(appkey + "_" + mobile, tmplCode, 120, signName);
      return system.getResult(vcodeResult);
   }
   /**
       * 发送短信
       * @param {*} pobj {mobile为要发送的手机号，tmplCode为发送短信编码，需在阿里开通，signName为短信头描述信息}
       * @param {*} query 
       * @param {*} req 
       */
   async fetchDefaultVCode(pobj, query, req) {
      var appkey = req.app.appkey;
      var mobile = pobj.mobile;
      if (!mobile) {
         return system.getResult(null, "电话号码不能为空.");
      }
      var vcodeResult = this.cacheManager["VCodeCache"].cache(appkey + "_" + mobile, null, 60);
      return system.getResult(vcodeResult);
   }
   async authAccessKey(pobj, query, req) {
      var acckapp = await this.cacheManager["ApiAccessKeyCache"].cache(pobj.checkAccessKey);
      if (!acckapp) {
         return system.getResultFail(system.tokenFail, "访问token失效，请重新获取");
      }
      return system.getResultSuccess({ isaccess: true, app: acckapp });
   }
   async getAccessKey(pobj, query, req) {
      var appkey = query.appkey;
      var secret = query.secret;
      if (!appkey) {
         appkey = pobj.appkey;
         secret = pobj.secret;
      }
      var appData = await this.authUtils.getTokenInfo(appkey, secret);
      if (appData.status != 0) {
         return appData;
      }

      //构造oath
      var oathstr = settings.homePage() + "?appKey=" + appData.data.appkey + "#/login";
      var oathreg = settings.homePage() + "?appKey=" + appData.data.appkey + "#/register";
      appData.data.oauthlogin = oathstr;
      appData.data.oauthreg = oathreg;
      var rtnData = {};
      rtnData.accessKey = appData.data.accessKey;
      delete appData.data["accessKey"];
      rtnData.app = appData.data;
      return system.getResultSuccess(rtnData);
   }
   async apiAccessCount(p, q, r) {
      var tradeObj = {
         srcappkey: p.srcappkey,
         tradeType: "consume",
         op: p.op,
         params: p.params,
         clientIp: p.clientIp,
         agent: p.agent,
         destappkey: p.destappkey
      }
      var t = await this.apitradeSvr.create(tradeObj);
      return system.getResultSuccess(t);
   }
   async register(pobj, qobj, req) {
      if (!pobj.userName) {
         return system.getResult(null, "用户名不能为空");
      }
      if (!pobj.mobile) {
         return system.getResult(null, "手机号不能为空");
      }
      if (!pobj.password) {
         return system.getResult(null, "密码不能为空");
      }
      var param = {
         app_id: req.app.id,
         userName: pobj.userName,
         mobile: pobj.mobile,
         password: pobj.password
      }
      var ruser = await this.userSve.register(param);
      if (!ruser) {
         return system.getResultFail(system.existUserRegFail, "已经存在此用户，注册失败");
      }//status=2000
      var opencode = super.getUUID();
      var authUrl = req.app.authUrl + "?opencode=" + opencode;
      await this.cacheManager["OpenCodeCache"].cache(opencode, ruser, 30);
      return system.getResultSuccess({ auth_url: authUrl, opencode: opencode, open_user_id: ruser.id, account_id: ruser.account_id });
   }

   async modifyPassword(pobj, qobj, req) {
      if (!pobj.account_id) {
         return system.getResult(null, "用户名不能为空");
      }

      if (!pobj.password) {
         return system.getResult(null, "密码不能为空");
      }

      var param = {
         account_id: pobj.account_id,
         password: pobj.password,
      }
      await this.userSve.modifyPassword(param);
      return system.getResultSuccess();
   }

   async login(pobj, qobj, req) {
      if (!pobj.userName) {
         return system.getResult(null, "用户名不能为空");
      }
      if (!pobj.password) {
         return system.getResult(null, "密码不能为空");
      }
      var param = {
         app_id: req.app.id,
         userName: pobj.userName,
         password: pobj.password
      }
      var ruser = await this.userSve.getUserByUserNamePwdForApi(param);
      if (!ruser) {
         return system.getResultFail(system.userLoginFail, "用户名或密码错误");
      }//status=2010
      var opencode = super.getUUID();
      var authUrl = req.app.authUrl + "?opencode=" + opencode;
      await this.cacheManager["OpenCodeCache"].cache(opencode, ruser, 120);
      return system.getResultSuccess({ auth_url: authUrl, opencode: opencode });
   }
   async loginByMobileWithoutReg(pobj, qobj, req) {
      if (!pobj.mobile) {
         return system.getResult(null, "电话不能为空");
      }
      var param = {
         app_id: req.app.id,
         mobile: pobj.mobile,
      }
      var ruser = await this.userSve.getUserByUserMobile(param);
      if (!ruser) {
         return system.getResult(null,"查无此用户");
      }
      var opencode = super.getUUID();
      var authUrl = req.app.authUrl + "?opencode=" + opencode;
      await this.cacheManager["OpenCodeCache"].cache(opencode, ruser, 120);
      return system.getResultSuccess({ auth_url: authUrl, opencode: opencode });
   }

   //根据openid查询是否与用户绑定
   async checkWxBind(p,q,req){
      var openid=p.openid;
      if (!openid) {
         return system.getResult(null, "openid不能为空");
      }
      var param = {
         app_id: req.app.id,
         openid: openid,
      }
      var ruser = await this.userSve.checkWxBind(param);
      if(!ruser){
         return system.getResult(null, "用户未绑定微信");
      }
      return system.getResult(ruser);
   }
   //根据openid登录
   async loginByOpenId(p,q,req){
      var openid=p.openid;
      if (!openid) {
         return system.getResult(null, "openid不能为空");
      }
      var param = {
         app_id: req.app.id,
         openid: openid,
      }
      var ruser = await this.userSve.checkWxBind(param);
      if(!ruser){
         return system.getResult(null, "用户未绑定微信");
      }
      var opencode = super.getUUID();
      var authUrl = req.app.authUrl + "?opencode=" + opencode;
      await this.cacheManager["OpenCodeCache"].cache(opencode, ruser, 120);
      return system.getResultSuccess({ auth_url: authUrl, opencode: opencode });
   }
   //wxbind
   async wxBind(pobj, qobj, req) {
      var appkey = req.app.appkey;
      if (!pobj.mobile) {
         return system.getResult(null, "电话号码不能未空.");
      }
      if (!pobj.vcode) {
         return system.getResult(null, "验证码不能未空.");
      }
      if (!pobj.openid) {
         return system.getResult(null, "openid不能未空.");
      }
      var cacheCode = await this.cacheManager["VCodeCache"].cache(appkey + "_" + pobj.mobile, null);
      if (pobj.vcode != cacheCode.vcode) {
         return system.getResult(system.verifyVCodeFail, "验证码校验不成功，请重新获取验证码验证.");
      }
      var param = {
         app_id: req.app.id,
         mobile: pobj.mobile,
         openid:pobj.openid
      }
      var ruser = await this.userSve.wxBind(param);
      if (!ruser) {
         param = {
            app_id: req.app.id,
            userName: pobj.mobile,
            nickName:pobj.mobile,
            mobile: pobj.mobile,
            password: pobj.mobile,
            wxopenid:pobj.openid
         }
         ruser = await this.userSve.register(param);
      }
      var opencode = super.getUUID();
      var authUrl = req.app.authUrl + "?opencode=" + opencode;
      await this.cacheManager["OpenCodeCache"].cache(opencode, ruser, 120);
      return system.getResultSuccess({ auth_url: authUrl, opencode: opencode });
   }
    //wxbind
    async wxBindWithoutVCode(pobj, qobj, req) {
      var appkey = req.app.appkey;
      if (!pobj.mobile) {
         return system.getResult(null, "电话号码不能未空.");
      }

      if (!pobj.openid) {
         return system.getResult(null, "openid不能未空.");
      }
      var param = {
         app_id: req.app.id,
         mobile: pobj.mobile,
         openid:pobj.openid
      }
      var ruser = await this.userSve.wxBind(param);
      if (!ruser) {
         param = {
            app_id: req.app.id,
            userName: pobj.mobile,
            nickName:pobj.mobile,
            mobile: pobj.mobile,
            password: pobj.mobile,
            wxopenid:pobj.openid
         }
         ruser = await this.userSve.register(param);
      }
      var opencode = super.getUUID();
      var authUrl = req.app.authUrl + "?opencode=" + opencode;
      await this.cacheManager["OpenCodeCache"].cache(opencode, ruser, 120);
      return system.getResultSuccess({ auth_url: authUrl, opencode: opencode });
   }

   async loginByMobileWithReg(pobj, qobj, req) {
      if (!pobj.mobile) {
         return system.getResult(null, "电话不能为空");
      }
      var param = {
         app_id: req.app.id,
         mobile: pobj.mobile,
      }
      var ruser = await this.userSve.getUserByUserMobile(param);
      if (!ruser) {
         param = {
            app_id: req.app.id,
            userName: pobj.mobile,
            nickName:pobj.mobile,
            mobile: pobj.mobile,
            password: pobj.mobile
         }
         ruser = await this.userSve.register(param);
      }
      var opencode = super.getUUID();
      var authUrl = req.app.authUrl + "?opencode=" + opencode;
      await this.cacheManager["OpenCodeCache"].cache(opencode, ruser, 120);
      return system.getResultSuccess({ auth_url: authUrl, opencode: opencode });
   }
   async loginByMd5Password(pobj, qobj, req) {
      if (!pobj.userName) {
         return system.getResult(null, "用户名不能为空");
      }
      if (!pobj.password) {
         return system.getResult(null, "密码不能为空");
      }
      var param = {
         app_id: req.app.id,
         userName: pobj.userName,
         password: pobj.password,
         isNavto: true
      }
      var ruser = await this.userSve.getUserByUserNamePwd(param);
      var opencode = super.getUUID();
      var authUrl = req.app.authUrl + "?opencode=" + opencode;
      await this.cacheManager["OpenCodeCache"].cache(opencode, ruser, 30);
      return system.getResultSuccess({ auth_url: authUrl, opencode: opencode });
   }
   /**
    * 按照手机号和验证码修改密码
    */
   async modiPasswordByMobile(pobj, qobj, req){
      var appid=req.app.id;
      var appkey=req.app.appkey;
      if (!pobj.mobile) {
         return system.getResult(null, "电话号码不能未空.");
      }
      if (!pobj.vcode) {
         return system.getResult(null, "验证码不能未空.");
      }
      if (!pobj.newPwd) {
         return system.getResult(null, "新密码不能未空.");
      }
      var cacheCode = await this.cacheManager["VCodeCache"].cache(appkey + "_" + pobj.mobile, null);
      if (pobj.vcode != cacheCode.vcode) {
         return system.getResultFail(-1, "验证码校验不成功，请重新获取验证码验证.",system.verifyVCodeFail);
      }
      //按照appid和电话查询出要修改的用户
      var ruser = await this.userSve.modiPasswordByMobile(appid,pobj.mobile,pobj.newPwd);
      return system.getResult(ruser);
  }
   /**
    * 按照账号和appid修改密码
    */
   async modiPasswordByUserName(pobj, qobj, req){
       var appid=req.app.id;
       if (!pobj.userName) {
         return system.getResult(null, "账号不能未空.");
      }
      if (!pobj.newPwd) {
         return system.getResult(null, "新密码不能未空.");
      }
      // if (!pobj.oldPwd) {
      //    return system.getResult(null, "旧密码不能未空.");
      // }
      var ruser = await this.userSve.modiPasswordByUserName(appid,pobj.userName,pobj.newPwd,pobj.oldPwd);
      return system.getResult(ruser);
   }
   /**
    * 如果验证成功而账户不存在就创建一个账号
    * @param {*} pobj 
    * @param {*} qobj 
    * @param {*} req 
    */
   async loginByVCode(pobj, qobj, req) {
      var appkey = req.app.appkey;
      if (!pobj.mobile) {
         return system.getResult(null, "电话号码不能未空.");
      }
      if (!pobj.vcode) {
         return system.getResult(null, "验证码不能未空.");
      }
      var cacheCode = await this.cacheManager["VCodeCache"].cache(appkey + "_" + pobj.mobile, null);
      if (pobj.vcode != cacheCode.vcode) {
         return system.getResult(system.verifyVCodeFail, "验证码校验不成功，请重新获取验证码验证.");
      }
      var param = {
         app_id: req.app.id,
         mobile: pobj.mobile,
      }
      var ruser = await this.userSve.getUserByUserMobile(param);
      if (!ruser) {
         param = {
            app_id: req.app.id,
            userName: pobj.mobile,
            nickName:pobj.mobile,
            mobile: pobj.mobile,
            password: pobj.mobile
         }
         ruser = await this.userSve.register(param);
      }
      var opencode = super.getUUID();
      var authUrl = req.app.authUrl + "?opencode=" + opencode;
      await this.cacheManager["OpenCodeCache"].cache(opencode, ruser, 120);
      return system.getResultSuccess({ auth_url: authUrl, opencode: opencode });
   }

   /**
    * 非平台应用回调处理函数中需要把
    * req.code提取出来，并调用下方函数，返回对应的openuser,给
    * 非平台应用
    * @param {*} pobj 
    * @param {*} query 
    * @param {*} req 
    */
   async authByCode(pobj, query, req) {
      if (!pobj.opencode) {
         return system.getResult(null, "opencode参数不能为空");
      }
      var cacheuser = await this.userSve.authByCode(pobj.opencode);
      if (!cacheuser) {
         return system.getResult(system.verifyOpencodeFail, "opencode存储的值已经失效，请重新获取用户的opencode.");
      }
      return system.getResultSuccess(cacheuser);
   }

   classDesc() {
      return {
         groupName: "auth",
         groupDesc: "认证相关的包",
         name: "AccessAuthAPI",
         desc: "关于认证的类",
         exam: `
             post  http://p.apps.com/api/auth/accessAuth/getAccessKey
             {
                appKey:xxxxx,
                secret:yyyyyy
             }
         `,
      };
   }
   methodDescs() {
      return [

         {
            methodDesc: "生成访问token,访问地址：http://......../api/auth/accessAuth/getAccessKey,访问token需要放置到后续API方法调用的请求头中",
            methodName: "getAccessKey",
            paramdescs: [
               {
                  paramDesc: "访问appkey",
                  paramName: "appkey",
                  paramType: "string",
                  defaultValue: "",
               },
               {
                  paramDesc: "访问secret",
                  paramName: "secret",
                  paramType: "string",
                  defaultValue: "",
               }
            ],
            rtnTypeDesc: "返回JSON对象字符串",
            rtnType: "json object {accessKey: xxxxxx, app: {xxx:xxx}},注意app,是当前app信息,详细见后面示例"
         },
         {
            methodDesc: "注册方法,访问地址：http://......../api/auth/accessAuth/register",
            methodName: "register",
            paramdescs: [
               {
                  paramDesc: "用户名",
                  paramName: "userName",
                  paramType: "string",
                  defaultValue: "",
               },
               {
                  paramDesc: "密码",
                  paramName: "password",
                  paramType: "string",
                  defaultValue: "",
               },
               {
                  paramDesc: "电话",
                  paramName: "mobile",
                  paramType: "string",
                  defaultValue: "",
               }
            ],
            rtnTypeDesc: "返回JSON对象字符串",
            rtnType: "json object{ auth_url: xxxxx/auth?opencode=xxxxx }),详细见后面示例"
         },
         {
            methodDesc: "登录方法,访问地址：http://......../api/auth/accessAuth/login",
            methodName: "login",
            paramdescs: [
               {
                  paramDesc: "用户名",
                  paramName: "userName",
                  paramType: "string",
                  defaultValue: "",
               },
               {
                  paramDesc: "密码",
                  paramName: "password",
                  paramType: "string",
                  defaultValue: "",
               }
            ],
            rtnTypeDesc: "返回JSON对象字符串",
            rtnType: "json object{ auth_url: xxxxx/auth?opencode=xxxxx }),详细见后面示例"
         },
         {
            methodDesc: "获取手机验证,访问地址：http://......../api/auth/accessAuth/fetchVCode,访问token需要放置到后续API方法调用的请求头中",
            methodName: "fetchVCode",
            paramdescs: [
               {
                  paramDesc: "手机号码",
                  paramName: "mobile",
                  paramType: "string",
                  defaultValue: "",
               },
               {
                  paramDesc: "模版CODE",
                  paramName: "tmplCode",
                  paramType: "string",
                  defaultValue: "",
               },
               {
                  paramDesc: "签名",
                  paramName: "signName",
                  paramType: "string",
                  defaultValue: "",
               },
            ],
            rtnTypeDesc: "返回JSON对象字符串,会在平台缓存60秒",
            rtnType: "json object{status:0,data:{ vcode: xxxxxx}},详细见后面示例"
         },
         {
            methodDesc: "验证码登录,访问地址：http://......../api/auth/accessAuth/loginByVCode,访问token需要放置到后续API方法调用的请求头中",
            methodName: "loginByVCode",
            paramdescs: [
               {
                  paramDesc: "手机号码",
                  paramName: "mobile",
                  paramType: "string",
                  defaultValue: "",
               },
               {
                  paramDesc: "获取到的验证码",
                  paramName: "vcode",
                  paramType: "string",
                  defaultValue: "",
               },
            ],
            rtnTypeDesc: "返回JSON对象字符串",
            rtnType: "json object{auth_url: xxxxx/auth?opencode=xxxxx }),详细见后面示例"
         },
         {
            methodDesc: "验证码登录,访问地址：http://......../api/auth/accessAuth/loginByMobileWithReg,访问token需要放置到后续API方法调用的请求头中",
            methodName: "loginByVCode",
            paramdescs: [
               {
                  paramDesc: "手机号码",
                  paramName: "mobile",
                  paramType: "string",
                  defaultValue: "",
               },
            ],
            rtnTypeDesc: "返回JSON对象字符串",
            rtnType: "json object{auth_url: xxxxx/auth?opencode=xxxxx }),详细见后面示例"
         },
         {
            methodDesc: "验证码登录,访问地址：http://......../api/auth/accessAuth/loginByMobileWithoutReg,访问token需要放置到后续API方法调用的请求头中",
            methodName: "loginByVCode",
            paramdescs: [
               {
                  paramDesc: "手机号码",
                  paramName: "mobile",
                  paramType: "string",
                  defaultValue: "",
               },
            ],
            rtnTypeDesc: "返回JSON对象字符串",
            rtnType: "json object{auth_url: xxxxx/auth?opencode=xxxxx }),详细见后面示例"
         },
         {
            methodDesc: "按照opencode获取用户信息,访问地址：http://......../api/auth/accessAuth/authByCode,访问token需要放置到后续API方法调用的请求头中",
            methodName: "authByCode",
            paramdescs: [
               {
                  paramDesc: "开放code,用于换取用户信息",
                  paramName: "opencode",
                  paramType: "string",
                  defaultValue: "",
               }
            ],
            rtnTypeDesc: "返回JSON对象字符串",
            rtnType: "json object{status:0,data:{}}),详细见后面示例"
         },
         {
            methodDesc: "按照手机号、验证码修改密码,访问地址：http://......../api/auth/accessAuth/modiPasswordByMobile,访问token需要放置到后续API方法调用的请求头中",
            methodName: "modiPasswordByMobile",
            paramdescs: [
               {
                  paramDesc: "手机号",
                  paramName: "mobile",
                  paramType: "string",
                  defaultValue: "",
               },
               {
                  paramDesc: "验证码",
                  paramName: "vcode",
                  paramType: "string",
                  defaultValue: "",
               },
               {
                  paramDesc: "新密码",
                  paramName: "newPwd",
                  paramType: "string",
                  defaultValue: "",
               }
            ],
            rtnTypeDesc: "返回修改后的用户",
            rtnType: "json object{status:-1,data:2030),验证码错误，成功：{status:0,...}"
         },
         {
            methodDesc: "按照微信openid检查是否绑定,访问地址：http://......../api/auth/accessAuth/checkWxBind,访问token需要放置到后续API方法调用的请求头中",
            methodName: "checkWxBind",
            paramdescs: [
               {
                  paramDesc: "微信openid",
                  paramName: "openid",
                  paramType: "string",
                  defaultValue: "",
               }
            ],
            rtnTypeDesc: "返回绑定后的用户",
            rtnType: "成功：{status:0,data:{}),未绑定:{status:-1}"
         },
         {
            methodDesc: "按照手机号、验证码,微信ID,绑定账号,访问地址：http://......../api/auth/accessAuth/wxBind,访问token需要放置到后续API方法调用的请求头中",
            methodName: "wxBind",
            paramdescs: [
               {
                  paramDesc: "手机号",
                  paramName: "mobile",
                  paramType: "string",
                  defaultValue: "",
               },
               {
                  paramDesc: "验证码",
                  paramName: "vcode",
                  paramType: "string",
                  defaultValue: "",
               },
               {
                  paramDesc: "微信openid",
                  paramName: "openid",
                  paramType: "string",
                  defaultValue: "",
               }
            ],
            rtnTypeDesc: "返回绑定后的获取用户的code",
            rtnType: "成功：{status:0,data:{auth_url: xxxxx/auth?opencode=xxxxx,opencode:yyyy})"
         },
         {
            methodDesc: "按照微信ID进行登录,访问地址：http://......../api/auth/accessAuth/loginByOpenId,访问token需要放置到后续API方法调用的请求头中",
            methodName: "loginByOpenId",
            paramdescs: [
               {
                  paramDesc: "微信openid",
                  paramName: "openid",
                  paramType: "string",
                  defaultValue: "",
               }
            ],
            rtnTypeDesc: "返回绑定后的获取用户的code",
            rtnType: "成功：{status:0,data:{auth_url: xxxxx/auth?opencode=xxxxx,opencode:yyyy})"
         },
         {
            methodDesc: "按照手机号、验证码修改密码,访问地址：http://......../api/auth/accessAuth/modiPasswordByUserName,访问token需要放置到后续API方法调用的请求头中",
            methodName: "modiPasswordByUserName",
            paramdescs: [
               {
                  paramDesc: "账户名",
                  paramName: "userName",
                  paramType: "string",
                  defaultValue: "",
               },
               {
                  paramDesc: "新密码",
                  paramName: "newPwd",
                  paramType: "string",
                  defaultValue: "",
               },
               {
                  paramDesc: "旧密码,可不传,不传时,供新建账号设置密码",
                  paramName: "oldPwd",
                  paramType: "string",
                  defaultValue: "",
               }
            ],
            rtnTypeDesc: "返回修改后的用户",
            rtnType: "json object{status:0,data:{}}),详细见后面示例"
         },
         {
            methodDesc: "查询应用注册用户,访问地址：http://......../api/auth/accessAuth/queryRegUser,访问token需要放置到后续API方法调用的请求头中",
            methodName: "queryRegUser",
            paramdescs: [
               {
             
               }
            ],
            rtnTypeDesc: "返回应用的注册用户",
            rtnType: "json object{status:0,data:[])"
         },
      ];
   }
   exam() {
      return `
     <pre>
      
     1.获取访问Token （建议在本地缓存<3600S）
        curl -k -H 'Content-type: application/json' -d '{appkey:xxxxx,secret:xxxx}' http://open.gongsibao.com/api/auth/accessAuth/getAccessKey
        返回：
        {
         status:0,
         data:{
          {
             accessKey:xxxxxxx,
             app:{xxxx:xxxx,xxx:......}
          }
         }
        }

      2.用户注册（注意请求头，设置1中获取的访问token）
      curl -k -H 'Content-type: application/json' -H  'AccessKey:{ak}' -d '{userName:xxxxx,password:xxxx,mobile:xxxxx}' http://open.gongsibao.com/api/auth/accessAuth/register
      返回：
      {
         status:0,
         data:   { 
            auth_url: "/auth?opencode=xxxxxx" 
         }
      }
      注意：接入程序后台需要处理/auth?opencode=xxx,按照opencode去开放平台获取openuser,然后在自己的应用内
      完成注册，并建立会话。

      3.用户登录（注意请求头，设置1中获取的访问token）
      curl -k -H 'Content-type: application/json' -H  'AccessKey:{ak}' -d '{userName:xxxxx,password:xxxx}' http://open.gongsibao.com/api/auth/accessAuth/login
      返回：
      {
         status:0,
         data:   { 
            auth_url: "/auth?opencode=xxxxxx" 
         }
      }
   
      注意：接入程序后台需要处理/auth?opencode=xxx,按照opencode去开放平台获取openuser,然后在自己的应用内
      完成登录，并建立会话。


      4、获取手机验证码（注意请求头，设置1中获取的访问token）
      curl -k -H 'Content-type: application/json' -H  'AccessKey:{ak}' -d '{mobile:xxxxx,tmplCode:"",signName:""}' http://open.gongsibao.com/api/auth/accessAuth/login
       返回：
       { 
          status:0,
          data:{
             vcode: "xxxxxx" 
          }
       }
       5、手机验证码登录（注意请求头，设置1中获取的访问token）
      curl -k -H 'Content-type: application/json' -H  'AccessKey:{ak}' -d '{mobile:xxxxx,vcode:xxxxxx}' http://open.gongsibao.com/api/auth/accessAuth/loginByVCode
       返回：
       { 
          status:0,
          data:   { 
            auth_url: "/auth?opencode=xxxxxx" 
         }
       }
       6、换取开放用户信息（注意请求头，设置1中获取的访问token）
       curl -k -H 'Content-type: application/json' -H  'AccessKey:{ak}' -d '{opencode:xxxxx}' http://open.gongsibao.com/api/auth/accessAuth/authByCode
        返回：
        { 
           status:0,
           data:   { 
             
           }
        }
     </pre>

  
      <h3>开放平台单点注册/登录流程图</h3>
      <img  src="data:image/png;base64,[encoded data, 35880 characters]"/>  `
         ;
   }

}
module.exports = AccessAuthAPI;
// var req={app:{appkey:"wx76a324c5d201d1a4"}};
// var p={mobile:"13381139519",tmplCode:"SMS_173946419","signName":"ibossx"};
// var x=new AccessAuthAPI();
// var ff=x.fetchVCode(p,null,req);
// ff.then((v)=>{
//   console.log(v);
// })