| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- <?php
- namespace common\logic\wechat;
- use addons\Store\common\enums\StoreEnum;
- use common\helpers\wechat\WechatTemplate;
- use common\models\mall\Mall;
- use common\models\user\User;
- use common\models\user\UserPartitionRelationship;
- use Yii;
- use yii\db\Exception;
- class WechatMessageLogic
- {
- private function getH5($mall_id){
- $h5 = \Yii::$app->services->setting->mall->get($mall_id, 'basic.web_url');
- if (empty($h5)) throw new Exception('h5域名没有配置');
- return substr($h5, 0, strpos($h5, '?'));
- }
- /**
- * 订单生成通知
- * @param $param ['mall_id'=>'mall_id','user_id'=>'user_id','order_id'=>'order_id','created_at'=>'created_at','goods_name'=>'goods_name','order_no'=>'order_no']
- * @return false|void
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function sendWechatTemplate($param) {
- try {
- $wechat = new WechatTemplate($param['mall_id']);
- $mall = Mall::findOne(['id'=>$param['mall_id']]);
- if(empty($mall)) return ;
- $page_path = WechatTemplate::getPagePath(WechatTemplate::ORDER_CREATE);
- $page_path =$page_path."?id=" . $param['order_id'].'&sign='.$mall['mall_sign'];
- $h5_url = $this->getH5($param['mall_id']).$page_path;
- $address_arr = explode(' ',$param['address']);
- $address_str = '';
- foreach ($address_arr as $v){
- $address_str.=$v;
- }
- $send_data = [
- 'character_string6'=>$param['order_no'],
- 'thing1'=>$param['goods_name'],
- 'time8'=>date("Y-m-d H:i:s", $param['created_at']),
- 'thing2'=>$param['receiver_name'],
- 'thing9'=>$address_str,
- ];
- $temp_key = WechatTemplate::ORDER_CREATE;
- $wechat->sendTemplate($param['user_id'], $temp_key, $h5_url, $send_data, $page_path);
- }catch (\Exception $e){
- Yii::error('OrderCreateListener:sendWechatTemplate:'.$e->getMessage());
- }
- }
- /**
- * 佣金到账发送模板消息
- * @param $send_message_data ['user_id'=>$user_id,'money'=>$data['money'],'wallet_type'=>$wallet_type, 'mall_id' => $data['mall_id'], 'change_type'=>$setData['change_type'],];
- * @return false|void
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function receiptSendMessage($send_message_data){
- try {
- $mall_id = $send_message_data['mall_id'];
- $user_id = $send_message_data['user_id'];
- if($send_message_data['change_type']!='add'||$send_message_data['wallet_type']!='commission') return true;
- $wechat = new WechatTemplate($mall_id);
- $mall = Mall::findOne(['id'=>$mall_id]);
- if(empty($mall)) return true;
- $page_path =WechatTemplate::getPagePath(WechatTemplate::COMMISSION_RECEIPT).'?sign='.$mall['mall_sign'];
- $h5_url = $this->getH5($mall_id).$page_path;
- $send_data = [];
- $temp_key = WechatTemplate::COMMISSION_RECEIPT;
- $wechat->sendTemplate($user_id, $temp_key, $h5_url, $send_data, $page_path);
- }catch (\Exception $e){
- Yii::error('UserWallet:receiptSendMessage:'.$e->getMessage());
- }
- }
- /**
- * 团队人员增加
- * @param $send_message_data ['user_id'=>$user_id, 'mall_id' => $data['mall_id']],];
- * @return false|void
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function teamAddSendMessage($send_message_data){
- try {
- $mall_id = $send_message_data['mall_id'];
- $user_id = $send_message_data['user_id'];
- $parent_id_arr = UserPartitionRelationship::getParentIdArr($user_id);
- if(empty($parent_id_arr))return false;
- $parent_id_arr = array_reverse($parent_id_arr);
- $mall = Mall::findOne(['id'=>$mall_id]);
- if(empty($mall)) return false;
- $page_path =WechatTemplate::getPagePath(WechatTemplate::TEAM_ADD).'?sign='.$mall['mall_sign'];
- $h5_url = $this->getH5($mall_id).$page_path;
- $user = \common\models\user\User::findOne(['id'=>$user_id]);
- if(empty($user)) return false;
- foreach ($parent_id_arr as $k=>$parent_id){
- $mobile = substr_replace($user['mobile'], '****', 3, 4);
- if($k==0){
- $wechat = new WechatTemplate($mall_id);
- $send_data = [
- 'thing2' => $user['nickname'],
- 'phone_number1' => $mobile,
- 'time3' => date('Y-m-d H:i:s',$user['created_at']),
- ];
- $temp_key = WechatTemplate::INVITATION_TO_REGISTER_SUCCESS;
- $wechat->sendTemplate($parent_id, $temp_key, $h5_url, $send_data, $page_path);
- continue;
- }
- $wechat = new WechatTemplate($mall_id);
- $send_data = [
- 'thing2' => $user['nickname'],
- 'phone_number1' => $mobile,
- 'time3' => date('Y-m-d H:i:s',$user['created_at']),
- ];
- $temp_key = WechatTemplate::TEAM_ADD;
- $wechat->sendTemplate($parent_id, $temp_key, $h5_url, $send_data, $page_path);
- }
- }catch (\Exception $e){
- Yii::error('User:teamAddSendMessage:'.$e->getMessage());
- }
- }
- /**
- * 提现成功
- * @param $send_message_data ['user_id'=>$user_id, 'money'=>$data['money'],'mall_id' => $data['mall_id']],];
- * @return false|void
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function withdrawalSuccessSendMessage($send_message_data){
- try {
- $mall_id = $send_message_data['mall_id'];
- $user_id = $send_message_data['user_id'];
- $wechat = new WechatTemplate($mall_id);
- $mall = Mall::findOne(['id'=>$mall_id]);
- if(empty($mall)) return false;
- $page_path =WechatTemplate::getPagePath(WechatTemplate::WITHDRAWAL_SUCCESS).'&sign='.$mall['mall_sign'];
- $h5_url = $this->getH5($mall_id).$page_path;
- $send_data = [
- 'amount1' => $send_message_data['money'],
- 'time3' => date("Y-m-d H:i:s", time()),
- ];
- $temp_key = WechatTemplate::WITHDRAWAL_SUCCESS;
- //var_dump($user_id, $temp_key, $h5_url, $send_data, $page_path);
- $wechat->sendTemplate($user_id, $temp_key, $h5_url, $send_data, $page_path);
- }catch (\Exception $e){
- Yii::error('User:withdrawalSuccessSendMessage:'.$e->getMessage());
- }
- }
- /**
- * 订单取消通知
- * @param $param ['mall_id'=>'mall_id','user_id'=>'user_id','receiver_name'=>'receiver_name','created_at'=>'created_at','pay_money'=>'pay_money','order_no'=>'order_no']
- * @return false|void
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function orderCancel($param) {
- try {
- $wechat = new WechatTemplate($param['mall_id']);
- $mall = Mall::findOne(['id'=>$param['mall_id']]);
- if(empty($mall)) return ;
- $page_path = WechatTemplate::getPagePath(WechatTemplate::ORDER_CREATE);
- $page_path =$page_path."?id=" . $param['order_id'].'&sign='.$mall['mall_sign'];
- $h5_url = $this->getH5($param['mall_id']).$page_path;
- $send_data = [
- 'character_string1' => $param['order_no'],
- 'phrase2' => '已取消',
- 'phrase3' => $param['phrase3'],
- ];
- $temp_key = WechatTemplate::ORDER_CANCEL;
- $wechat->sendTemplate($param['user_id'], $temp_key, $h5_url, $send_data, $page_path);
- }catch (\Exception $e){
- Yii::error('OrderCreateListener:sendWechatTemplate:'.$e->getMessage());
- }
- }
- /**
- * 订单发货通知
- * @param $param ['mall_id'=>'mall_id','user_id'=>'user_id','goods_name'=>'goods_name','order_no'=>'order_no','express_name'=>'express_name','express_no'=>'express_no','receiver_msg'=>'receiver_msg',]
- * @return false|void
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function orderSent($param) {
- try {
- $wechat = new WechatTemplate($param['mall_id']);
- $mall = Mall::findOne(['id'=>$param['mall_id']]);
- if(empty($mall)) return ;
- $page_path = WechatTemplate::getPagePath(WechatTemplate::ORDER_CREATE);
- $page_path =$page_path."?id=" . $param['order_id'].'&sign='.$mall['mall_sign'];
- $h5_url = $this->getH5($param['mall_id']).$page_path;
- $address_arr = explode(' ',$param['receiver_msg']);
- $address_str = '';
- foreach ($address_arr as $v){
- $address_str.=$v;
- }
- $send_data = [
- 'thing12' => $param['goods_name'],
- 'character_string1' => $param['order_no'],
- 'thing11' => $param['express_name'],
- 'character_string2' => $param['express_no'],
- 'thing5' => $address_str,
- ];
- $temp_key = WechatTemplate::ORDER_SENT;
- $wechat->sendTemplate($param['user_id'], $temp_key, $h5_url, $send_data, $page_path);
- }catch (\Exception $e){
- Yii::error('OrderCreateListener:sendWechatTemplate:'.$e->getMessage());
- }
- }
- /**
- * 订单确认收货通知
- * @param $param ['mall_id'=>'mall_id','user_id'=>'user_id','goods_name'=>'goods_name','order_no'=>'order_no','express_name'=>'express_name','express_no'=>'express_no','receiver_msg'=>'receiver_msg',]
- * @return false|void
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function orderReceipt($param) {
- try {
- $wechat = new WechatTemplate($param['mall_id']);
- $mall = Mall::findOne(['id'=>$param['mall_id']]);
- if(empty($mall)) return ;
- $page_path = WechatTemplate::getPagePath(WechatTemplate::ORDER_CREATE);
- $page_path =$page_path."?id=" . $param['order_id'].'&sign='.$mall['mall_sign'];
- $h5_url = $this->getH5($param['mall_id']).$page_path;
- $send_data = [
- 'character_string7' => $param['order_no'],
- 'thing6' => $param['goods_name'],
- 'time4' => date('Y-m-d H:i:s',$param['sign_time']),
- ];
- $temp_key = WechatTemplate::ORDER_CONFIRMED;
- $wechat->sendTemplate($param['user_id'], $temp_key, $h5_url, $send_data, $page_path);
- }catch (\Exception $e){
- Yii::error('OrderCreateListener:sendWechatTemplate:'.$e->getMessage());
- }
- }
- /**
- * 订单支付成功通知
- * @param $param ['mall_id'=>'mall_id','user_id'=>'user_id','order_no'=>'order_no','receiver_name'=>'receiver_name','mobile'=>'mobile','address'=>'address',]
- * @return false|void
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function orderPay($param) {
- try {
- $wechat = new WechatTemplate($param['mall_id']);
- $mall = Mall::findOne(['id'=>$param['mall_id']]);
- if(empty($mall)) return ;
- if (empty($param['page_path'])) {
- $page_path = WechatTemplate::getPagePath(WechatTemplate::ORDER_CREATE);
- $page_path =$page_path."?id=" . $param['order_id'].'&sign='.$mall['mall_sign'];
- $h5_url = $this->getH5($param['mall_id']).$page_path;
- } else {
- $h5_url = $this->getH5($param['mall_id']) . $param['page_path'];
- }
- $address_arr = explode(' ',$param['address']);
- $address_str = '';
- foreach ($address_arr as $v){
- $address_str.=$v;
- }
- $send_data = [
- 'amount3' => $param['pay_money'],
- 'thing1' => $param['goods_name'],
- 'thing4' => $address_str,
- 'character_string2' => $param['order_no'],
- ];
- $temp_key = WechatTemplate::ORDER_PAY_SUCCESS;
- $wechat->sendTemplate($param['user_id'], $temp_key, $h5_url, $send_data, $page_path);
- $this->friendPaySuccess($param);
- }catch (\Exception $e){
- Yii::error('OrderCreateListener:sendWechatTemplate:'.$e->getMessage());
- }
- }
- /**
- * 好友购买成功通知
- * @Author:lun
- * @Date:2024-04-28 18:04
- * @Copyright:copyright(c)2024 广东七件事集团
- * @param $param
- * @return true
- */
- public function friendPaySuccess($param)
- {
- try {
- $user = User::find()->select('parent_id,nickname,mobile')->where(['id' => $param['user_id']])->asArray()->one();
- if (empty($user['parent_id'])) return true;
- $mall = Mall::findOne(['id'=>$param['mall_id']]);
- if(empty($mall)) return true;
- $page_path = WechatTemplate::getPagePath(WechatTemplate::DEFAULT_PAGE);
- $page_path = $page_path . '?sign=' . $mall['mall_sign'];
- $h5_url = $this->getH5($param['mall_id']) . $page_path;
- // 参数组合
- $send_data = [
- 'character_string8' => $param['order_no'],
- 'thing1' => $user['nickname'] == '微信用户' ? $user['mobile'] : $user['nickname'],
- 'time2' => date('Y-m-d', $param['pay_time']),
- 'time10' => date('H:i:s', $param['pay_time']),
- ];
- $wechat = new WechatTemplate($param['mall_id']);
- $wechat->sendTemplate($user['parent_id'], WechatTemplate::RECOMMEND_PAY_SUCCESS, $h5_url, $send_data, $page_path);
- return true;
- } catch (\Exception $e) {
- Yii::$app->custom->logs('好友付款成功消息推送:' . $e->getMessage(), $param);
- return true;
- }
- }
- public function OrderCommissionSettle($param)
- {
- try {
- $wechat = new WechatTemplate($param['mall_id']);
- $mall = Mall::findOne(['id'=>$param['mall_id']]);
- if(empty($mall)) return ;
- $page_path = WechatTemplate::getPagePath(WechatTemplate::ORDER_COMMISSION_SETTLE);
- $page_path = $page_path . '?sign=' . $mall['mall_sign'];
- $h5_url = $this->getH5($param['mall_id']) . $page_path;
-
- $send_data = [
- 'amount2' => $param['money'],
- 'time3' => date('Y-m-d H:i', time()),
- ];
- $temp_key = WechatTemplate::ORDER_COMMISSION_SETTLE;
- $wechat->sendTemplate($param['user_id'], $temp_key, $h5_url, $send_data, $page_path);
- } catch (\Exception $e) {
- Yii::$app->custom->logs('佣金到账通知消息推送:' . $e->getMessage(), $param);
- return true;
- }
- }
- public function orderRefundAgree($param) {
- // try {
- // $wechat = new WechatTemplate($param['mall_id']);
- // $mall = Mall::findOne(['id'=>$param['mall_id']]);
- // if(empty($mall)) return ;
- // $page_path = WechatTemplate::getPagePath(WechatTemplate::ORDER_REFUND_AGREE);
- // $page_path =$page_path."?id=" . $param['refund_id'].'&sign='.$mall['mall_sign'];
- // $h5_url = $this->getH5($param['mall_id']).$page_path;
- // $send_data = [
- // 'first' => '您好,您的退货申请已通过',
- // 'keyword1' => $param['order_no'],
- // 'keyword2' => $param['receiver_name'],
- // 'keyword3' => $param['mobile'],
- // 'keyword4' => $param['address'],
- // 'remark' => '感谢您的使用'
- // ];
- // $temp_key = WechatTemplate::ORDER_REFUND_AGREE;
- // $wechat->sendTemplate($param['user_id'], $temp_key, $h5_url, $send_data, $page_path);
- // }catch (\Exception $e){
- // Yii::error('OrderCreateListener:sendWechatTemplate:'.$e->getMessage());
- // }
- }
- /**
- * 订单支付成功通知
- * @param $param ['mall_id'=>'mall_id','user_id'=>'user_id','order_id'=>'order_id','order_no'=>'order_no','result'=>'result','address'=>'handle_time',]
- * @return false|void
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function orderRefund($param) {
- try {
- $wechat = new WechatTemplate($param['mall_id']);
- $mall = Mall::findOne(['id'=>$param['mall_id']]);
- if(empty($mall)) return ;
- if (empty($param['page_path'])) {
- $page_path = WechatTemplate::getPagePath(WechatTemplate::ORDER_CREATE);
- $page_path =$page_path."?id=" . $param['order_id'].'&sign='.$mall['mall_sign'];
- $h5_url = $this->getH5($param['mall_id']).$page_path;
- } else {
- $h5_url = $this->getH5($param['mall_id']) . $param['page_path'];
- }
- $send_data = [
- 'time3' => $param['handle_time'],
- 'const5' => $param['result'],
- 'character_string1' => $param['order_no'],
- ];
- $temp_key = WechatTemplate::ORDER_REFUND_RESULT;
- $res = $wechat->sendTemplate($param['user_id'], $temp_key, $h5_url, $send_data, $page_path, $param['template_id']);
- return true;
- }catch (\Exception $e){
- // var_dump($e->getMessage());die();
- Yii::error('orderRefund:sendWechatTemplate:'.$e->getMessage());
- }
- }
- public function storeOrderPay($param)
- {
- try {
- $wechat = new WechatTemplate($param['mall_id']);
- $mall = Mall::findOne(['id' => $param['mall_id']]);
- if (empty($mall)) return;
- if (empty($param['page_path'])) {
- $page_path = WechatTemplate::getPagePath(WechatTemplate::ORDER_CREATE);
- $page_path = $page_path . "?id=" . $param['order_id'] . '&sign=' . $mall['mall_sign'];
- $h5_url = $this->getH5($param['mall_id']) . $page_path;
- } else {
- $h5_url = $this->getH5($param['mall_id']) . $param['page_path'];
- }
- $send_data = [
- 'amount2' => $param['pay_money'],
- 'time3' => $param['pay_time_text'],
- 'character_string1' => $param['order_no'],
- ];
- $temp_key = WechatTemplate::STORE_ORDER_PAY_SUCCESS;
- $wechat->sendTemplate($param['user_id'], $temp_key, $h5_url, $send_data, $page_path, $param['template_id']);
- } catch (\Exception $e) {
- Yii::error('OrderCreateListener:sendWechatTemplate:' . $e->getMessage());
- }
- }
- }
|