WeixinController.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class WeixinController extends Controller {
  5. /**
  6. * 获取微信access_token
  7. */
  8. public function get_access_token(){
  9. $access_token = M('config')->field('value,time')->where(array('name'=>'access'))->find();
  10. $invalid_time = $access_token['time']+7000;
  11. if(!empty($access_token['value']) && $invalid_time > NEW_TIME){
  12. return $access_token['value'];
  13. }else{
  14. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.C('appid').'&secret='.C('appsecret');
  15. $data = curl_request($url);
  16. M('config')->where(array('name'=>'access'))->save(array('value'=>$data['access_token'],'time'=>NEW_TIME));
  17. return $data['access_token'];
  18. }
  19. }
  20. public function get_user_info(){
  21. $access_token = $this->get_access_token();
  22. if(!session('?openid')){
  23. $open_id = $this->get_user_access_token();
  24. }else{
  25. $open_id = session('openid');
  26. }
  27. $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid='.$open_id.'&lang=zh_CN';
  28. $data = curl_request($url);
  29. return $data;
  30. }
  31. /**
  32. * 获取微信code
  33. */
  34. public function get_code($url){
  35. $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.C('appid').'&redirect_uri='.urlencode(C('URL').$url).'&response_type=code&scope=snsapi_base&state=STATE&connect_redirect=1#wechat_redirect';
  36. echo "<script>location.href='{$url}'</script>";die;
  37. }
  38. /**
  39. * 通过code 获取用户 access_token
  40. */
  41. public function get_user_access_token(){
  42. if (empty(I('get.code'))){
  43. $this->get_code(CONTROLLER_NAME.'/'.ACTION_NAME);
  44. }else{
  45. if(!empty(I('get.code'))){
  46. $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.C('appid').'&secret='.C('appsecret').'&code='.I('get.code').'&grant_type=authorization_code';
  47. $data = curl_request($url);
  48. $openid = $data['openid'];
  49. $uid = M('User')->where(array('openid'=>$openid))->getField('uid');
  50. if(empty($uid)){
  51. $data['openid'] = $openid;
  52. $uid = M('User')->add($data);
  53. }
  54. if(!empty($uid)){
  55. session('uid',$uid);
  56. }
  57. if(!empty($openid)){
  58. session('openid',$openid);
  59. }
  60. }
  61. }
  62. return $openid?$openid:session('openid');
  63. }
  64. /**
  65. * 发放微信红包
  66. */
  67. public function set_user_envelopes($amd){
  68. if(!session('?openid') || !session('?uid')){
  69. $openid = $this->get_user_access_token();
  70. }else{
  71. $openid = session('openid');
  72. }
  73. $string = get_random_string(32);
  74. $envelopes = array(
  75. 'nonce_str' => $string,
  76. 'mch_billno' => substr(session('openid'),0,4).date('YmdHsi'),
  77. 'mch_id'=>C('key'),
  78. 'wxappid'=>C('appid'),
  79. 'send_name'=>C('name'),
  80. 're_openid'=>$openid,
  81. 'total_amount'=>$amd,
  82. 'total_num'=>1,
  83. 'wishing'=>C('wishing'),
  84. 'client_ip'=>'39.107.251.71',
  85. 'act_name'=>C('act_name'),
  86. 'remark'=>C('remark'),
  87. 'scene_id'=>'PRODUCT_2',
  88. );
  89. $envelopes['sign'] = A('Home/Weixinpay')->getSign($envelopes);
  90. $envelopes = arrayToXml($envelopes);
  91. $data = xmlToArray(curlPost('https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack',$envelopes));
  92. if($data['err_code'] == 'SUCCESS'){
  93. $wx_data = array(
  94. 'ststus'=>'1',
  95. 'send_listid'=>$data['send_listid'],
  96. 'openid'=>$data['re_openid'],
  97. 'info'=>$data['err_code'].$data['err_code_des'],
  98. );
  99. }else{
  100. $wx_data = array(
  101. 'ststus'=>'0',
  102. 'info'=>$data['err_code'].$data['err_code_des'],
  103. );
  104. }
  105. return $wx_data;
  106. }
  107. }