MessageOne.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\common\components;
  3. use app\common\components\SiteUrl;
  4. use app\modules\ucenter\models\Message;
  5. use app\modules\ucenter\models\User;
  6. use app\modules\ucenter\models\UserOpenAuth;
  7. use app\modules\doc\models\DocPaylog;
  8. use app\modules\plugins\models\MpMsgTpl;
  9. use app\modules\plugins\models\MpMsg;
  10. use app\components\WeiXin\WeiXin;
  11. use app\modules\ucenter\models\UserVipType;
  12. use Yii;
  13. class MessageOne
  14. {
  15. //文档出售通知
  16. public static function soldDoc($title,$doc,$left_coin)
  17. {
  18. if($doc->user_id)
  19. {
  20. $url = $doc->doc_type==2?SiteUrl::colDetail($doc->id):SiteUrl::docDetail($doc->id);
  21. $msg = $title."<a href='".$url."' target='_blank'>《".$doc->title."》</a>";
  22. self::sentMsg($doc->user_id,$title,$msg);
  23. //微信模板消息
  24. if(Yii::$app->params['mp']['openmsg'])
  25. {
  26. $args = [
  27. 'title'=>$doc->title,
  28. 'datetime'=>get_date(time()),
  29. 'sellPrice'=>number_format($doc->coin_price,2).Yii::$app->params['coin']['coin_name'],
  30. 'incomePrice'=>number_format($left_coin,2).Yii::$app->params['coin']['coin_name']
  31. ];
  32. self::sentWxMsg('sold_doc',$doc->user_id,$args,$url);
  33. }
  34. }
  35. }
  36. //文档分销通知
  37. public static function commissionDoc($referer_id,$doc,$coin_num,$commission)
  38. {
  39. if($referer_id)
  40. {
  41. $url = $doc->doc_type==2?SiteUrl::colDetail($doc->id):SiteUrl::docDetail($doc->id);
  42. //微信模板消息
  43. if(Yii::$app->params['mp']['openmsg'])
  44. {
  45. $args = [
  46. 'title'=>$doc->title,
  47. 'datetime'=>get_date(time()),
  48. 'sellPrice'=>number_format($coin_num,2).Yii::$app->params['coin']['coin_name'],
  49. 'incomePrice'=>number_format($commission,2).Yii::$app->params['coin']['coin_name']
  50. ];
  51. self::sentWxMsg('commission_success',$referer_id,$args,$url);
  52. }
  53. }
  54. }
  55. //VIP分销通知
  56. public static function commissionVip($referer_id,$userVipOrder,$commission)
  57. {
  58. if($referer_id)
  59. {
  60. $url = SiteUrl::vip();
  61. $userVipType = UserVipType::findOne($userVipOrder->vip_type);
  62. //微信模板消息
  63. if(Yii::$app->params['mp']['openmsg'])
  64. {
  65. $args = [
  66. 'title'=>$userVipType->title,
  67. 'datetime'=>get_date(time()),
  68. 'sellPrice'=>number_format($userVipOrder->money,2).'元',
  69. 'incomePrice'=>number_format($commission,2).'元'
  70. ];
  71. self::sentWxMsg('commission_success',$referer_id,$args,$url);
  72. }
  73. }
  74. }
  75. //发送消息
  76. public static function sentMsg($to_user,$subject,$content,$sent_user=0,$message_type=0,$to_group=0)
  77. {
  78. $msg = new Message();
  79. $msg->to_user = $to_user;
  80. $msg->subject = $subject;
  81. $msg->content = $content;
  82. $msg->sent_user = $sent_user;
  83. $msg->message_type = $message_type;
  84. $msg->to_group = $to_group;
  85. $msg->sent_time = TIMESTAMP;
  86. $msg->save();
  87. return true;
  88. }
  89. //发送微信消息
  90. public static function sentWxMsg($key,$user_id,$args,$url)
  91. {
  92. $openAuth = UserOpenAuth::find()->where("user_id=".$user_id." and app='mpmsg'")->one();
  93. $openid = $openAuth->app_uid;
  94. $tpl = MpMsgTpl::find()->alias('a')->where("a.key='".$key."'")->one();
  95. if($openid&&$tpl)
  96. {
  97. $tplSettings = string2array($tpl->settings);
  98. foreach($tplSettings as $tplSetting)
  99. {
  100. foreach($args as $k=>$v)
  101. {
  102. $ks[] = '${'.$k.'}';
  103. $vs[] = $v;
  104. }
  105. $msgdata[$tplSetting['key']] = ['value'=>str_replace($ks,$vs,$tplSetting['value']),'color'=>$tplSetting['color']];
  106. }
  107. $wxargs = array(
  108. 'appId'=>Yii::$app->params['mp']['mpAppId'],
  109. 'appSecret'=>Yii::$app->params['mp']['mpAppSecret'],
  110. 'token'=>Yii::$app->params['mp']['mpToken'],
  111. );
  112. $weixin = new WeiXin($wxargs);
  113. $weixin->init();
  114. $result = $weixin->sendTemplateMsg($openid,$tpl->tpl_no,$msgdata,$url);
  115. //写发送记录
  116. $mpmsg = new MpMsg();
  117. $mpmsg->subject = $key;
  118. $mpmsg->user_id = $user_id;
  119. $mpmsg->openid = $openid;
  120. $mpmsg->content = array2string($msgdata);
  121. $mpmsg->sent_time = TIMESTAMP;
  122. $mpmsg->save();
  123. }
  124. }
  125. //未读消息数
  126. public static function msgNum($to_user)
  127. {
  128. $num = Message::find()->where("to_user=$to_user and have_read=0 and to_delete=0")->count();
  129. return $num;
  130. }
  131. }