weixin.class.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * 微信支付服务器端下单
  4. * 微信APP支付文档地址: https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_6
  5. * 使用示例
  6. * 构造方法参数
  7. * 'appid' => //填写微信分配的公众账号ID
  8. * 'mch_id' => //填写微信支付分配的商户号
  9. * 'notify_url'=> //填写微信支付结果回调地址
  10. * 'key' => //填写微信商户支付密钥
  11. * );
  12. * 统一下单方法
  13. * $WechatAppPay = new wechatAppPay($options);
  14. * $params['body'] = '商品描述'; //商品描述
  15. * $params['out_trade_no'] = '1217752501201407'; //自定义的订单号,不能重复
  16. * $params['total_fee'] = '100'; //订单金额 只能为整数 单位为分
  17. * $params['trade_type'] = 'APP'; //交易类型 JSAPI | NATIVE |APP | WAP
  18. * $wechatAppPay->unifiedOrder( $params );
  19. */
  20. class weixin
  21. {
  22. private $appid;
  23. private $seceret;
  24. public function __construct($appid, $seceret)
  25. {
  26. $this->appid = $appid;
  27. $this->seceret = $seceret;
  28. }
  29. /**
  30. * code获取openid
  31. * @param $code
  32. * @return mixed
  33. */
  34. public function code2Session($code)
  35. {
  36. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->appid}&secret={$this->seceret}&js_code={$code}&grant_type=authorization_code";
  37. $res = Curl::get($url,true);
  38. return json_decode($res,true);
  39. }
  40. public function getAccessToken()
  41. {
  42. $rs = Baseconfig::getInfoByKey(TOKEN_BASE_KEY);
  43. if (empty($rs) || (time() - $rs["lastupdate"]) > TOKEN_LIMIT_TIME) {
  44. $accessToken = self::refreshToken();
  45. } else {
  46. $accessToken = $rs["value"];
  47. }
  48. return $accessToken;
  49. }
  50. public function refreshToken()
  51. {
  52. $token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->seceret;
  53. $res = file_get_contents($token_access_url); //
  54. $result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量s
  55. $access_token = $result['access_token'];
  56. $rs = Baseconfig::getInfoByKey(TOKEN_BASE_KEY);
  57. if ($rs) {
  58. $res = Baseconfig::updateByKey(TOKEN_BASE_KEY, array("value" => $access_token, "lastupdate" => time()));
  59. } else {
  60. $attrs = array(
  61. "key"=>TOKEN_BASE_KEY,
  62. "value"=>$access_token,
  63. "name"=>"token"
  64. );
  65. $res = Baseconfig::add($attrs);
  66. }
  67. if ($res) {
  68. return $access_token;
  69. }
  70. }
  71. public function createBase64($path,$scene)
  72. {
  73. $accessToken = $this->getAccessToken();
  74. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}";
  75. // if(!file_exists(FILE_PATH.$destPath))
  76. // {
  77. // mkdir(FILE_PATH.$destPath,0777,true);
  78. // }
  79. // $fileName = date("YmdHis").randcode(4).".jpg";
  80. $resData = Curl::post($url,json_encode(array("page"=>$path,"scene"=>$scene)));
  81. return Image::BinaryToBase64($resData);
  82. // file_put_contents(FILE_PATH.$destPath.$fileName,$resData);
  83. }
  84. public function createQrCode($path,$scene,$destPath)
  85. {
  86. $accessToken = $this->getAccessToken();
  87. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}";
  88. if(!file_exists(FILE_PATH.$destPath))
  89. {
  90. mkdir(FILE_PATH.$destPath,0777,true);
  91. }
  92. $fileName = date("YmdHis").randcode(4).".jpg";
  93. $resData = Curl::post($url,json_encode(array("page"=>$path,"scene"=>$scene)));
  94. file_put_contents(FILE_PATH.$destPath.$fileName,$resData);
  95. return $destPath.$fileName;
  96. }
  97. public function sendBaomingInform($openId,$formId,$page,$name,$title){
  98. $accessToken = $this->getAccessToken();
  99. $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={$accessToken}";
  100. $time = date("Y-m-d H:i:s");
  101. $data = '{
  102. "touser": "'.$openId.'",
  103. "template_id": "'.BAOMING_TEMPLATE_ID.'",
  104. "page": "'.$page.'",
  105. "form_id": "'.$formId.'",
  106. "data": {
  107. "keyword1": {
  108. "value": "'.$name.'"
  109. },
  110. "keyword2": {
  111. "value": "'.$title.'"
  112. },
  113. "keyword3": {
  114. "value": "'.$time.'"
  115. }
  116. }
  117. }';
  118. $rs = Curl::post($url,$data);
  119. }
  120. public function sendSignSuccess($openId,$formId,$page,$name,$title){
  121. $accessToken = $this->getAccessToken();
  122. $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={$accessToken}";
  123. $time = date("Y-m-d H:i:s");
  124. $data = '{
  125. "touser": "'.$openId.'",
  126. "template_id": "'.SIGN_SUCCESS_TEMPLATE_ID.'",
  127. "page": "'.$page.'",
  128. "form_id": "'.$formId.'",
  129. "data": {
  130. "keyword1": {
  131. "value": "'.$name.'"
  132. },
  133. "keyword2": {
  134. "value": "'.$time.'"
  135. },
  136. "keyword3": {
  137. "value": "'.$title.'"
  138. }
  139. }
  140. }';
  141. $rs = Curl::post($url,$data);
  142. }
  143. /***发送动开始通知 订阅消息***/
  144. public function sendBeginInform($openId,$page,$auth,$title,$time,$address){
  145. $accessToken = $this->getAccessToken();
  146. $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$accessToken}";
  147. $data = '{
  148. "touser": "'.$openId.'",
  149. "template_id": "'.BAOMING_BEGIN_INFORM_TEMPLATE_ID.'",
  150. "page": "'.$page.'",
  151. "data": {
  152. "name1": {
  153. "value": "'.$auth.'"
  154. },
  155. "thing4": {
  156. "value": "'.$title.'"
  157. },
  158. "date5": {
  159. "value": "'.$time.'"
  160. },
  161. "thing6": {
  162. "value": "'.$address.'"
  163. }
  164. }
  165. }';
  166. $rs = Curl::post($url,$data);
  167. }
  168. /***发送奖品通知 订阅消息***/
  169. public function sendAwardInform($openId,$page,$auth,$title,$phone,$awardName){
  170. $accessToken = $this->getAccessToken();
  171. $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$accessToken}";
  172. $data = '{
  173. "touser": "'.$openId.'",
  174. "template_id": "'.BAOMING_AWARD_INFORM_TEMPLATE_ID.'",
  175. "page": "'.$page.'",
  176. "data": {
  177. "thing1": {
  178. "value": "'.$title.'"
  179. },
  180. "thing2": {
  181. "value": "'.$awardName.'"
  182. },
  183. "phone_number3": {
  184. "value": "'.$phone.'"
  185. },
  186. "name4": {
  187. "value": "'.$auth.'"
  188. }
  189. }
  190. }';
  191. $rs = Curl::post($url,$data);
  192. }
  193. }