wxgzh.php 3.9 KB

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