WxSendMessageService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Service\Weixin;
  3. use App\Model\WxEmployeeOfficial;
  4. use App\Service\Service;
  5. use Illuminate\Support\Facades\Log;
  6. class WxSendMessageService extends WeixinService
  7. {
  8. /**
  9. * @param $user_id
  10. * @param $type 1审核申请 2抄送 3 审核结果
  11. * @param $state 0申请审核1审核通过2审核拒绝
  12. * @param $menu_id
  13. * @param $order_data
  14. * @return array
  15. */
  16. public function wx_sendMsg($user_id,$type,$state,$menu_id,$order_data){
  17. $openid = WxEmployeeOfficial::where('employee_id',$user_id)->value('openid');
  18. if(empty($openid)) return [false,'not invaild openid'];
  19. $config = config('wx.msg');
  20. switch ($type){
  21. case '1':
  22. case '2':
  23. $menu_type = $menu_id.'_'.$type;
  24. break;
  25. case '3':
  26. $menu_type = $menu_id.'_'.$type.'_'.$state;
  27. break;
  28. default :
  29. $menu_type = '';
  30. }
  31. if(!isset($config['wx_menu'][$menu_type])) return [false,'not invaild menu_type'];
  32. $tmp_data = $config['wx_tmp_id'][$config['wx_menu'][$menu_type]];
  33. $detail = $tmp_data['param'];
  34. $tmp_id = $tmp_data['tmp_id'];
  35. $data = [];
  36. foreach ($detail as $k=>$v){
  37. $data[$v] = $order_data[$k];
  38. }
  39. list($status,$msg) = $this->sendTmpMsg($openid,$tmp_id,['detail'=>$data]);
  40. if(! $status) return [false, $msg];
  41. return [true,''];
  42. }
  43. public function sendTmpMsg($openid,$tempid,$data)
  44. {
  45. $reload_url = $data['reload_url'] ?? '';
  46. list($status, $token) = $this->getToken();
  47. if (!$status) return [false, $token];
  48. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $token;
  49. $post = '{
  50. "touser":"' . $openid . '",
  51. "template_id":"' . $tempid . '",
  52. "url":"' . $reload_url . '",
  53. "data":{
  54. "first": {
  55. "value":"1",
  56. "color":"#173177"
  57. },
  58. %s
  59. "remark":{
  60. "value":"1",
  61. "color":"#173177"
  62. }
  63. }
  64. }';
  65. $content = "";
  66. foreach ($data['detail'] as $k => $v) {
  67. $content .= '"' . $k . '": {
  68. "value":"' . $v . '",
  69. "color":"#173177"
  70. },';
  71. }
  72. $post = sprintf($post, $content);
  73. $res = $this->curlOpen($url, ['post' => $post]);
  74. $res = json_decode($res, true);
  75. if (isset($res['errcode']) && $res['errcode'] != 0) return [false, $res['errmsg']];
  76. if (isset($res['errcode']) && $res['errcode'] === 0) return [true, ''];
  77. return [false, json_encode($res)];
  78. }
  79. }