1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace App\Service\Weixin;
- use App\Model\WxEmployeeOfficial;
- use App\Service\Service;
- use Illuminate\Support\Facades\Log;
- class WxSendMessageService extends WeixinService
- {
- /**
- * @param $user_id
- * @param $type 1审核申请 2抄送 3 审核结果
- * @param $state 0申请审核1审核通过2审核拒绝
- * @param $menu_id
- * @param $order_data
- * @return array
- */
- public function wx_sendMsg($user_id,$type,$state,$menu_id,$order_data,$pagepath = "",$openid = ""){
- //如果没提供openid 则查询
- if(empty($openid)) $openid = WxEmployeeOfficial::where('employee_id',$user_id)->value('openid');
- if(empty($openid)) return [false,'not invaild openid'];
- $config = config('wx.msg');
- switch ($type){
- case '1':
- case '2':
- $menu_type = $menu_id.'_'.$type;
- break;
- case '3':
- $menu_type = $menu_id.'_'.$type.'_'.$state;
- break;
- default :
- $menu_type = '';
- }
- if(!isset($config['wx_menu'][$menu_type])) return [false,'not invaild menu_type'];
- $tmp_data = $config['wx_tmp_id'][$config['wx_menu'][$menu_type]];
- $detail = $tmp_data['param'];
- $tmp_id = $tmp_data['tmp_id'];
- $data = [];
- foreach ($detail as $k=>$v){
- $data[$v] = $order_data[$k];
- }
- list($status,$msg) = $this->sendTmpMsg($openid,$tmp_id,['detail'=>$data],$pagepath);
- if(! $status) return [false, $msg];
- return [true,''];
- }
- public function sendTmpMsg($openid,$tempid,$data,$pagepath="")
- {
- $reload_url = $data['reload_url'] ?? '';
- list($status, $token) = $this->getToken();
- if (!$status) return [false, $token];
- $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $token;
- $post = '{
- "touser":"' . $openid . '",
- "template_id":"' . $tempid . '",
- "url":"' . $reload_url . '",
- "miniprogram":{
- "appid":"wxc0bc3dfc58b4e00e",
- "pagepath":"' . $pagepath . '"
- },
- "data":{
- "first": {
- "value":"1",
- "color":"#173177"
- },
- %s
- "remark":{
- "value":"1",
- "color":"#173177"
- }
- }
- }';
- $content = "";
- foreach ($data['detail'] as $k => $v) {
- $content .= '"' . $k . '": {
- "value":"' . $v . '",
- "color":"#173177"
- },';
- }
- $post = sprintf($post, $content);
- $res = $this->curlOpen($url, ['post' => $post]);
- $res = json_decode($res, true);
- if (isset($res['errcode']) && $res['errcode'] != 0) return [false, $res['errmsg']];
- if (isset($res['errcode']) && $res['errcode'] === 0) return [true, ''];
- return [false, json_encode($res)];
- }
- }
|