WxSendMessageService.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. file_put_contents('tt.txt',json_encode([$user_id,$type,$state,$menu_id,$order_data]));
  18. $openid = WxEmployeeOfficial::where('employee_id',$user_id)->value('openid');
  19. if(empty($openid)) return [false,'not invaild openid'];
  20. $config = config('wx.msg');
  21. switch ($type){
  22. case '1':
  23. case '2':
  24. $menu_type = $menu_id.'_'.$type;
  25. break;
  26. case '3':
  27. $menu_type = $menu_id.'_'.$type.'_'.$state;
  28. break;
  29. default :
  30. $menu_type = '';
  31. }
  32. if(!isset($config['wx_menu'][$menu_type])) return [false,'not invaild menu_type'];
  33. $tmp_data = $config['wx_tmp_id'][$config['wx_menu'][$menu_type]];
  34. $detail = $tmp_data['param'];
  35. $tmp_id = $tmp_data['tmp_id'];
  36. $data = [];
  37. foreach ($detail as $k=>$v){
  38. $data[$v] = $order_data[$k];
  39. }
  40. list($status,$msg) = $this->sendTmpMsg($openid,$tmp_id,['detail'=>$data]);
  41. if(! $status) return [false, $msg];
  42. return [true,''];
  43. }
  44. public function sendTmpMsg($openid,$tempid,$data)
  45. {
  46. $reload_url = $data['reload_url'] ?? '';
  47. list($status, $token) = $this->getToken();
  48. if (!$status) return [false, $token];
  49. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $token;
  50. $post = '{
  51. "touser":"' . $openid . '",
  52. "template_id":"' . $tempid . '",
  53. "url":"' . $reload_url . '",
  54. "data":{
  55. "first": {
  56. "value":"1",
  57. "color":"#173177"
  58. },
  59. %s
  60. "remark":{
  61. "value":"1",
  62. "color":"#173177"
  63. }
  64. }
  65. }';
  66. $content = "";
  67. foreach ($data['detail'] as $k => $v) {
  68. $content .= '"' . $k . '": {
  69. "value":"' . $v . '",
  70. "color":"#173177"
  71. },';
  72. }
  73. $post = sprintf($post, $content);
  74. $res = $this->curlOpen($url, ['post' => $post]);
  75. $res = json_decode($res, true);
  76. if (isset($res['errcode']) && $res['errcode'] != 0) return [false, $res['errmsg']];
  77. if (isset($res['errcode']) && $res['errcode'] === 0) return [true, ''];
  78. return [false, json_encode($res)];
  79. }
  80. }