1、需要使用工作的模块,首先,需要一个wfid字段,smallint,5,工作流ID,这里可以参考一下,文档管理 - 分类管理
2、需要一个is_qf字段,tinyint,1,是否签发信息,1为签发信息,0为非签发信息,这里可以参考一下,文字管理 - 默认文章
3、在信息提交时,获取该信息关联的wfid,工作流,然后将该信息的is_qf字段设为true
4、最后一个节点,切记,记得设置为结束编号100
2021-09-16
1、model 加入afterSave钩子函数:
- public function afterSave($insert, $changedAttributes)
- {
- parent::afterSave($insert, $changedAttributes);
- if ($insert) {
- //这里是新增数据
- if ($this->is_qf) {
- try {
- // 2021-02-04更新,可以上传用户手写签名图片signPic,这里要注意有没有user_sign这个字段
- Yii::$app->services->workflowService->infoInsertToWorkflow(__CLASS__, $this->id, $this->wfid, "investment", "contract", $this->title, $this->user_sign);
- // 如果已经写入工作流信息,并且有下一审核人
- // 给下一审核人发推送
- if($this->wfinfo && $this->wfinfo->userids){
- // p2("给审核人发推送:" . $this->wfinfo->userids);
- Yii::$app->services->workflowService->pushAuditUsers($this->wfinfo->userids,__CLASS__, $this->id);
- }
- } catch (\Exception $e) {
- }
- }
- }
- }
PS:如果是新加的entity 类型,到WorkflowService里面看一下 pushAuditUsers 方法
2、和工作流的信息表,添加关联关系
- /**
- * 获取签发信息以及签发用户
- * @return \yii\db\ActiveQuery
- */
- public function getWfinfo()
- {
- return $this->hasOne(Enewswfinfo::className(), ["entity_id" => "id"])->andOnCondition(["entity" => __CLASS__]);
- }
3、API人工签发,审核
- /**
- * @api {post} /investment/contract/audit 合同签发/审核
- * @apiVersion 1.0.0
- * @apiName InvestmentContractAudit
- * @apiGroup 投资分析管理
- * @apiPermission user
- *
- * @apiDescription 只有指定人才有审核权限,后端也会判断的
- *
- * @apiHeader {String} access-token 登录接口返回的access token.
- *
- * @apiParam {int} contract_id 合同ID
- * @apiParam {int} doing 操作,1为通过,2为返工,3为否决,0为送审
- * @apiParam {String} user_sign 签发人签名
- * @apiParam {String} check_text 签发人评语
- *
- * @apiSuccess {Int} code 200正常,非200错误,401token过期,需要重新登录
- * @apiSuccess {String} message 当code!=200的时候,message有意义
- * @apiSuccess {Int} data 信息
- *
- *@apiParamExample {json} request-example
- * {
- * "id":1,
- * "doing":1,
- * "check_text":"同意",
- * "user_sign":"http://v2.zckj.ademo.ccc/storage/upload/20201211RLtpsowsWfV1aOXqSqanwCBZDwC46857OrZEr3Ec.jpg",
- * }
- * @apiError message Only authenticated Admins can access the data.
- *
- * @apiErrorExample Response (example):
- * HTTP/1.1 401 Not Authenticated
- * {
- * "code":401,
- * "message": "Your request was made with invalid credentials.",
- * "data":{
- *
- * }
- * }
- *
- */
- public function actionAudit()
- {
- $entity_id = (string)\Yii::$app->request->post("contract_id"); // 合同ID
- $model = InvestmentContract::find()->where(['id' => $entity_id])->one();
- if ($model === null) {
- throw new NotFoundHttpException('合同台帐不存在,请联系管理员');
- }
- if ($model->is_qf == 0) {
- return ResultHelper::json("error", "该合同台帐无需签发");
- }
- // 提交签发结果
- $doing = (int)Yii::$app->request->post("doing");
- $checkText = Yii::$app->request->post("check_text");
- $entity = \common\modules\investment\models\InvestmentContract::className();
- $currentUserId = Yii::$app->user->id;
- $signPic = Yii::$app->request->post("user_sign");
- $res = Yii::$app->services->workflowService->doWfInfo($entity, $entity_id, $currentUserId, $doing, $signPic, $checkText, "backend");
- // tno=100表示完成,完成之后【在工作流里面会发通知的】,这里无需再发一次
- // 完成后,工作流里面也会发送通知的
- return ResultHelper::json($res["code"], $res["message"]);
- }
4、被打回后,API重新送审
- /**
- * @api {get} /investment/contract/update-audit 合同重新送审
- * @apiVersion 1.0.0
- * @apiName InvestmentUpdateAudit
- * @apiGroup 投资分析管理
- * @apiPermission user
- * @apiDescription 当详情info.check_tno = 101表示只能操作重新送审,不允许再次签发
- *
- * @apiHeader {String} access-token 登录接口返回的access token.
- * @apiParam {Int} id 唯一标识,合同的id
- *
- * @apiSuccess {Int} code 200正常,非200错误,401token过期,需要重新登录
- * @apiSuccess {String} message 当code!=200的时候,message有意义
- * @apiSuccess {Int} data 信息
- *
- * @apiError message Only authenticated Admins can access the data.
- *
- * @apiErrorExample Response (example):
- * HTTP/1.1 401 Not Authenticated
- * {
- * "code":401,
- * "message": "Your request was made with invalid credentials.",
- * "data":{
- *
- * }
- * }
- *
- */
- public function actionUpdateAudit()
- {
- $entity_id = (string)\Yii::$app->request->get("id");
- $model = InvestmentContract::find()->where(['id' => $entity_id])->one();
- if ($model === null) {
- throw new NotFoundHttpException('合同台帐不存在,请联系管理员');
- }
- $entity = \common\modules\investment\models\InvestmentContract::className();
- $currentUserId = Yii::$app->user->id;
- return Yii::$app->services->workflowService->infoUpdateToWorkflow($entity, $entity_id, $currentUserId);
- }
- public function afterSave($insert, $changedAttributes)
- {
- parent::afterSave($insert, $changedAttributes);
- if ($insert) {
- //这里是新增数据
- if ($this->is_qf) {
- try {
- // 2021-02-04更新,可以上传用户手写签名图片signPic,这里要注意有没有user_sign这个字段
- WorkflowService::infoInsertToWorkflow(__CLASS__, $this->id, $this->wfid, "investment", "contract", $this->title, $this->user_sign);
- } catch (Exception $e) {
- }
- $this->pushNextAuditUsers(); // 给下一审核人发推送
- }
- }
- }
- // 删除后对应删除副表,信息表,日志表
- public function afterDelete()
- {
- // delete dependencies
- Enewswfinfo::deleteAll(["entity" => __CLASS__, "entity_id" => $this->id]);
- Enewswfinfolog::deleteAll(["entity" => __CLASS__, "entity_id" => $this->id]);
- // 删除待办事项
- $ids = Notify::find()->select("id")->where(["target_type"=>"investment","action"=>"contract","target_id"=>$this->id])->column();
- NotifyMember::deleteAll(["notify_id"=>$ids]);
- Notify::deleteAll(["id"=>$ids]);
- parent::afterDelete();
- }
- /**
- * 获取签发信息以及签发用户
- * @return \yii\db\ActiveQuery
- */
- public function getWfinfo()
- {
- return $this->hasOne(Enewswfinfo::className(), ["entity_id" => "id"])->andOnCondition(["entity" => __CLASS__]);
- }
- /**
- * 需要签发时给下一签发人发推送,不需要签发时给联系人发推送
- * @return bool
- */
- public function pushNextAuditUsers()
- {
- $packageName = \Yii::$app->config->get("APP_ANDROID_PACKAGE"); //app的Android包名
- if (!$packageName) {
- return false;
- }
- $title = "待办事项";
- $content = $this->title . "待审批"; // 为了与工作流命名一致
- $payload = ['type' => 'contract', 'id' => $this->id];
- if ($this->is_qf) {
- // 如果是签发信息
- // 给下一签发人发推送
- if ($this->wfinfo) {
- // 如果有Android的包名并且有签发信息
- $userids = explode(",", $this->wfinfo->userids);
- foreach ($userids as $userid) {
- \Yii::$app->services->geTui->pushSingle($packageName, $title, $content, $payload, [$userid]);
- }
- }
- } else {
- // 如果不用签发
- // 给发起人发送app通知提醒
- $title = $this->title;
- $content = "审批通过";
- $this->pushContactUser($title, $content);
- }
- }
- public function pushContactUser($title, $content)
- {
- $packageName = \Yii::$app->config->get("APP_ANDROID_PACKAGE"); //app的Android包名
- if (!$packageName) {
- return false;
- }
- $payload = ['type' => 'purchase', 'id' => $this->id];
- $userid = $this->user_id;
- \Yii::$app->services->geTui->pushSingle($packageName, $title, $content, $payload, [$userid]);
- }
如果在index中需要添加签发的按钮:
- [
- 'class' => 'backend\widgets\grid\ActionColumnLayer',
- 'template' => '{view} {update} {qianfa} {delete}',
- 'buttons' => [
- 'view' => function ($url, $model, $key) {
- return Html::createLayer(['view', 'id' => $model->id], '<i class="icon wb-eye"></i>', [
- 'data-ajax' => 1,
- 'data-target' => '_blank',
- 'data-toggle' => 'tooltip',
- 'data-original-title' => '查看',
- 'data-width' => '80%',
- 'data-height' => '90%'
- ]);
- },
- 'update' => function ($url, $model, $key) {
- return Html::createLayer(['update', 'id' => $model->id], '<i class="icon wb-edit"></i>', [
- 'class' => 'btn btn-sm btn-flat',
- 'target' => '_blank',
- 'data-toggle' => 'tooltip',
- 'data-original-title' => '修改',
- 'data-width' => '80%',
- 'data-height' => '90%'
- ]);
- },
- 'qianfa' => function ($url, $model, $key) {
- if ($model->is_qf) {
- $entity = common\modules\document\models\Document::className();
- $entityId = $model->id;
- $wfinfo = $model->wfinfo;
- if ($wfinfo) {
- $currentAuditUser = $wfinfo->userTrueNames;
- $html = Html::createLayer(["/workflow/info/audit", "entity" => $entity, "entity_id" => $entityId], "待" . $currentAuditUser . "签发", ['class' => 'btn btn-warning btn-xs','data-title'=>'签发信息']);
- if($wfinfo->check_tno == 101){
- $html .= " " . Html::createAjaxBtn(["/workflow/info/update-audit", "entity" => $entity, "entity_id" => $entityId], "重新送审", ['class' => 'btn btn-warning btn-xs']);
- }
- return $html;
- } else {
- return "<span class='btn btn-xs btn-danger'>签发信息丢失</span>";
- }
- } else {
- return "";
- }
- },
- ]
- ],
4、表单提交签发信息,返回的是json格式的
- // 提交签发结果
- $doing = Yii::$app->request->post("doing");
- $doing = (int) $doing;
- $currentUserId = Yii::$app->user->id;
- $currentUserId = (int) $currentUserId;
- return WorkflowService::doWfInfo($entity,$entity_id,$currentUserId,$doing,Yii::$app->request->post("check_text"));
5、相关查询方法:
- // 根据当前model命名空间和主键id,获取工作流信息
- $info = Enewswfinfo::find()->where(["entity" => $entity, "entity_id" => $entity_id])->with(["workflow","workflow.items","workflowlog","workflowlog.profile"])->one();
- $workflow = $info->workflow; // 工作流信息所属工作流
- $items = $info->workflow->items; // 工作流下面的所有子节点
- // 获取工作流信息所有审核记录
- $infoLog = $info->workflowlog;
隐藏获取属性:
当前签发信息签发状态(通过、返工、否决):$info->checktnoTxt
当前签发信息详情:$info->detail
签发信息获取签发用户的真实姓名:$info->userTrueNames
签发日志的签发用户真实姓名:$infoLog->userTrueName
签发日志的签发状态:$infoLog->checktTypeTxt
这里的打回操作,指的是用户重新送审时,步骤从哪一步继续开始,重新送审后继续从下一个签发用户开始
API说明:
1、审核通过 / 不通过:
传参:ID / doing(操作,1为通过,2为返工,3为否决,0为送审) / check_text(签发人评语) / user_sign(签发人签名图片url)
- public function actionAudit()
- {
- $entity_id = (string) \Yii::$app->request->post("id");
- // 提交签发结果
- $entity = Object::className();
- $doing = (int) Yii::$app->request->post("doing");
- $checkText = Yii::$app->request->post("check_text");
- $currentUserId = Yii::$app->user->id;
- $signPic = Yii::$app->request->post("user_sign");
- return WorkflowService::doWfInfo($entity, $entity_id, $currentUserId, $doing, $signPic, $checkText);
- }
2、当被否决或返回提交者时,需要重新送审
- public function actionUpdateAudit()
- {
- $entity_id = (string) \Yii::$app->request->get("id");
- $entity = Object::className();
- $currentUserId = Yii::$app->user->id;
- return WorkflowService::infoUpdateToWorkflow($entity,$entity_id,$currentUserId);
- }
在API的model中返回签发信息和和签发日志
- // 签发详情
- 'info' => function ($model) {
- if (!$model->is_qf) {
- // 非签发信息
- return [];
- }
- $wfinfo = $model->wfinfo;
- if ($wfinfo) {
- return [
- "userids" => $wfinfo->userids,
- "usernames" => $wfinfo->userTrueNames,
- "check_num" => $wfinfo->check_num,
- "tstatus" => $wfinfo->tstatus,
- "check_tno" => $wfinfo->check_tno,
- ];
- } else {
- return [
- // "userids" => "",
- // "usernames" => "",
- // "check_num" => 0,
- // "tstatus" => "",
- // "check_tno" => 0,
- ];
- }
- },
- // 签发日志
- 'audit' => function ($model) {
- if(!$model->wfinfo){
- // 如果有签发流程
- return [];
- }
- $workflowlog = $model->wfinfo->workflowlog;
- $res = [];
- foreach ($workflowlog as $k => $log) {
- $res[] = [
- "id" => $log->id,
- "wfid" => $log->wfid,
- "tid" => $log->tid,
- "user_id" => $log->user_id,
- "user_name" => $log->userTrueName,
- "user_sign" => $log->user_sign,
- "check_text" => $log->check_text,
- "check_num" => $log->check_num,
- "check_type" => $log->check_type,
- "created_at" => date("Y-m-d H:i:s", $log->created_at),
- ];
- }
- return $res;
- },
- /**
- * 采购单签发/审核
- * @return array|mixed
- * @throws NotFoundHttpException
- * @throws \Throwable
- * @throws \yii\db\Exception
- * @throws \yii\db\StaleObjectException
- */
- public function actionAudit()
- {
- $entity_id = (string)\Yii::$app->request->post("purchase_id");
- $model = Purchase::find()->where(['purchase_id' => $entity_id])->one();
- if ($model === null) {
- throw new NotFoundHttpException('采购单不存在,请联系管理员');
- }
- if($model->is_qf == 0){
- return ResultHelper::json("error", "该采购单无需签发");
- }
- // 提交签发结果
- $doing = (int)Yii::$app->request->post("doing");
- $checkText = Yii::$app->request->post("check_text");
- $entity = \common\modules\material\models\Purchase::className();
- $currentUserId = Yii::$app->user->id;
- $signPic = Yii::$app->request->post("user_sign");
- $res = Yii::$app->services->workflowService->doWfInfo($entity, $entity_id, $currentUserId, $doing, $signPic, $checkText,"backend");
- // tno=100表示完成,完成之后在工作流里面会发通知的,这里无需再发一次
- if(($res["code"] == 201 || $res["code"] == 200) && $res["data"]["tno"] != 100){
- // 给发起人发送app通知提醒
- $title = $model->title;
- $content = $res["message"];
- $model->pushContactUser($title,$content);
- }
- // 写入日志变动记录
- $recordModel = new Record();
- $recordModel->type = "purchase";
- $recordModel->action = "audit";
- // $recordModel->content = "签发采购申请单";
- $recordModel->content = $res["message"];
- $recordModel->link_id = $model->purchase_id;
- $recordModel->project_id = $model->project_id;
- if (!$recordModel->save()) {
- throw new HttpException(400, current($recordModel->getFirstErrors()));
- }
- return ResultHelper::json($res["code"],$res["message"]);
- }
- /**
- * 重新送审
- * @return array|mixed
- * @throws NotFoundHttpException
- * @throws \yii\db\Exception
- */
- public function actionUpdateAudit()
- {
- $entity_id = (string)\Yii::$app->request->get("id");
- $model = Purchase::find()->where(['purchase_id' => $entity_id])->one();
- if ($model === null) {
- throw new NotFoundHttpException('采购单不存在,请联系管理员');
- }
- // 写入日志变动记录
- $recordModel = new Record();
- $recordModel->type = "purchase";
- $recordModel->action = "audit";
- $recordModel->content = "重新送审采购申请单";
- $recordModel->link_id = $model->purchase_id;
- $recordModel->project_id = $model->project_id;
- if (!$recordModel->save()) {
- throw new HttpException(400, current($recordModel->getFirstErrors()));
- }
- $entity = \common\modules\material\models\Purchase::className();
- $currentUserId = Yii::$app->user->id;
- return Yii::$app->services->workflowService->infoUpdateToWorkflow($entity, $entity_id, $currentUserId);
- }
- /**
- * 获取审核流程,获取签发流程
- * @return array
- */
- public function actionAuditSetting()
- {
- $projectId = Yii::$app->request->get("project_id");
- $wfid = Purchase::getWfid($projectId);
- if (!$wfid) {
- return [];
- }
- $items = Enewsworkflowitem::find()->where(["wfid" => $wfid])->all();
- $res = [];
- foreach ($items as $k => $item) {
- $res[] = [
- "tid" => $item->tid,
- "wfid" => $item->wfid,
- "tname" => $item->tname,
- "tno" => $item->tno,
- "ttext" => $item->ttext,
- "userids" => $item->userids,
- "usernames" => $item->userTrueNames,
- "tbdo" => $item->tbdo,
- "tddo" => $item->tddo,
- "tstatus" => $item->tstatus,
- ];
- }
- return $res;
- }