WxSendMessageService.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "miniprogram":{
  54. "appid":"wxc0bc3dfc58b4e00e",
  55. "pagepath":""
  56. },
  57. "data":{
  58. "first": {
  59. "value":"1",
  60. "color":"#173177"
  61. },
  62. %s
  63. "remark":{
  64. "value":"1",
  65. "color":"#173177"
  66. }
  67. }
  68. }';
  69. $content = "";
  70. foreach ($data['detail'] as $k => $v) {
  71. $content .= '"' . $k . '": {
  72. "value":"' . $v . '",
  73. "color":"#173177"
  74. },';
  75. }
  76. $post = sprintf($post, $content);
  77. $res = $this->curlOpen($url, ['post' => $post]);
  78. $res = json_decode($res, true);
  79. if (isset($res['errcode']) && $res['errcode'] != 0) return [false, $res['errmsg']];
  80. if (isset($res['errcode']) && $res['errcode'] === 0) return [true, ''];
  81. return [false, json_encode($res)];
  82. }
  83. }