WxSendMessageService.php 3.0 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,$pagepath = "",$openid = ""){
  17. //如果没提供openid 则查询
  18. if(empty($openid)) $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],$pagepath);
  41. if(! $status) return [false, $msg];
  42. return [true,''];
  43. }
  44. public function sendTmpMsg($openid,$tempid,$data,$pagepath="")
  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. "miniprogram":{
  55. "appid":"wxc0bc3dfc58b4e00e",
  56. "pagepath":"' . $pagepath . '"
  57. },
  58. "data":{
  59. "first": {
  60. "value":"1",
  61. "color":"#173177"
  62. },
  63. %s
  64. "remark":{
  65. "value":"1",
  66. "color":"#173177"
  67. }
  68. }
  69. }';
  70. $content = "";
  71. foreach ($data['detail'] as $k => $v) {
  72. $content .= '"' . $k . '": {
  73. "value":"' . $v . '",
  74. "color":"#173177"
  75. },';
  76. }
  77. $post = sprintf($post, $content);
  78. $res = $this->curlOpen($url, ['post' => $post]);
  79. $res = json_decode($res, true);
  80. if (isset($res['errcode']) && $res['errcode'] != 0) return [false, $res['errmsg']];
  81. if (isset($res['errcode']) && $res['errcode'] === 0) return [true, ''];
  82. return [false, json_encode($res)];
  83. }
  84. }