'ID', 'cx_mch_id' => '平台商户ID', 'sys_msg_id' => '系统消息ID', 'user_id' => '用户ID', 'is_read' => '是否已读:0=否,1=是', 'read_time' => '阅读时间', 'is_sound' => '是否提醒:0=否,1=是', 'sound_time' => '提醒时间', 'created_at' => '添加时间', 'updated_at' => '更新时间', 'is_delete' => '是否删除:0=否,1=是', 'deleted_at' => '删除时间', ]; } public function beforeSave($insert) { if(parent::beforeSave($insert)){ if($this->isNewRecord){ $this->created_at = time(); } $this->updated_at = time(); if($this->is_delete == 1) $this->deleted_at = time(); return true; } else { return false; } } /** * 阅读消息 */ public static function handleRead($sys_msg_id, $user_id, $cx_mch_id = 0) { $model = UserSysMsg::findOne(['cx_mch_id' => $cx_mch_id, 'user_id' => $user_id, 'sys_msg_id' => $sys_msg_id, 'is_delete' => 0]); if($model != null && $model->is_read == 1){ return Model::asReturnError('消息已读'); } $sys_msg = SysMsg::findOne(['id' => $sys_msg_id, 'cx_mch_id' => $cx_mch_id, 'is_delete' => 0, 'status' => SysMsg::STATUS_PUBLISHED]); if($sys_msg == null){ return Model::asReturnError('系统消息不存在'); } if($model == null){ $model = new UserSysMsg(); $model->cx_mch_id = $cx_mch_id; $model->user_id = $user_id; $model->sys_msg_id = $sys_msg_id; } $model->is_read = 1; $model->read_time = time(); if(!$model->save()){ return (new Model())->getModelError($model); } return Model::asReturnSuccess(); } /** * 消息已提醒 */ public static function handleSound($sys_msg_id, $user_id, $cx_mch_id = 0) { $model = UserSysMsg::findOne(['cx_mch_id' => $cx_mch_id, 'user_id' => $user_id, 'sys_msg_id' => $sys_msg_id, 'is_delete' => 0]); if($model != null && $model->is_sound == 1){ return Model::asReturnError('消息已提醒'); } $sys_msg = SysMsg::findOne(['id' => $sys_msg_id, 'cx_mch_id' => $cx_mch_id, 'is_delete' => 0, 'status' => SysMsg::STATUS_PUBLISHED]); if($sys_msg == null){ return Model::asReturnError('系统消息不存在'); } if($model == null){ $model = new UserSysMsg(); $model->cx_mch_id = $cx_mch_id; $model->user_id = $user_id; $model->sys_msg_id = $sys_msg_id; } $model->is_sound = 1; $model->sound_time = time(); if(!$model->save()){ return (new Model())->getModelError($model); } return Model::asReturnSuccess(); } /** * 获取消息状态 * @param integer $type 类型:0=消息阅读状态,1=消息提醒状态 * return boolean */ public static function getUserSysStatus($sys_msg_id, $user_id, $type, $cx_mch_id = 0) { $model = UserSysMsg::findOne(['cx_mch_id' => $cx_mch_id, 'user_id' => $user_id, 'sys_msg_id' => $sys_msg_id, 'is_delete' => 0]); if($model == null) return false; if($type == 0 && $model->is_read == 1) return true; if($type == 1 && $model->is_sound == 1) return true; return false; } }