添加网站文件
This commit is contained in:
115
application/common/behavior/CancelOrder.php
Normal file
115
application/common/behavior/CancelOrder.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\common\behavior;
|
||||
|
||||
use app\api\logic\DistributionLogic;
|
||||
use app\api\model\Order;
|
||||
use app\common\logic\IntegralLogic;
|
||||
use app\common\logic\OrderGoodsLogic;
|
||||
use app\common\logic\OrderLogLogic;
|
||||
use app\common\model\AccountLog;
|
||||
use app\common\model\OrderLog;
|
||||
use think\Db;
|
||||
|
||||
|
||||
/**
|
||||
* 取消订单后的操作
|
||||
* Class CancelOrder
|
||||
* @package app\common\behavior
|
||||
*/
|
||||
class CancelOrder
|
||||
{
|
||||
public function run($params)
|
||||
{
|
||||
|
||||
$order_id = $params['order_id'];
|
||||
$handle_id = $params['handle_id'] ?? 0;
|
||||
$handle_type = $params['handle_type'] ?? 0;
|
||||
|
||||
$order = Order::get(['id' => $order_id], ['orderGoods']);
|
||||
if (empty($order)){
|
||||
return false;
|
||||
}
|
||||
|
||||
$user_id = $order['user_id'];
|
||||
|
||||
//订单取消后更新分销订单为已失效状态
|
||||
DistributionLogic::setDistributionOrderFail($order_id);
|
||||
|
||||
//下单扣库存的话(deduct_type=1),回退库存,支付后扣库存的话(deduct_type=0),判断订单是否支付才去回退库存
|
||||
OrderGoodsLogic::backStock($order['orderGoods'], $order['pay_status']);
|
||||
|
||||
//扣除每日首单奖励
|
||||
// IntegralLogic::backIntegral($user_id, $order_id);
|
||||
|
||||
//下单是否使用积分
|
||||
if ($order['use_integral'] > 0){
|
||||
IntegralLogic::handleIntegral(
|
||||
$user_id,
|
||||
$order['use_integral'],
|
||||
AccountLog::cancel_order_refund_integral,
|
||||
$order_id
|
||||
);
|
||||
}
|
||||
|
||||
//返回优惠券
|
||||
if (!empty($order['coupon_list_id'])) {
|
||||
Db::name('coupon_list')->where(['id' => $order['coupon_list_id']])->update([
|
||||
'status' => 0,
|
||||
'use_time' => '',
|
||||
'order_id' => '',
|
||||
'update_time' => time(),
|
||||
]);
|
||||
}
|
||||
|
||||
//订单日志
|
||||
switch ($handle_type){
|
||||
case OrderLog::TYPE_USER:
|
||||
OrderLogLogic::record(
|
||||
OrderLog::TYPE_USER,
|
||||
OrderLog::USER_CANCEL_ORDER,
|
||||
$order_id,
|
||||
$user_id,
|
||||
OrderLog::USER_CANCEL_ORDER
|
||||
);
|
||||
break;
|
||||
|
||||
case OrderLog::TYPE_SHOP:
|
||||
OrderLogLogic::record(
|
||||
OrderLog::TYPE_SHOP,
|
||||
OrderLog::SHOP_CANCEL_ORDER,
|
||||
$order_id,
|
||||
$handle_id,
|
||||
OrderLog::SHOP_CANCEL_ORDER
|
||||
);
|
||||
break;
|
||||
|
||||
default:
|
||||
OrderLogLogic::record(
|
||||
OrderLog::TYPE_SYSTEM,
|
||||
OrderLog::SYSTEM_CANCEL_ORDER,
|
||||
$order_id,
|
||||
0,
|
||||
OrderLog::SYSTEM_CANCEL_ORDER
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
113
application/common/behavior/Footprint.php
Normal file
113
application/common/behavior/Footprint.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\common\behavior;
|
||||
|
||||
use app\common\model\Footprint as FootprintModel;
|
||||
use app\common\model\FootprintRecord as FootprintRecordModel;
|
||||
use app\common\server\ConfigServer;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 足迹记录
|
||||
* Class Footprint
|
||||
* @package app\common\behavior
|
||||
*/
|
||||
class Footprint
|
||||
{
|
||||
public function run($params)
|
||||
{
|
||||
try {
|
||||
// 全局气泡开关
|
||||
$footprint_status = ConfigServer::get('footprint', 'footprint_status', 1);
|
||||
if (!$footprint_status) return;
|
||||
// 判断某个气泡功能是否开启
|
||||
if ($this->getFootPrintIsAllowByType($params['type'])) {
|
||||
$this->record($params);
|
||||
}
|
||||
} catch (\Exception $e) {}
|
||||
}
|
||||
|
||||
// 记录足迹逻辑
|
||||
private function record($params)
|
||||
{
|
||||
// 获取参数(主要参数: type, user_id, goods_id[可能不存在])
|
||||
if (empty($params['type']) || !$params['type']) return;
|
||||
if (empty($params['user_id']) || !$params['user_id']) return;
|
||||
|
||||
switch ($params['type']) {
|
||||
case FootprintModel::enter_mall: //进入商城(30分钟内没记录才记录)
|
||||
if(!FootprintRecordModel::getFootPrintOneHourInner($params)) {
|
||||
$tpl = '访问了商城';
|
||||
FootprintRecordModel::add($params, $tpl);
|
||||
}
|
||||
break;
|
||||
case FootprintModel::browse_goods: //浏览商品(30分钟内没记录才记录)
|
||||
if(!FootprintRecordModel::getFootPrintOneHourInner($params)) {
|
||||
$tpl = '正在浏览'.$this->getGoodsName($params['foreign_id']);
|
||||
FootprintRecordModel::add($params, $tpl);
|
||||
}
|
||||
break;
|
||||
case FootprintModel::add_cart: //加入购物车
|
||||
$tpl = '正在购买'.$this->getGoodsName($params['foreign_id']);
|
||||
FootprintRecordModel::add($params, $tpl);
|
||||
break;
|
||||
case FootprintModel::receive_coupon: //领取优惠券
|
||||
$tpl = '正在领取'.$this->getCouponName($params['foreign_id']).'优惠券';
|
||||
FootprintRecordModel::add($params, $tpl);
|
||||
break;
|
||||
case FootprintModel::place_order: //下单结算
|
||||
$tpl = '成功下单'.$params['total_money'];
|
||||
FootprintRecordModel::add($params, $tpl);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 获取商品名称
|
||||
private function getGoodsName($goods_id)
|
||||
{
|
||||
$goods = Db::name('goods')->where(['id'=>(int)$goods_id])
|
||||
->field('id,name')->find();
|
||||
if ($goods) {
|
||||
if (mb_strlen($goods['name']) > 6) {
|
||||
return mb_substr($goods['name'], 0, 6). '**';
|
||||
}
|
||||
return $goods['nickname'];
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
// 获取优惠券名称
|
||||
private function getCouponName($coupon_id)
|
||||
{
|
||||
$coupon = Db::name('coupon')->where(['id'=>(int)$coupon_id])
|
||||
->field('id,name')->find();
|
||||
return $coupon ? $coupon['name'] : '';
|
||||
}
|
||||
|
||||
// 获取足迹模板(是否允许使用)
|
||||
public function getFootPrintIsAllowByType($type)
|
||||
{
|
||||
$model = Db::name('footprint')->where(['type'=>(int)$type])->find();
|
||||
if ($model and $model['status']) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
104
application/common/behavior/GiveReward.php
Normal file
104
application/common/behavior/GiveReward.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace app\common\behavior;
|
||||
|
||||
|
||||
use app\common\logic\AccountLogLogic;
|
||||
use app\common\model\AccountLog;
|
||||
use Exception;
|
||||
use think\Db;
|
||||
use think\facade\Log;
|
||||
|
||||
class GiveReward
|
||||
{
|
||||
/**
|
||||
* @Notes: 赠送积分或成长值
|
||||
* @Author: 张无忌
|
||||
* @param $params
|
||||
* @return bool
|
||||
*/
|
||||
public function run($params)
|
||||
{
|
||||
try {
|
||||
$order_id = $params['order_id']; //(订单ID)
|
||||
$scene = $params['scene']; //(赠送场景 [1=订单支付, 2=订单发货, 3=订单完成, 4=订单结算])
|
||||
|
||||
$order = Db::name('order')->find($order_id);
|
||||
if (!$order) return false;
|
||||
if (empty($order['attach_values'])) return false;
|
||||
$attach = json_decode($order['attach_values'], true);
|
||||
|
||||
$this->giveIntegral($order, $attach, $scene);
|
||||
$this->giveGrowth($order, $attach, $scene);
|
||||
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
Log::write('赠送奖励失败:'.$e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Notes: 送积分
|
||||
* @Author: 张无忌
|
||||
* @param $order
|
||||
* @param $attach
|
||||
* @param $scene
|
||||
* @return bool
|
||||
* @throws \think\Exception
|
||||
*/
|
||||
private function giveIntegral($order, $attach, $scene)
|
||||
{
|
||||
if ($attach['give_integral_scene'] != $scene) return true;
|
||||
if ($attach['give_integral_num'] <= 0) return true;
|
||||
|
||||
Db::name('user')->where(['id'=>$order['user_id']])->update([
|
||||
'user_integral' => ['inc', $attach['give_integral_num']],
|
||||
'update_time' => time()
|
||||
]);
|
||||
|
||||
$change_type = 1;
|
||||
$channel = AccountLog::order_add_integral;
|
||||
AccountLogLogic::AccountRecord(
|
||||
$order['user_id'],
|
||||
$attach['give_integral_num'],
|
||||
$change_type,
|
||||
$channel, '', $order['id']
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Notes: 送成长值
|
||||
* @Author: 张无忌
|
||||
* @param $order
|
||||
* @param $attach
|
||||
* @param $scene
|
||||
* @return bool
|
||||
* @throws \think\Exception
|
||||
*/
|
||||
private function giveGrowth($order, $attach, $scene)
|
||||
{
|
||||
if ($attach['give_growth_scene'] != $scene) return true;
|
||||
if ($attach['give_growth_num'] <= 0) return true;
|
||||
|
||||
Db::name('user')->where(['id'=>$order['user_id']])->update([
|
||||
'user_growth' => ['inc', $attach['give_growth_num']],
|
||||
'update_time' => time()
|
||||
]);
|
||||
|
||||
$change_type = 1;
|
||||
$channel = AccountLog::order_give_growth;
|
||||
AccountLogLogic::AccountRecord(
|
||||
$order['user_id'],
|
||||
$attach['give_growth_num'],
|
||||
$change_type,
|
||||
$channel, '', $order['id']
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
147
application/common/behavior/Notice.php
Normal file
147
application/common/behavior/Notice.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\common\behavior;
|
||||
|
||||
use app\common\logic\NoticeLogic;
|
||||
use app\common\model\Client_;
|
||||
use app\common\model\NoticeSetting;
|
||||
use app\common\server\WxMessageServer;
|
||||
use think\Db;
|
||||
use think\Exception;
|
||||
|
||||
/**
|
||||
* 通知信息
|
||||
* Class Notice
|
||||
* @package app\common\behavior
|
||||
*/
|
||||
class Notice
|
||||
{
|
||||
public function run($params)
|
||||
{
|
||||
try {
|
||||
if (empty($params['scene'])) {
|
||||
throw new Exception('参数缺失');
|
||||
}
|
||||
|
||||
//找到当前场景的通知设置记录,发送通知
|
||||
$this->noticeByScene($params['user_id'], $params);
|
||||
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Notes: 根据各个场景发送通知
|
||||
* @param $user_id
|
||||
* @param $params
|
||||
* @author 段誉(2021/4/28 18:21)
|
||||
* @throws Exception
|
||||
*/
|
||||
public function noticeByScene($user_id, $params)
|
||||
{
|
||||
$scene_info = NoticeSetting::where('scene', $params['scene'])->find();
|
||||
|
||||
if (empty($scene_info)) {
|
||||
throw new Exception('信息错误');
|
||||
}
|
||||
|
||||
$params = $this->mergeParams($params);
|
||||
|
||||
//发送系统消息
|
||||
if (isset($scene_info['system_notice']['status']) && $scene_info['system_notice']['status'] == 1) {
|
||||
$content = NoticeLogic::contentFormat($scene_info['system_notice']['content'], $params);
|
||||
NoticeLogic::addNoticeLog($params, $scene_info,NoticeSetting::SYSTEM_NOTICE, $content);
|
||||
}
|
||||
|
||||
//发送短信记录
|
||||
if (isset($scene_info['sms_notice']['status']) && $scene_info['sms_notice']['status'] == 1) {
|
||||
|
||||
}
|
||||
|
||||
//发送公众号记录
|
||||
if (isset($scene_info['oa_notice']['status']) && $scene_info['oa_notice']['status'] == 1) {
|
||||
(new WxMessageServer($user_id,Client_::oa))->send($params);
|
||||
}
|
||||
|
||||
//发送小程序记录
|
||||
if (isset($scene_info['mnp_notice']['status']) && $scene_info['mnp_notice']['status'] == 1) {
|
||||
(new WxMessageServer($user_id, Client_::mnp))->send($params);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Notes: 拼装额外参数
|
||||
* @param $params
|
||||
* @author 段誉(2021/4/28 18:21)
|
||||
* @return mixed
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws \think\exception\DbException
|
||||
*/
|
||||
public function mergeParams($params)
|
||||
{
|
||||
//订单相关信息
|
||||
if (!empty($params['order_id'])) {
|
||||
$order = Db::name('order')->where(['id' => $params['order_id']])->find();
|
||||
$order_goods = Db::name('order_goods')->alias('og')
|
||||
->field('g.name')
|
||||
->join('goods g', 'og.goods_id = g.id')
|
||||
->where('og.order_id', $params['order_id'])
|
||||
->find();
|
||||
|
||||
$goods_name = $order_goods['name'] ?? '商品';
|
||||
if (mb_strlen($goods_name) > 8 ) {
|
||||
$goods_name = mb_substr($goods_name,0,8) . '..';
|
||||
}
|
||||
$params['goods_name'] = $goods_name;
|
||||
$params['order_sn'] = $order['order_sn'];
|
||||
$params['time'] = date('Y-m-d H:i', $order['create_time']);
|
||||
$params['total_num'] = $order['total_num'];
|
||||
$params['order_amount'] = $order['order_amount'];
|
||||
}
|
||||
|
||||
//用户相关信息
|
||||
if (!empty($params['user_id'])) {
|
||||
$user = Db::name('user')->where('id', $params['user_id'])->find();
|
||||
$params['nickname'] = $user['nickname'];
|
||||
}
|
||||
|
||||
//下级名称;(邀请人场景)
|
||||
if (!empty($params['lower_id'])) {
|
||||
$params['lower_name'] = Db::name('user')->where('id', $params['lower_id'])->value('nickname');
|
||||
}
|
||||
|
||||
//跳转路径
|
||||
$jump_path = NoticeSetting::getPathByScene($params['scene'], $params['order_id'] ?? 0);
|
||||
$params['url'] = $jump_path['url'];
|
||||
$params['page'] = $jump_path['page'];
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
89
application/common/behavior/Printer.php
Normal file
89
application/common/behavior/Printer.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\behavior;
|
||||
use app\common\model\Order;
|
||||
use app\common\server\ConfigServer;
|
||||
use app\common\server\YlyPrinter;
|
||||
use think\Db;
|
||||
|
||||
class Printer
|
||||
{
|
||||
public function run($params)
|
||||
{
|
||||
try{
|
||||
$order_id = $params['order_id'] ?? '';
|
||||
if(empty($order_id)){
|
||||
return false;
|
||||
}
|
||||
//打印机配置
|
||||
$printer_config = Db::name('printer_config')
|
||||
->where(['status'=>1])
|
||||
->find();
|
||||
|
||||
//打印机列表
|
||||
$printer_list = Db::name('printer')
|
||||
->where(['type'=>$printer_config['id'],'del'=>0,'auto_print'=>1])
|
||||
->select();
|
||||
|
||||
if(empty($printer_list) || empty($printer_list)){
|
||||
return false;
|
||||
}
|
||||
//获取订单信息
|
||||
$order = $this->getOrderList($order_id);
|
||||
//获取打印模板
|
||||
$template_config = ConfigServer::get('printer', 'yly_template', []);
|
||||
//示例化打印机类
|
||||
$yly_print = new YlyPrinter($printer_config['client_id'],$printer_config['client_secret']);
|
||||
//调用打印机
|
||||
$yly_print->ylyPrint($printer_list,$order,$template_config);
|
||||
|
||||
return true;
|
||||
|
||||
}catch (\Exception $e){
|
||||
|
||||
return $e->getMessage();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes:获取订单信息
|
||||
* @param $id
|
||||
* @author: cjhao 2021/4/24 15:22
|
||||
*/
|
||||
public function getOrderList($order_id){
|
||||
$order = new Order();
|
||||
|
||||
$order = $order
|
||||
->with(['order_goods'])
|
||||
->where('id', $order_id)
|
||||
->append(['delivery_address'])
|
||||
->find();
|
||||
|
||||
foreach ($order['order_goods'] as &$order_goods) {
|
||||
$info = json_decode($order_goods['goods_info'], true);
|
||||
$order_goods['name'] = $info['goods_name'];
|
||||
$order_goods['spec_value_str'] = $info['spec_value_str'];
|
||||
$order_goods['goods_image'] = empty($info['spec_image']) ? $info['image'] : $info['spec_image'];
|
||||
}
|
||||
return $order;
|
||||
}
|
||||
|
||||
}
|
||||
225
application/common/behavior/SmsSend.php
Normal file
225
application/common/behavior/SmsSend.php
Normal file
@@ -0,0 +1,225 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\behavior;
|
||||
|
||||
use app\common\logic\NoticeLogic;
|
||||
use app\common\model\{NoticeSetting, SmsLog, SmsConfig};
|
||||
use app\common\server\Alisms;
|
||||
use app\common\server\TencentSms;
|
||||
use think\Exception;
|
||||
|
||||
class SmsSend
|
||||
{
|
||||
protected $channel = 0; //发短信渠道[1=阿里短信, 2=腾讯短信]
|
||||
protected $sms_key = ''; //短信KEY
|
||||
protected $mobile = ''; //发送手机号码
|
||||
protected $sms_params = ''; //发送参数
|
||||
protected $config = ''; //短信配置
|
||||
protected $code = ''; //验证码(短信key为注册、找回密码)
|
||||
protected $sms = ''; //短信内容
|
||||
protected $content = ''; //短信准备发送内容
|
||||
protected $sms_log = ''; //短信记录model
|
||||
|
||||
protected $notice_id = null; //通知记录id
|
||||
|
||||
public function run($params)
|
||||
{
|
||||
try{
|
||||
// 设置短信参数
|
||||
$params['scene'] = $params['key'];
|
||||
$this->sms_key = $params['key'];
|
||||
$this->sms_params = $params['params'];
|
||||
$this->mobile = $params['mobile'];
|
||||
$result = $this->getSmsConfig($params['key']);
|
||||
if ($result !== true) return $result;
|
||||
|
||||
$this->sms_log = new SmsLog();
|
||||
$this->setSmsContent(); //短信内容
|
||||
$this->setSmsCode(); //短信验证码
|
||||
$this->createSmsLog(); //增加短信记录
|
||||
|
||||
//增加通知记录
|
||||
$this->notice_id = NoticeLogic::addNoticeLog($params, $this->sms, NoticeSetting::SMS_NOTICE, $this->content);
|
||||
|
||||
if ($this->channel == 1) {
|
||||
// 阿里云短信
|
||||
$alisms = new Alisms($this->config);
|
||||
$res = $alisms->setMobile($this->mobile)
|
||||
->setTemplateCode($this->sms['template_code'])
|
||||
->setTemplateParam($this->sms_params)
|
||||
->sendSms();
|
||||
|
||||
if (isset($res['Code']) && $res['Code'] == 'OK') {
|
||||
$send_status = SmsLog::send_success;
|
||||
$this->updateSmsLog($send_status, $res);
|
||||
return true;
|
||||
} else {
|
||||
$send_status = SmsLog::send_fail;
|
||||
$this->updateSmsLog($send_status, $res);
|
||||
$message = $res['Message'] ?? $res;
|
||||
throw new Exception('短信配置错误:' . $message);
|
||||
}
|
||||
} elseif ($this->channel == 2) {
|
||||
// 腾讯云短信
|
||||
$res = (new TencentSms($this->config))
|
||||
->setMobile($this->mobile)
|
||||
->setTemplateCode($this->sms['template_code'])
|
||||
->setTemplateParam($this->handleTcParams())
|
||||
->sendSms();
|
||||
|
||||
if (isset($res['SendStatusSet']) && $res['SendStatusSet'][0]['Code'] == 'Ok') {
|
||||
$send_status = SmsLog::send_success;
|
||||
$this->updateSmsLog($send_status, $res);
|
||||
return true;
|
||||
} else {
|
||||
$send_status = SmsLog::send_fail;
|
||||
$this->updateSmsLog($send_status, $res);
|
||||
$message = $res['SendStatusSet'][0]['Message'] ?? json_encode($res);
|
||||
throw new Exception('短信配置错误:' . $message);
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception('短信渠道不存在');
|
||||
} catch (\Exception $e) {
|
||||
$this->updateSmsLog(SmsLog::send_fail, $e->getMessage());
|
||||
NoticeLogic::updateNotice($this->notice_id, $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 获取短信配置 (场景是否开启)
|
||||
* @param $scene (场景编号)
|
||||
* @return bool|string
|
||||
*/
|
||||
public function getSmsConfig($scene)
|
||||
{
|
||||
// 短信是否开启
|
||||
$able_send = config('project.sms');
|
||||
$this->config = SmsConfig::get(['status' => 1]);
|
||||
if (!$this->config || true != $able_send) {
|
||||
return '短信功能未开启';
|
||||
}
|
||||
|
||||
// 获取短信内容
|
||||
$this->channel = $this->config['id'];
|
||||
$sms_scene = NoticeSetting::where('scene', $scene)->find();
|
||||
$this->sms = $sms_scene['sms_notice'];
|
||||
$this->variable = $sms_scene['variable'];
|
||||
if (!$this->sms) { return '短信模板不存在'; }
|
||||
if ($this->sms['status'] == 0) { return '相关通知未开启'; }
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes:短信内容
|
||||
*/
|
||||
public function setSmsContent()
|
||||
{
|
||||
$this->content = $this->sms['content'];
|
||||
foreach ($this->sms_params as $item => $val) {
|
||||
$search_replace = '{' . $item . '}';
|
||||
$this->content = str_replace($search_replace, $val, $this->content);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes:是否需要短信验证码
|
||||
*/
|
||||
public function setSmsCode()
|
||||
{
|
||||
if (in_array($this->sms_key, NoticeSetting::NOTICE_NEED_CODE)) {
|
||||
$code = array_intersect_key($this->sms_params, $this->variable);
|
||||
if ($code) $this->code = array_shift($code);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes:记录短信日志
|
||||
*/
|
||||
public function createSmsLog()
|
||||
{
|
||||
$this->sms_log->message_key = $this->sms_key;
|
||||
$this->sms_log->mobile = $this->mobile;
|
||||
$this->sms_log->content = $this->content;
|
||||
$this->sms_log->code = $this->code;
|
||||
$this->sms_log->send_status = SmsLog::send_ing;
|
||||
$this->sms_log->send_time = time();
|
||||
$this->sms_log->create_time = time();
|
||||
$this->sms_log->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes:更新短信状态(发送成功或发送失败)
|
||||
* @param $send_status int 短信状态
|
||||
* @param $result int 发送返回内容
|
||||
*/
|
||||
public function updateSmsLog($send_status, $result)
|
||||
{
|
||||
$this->sms_log->send_status = $send_status;
|
||||
$this->sms_log->results = json_encode($result);
|
||||
$this->sms_log->save();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @notes 腾讯云参数
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2021/8/4 14:10
|
||||
*/
|
||||
public function handleTcParams()
|
||||
{
|
||||
//腾讯云特殊处理
|
||||
$arr = [];
|
||||
$content = $this->sms['content'];
|
||||
foreach ($this->sms_params as $item => $val) {
|
||||
$search = '{' . $item . '}';
|
||||
if(strpos($content, $search) !== false
|
||||
&& !in_array($item, $arr)
|
||||
) {
|
||||
//arr => 获的数组[nickname, order_sn] //顺序可能是乱的
|
||||
$arr[] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
//arr2 => 获得数组[nickname, order_sn] //调整好顺序的变量名数组
|
||||
$arr2 = [];
|
||||
if (!empty($arr)) {
|
||||
foreach ($arr as $v) {
|
||||
$key = strpos($content, $v);
|
||||
$arr2[$key] = $v;
|
||||
}
|
||||
}
|
||||
//格式化 arr2 => 以小到大的排序的数组
|
||||
ksort($arr2);
|
||||
$arr3 = array_values($arr2);
|
||||
|
||||
//arr4 => 获取到变量数组的对应的值 [mofung, 123456789]
|
||||
$arr4 = [];
|
||||
foreach ($arr3 as $v2) {
|
||||
if(isset($this->sms_params[$v2])) {
|
||||
$arr4[] = $this->sms_params[$v2];
|
||||
}
|
||||
}
|
||||
return $arr4;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user