usersig.auto_cache.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. class usersig_auto_cache extends auto_cache{
  3. public function load($param)
  4. {
  5. $m_config = load_auto_cache("m_config");//参数
  6. $sdkappid = $m_config['tim_sdkappid'];
  7. $id = strim($param['id']);
  8. $key = "usersig:".$sdkappid.":".$id;
  9. $root = $GLOBALS['cache']->get($key);
  10. $open_usersig_cache = intval($m_config['open_usersig_cache']);
  11. if($root === false||$open_usersig_cache)
  12. {
  13. $private_pem_path = APP_ROOT_PATH."system/tim/ec_key.pem";
  14. $root = array();
  15. if(!file_exists($private_pem_path))
  16. {
  17. $root['error'] = "私钥文件不存在:".$private_pem_path;
  18. $root['status'] = 0;
  19. }elseif ($id == ''){
  20. $root['error'] = "参数id不能为空";
  21. $root['status'] = 0;
  22. }else{
  23. require_once(APP_ROOT_PATH.'system/tim/TimApi.php');
  24. require_once(APP_ROOT_PATH.'system/tim/TimRestApi.php');
  25. $identifier = $m_config['tim_identifier'];
  26. $api = createRestAPI();
  27. $api->init($sdkappid, $identifier);
  28. //var_dump($api);
  29. $signature = get_signature();
  30. $expiry_after = 86400;//一天有效期
  31. $ret = $api->generate_user_sig((string)$id, $expiry_after, $private_pem_path, $signature);
  32. if($ret == null || strstr($ret[0], "failed")){
  33. $root['error'] = $sdkappid.":获取usrsig失败, 请确保:".$signature." 文件有执行的权限.";
  34. $root['status'] = 0;
  35. }else{
  36. $root['usersig'] = $ret[0];
  37. $root['status'] = 1;
  38. $GLOBALS['cache']->set($key,$root,$expiry_after - 60);
  39. //$expiry_after = NOW_TIME + 86400;
  40. //$GLOBALS['db']->query("update ".DB_PREFIX."user set usersig = '".$ret[0]."',expiry_after=".$expiry_after." where id = '".$id."'");
  41. }
  42. }
  43. }
  44. return $root;
  45. }
  46. public function rm($param)
  47. {
  48. $m_config = load_auto_cache("m_config");//参数
  49. $sdkappid = $m_config['tim_sdkappid'];
  50. $id = strim($param['id']);
  51. $key = "usersig:".$sdkappid.":".$id;
  52. $GLOBALS['cache']->rm($key);
  53. }
  54. public function clear_all($param)
  55. {
  56. $m_config = load_auto_cache("m_config");//参数
  57. $sdkappid = $m_config['tim_sdkappid'];
  58. $id = strim($param['id']);
  59. $key = "usersig:".$sdkappid.":".$id;
  60. $GLOBALS['cache']->rm($key);
  61. }
  62. }
  63. ?>