80 lines
2.6 KiB
PHP
80 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace extend\chat\model;
|
|
|
|
use ba\Random;
|
|
use EasyWeChat\Factory;
|
|
use support\Redis;
|
|
|
|
class Business
|
|
{
|
|
// 存消息 未读消息保存30天 30天以后自动删除
|
|
public static function setRedisMessage($uid, $user_type, $data, $open_id = '')
|
|
{
|
|
$str_uid = Base::getUid($uid, $user_type);
|
|
$rand_msg_str = $str_uid . Random::build();
|
|
$set_str = 'nomsg:message:' . $str_uid;
|
|
$set_info_str = 'nomsg:message:' . $rand_msg_str;
|
|
$sent = Redis::get($set_str);
|
|
if($sent) {
|
|
$sent = json_decode($sent, true);
|
|
$sent[count($sent)] = $set_info_str;
|
|
Redis::setEx($set_str, 30 * 86400, json_encode($sent));
|
|
}else {
|
|
Redis::setEx($set_str, 30 * 86400, json_encode([$set_info_str]));
|
|
}
|
|
Redis::setEx($set_info_str, 30 * 86400, json_encode($data));
|
|
|
|
|
|
if($open_id) {
|
|
|
|
// 保存的同时发送模版消息通知用户有未读消息
|
|
$config = [
|
|
'app_id' => config('cons.app_id'),
|
|
'secret' => config('cons.secret'),
|
|
'response_type' => 'array',
|
|
];
|
|
$app = Factory::officialAccount($config);
|
|
$acc_token = Redis::get('wx:ac:AccessToken');
|
|
if($acc_token) {
|
|
$app['access_token']->setToken($acc_token);
|
|
}
|
|
$wechat_res = $app->template_message->send([
|
|
'touser' => $open_id,
|
|
'template_id' => 'hGsUGZlWqWC9TMm4ZGZuz0OwE4gwSmvgbr5ecmTDvq4',
|
|
'miniprogram' => [
|
|
'appid' => 'wxd88fffa983758a30', // 零点八零
|
|
'pagepath' => 'pages/index/index', // 零点八零
|
|
],
|
|
'data' => [
|
|
'thing7' => $record['shop_name'],
|
|
'thing14' => $record['activity_detail'],
|
|
'time3' => $record['activity_time'],
|
|
'thing16' => $record['address'],
|
|
],
|
|
]);
|
|
|
|
}
|
|
}
|
|
|
|
// 拉消息
|
|
public static function getRedisMessage($uid, $user_type)
|
|
{
|
|
$str_uid = Base::getUid($uid, $user_type);
|
|
$set_str = 'nomsg:message:' . $str_uid;
|
|
$sent = Redis::get($set_str);
|
|
$data = [];
|
|
if($sent) {
|
|
$sent = json_decode($sent, true);
|
|
foreach ($sent as $k => $v) {
|
|
$msg = Redis::get($v);
|
|
if($msg) {
|
|
$data[] = json_decode(Redis::get($v), true);
|
|
}
|
|
Redis::del($v);
|
|
}
|
|
Redis::del($set_str);
|
|
}
|
|
return $data;
|
|
}
|
|
} |