Withdraw.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace app\common\service\message\dealer;
  3. use app\common\service\message\Basics;
  4. use app\common\model\Setting as SettingModel;
  5. use app\common\enum\dealer\withdraw\ApplyStatus as ApplyStatusEnum;
  6. /**
  7. * 消息通知服务 [分销商提现]
  8. * Class Withdraw
  9. * @package app\common\service\message\dealer
  10. */
  11. class Withdraw extends Basics
  12. {
  13. /**
  14. * 参数列表
  15. * @var array
  16. */
  17. protected $param = [
  18. 'withdraw' => [], // 提现记录
  19. ];
  20. /**
  21. * 发送消息通知
  22. * @param array $param
  23. * @return mixed|void
  24. * @throws \think\Exception
  25. */
  26. public function send($param)
  27. {
  28. // 记录参数
  29. $this->param = $param;
  30. // 微信订阅消息通知用户
  31. $this->onSendWxSubMsg();
  32. }
  33. /**
  34. * 微信订阅消息通知用户
  35. * @return bool|mixed
  36. * @throws \app\common\exception\BaseException
  37. * @throws \think\Exception
  38. * @throws \think\exception\DbException
  39. */
  40. private function onSendWxSubMsg()
  41. {
  42. $withdrawInfo = $this->param['withdraw'];
  43. $userInfo = $this->param['user'];
  44. $wxappId = $withdrawInfo['wxapp_id'];
  45. // 根据提现状态获取对应的消息模板
  46. $template = $this->getTemplateByStatus($withdrawInfo);
  47. if ($template === false) {
  48. return false;
  49. }
  50. // 发送订阅消息
  51. return $this->sendWxSubMsg($wxappId, [
  52. 'touser' => $userInfo['open_id'],
  53. 'template_id' => $template['template_id'],
  54. 'page' => 'pages/dealer/index/index',
  55. 'data' => $this->getTemplateData($withdrawInfo, $template)
  56. ]);
  57. }
  58. /**
  59. * 生成消息内容
  60. * @param $withdrawInfo
  61. * @param $template
  62. * @return array
  63. */
  64. private function getTemplateData($withdrawInfo, $template)
  65. {
  66. if ($withdrawInfo['apply_status'] == ApplyStatusEnum::AUDIT_PASS) {
  67. return [
  68. // 提现金额
  69. $template['keywords'][0] => ['value' => $withdrawInfo['money']],
  70. // 打款方式
  71. $template['keywords'][1] => ['value' => $withdrawInfo['pay_type']['text']],
  72. // 打款原因
  73. $template['keywords'][2] => ['value' => '分销商提现'],
  74. ];
  75. }
  76. if ($withdrawInfo['apply_status'] == ApplyStatusEnum::AUDIT_REJECT) {
  77. return [
  78. // 提现金额
  79. $template['keywords'][0] => ['value' => $withdrawInfo['money']],
  80. // 申请时间
  81. $template['keywords'][1] => ['value' => $withdrawInfo['create_time']],
  82. // 原因
  83. $template['keywords'][2] => ['value' => $this->getSubstr($withdrawInfo['reject_reason'])],
  84. ];
  85. }
  86. return [];
  87. }
  88. /**
  89. * 根据提现状态获取对应的消息模板
  90. * @param $withdrawInfo
  91. * @return bool
  92. */
  93. private function getTemplateByStatus($withdrawInfo)
  94. {
  95. $wxappId = $withdrawInfo['wxapp_id'];
  96. // 获取订阅消息配置
  97. $templateGroup = SettingModel::getItem('submsg', $wxappId)['dealer'];
  98. if (
  99. $withdrawInfo['apply_status'] == ApplyStatusEnum::AUDIT_PASS
  100. && !empty($templateGroup['withdraw_01']['template_id'])
  101. ) {
  102. return $templateGroup['withdraw_01'];
  103. }
  104. if (
  105. $withdrawInfo['apply_status'] == ApplyStatusEnum::AUDIT_REJECT
  106. && !empty($templateGroup['withdraw_02']['template_id'])
  107. ) {
  108. return $templateGroup['withdraw_02'];
  109. }
  110. return false;
  111. }
  112. }