WxService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Service\Wx;
  3. use App\Model\WxEmployeeOfficial;
  4. use Illuminate\Support\Facades\Redis;
  5. class WxService extends Service
  6. {
  7. public $appid = '';
  8. public $secret = '';
  9. public function __construct()
  10. {
  11. $this->appid = config("wx_msg.appid");
  12. $this->secret = config("wx_msg.appSecret");
  13. }
  14. public function getOpenid($code){
  15. $appid = $this->appid;
  16. $secret = $this->secret;
  17. // $code = '0b1tFv100Sm91Q1kko0004vZGu0tFv12';
  18. $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';
  19. list($status,$res) = $this->wx_return($url);
  20. if($status) return [true,$res['openid']];
  21. else return [false,$res];
  22. }
  23. public function getToken(){
  24. $token_key = $this->appid.'_wx_token';
  25. $token = Redis::get($token_key);
  26. if(!empty($token)){
  27. return [true,$token];
  28. }
  29. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret='.$this->secret;
  30. list($status,$res) = $this->wx_return($url);
  31. if($status) {
  32. Redis::setex($token_key,7100,$res['access_token']);
  33. return [true,$res['access_token']];
  34. }
  35. else return [false,$res];
  36. }
  37. public function getMobile($code){
  38. list($status,$token) = $this->getToken();
  39. $url = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$token;
  40. $post = [
  41. 'post'=>json_encode([
  42. 'code' => $code,
  43. ]),
  44. ];
  45. $post['header'][] = "Content-Type:application/json";
  46. list($status,$res) = $this->wx_return($url,$post);
  47. if($status) return [true,$res['phone_info']['phoneNumber']];
  48. else return [false,$res];
  49. }
  50. private function wx_return($url,$data=[]){
  51. $res = $this->curlOpen($url,$data);
  52. $res = json_decode($res,true);
  53. if(isset($res['errcode'])&&$res['errcode'] !== 0) return [false,$res['errmsg']];
  54. return [true,$res];
  55. }
  56. //写一个单独的微信推送
  57. /**
  58. * @param $user_id
  59. * @param $type 1审核申请 2抄送 3 审核结果
  60. * @param $state 0申请审核1审核通过2审核拒绝
  61. * @param $menu_id
  62. * @param $order_data
  63. * @return array
  64. */
  65. public function wx_sendMsg($user_id,$type,$state,$menu_id,$order_data){
  66. file_put_contents('tt.txt',json_encode([$user_id,$type,$state,$menu_id,$order_data]));
  67. $openid = WxEmployeeOfficial::where('employee_id',$user_id)->value('openid');
  68. if(empty($openid)) return [false,'not invaild openid'];
  69. $config = config('wx.msg');
  70. switch ($type){
  71. case '1':
  72. case '2':
  73. $menu_type = $menu_id.'_'.$type;
  74. break;
  75. case '3':
  76. $menu_type = $menu_id.'_'.$type.'_'.$state;
  77. break;
  78. default :
  79. $menu_type = '';
  80. }
  81. if(!isset($config['wx_menu'][$menu_type])) return [false,'not invaild menu_type'];
  82. $tmp_data = $config['wx_tmp_id'][$config['wx_menu'][$menu_type]];
  83. $detail = $tmp_data['param'];
  84. $tmp_id = $tmp_data['tmp_id'];
  85. $data = [];
  86. foreach ($detail as $k=>$v){
  87. $data[$v] = $order_data[$k];
  88. }
  89. $this->sendTmpMsg($openid,$tmp_id,['detail'=>$data]);
  90. return [true,''];
  91. }
  92. public function sendTmpMsg($openid,$tempid,$data){
  93. $reload_url = $data['reload_url']??'';
  94. list($status,$token) = $this->getToken();
  95. if(!$status) return [false,$token];
  96. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;
  97. $post = '{
  98. "touser":"'.$openid.'",
  99. "template_id":"'.$tempid.'",
  100. "url":"'.$reload_url.'",
  101. "data":{
  102. "first": {
  103. "value":"1",
  104. "color":"#173177"
  105. },
  106. %s
  107. "remark":{
  108. "value":"1",
  109. "color":"#173177"
  110. }
  111. }
  112. }';
  113. $content = "";
  114. foreach ($data['detail'] as $k=>$v){
  115. $content .= '"'.$k.'": {
  116. "value":"'.$v.'",
  117. "color":"#173177"
  118. },';
  119. }
  120. $post = sprintf($post,$content);
  121. $res = $this->curlOpen($url,['post'=>$post]);
  122. $res = json_decode($res,true);
  123. if(isset($res['errcode'])&&$res['errcode'] != 0) return [false,$res['errmsg']];
  124. if(isset($res['errcode'])&&$res['errcode'] === 0) return [true,''];
  125. return [false,json_encode($res)];
  126. }
  127. }