194 lines
7.6 KiB
PHP
194 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace app\api\model;
|
|
|
|
use think\Db;
|
|
use think\Model;
|
|
|
|
class AsyncPushMessage
|
|
{
|
|
//数据存入队列
|
|
public $keyname = 'async:push:message';
|
|
public $mutil = 0.8;
|
|
public $is_compute = 0; //1 计算 0 不计算
|
|
public $integral_name = '金币';
|
|
/*
|
|
$type=1 1 转赠 2 单个礼物 3 一键送礼物 4 盲盒
|
|
*/
|
|
public function store_message($message_data, $type)
|
|
{
|
|
$this->deal_message_data($message_data, $type);
|
|
}
|
|
private function _store_message($from_uid, $to_uid, $message, $type)
|
|
{
|
|
$data = [
|
|
'from_uid' => $from_uid,
|
|
'to_uid' => $to_uid,
|
|
'message' => $message,
|
|
'type' => $type,
|
|
];
|
|
$json_data = json_encode($data);
|
|
$redis = connectionRedis();
|
|
$redis->rpush($this->keyname, $json_data);
|
|
}
|
|
|
|
//发送消息
|
|
public function send_message()
|
|
{
|
|
$redis = connectionRedis();
|
|
$redis_len = $redis->llen($this->keyname);
|
|
// dump($redis_len);die;
|
|
if(empty($redis_len)) {
|
|
return true;
|
|
}
|
|
$json_data = $redis->blpop($this->keyname, 1);
|
|
if(empty($json_data)) {
|
|
return true;
|
|
}
|
|
$json_data = $json_data[1];
|
|
$data = json_decode($json_data, true);
|
|
$from_uid = $data['from_uid'];
|
|
$to_uid = $data['to_uid'];
|
|
$message = $data['message'];
|
|
$type = $data['type'];
|
|
$send_data['text'] = $message;
|
|
if($type == 2) {
|
|
$send_data['businessID'] = 'custom_gift_record';
|
|
}elseif($type == 1) {
|
|
$send_data['businessID'] = 'custom_hongbao';
|
|
} else {
|
|
return true;
|
|
}
|
|
$tencent_message = json_encode($send_data);
|
|
$res = model('api/Tencent')->user_custom_sendmsg($from_uid, $to_uid, $tencent_message, 1);
|
|
|
|
}
|
|
|
|
public function deal_message_data($message_data, $type)
|
|
{
|
|
switch ($type) {
|
|
case 1:
|
|
$from_uid = $message_data['from_uid'];
|
|
$to_uid = $message_data['to_uid'];
|
|
$integral = $message_data['integral'];
|
|
$message = '给您发了' . $integral . $this->integral_name;
|
|
$this->_store_message($from_uid, $to_uid, $message, 1);
|
|
$async_push_message_data = [
|
|
'uid' => $from_uid,
|
|
'receive_uid' => $to_uid,
|
|
'type' => $type,
|
|
'message' => $message,
|
|
'total_amount' => $integral,
|
|
'add_time' => time(),
|
|
'rid' => 0
|
|
];
|
|
Db::name('async_push_message_log')->insert($async_push_message_data);
|
|
// code...
|
|
break;
|
|
case 2:
|
|
$async_push_message_data = [];
|
|
foreach($message_data as $val) {
|
|
$from_uid = $val['from_uid'];
|
|
$to_uid = $val['to_uid'];
|
|
$gift_name = $val['gift_name'];
|
|
$gift_num = $val['gift_num'];
|
|
$gift_price = $val['gift_price'];
|
|
$total_gift_price = $gift_price * $gift_num;
|
|
$message = "送出了{$gift_name}({$gift_price}".$this->integral_name.")*{$gift_num}";
|
|
if($this->is_compute == 1) {
|
|
$message .= "\n总计:" . $total_gift_price . '*' . $this->mutil . '=' . (bcmul($total_gift_price, $this->mutil, 2));
|
|
} else {
|
|
$message .= "总计:" . $total_gift_price;
|
|
}
|
|
$this->_store_message($from_uid, $to_uid, $message, 2);
|
|
$temp = [
|
|
'uid' => $from_uid,
|
|
'receive_uid' => $to_uid,
|
|
'type' => $type,
|
|
'message' => $message,
|
|
'total_amount' => $total_gift_price,
|
|
'add_time' => time(),
|
|
'rid' => $val['rid']
|
|
];
|
|
$async_push_message_data[] = $temp;
|
|
}
|
|
if($async_push_message_data) {
|
|
Db::name('async_push_message_log')->insertAll($async_push_message_data);
|
|
}
|
|
// code...
|
|
break;
|
|
case 3:
|
|
$from_uid = $message_data['from_uid'];
|
|
$to_uid = $message_data['to_uid'];
|
|
$gift_list = $message_data['gift_list'];
|
|
$rid = $message_data['rid'];
|
|
$total_amount = 0;
|
|
$message = '送出了';
|
|
foreach($gift_list as $val) {
|
|
|
|
$gift_name = $val['gift_name'];
|
|
$gift_num = $val['gift_num'];
|
|
$gift_price = $val['gift_price'];
|
|
$gift_total_price = ($gift_price * $gift_num);
|
|
$total_amount += $gift_total_price;
|
|
$message .= "{$gift_name}({$gift_price}".$this->integral_name.")*{$gift_num}";
|
|
}
|
|
$message .= ";";
|
|
if($this->is_compute == 1) {
|
|
$message .= "总计:". $total_amount . "*" . $this->mutil . "=" . (bcmul($total_amount, $this->mutil, 2));
|
|
} else {
|
|
$message .= "总计:" . $total_amount;
|
|
}
|
|
$this->_store_message($from_uid, $to_uid, $message, 2);
|
|
$async_push_message_data = [
|
|
'uid' => $from_uid,
|
|
'receive_uid' => $to_uid,
|
|
'type' => $type,
|
|
'message' => $message,
|
|
'total_amount' => $total_amount,
|
|
'add_time' => time(),
|
|
'rid' => $rid
|
|
];
|
|
Db::name('async_push_message_log')->insert($async_push_message_data);
|
|
// code...
|
|
break;
|
|
case 4:
|
|
$from_uid = $message_data['from_uid'];
|
|
$to_uid = $message_data['to_uid'];
|
|
$gift_list = $message_data['gift_list'];
|
|
$rid = $message_data['rid'];
|
|
$total_amount = 0;
|
|
$message = '送出了';
|
|
foreach($gift_list as $val) {
|
|
$gift_name = $val['gift_name'];
|
|
$gift_num = $val['gift_num'];
|
|
$gift_price = $val['gift_price'];
|
|
$gift_total_price = ($gift_price * $gift_num);
|
|
$total_amount += $gift_total_price;
|
|
$message .= "{$gift_name}({$gift_price}".$this->integral_name.")*{$gift_num}";
|
|
}
|
|
$message .= ";";
|
|
if($this->is_compute == 1) {
|
|
$message .= "总计:". $total_amount . "*" . $this->mutil . "=" . (bcmul($total_amount, $this->mutil, 2));
|
|
} else {
|
|
$message .= "总计:" . $total_amount;
|
|
}
|
|
$this->_store_message($from_uid, $to_uid, $message, 2);
|
|
$async_push_message_data = [
|
|
'uid' => $from_uid,
|
|
'receive_uid' => $to_uid,
|
|
'type' => $type,
|
|
'message' => $message,
|
|
'total_amount' => $total_amount,
|
|
'add_time' => time(),
|
|
'rid' => $rid
|
|
];
|
|
Db::name('async_push_message_log')->insert($async_push_message_data);
|
|
break;
|
|
default:
|
|
// code...
|
|
break;
|
|
}
|
|
}
|
|
}
|