123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- namespace app\common\components;
- use app\common\components\SiteUrl;
- use app\modules\ucenter\models\Message;
- use app\modules\ucenter\models\User;
- use app\modules\ucenter\models\UserOpenAuth;
- use app\modules\doc\models\DocPaylog;
- use app\modules\plugins\models\MpMsgTpl;
- use app\modules\plugins\models\MpMsg;
- use app\components\WeiXin\WeiXin;
- use app\modules\ucenter\models\UserVipType;
- use Yii;
- class MessageOne
- {
- //文档出售通知
- public static function soldDoc($title,$doc,$left_coin)
- {
- if($doc->user_id)
- {
- $url = $doc->doc_type==2?SiteUrl::colDetail($doc->id):SiteUrl::docDetail($doc->id);
- $msg = $title."<a href='".$url."' target='_blank'>《".$doc->title."》</a>";
- self::sentMsg($doc->user_id,$title,$msg);
- //微信模板消息
- if(Yii::$app->params['mp']['openmsg'])
- {
- $args = [
- 'title'=>$doc->title,
- 'datetime'=>get_date(time()),
- 'sellPrice'=>number_format($doc->coin_price,2).Yii::$app->params['coin']['coin_name'],
- 'incomePrice'=>number_format($left_coin,2).Yii::$app->params['coin']['coin_name']
- ];
- self::sentWxMsg('sold_doc',$doc->user_id,$args,$url);
- }
- }
- }
- //文档分销通知
- public static function commissionDoc($referer_id,$doc,$coin_num,$commission)
- {
- if($referer_id)
- {
- $url = $doc->doc_type==2?SiteUrl::colDetail($doc->id):SiteUrl::docDetail($doc->id);
- //微信模板消息
- if(Yii::$app->params['mp']['openmsg'])
- {
- $args = [
- 'title'=>$doc->title,
- 'datetime'=>get_date(time()),
- 'sellPrice'=>number_format($coin_num,2).Yii::$app->params['coin']['coin_name'],
- 'incomePrice'=>number_format($commission,2).Yii::$app->params['coin']['coin_name']
- ];
- self::sentWxMsg('commission_success',$referer_id,$args,$url);
- }
- }
- }
- //VIP分销通知
- public static function commissionVip($referer_id,$userVipOrder,$commission)
- {
- if($referer_id)
- {
- $url = SiteUrl::vip();
- $userVipType = UserVipType::findOne($userVipOrder->vip_type);
- //微信模板消息
- if(Yii::$app->params['mp']['openmsg'])
- {
- $args = [
- 'title'=>$userVipType->title,
- 'datetime'=>get_date(time()),
- 'sellPrice'=>number_format($userVipOrder->money,2).'元',
- 'incomePrice'=>number_format($commission,2).'元'
- ];
- self::sentWxMsg('commission_success',$referer_id,$args,$url);
- }
- }
- }
- //发送消息
- public static function sentMsg($to_user,$subject,$content,$sent_user=0,$message_type=0,$to_group=0)
- {
- $msg = new Message();
- $msg->to_user = $to_user;
- $msg->subject = $subject;
- $msg->content = $content;
- $msg->sent_user = $sent_user;
- $msg->message_type = $message_type;
- $msg->to_group = $to_group;
- $msg->sent_time = TIMESTAMP;
- $msg->save();
- return true;
- }
- //发送微信消息
- public static function sentWxMsg($key,$user_id,$args,$url)
- {
- $openAuth = UserOpenAuth::find()->where("user_id=".$user_id." and app='mpmsg'")->one();
- $openid = $openAuth->app_uid;
- $tpl = MpMsgTpl::find()->alias('a')->where("a.key='".$key."'")->one();
- if($openid&&$tpl)
- {
- $tplSettings = string2array($tpl->settings);
- foreach($tplSettings as $tplSetting)
- {
- foreach($args as $k=>$v)
- {
- $ks[] = '${'.$k.'}';
- $vs[] = $v;
- }
- $msgdata[$tplSetting['key']] = ['value'=>str_replace($ks,$vs,$tplSetting['value']),'color'=>$tplSetting['color']];
- }
- $wxargs = array(
- 'appId'=>Yii::$app->params['mp']['mpAppId'],
- 'appSecret'=>Yii::$app->params['mp']['mpAppSecret'],
- 'token'=>Yii::$app->params['mp']['mpToken'],
- );
- $weixin = new WeiXin($wxargs);
- $weixin->init();
- $result = $weixin->sendTemplateMsg($openid,$tpl->tpl_no,$msgdata,$url);
- //写发送记录
- $mpmsg = new MpMsg();
- $mpmsg->subject = $key;
- $mpmsg->user_id = $user_id;
- $mpmsg->openid = $openid;
- $mpmsg->content = array2string($msgdata);
- $mpmsg->sent_time = TIMESTAMP;
- $mpmsg->save();
- }
- }
- //未读消息数
- public static function msgNum($to_user)
- {
- $num = Message::find()->where("to_user=$to_user and have_read=0 and to_delete=0")->count();
- return $num;
- }
- }
|