quanwei
2025-12-05 feda780069d64479c0c20493603717e100655da9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
 
namespace app\common\service\message;
 
use app\common\library\easywechat\AppWx;
 
/**
 * 微信小程序消息通知服务
 */
class WxMessageService
{
    /**
     * 订单支付成功后通知
     */
    public static function send($data, $wx_template, $touser, $app_id)
    {
        try{
            $wx_template = json_decode($wx_template, true);
 
            $var_data = $wx_template['var_data'];
            $send_data = [];
            foreach ($var_data as $key => $value){
                if(isset($data[$key])){
                    $send_data[$value['field_name']] = [
                        'value' => urldecode($data[$key])
                    ];
                }else{
                    $send_data[$value['field_name']] = [
                        'value' => urldecode($value['filed_value'])
                    ];
                }
            }
 
 
            foreach($send_data as $key => $value){
                if(mb_strlen($value['value']) > 20){
                    $send_data[$key]['value'] = mb_substr($value['value'],0, 20);
                }
            }
 
            $app = AppWx::getApp($app_id);
            $result = $app->subscribe_message->send([
                'template_id' => $wx_template['template_id'],
                'touser' => $touser,
                'page' => '/pages/index/index',
                'data' => $send_data,
            ]);
 
            log_write($result);
        }catch (\Exception $e){
            log_write('小程序订阅消息发送失败');
            log_write($e->getMessage());
        }
    }
 
}