WechatMessageLogic.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. namespace common\logic\wechat;
  3. use addons\Store\common\enums\StoreEnum;
  4. use common\helpers\wechat\WechatTemplate;
  5. use common\models\mall\Mall;
  6. use common\models\user\User;
  7. use common\models\user\UserPartitionRelationship;
  8. use Yii;
  9. use yii\db\Exception;
  10. class WechatMessageLogic
  11. {
  12. private function getH5($mall_id){
  13. $h5 = \Yii::$app->services->setting->mall->get($mall_id, 'basic.web_url');
  14. if (empty($h5)) throw new Exception('h5域名没有配置');
  15. return substr($h5, 0, strpos($h5, '?'));
  16. }
  17. /**
  18. * 订单生成通知
  19. * @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']
  20. * @return false|void
  21. * @throws \GuzzleHttp\Exception\GuzzleException
  22. */
  23. public function sendWechatTemplate($param) {
  24. try {
  25. $wechat = new WechatTemplate($param['mall_id']);
  26. $mall = Mall::findOne(['id'=>$param['mall_id']]);
  27. if(empty($mall)) return ;
  28. $page_path = WechatTemplate::getPagePath(WechatTemplate::ORDER_CREATE);
  29. $page_path =$page_path."?id=" . $param['order_id'].'&sign='.$mall['mall_sign'];
  30. $h5_url = $this->getH5($param['mall_id']).$page_path;
  31. $address_arr = explode(' ',$param['address']);
  32. $address_str = '';
  33. foreach ($address_arr as $v){
  34. $address_str.=$v;
  35. }
  36. $send_data = [
  37. 'character_string6'=>$param['order_no'],
  38. 'thing1'=>$param['goods_name'],
  39. 'time8'=>date("Y-m-d H:i:s", $param['created_at']),
  40. 'thing2'=>$param['receiver_name'],
  41. 'thing9'=>$address_str,
  42. ];
  43. $temp_key = WechatTemplate::ORDER_CREATE;
  44. $wechat->sendTemplate($param['user_id'], $temp_key, $h5_url, $send_data, $page_path);
  45. }catch (\Exception $e){
  46. Yii::error('OrderCreateListener:sendWechatTemplate:'.$e->getMessage());
  47. }
  48. }
  49. /**
  50. * 佣金到账发送模板消息
  51. * @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'],];
  52. * @return false|void
  53. * @throws \GuzzleHttp\Exception\GuzzleException
  54. */
  55. public function receiptSendMessage($send_message_data){
  56. try {
  57. $mall_id = $send_message_data['mall_id'];
  58. $user_id = $send_message_data['user_id'];
  59. if($send_message_data['change_type']!='add'||$send_message_data['wallet_type']!='commission') return true;
  60. $wechat = new WechatTemplate($mall_id);
  61. $mall = Mall::findOne(['id'=>$mall_id]);
  62. if(empty($mall)) return true;
  63. $page_path =WechatTemplate::getPagePath(WechatTemplate::COMMISSION_RECEIPT).'?sign='.$mall['mall_sign'];
  64. $h5_url = $this->getH5($mall_id).$page_path;
  65. $send_data = [];
  66. $temp_key = WechatTemplate::COMMISSION_RECEIPT;
  67. $wechat->sendTemplate($user_id, $temp_key, $h5_url, $send_data, $page_path);
  68. }catch (\Exception $e){
  69. Yii::error('UserWallet:receiptSendMessage:'.$e->getMessage());
  70. }
  71. }
  72. /**
  73. * 团队人员增加
  74. * @param $send_message_data ['user_id'=>$user_id, 'mall_id' => $data['mall_id']],];
  75. * @return false|void
  76. * @throws \GuzzleHttp\Exception\GuzzleException
  77. */
  78. public function teamAddSendMessage($send_message_data){
  79. try {
  80. $mall_id = $send_message_data['mall_id'];
  81. $user_id = $send_message_data['user_id'];
  82. $parent_id_arr = UserPartitionRelationship::getParentIdArr($user_id);
  83. if(empty($parent_id_arr))return false;
  84. $parent_id_arr = array_reverse($parent_id_arr);
  85. $mall = Mall::findOne(['id'=>$mall_id]);
  86. if(empty($mall)) return false;
  87. $page_path =WechatTemplate::getPagePath(WechatTemplate::TEAM_ADD).'?sign='.$mall['mall_sign'];
  88. $h5_url = $this->getH5($mall_id).$page_path;
  89. $user = \common\models\user\User::findOne(['id'=>$user_id]);
  90. if(empty($user)) return false;
  91. foreach ($parent_id_arr as $k=>$parent_id){
  92. $mobile = substr_replace($user['mobile'], '****', 3, 4);
  93. if($k==0){
  94. $wechat = new WechatTemplate($mall_id);
  95. $send_data = [
  96. 'thing2' => $user['nickname'],
  97. 'phone_number1' => $mobile,
  98. 'time3' => date('Y-m-d H:i:s',$user['created_at']),
  99. ];
  100. $temp_key = WechatTemplate::INVITATION_TO_REGISTER_SUCCESS;
  101. $wechat->sendTemplate($parent_id, $temp_key, $h5_url, $send_data, $page_path);
  102. continue;
  103. }
  104. $wechat = new WechatTemplate($mall_id);
  105. $send_data = [
  106. 'thing2' => $user['nickname'],
  107. 'phone_number1' => $mobile,
  108. 'time3' => date('Y-m-d H:i:s',$user['created_at']),
  109. ];
  110. $temp_key = WechatTemplate::TEAM_ADD;
  111. $wechat->sendTemplate($parent_id, $temp_key, $h5_url, $send_data, $page_path);
  112. }
  113. }catch (\Exception $e){
  114. Yii::error('User:teamAddSendMessage:'.$e->getMessage());
  115. }
  116. }
  117. /**
  118. * 提现成功
  119. * @param $send_message_data ['user_id'=>$user_id, 'money'=>$data['money'],'mall_id' => $data['mall_id']],];
  120. * @return false|void
  121. * @throws \GuzzleHttp\Exception\GuzzleException
  122. */
  123. public function withdrawalSuccessSendMessage($send_message_data){
  124. try {
  125. $mall_id = $send_message_data['mall_id'];
  126. $user_id = $send_message_data['user_id'];
  127. $wechat = new WechatTemplate($mall_id);
  128. $mall = Mall::findOne(['id'=>$mall_id]);
  129. if(empty($mall)) return false;
  130. $page_path =WechatTemplate::getPagePath(WechatTemplate::WITHDRAWAL_SUCCESS).'&sign='.$mall['mall_sign'];
  131. $h5_url = $this->getH5($mall_id).$page_path;
  132. $send_data = [
  133. 'amount1' => $send_message_data['money'],
  134. 'time3' => date("Y-m-d H:i:s", time()),
  135. ];
  136. $temp_key = WechatTemplate::WITHDRAWAL_SUCCESS;
  137. //var_dump($user_id, $temp_key, $h5_url, $send_data, $page_path);
  138. $wechat->sendTemplate($user_id, $temp_key, $h5_url, $send_data, $page_path);
  139. }catch (\Exception $e){
  140. Yii::error('User:withdrawalSuccessSendMessage:'.$e->getMessage());
  141. }
  142. }
  143. /**
  144. * 订单取消通知
  145. * @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']
  146. * @return false|void
  147. * @throws \GuzzleHttp\Exception\GuzzleException
  148. */
  149. public function orderCancel($param) {
  150. try {
  151. $wechat = new WechatTemplate($param['mall_id']);
  152. $mall = Mall::findOne(['id'=>$param['mall_id']]);
  153. if(empty($mall)) return ;
  154. $page_path = WechatTemplate::getPagePath(WechatTemplate::ORDER_CREATE);
  155. $page_path =$page_path."?id=" . $param['order_id'].'&sign='.$mall['mall_sign'];
  156. $h5_url = $this->getH5($param['mall_id']).$page_path;
  157. $send_data = [
  158. 'character_string1' => $param['order_no'],
  159. 'phrase2' => '已取消',
  160. 'phrase3' => $param['phrase3'],
  161. ];
  162. $temp_key = WechatTemplate::ORDER_CANCEL;
  163. $wechat->sendTemplate($param['user_id'], $temp_key, $h5_url, $send_data, $page_path);
  164. }catch (\Exception $e){
  165. Yii::error('OrderCreateListener:sendWechatTemplate:'.$e->getMessage());
  166. }
  167. }
  168. /**
  169. * 订单发货通知
  170. * @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',]
  171. * @return false|void
  172. * @throws \GuzzleHttp\Exception\GuzzleException
  173. */
  174. public function orderSent($param) {
  175. try {
  176. $wechat = new WechatTemplate($param['mall_id']);
  177. $mall = Mall::findOne(['id'=>$param['mall_id']]);
  178. if(empty($mall)) return ;
  179. $page_path = WechatTemplate::getPagePath(WechatTemplate::ORDER_CREATE);
  180. $page_path =$page_path."?id=" . $param['order_id'].'&sign='.$mall['mall_sign'];
  181. $h5_url = $this->getH5($param['mall_id']).$page_path;
  182. $address_arr = explode(' ',$param['receiver_msg']);
  183. $address_str = '';
  184. foreach ($address_arr as $v){
  185. $address_str.=$v;
  186. }
  187. $send_data = [
  188. 'thing12' => $param['goods_name'],
  189. 'character_string1' => $param['order_no'],
  190. 'thing11' => $param['express_name'],
  191. 'character_string2' => $param['express_no'],
  192. 'thing5' => $address_str,
  193. ];
  194. $temp_key = WechatTemplate::ORDER_SENT;
  195. $wechat->sendTemplate($param['user_id'], $temp_key, $h5_url, $send_data, $page_path);
  196. }catch (\Exception $e){
  197. Yii::error('OrderCreateListener:sendWechatTemplate:'.$e->getMessage());
  198. }
  199. }
  200. /**
  201. * 订单确认收货通知
  202. * @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',]
  203. * @return false|void
  204. * @throws \GuzzleHttp\Exception\GuzzleException
  205. */
  206. public function orderReceipt($param) {
  207. try {
  208. $wechat = new WechatTemplate($param['mall_id']);
  209. $mall = Mall::findOne(['id'=>$param['mall_id']]);
  210. if(empty($mall)) return ;
  211. $page_path = WechatTemplate::getPagePath(WechatTemplate::ORDER_CREATE);
  212. $page_path =$page_path."?id=" . $param['order_id'].'&sign='.$mall['mall_sign'];
  213. $h5_url = $this->getH5($param['mall_id']).$page_path;
  214. $send_data = [
  215. 'character_string7' => $param['order_no'],
  216. 'thing6' => $param['goods_name'],
  217. 'time4' => date('Y-m-d H:i:s',$param['sign_time']),
  218. ];
  219. $temp_key = WechatTemplate::ORDER_CONFIRMED;
  220. $wechat->sendTemplate($param['user_id'], $temp_key, $h5_url, $send_data, $page_path);
  221. }catch (\Exception $e){
  222. Yii::error('OrderCreateListener:sendWechatTemplate:'.$e->getMessage());
  223. }
  224. }
  225. /**
  226. * 订单支付成功通知
  227. * @param $param ['mall_id'=>'mall_id','user_id'=>'user_id','order_no'=>'order_no','receiver_name'=>'receiver_name','mobile'=>'mobile','address'=>'address',]
  228. * @return false|void
  229. * @throws \GuzzleHttp\Exception\GuzzleException
  230. */
  231. public function orderPay($param) {
  232. try {
  233. $wechat = new WechatTemplate($param['mall_id']);
  234. $mall = Mall::findOne(['id'=>$param['mall_id']]);
  235. if(empty($mall)) return ;
  236. if (empty($param['page_path'])) {
  237. $page_path = WechatTemplate::getPagePath(WechatTemplate::ORDER_CREATE);
  238. $page_path =$page_path."?id=" . $param['order_id'].'&sign='.$mall['mall_sign'];
  239. $h5_url = $this->getH5($param['mall_id']).$page_path;
  240. } else {
  241. $h5_url = $this->getH5($param['mall_id']) . $param['page_path'];
  242. }
  243. $address_arr = explode(' ',$param['address']);
  244. $address_str = '';
  245. foreach ($address_arr as $v){
  246. $address_str.=$v;
  247. }
  248. $send_data = [
  249. 'amount3' => $param['pay_money'],
  250. 'thing1' => $param['goods_name'],
  251. 'thing4' => $address_str,
  252. 'character_string2' => $param['order_no'],
  253. ];
  254. $temp_key = WechatTemplate::ORDER_PAY_SUCCESS;
  255. $wechat->sendTemplate($param['user_id'], $temp_key, $h5_url, $send_data, $page_path);
  256. $this->friendPaySuccess($param);
  257. }catch (\Exception $e){
  258. Yii::error('OrderCreateListener:sendWechatTemplate:'.$e->getMessage());
  259. }
  260. }
  261. /**
  262. * 好友购买成功通知
  263. * @Author:lun
  264. * @Date:2024-04-28 18:04
  265. * @Copyright:copyright(c)2024 广东七件事集团
  266. * @param $param
  267. * @return true
  268. */
  269. public function friendPaySuccess($param)
  270. {
  271. try {
  272. $user = User::find()->select('parent_id,nickname,mobile')->where(['id' => $param['user_id']])->asArray()->one();
  273. if (empty($user['parent_id'])) return true;
  274. $mall = Mall::findOne(['id'=>$param['mall_id']]);
  275. if(empty($mall)) return true;
  276. $page_path = WechatTemplate::getPagePath(WechatTemplate::DEFAULT_PAGE);
  277. $page_path = $page_path . '?sign=' . $mall['mall_sign'];
  278. $h5_url = $this->getH5($param['mall_id']) . $page_path;
  279. // 参数组合
  280. $send_data = [
  281. 'character_string8' => $param['order_no'],
  282. 'thing1' => $user['nickname'] == '微信用户' ? $user['mobile'] : $user['nickname'],
  283. 'time2' => date('Y-m-d', $param['pay_time']),
  284. 'time10' => date('H:i:s', $param['pay_time']),
  285. ];
  286. $wechat = new WechatTemplate($param['mall_id']);
  287. $wechat->sendTemplate($user['parent_id'], WechatTemplate::RECOMMEND_PAY_SUCCESS, $h5_url, $send_data, $page_path);
  288. return true;
  289. } catch (\Exception $e) {
  290. Yii::$app->custom->logs('好友付款成功消息推送:' . $e->getMessage(), $param);
  291. return true;
  292. }
  293. }
  294. public function OrderCommissionSettle($param)
  295. {
  296. try {
  297. $wechat = new WechatTemplate($param['mall_id']);
  298. $mall = Mall::findOne(['id'=>$param['mall_id']]);
  299. if(empty($mall)) return ;
  300. $page_path = WechatTemplate::getPagePath(WechatTemplate::ORDER_COMMISSION_SETTLE);
  301. $page_path = $page_path . '?sign=' . $mall['mall_sign'];
  302. $h5_url = $this->getH5($param['mall_id']) . $page_path;
  303. $send_data = [
  304. 'amount2' => $param['money'],
  305. 'time3' => date('Y-m-d H:i', time()),
  306. ];
  307. $temp_key = WechatTemplate::ORDER_COMMISSION_SETTLE;
  308. $wechat->sendTemplate($param['user_id'], $temp_key, $h5_url, $send_data, $page_path);
  309. } catch (\Exception $e) {
  310. Yii::$app->custom->logs('佣金到账通知消息推送:' . $e->getMessage(), $param);
  311. return true;
  312. }
  313. }
  314. public function orderRefundAgree($param) {
  315. // try {
  316. // $wechat = new WechatTemplate($param['mall_id']);
  317. // $mall = Mall::findOne(['id'=>$param['mall_id']]);
  318. // if(empty($mall)) return ;
  319. // $page_path = WechatTemplate::getPagePath(WechatTemplate::ORDER_REFUND_AGREE);
  320. // $page_path =$page_path."?id=" . $param['refund_id'].'&sign='.$mall['mall_sign'];
  321. // $h5_url = $this->getH5($param['mall_id']).$page_path;
  322. // $send_data = [
  323. // 'first' => '您好,您的退货申请已通过',
  324. // 'keyword1' => $param['order_no'],
  325. // 'keyword2' => $param['receiver_name'],
  326. // 'keyword3' => $param['mobile'],
  327. // 'keyword4' => $param['address'],
  328. // 'remark' => '感谢您的使用'
  329. // ];
  330. // $temp_key = WechatTemplate::ORDER_REFUND_AGREE;
  331. // $wechat->sendTemplate($param['user_id'], $temp_key, $h5_url, $send_data, $page_path);
  332. // }catch (\Exception $e){
  333. // Yii::error('OrderCreateListener:sendWechatTemplate:'.$e->getMessage());
  334. // }
  335. }
  336. /**
  337. * 订单支付成功通知
  338. * @param $param ['mall_id'=>'mall_id','user_id'=>'user_id','order_id'=>'order_id','order_no'=>'order_no','result'=>'result','address'=>'handle_time',]
  339. * @return false|void
  340. * @throws \GuzzleHttp\Exception\GuzzleException
  341. */
  342. public function orderRefund($param) {
  343. try {
  344. $wechat = new WechatTemplate($param['mall_id']);
  345. $mall = Mall::findOne(['id'=>$param['mall_id']]);
  346. if(empty($mall)) return ;
  347. if (empty($param['page_path'])) {
  348. $page_path = WechatTemplate::getPagePath(WechatTemplate::ORDER_CREATE);
  349. $page_path =$page_path."?id=" . $param['order_id'].'&sign='.$mall['mall_sign'];
  350. $h5_url = $this->getH5($param['mall_id']).$page_path;
  351. } else {
  352. $h5_url = $this->getH5($param['mall_id']) . $param['page_path'];
  353. }
  354. $send_data = [
  355. 'time3' => $param['handle_time'],
  356. 'const5' => $param['result'],
  357. 'character_string1' => $param['order_no'],
  358. ];
  359. $temp_key = WechatTemplate::ORDER_REFUND_RESULT;
  360. $res = $wechat->sendTemplate($param['user_id'], $temp_key, $h5_url, $send_data, $page_path, $param['template_id']);
  361. return true;
  362. }catch (\Exception $e){
  363. // var_dump($e->getMessage());die();
  364. Yii::error('orderRefund:sendWechatTemplate:'.$e->getMessage());
  365. }
  366. }
  367. public function storeOrderPay($param)
  368. {
  369. try {
  370. $wechat = new WechatTemplate($param['mall_id']);
  371. $mall = Mall::findOne(['id' => $param['mall_id']]);
  372. if (empty($mall)) return;
  373. if (empty($param['page_path'])) {
  374. $page_path = WechatTemplate::getPagePath(WechatTemplate::ORDER_CREATE);
  375. $page_path = $page_path . "?id=" . $param['order_id'] . '&sign=' . $mall['mall_sign'];
  376. $h5_url = $this->getH5($param['mall_id']) . $page_path;
  377. } else {
  378. $h5_url = $this->getH5($param['mall_id']) . $param['page_path'];
  379. }
  380. $send_data = [
  381. 'amount2' => $param['pay_money'],
  382. 'time3' => $param['pay_time_text'],
  383. 'character_string1' => $param['order_no'],
  384. ];
  385. $temp_key = WechatTemplate::STORE_ORDER_PAY_SUCCESS;
  386. $wechat->sendTemplate($param['user_id'], $temp_key, $h5_url, $send_data, $page_path, $param['template_id']);
  387. } catch (\Exception $e) {
  388. Yii::error('OrderCreateListener:sendWechatTemplate:' . $e->getMessage());
  389. }
  390. }
  391. }