🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
>[danger]每次访问接口时,都必须携带5个公共参数,如果需要用户信息的接口,还需要携带cowcms_userid参数(建议每个连接都携带该参数) | 参数名称 | 参数描述 | | --- | --- | | appid | 接口网站分配个请求网站的appid | | interval | timestamp参数有效时时长 默认7200,单位为秒 | | timestamp | 请求时间戳,该值和当前时间差不能大于 interval 值(默认7200秒,时间在后台可设置) | | nonceStr | 随机字符串 | | signature | 请求签名 | | cowcms_userid | 登录用户id 32位字符串,该参数在用户登录时,返回给前端的用户登录凭证,前端需要保存该凭证,在调用强制用户登录的接口时必传该参数 | >[danger]调用API接口流程 1.获取访问API接口的必须参数。 2.将获取的参数缓存到前端。 3.访问接口时判定必须参数是否在有效时间内 4.如果参数有效直接访问,如果参数无效,重新获取然参数后,执行第二步 >站内调用接口获取访问API接口的必须参数 ``` namespace app\user\controller; use user\Member; class Sign extends Member { function getCowcmsUserid() { //$id API站点id,如果是本站默认id为1,开发者可根据自己需求填写 $param = exeFun('getApiParam',[$id=1],'api'); dump($param) ; } } $param打印出来的参数为 array(5) { ["appid"] => string(40) "50fb0e2cde242fb428e5d35c719e82f3bfca8dcf" ["timestamp"] => int(1584416641) ["nonceStr"] => string(6) "gtug8k" ["signature"] => string(40) "fcee23aa9c3f9055d8d63e4cba51d71a630a03a3" ["interval"] => int(7200) } ``` >其它网站访问接口获取必须参数 ``` namespace app\user\controller; use user\Member; class Sign extends Member { function getCowcmsUserid() { $url = "http://xxxx.com/public/index.php/api/Apiget/get_param/appid/XXX/appsecret/XXXX/token/xxx" $param = http_curl($url); dump($param); } } $param打印出来的参数为 array(5) { ["appid"] => string(40) "50fb0e2cde242fb428e5d35c719e82f3bfca8dcf" ["timestamp"] => int(1584416641) ["nonceStr"] => string(6) "gtug8k" ["signature"] => string(40) "fcee23aa9c3f9055d8d63e4cba51d71a630a03a3" ["interval"] => int(7200) } ``` >[danger]自行生成访问接口获取必须参数,将 appid,appsecret,timestamp,nonceStr参数按照字典排序用&连接,进行(sha1或者md5,此处主要看后台设置加密类型)加密生成signature签名,如: signature =sha1(appid=6a9f5b5a0cd0594731113041f989e2a3a27afbcb&appsecret=aa0d037bfd95978e154aecb75739295681060346&nonceStr=8UsD7F&timestamp=1586238156) 2.将获取的参数缓存到前端 ``` localStorage.setItem("appid","...."); localStorage.setItem("timestamp","...."); localStorage.setItem("nonceStr","...."); localStorage.setItem("signature","...."); localStorage.setItem("interval","...."); ``` 3.js每次访问接口时,检测timestamp是否失效,如果失效重新获取参数,并缓存数据,否则访问接口 ``` appid = localStorage.getItem("appid"); timestamp = localStorage.getItem("timestamp"); nonceStr = localStorage.getItem("nonceStr"); signature = localStorage.getItem("signature"); interval = localStorage.getItem("interval"); timestamp = (new Date()).getTime();; //当前时间戳 //提前10秒兑换新的参数 if(time - timestamp - interval > 10) { //参数失效,并重新获取 url = "http://xxxx.com/public/index.php/api/Apiget/get_param_change"; param= {appid:'50fb0e2cde242fb428e5d35c719e82f3bfca8dcf',timestamp:1584418592,nonceStr:'IYI0wK',signature:'d3c512eef5bc25d09b767e75b8f5eff3bd91bec8'}; $.getJSON(url,param, function(json){ localStorage.setItem("appid","...."); localStorage.setItem("timestamp","...."); localStorage.setItem("nonceStr","...."); localStorage.setItem("signature","...."); localStorage.setItem("interval","...."); }); } ``` >cowcms_userid值的获取 方式 当用户登录后由后端返回