1
0

wxgzh.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. class wxgzhModel extends Model
  3. {
  4. //定义远程连接的
  5. protected $URL_public = 'https:&#47;&#47;api.weixin.qq.com/cgi-bin/';
  6. protected $URL_gettoken = 'token';
  7. protected $URL_jsapiticket = 'ticket/getticket';
  8. //获取用户信息跟openid
  9. protected $URL_userinfo = 'user/info';
  10. //发模版消息的
  11. protected $URL_tplsend = 'message/template/send';
  12. protected $URL_tplgetlist = 'template/get_all_private_template';
  13. public $appid = '';
  14. public $optionpid = '-4';
  15. public $backarr = array();
  16. protected $secret = '';
  17. public function initWxgzh(){}
  18. public function initModel()
  19. {
  20. $this->backarr = array('errcode'=>-1, 'msg'=>'sorry,error');
  21. $this->option = m('option');
  22. $this->readwxset();
  23. $this->initWxgzh();
  24. }
  25. public function gettourl($can)
  26. {
  27. $url = $this->URL_public;
  28. if(substr($url,0,4)!='http'){
  29. $url=$this->rock->jm->uncrypt($url);
  30. $url.=$this->rock->jm->uncrypt($this->$can);
  31. }else{
  32. $url.=$this->$can;
  33. }
  34. return $url;
  35. }
  36. //读取配置
  37. public function readwxset()
  38. {
  39. if($this->appid!='')return $this->appid;
  40. $this->appid = $this->option->getval('wxgzh_appid');
  41. $this->secret = $this->option->getval('wxgzh_secret');
  42. $this->corpid = $this->option->getval('weixinqy_corpid');
  43. return $this->appid;
  44. }
  45. /**
  46. * 判断是否可以使用公众号定位的
  47. * $lx 0有企业号判断
  48. */
  49. public function isusegzh($lx=0)
  50. {
  51. if(!$this->rock->iswebbro(0) || $this->rock->isqywx)return 0;
  52. $this->readwxset();
  53. $is = 1;
  54. if($this->appid=='' || $this->secret=='')$is = 0;
  55. if($lx==0 && !isempt($this->corpid))$is = 0;
  56. return $is;
  57. }
  58. /**
  59. * 是否有设置微信公众号
  60. */
  61. public function issetwxgzh()
  62. {
  63. $str = $this->option->getval('wxgzh_token');
  64. if($str){
  65. return true;
  66. }else{
  67. return false;
  68. }
  69. }
  70. //获取token
  71. public function gettoken()
  72. {
  73. $time = date('Y-m-d H:i:s', time()-2*3600);
  74. $num = 'wxgzh_token';
  75. $rs = $this->option->getone("`num`='$num' and `optdt`>'$time'");
  76. $val = '';
  77. if($rs)$val = $rs['value'];
  78. if(isempt($val)){
  79. $this->readwxset();
  80. $secret = $this->secret;
  81. if($this->appid=='' || $this->secret=='')showreturn('','没有设置公众号',201);
  82. if(isempt($secret))return '';
  83. $url = ''.$this->gettourl('URL_gettoken').'?grant_type=client_credential&appid='.$this->appid.'&secret='.$secret.'';
  84. $result = c('curl')->getcurl($url);
  85. if($result != ''){
  86. $arr = json_decode($result);
  87. if(!isset($arr->access_token)){
  88. showreturn('',$result,201);
  89. }else{
  90. $val = $arr->access_token;
  91. $this->option->setval($num.'@'.$this->optionpid.'', $val);
  92. }
  93. }
  94. }
  95. return $val;
  96. }
  97. public function getticket()
  98. {
  99. $time = date('Y-m-d H:i:s', time()-2*3600);
  100. $num = 'wxgzh_ticket';
  101. $rs = $this->option->getone("`num`='$num' and `optdt`>'$time'");
  102. $val = '';
  103. if($rs)$val = $rs['value'];
  104. if(isempt($val)){
  105. $token = $this->gettoken();
  106. $url = ''.$this->gettourl('URL_jsapiticket').'?access_token='.$token.'&type=jsapi';
  107. $result = c('curl')->getcurl($url);
  108. if($result != ''){
  109. $arr = json_decode($result);
  110. if(!isset($arr->ticket)){
  111. showreturn('', $result, 201);
  112. }else{
  113. $val = $arr->ticket;
  114. $this->option->setval($num.'@'.$this->optionpid.'', $val);
  115. }
  116. }
  117. }
  118. return $val;
  119. }
  120. public function setbackarr($msg, $code=-1)
  121. {
  122. $this->backarr = array('errcode'=>$code, 'msg'=>$msg);
  123. return $this->backarr;
  124. }
  125. public function clearalltoken()
  126. {
  127. $this->option->update("value=null", "`num` like 'wxgzh\_%'");
  128. }
  129. }
粤ICP备19079148号