123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908 |
- "use strict";
- /**
- * controller for User Profile Example
- */
- app.controller("summaryDetailCtrl", [
- "$rootScope",
- "$scope",
- "$state",
- "$stateParams",
- "$timeout",
- "$interval",
- "$modal",
- "SweetAlert",
- "i18nService",
- "uiGridConstants",
- "uiGridGroupingConstants",
- "Restangular",
- "api_bpm_schedule",
- "api_bpm_data",
- "api_wechatfile",
- "api_configure_data",
- "moment",
- function (
- $rootScope,
- $scope,
- $state,
- $stateParams,
- $timeout,
- $interval,
- $modal,
- SweetAlert,
- i18nService,
- uiGridConstants,
- uiGridGroupingConstants,
- Restangular,
- api_bpm_schedule,
- api_bpm_data,
- api_wechatfile,
- api_configure_data,
- moment
- ) {
- $scope.langs = i18nService.getAllLangs();
- $scope.lang = "zh-cn";
- i18nService.setCurrentLang($scope.lang);
- var loginUser = $rootScope.user;
- $scope.summary_hc_add = false;
- $scope.summary_hc_remove = false;
- $scope.summary_hc_edit = false;
- $scope.summary_gs_add = false;
- $scope.summary_gs_remove = false;
- $scope.summary_gs_edit = false;
- for (var i = 0; i < loginUser.menu.length; i++) {
- if (loginUser.menu[i].link == "summary_hc_add") {
- $scope.summary_hc_add = true;
- }
- if (loginUser.menu[i].link == "summary_hc_remove") {
- $scope.summary_hc_remove = true;
- }
- if (loginUser.menu[i].link == "summary_hc_edit") {
- $scope.summary_hc_edit = true;
- }
- if (loginUser.menu[i].link == "summary_gs_add") {
- $scope.summary_gs_add = true;
- }
- if (loginUser.menu[i].link == "summary_gs_remove") {
- $scope.summary_gs_remove = true;
- }
- if (loginUser.menu[i].link == "summary_gs_edit") {
- $scope.summary_gs_edit = true;
- }
- }
- // 获取信息
- $scope.consumableList = [];
- $scope.incidentDTO = {};
- $scope.workHourManagementList = [];
- $scope.totalMaintenancePrice = null;
- $scope.totalPriceConsumable = null;
- $scope.totalPriceWorkHourManagement = null;
- $scope.getInfo = function(){
- console.log($stateParams.incidentId);
- $rootScope.isMask = true;
- api_bpm_data.querySummaryDoc({"incidentId":$stateParams.incidentId}).then(
- function (data) {
- $rootScope.isMask = false;
- $scope.consumableList = data.consumableList || [];
- $scope.incidentDTO = data.incidentDTO || {};
- $scope.workHourManagementList = data.workHourManagementList || [];
- $scope.totalMaintenancePrice = data.totalMaintenancePrice;
- $scope.totalPriceConsumable = data.consumablePrice;
- $scope.totalPriceWorkHourManagement = data.workHourPrice;
- },
- function () {
- $rootScope.isMask = false;
- }
- );
- }
- $scope.getInfo();
- //返回列表
- $scope.goToList = function(){
- $state.go("app.incident.summary");
- }
- $scope.transferTime = function (time) {
- if(time){
- return moment(time).format('YYYY-MM-DD HH:mm');
- }else{
- return '';
- }
- }
- //查看事件
- $scope.goToIncident = function(){
- $state.go('app.incident.incidentDetail', {
- id: $scope.incidentDTO.id,
- });
- }
- // 撤销耗材
- $scope.removeHc = function(consumable){
- var modalInstance = $modal.open({
- templateUrl: "assets/views/delete.html",
- controller: function ($scope, $modalInstance) {
- $scope.title = "撤销";
- $scope.connect = "您确认要撤销此条耗材("+ consumable.consumableName + "【"+ consumable.consumablesNum +"】" +")的使用吗?";
- $scope.ok = function () {
- $modalInstance.close("start");
- };
- $scope.cancel = function () {
- $modalInstance.dismiss("cancel");
- };
- },
- size: "sm",
- });
- modalInstance.result.then(
- function (result) {
- if(result == 'start'){
- $rootScope.isMask = true;
- api_bpm_data.removeHc({
- summaryId: +$stateParams.id,
- remove: 'remove',
- consumableList: [
- {
- consumablesId: consumable.consumableId,
- consumablesNum: consumable.consumablesNum,
- }
- ]
- }).then(
- function (response) {
- $rootScope.isMask = false;
- if(response.status == 200){
- SweetAlert.swal({
- title: "撤销成功!",
- type: "success"
- }, function() {
- $scope.getInfo()
- })
- } else {
- SweetAlert.swal({
- title: "撤销失败!",
- text: response.msg || '请求数据失败!',
- type: "error"
- }, function() {
- $scope.getInfo()
- })
- }
- },
- function () {
- $rootScope.isMask = false;
- }
- );
- }
- },
- function () {}
- );
- }
- const uniqBy = (arr, predicate) => {
- const cb = typeof predicate === 'function' ? predicate : (o) => o[predicate];
- return [...arr.reduce((map, item) => {
- const key = (item === null || item === undefined) ?
- item : cb(item);
- map.has(key) || map.set(key, item);
- return map;
- }, new Map()).values()];
- };
- // 修改耗材
- $scope.updateHc = function(consumable){
- console.log(consumable)
- var modalInstance = $modal.open({
- templateUrl: 'assets/views/system/tpl/hcChange.html',
- controller: function($scope, scope, $modalInstance, api_user_data, api_bpm) {
- $scope.title = '耗材修改';
- $scope.isAdd = false;
- $scope.dataInfo = {
- num: consumable.consumablesNum,
- consumable: {id: consumable.consumableId, name: consumable.consumableName},
- }
- $scope.getHcList=function(keyword = ''){
- // 查询故障耗材列表
- var postData = {
- "idx": 0,
- "sum": 20,
- incidentCategoryConsumable: {
- duty: $stateParams.dutyId,
- category: { id: $stateParams.categoryId },
- }
- };
- api_user_data.fetchDataList('incidentCategoryConsumable',postData).then(function(res){
- if(res.status == 200){
- let list = res.list || [];
- if(list.length){
- let arr = list.map(v => v.consumableDTOS).flat();
- $scope.hcList = uniqBy(arr, 'id');
- }else{
- // 查询耗材列表
- var postData = {
- "idx": 0,
- "sum": 20,
- consumable: {
- keyWord: keyword,
- dutyDTO: { id: $stateParams.dutyId },
- showZero: true,
- }
- };
- api_user_data.fetchDataList('consumable',postData).then(function(res){
- if(res.status == 200){
- $scope.hcList = res.list || [];
- }else{
- uni.showToast({
- icon: 'none',
- title: res.msg || '请求数据失败!'
- });
- }
- })
- }
- }else{
- uni.showToast({
- icon: 'none',
- title: res.msg || '请求数据失败!'
- });
- }
- })
- }
- $scope.getHcList();
- $scope.changeNum = function(val){
- $scope.dataInfo.num = val?Math.abs(parseFloat(val.toFixed(2), 10)):val;
- }
- $scope.cancel = function() {
- $modalInstance.dismiss('cancel');
- };
- $scope.savercode = function() {
- if ($scope.dataInfo.num && $scope.dataInfo.consumable && $scope.dataInfo.consumable.id){
- $rootScope.isMask = true;
- let postData = {
- "consumableList": [
- {
- "consumablesId": $scope.dataInfo.consumable ? $scope.dataInfo.consumable.id : undefined,
- "consumablesNum": $scope.dataInfo.num
- }
- ],
- modifyNum: "edit",
- "summaryId": $stateParams.id,
- };
- api_bpm.addSummaryDoc(postData).then(function(response) {
- if (response) {
- if (response.status == 200) {
- SweetAlert.swal({
- title: "修改成功!",
- type: "success"
- }, function() {
- scope.getInfo()
- })
- } else {
- SweetAlert.swal({
- title: "修改失败!",
- text: response.msg || '请求数据失败!',
- type: "error"
- }, function() {
- scope.getInfo()
- })
- }
- $modalInstance.close();
- }else{
- SweetAlert.swal({
- title: "修改失败!",
- text: response.msg || '请求数据失败!',
- type: "error"
- }, function() {
- scope.getInfo()
- })
- }
- })
- }else{
- SweetAlert.swal({
- title: "新增失败!",
- text: "请填写必填项!",
- type: "error",
- confirmButtonColor: "#DD6B55"
- }, function () {
- });
- }
- }
- },
- resolve: {
- scope: function() {
- return $scope;
- }
- }
- });
- }
- // 新增耗材
- $scope.addHc = function(){
- var modalInstance = $modal.open({
- templateUrl: 'assets/views/system/tpl/hcChange.html',
- controller: function($scope, scope, $modalInstance, api_user_data, api_bpm) {
- $scope.title = '耗材新增';
- $scope.isAdd = true;
- $scope.dataInfo = {
- num: 1,
- consumable: null,
- }
- $scope.getHcList=function(keyword = ''){
- // 查询故障耗材列表
- var postData = {
- "idx": 0,
- "sum": 20,
- incidentCategoryConsumable: {
- duty: $stateParams.dutyId,
- category: { id: $stateParams.categoryId },
- }
- };
- api_user_data.fetchDataList('incidentCategoryConsumable',postData).then(function(res){
- if(res.status == 200){
- let list = res.list || [];
- if(list.length){
- let arr = list.map(v => v.consumableDTOS).flat();
- $scope.hcList = uniqBy(arr, 'id');
- }else{
- // 查询耗材列表
- var postData = {
- "idx": 0,
- "sum": 20,
- consumable: {
- keyWord: keyword,
- dutyDTO: { id: $stateParams.dutyId },
- showZero: true,
- }
- };
- api_user_data.fetchDataList('consumable',postData).then(function(res){
- if(res.status == 200){
- $scope.hcList = res.list || [];
- }else{
- uni.showToast({
- icon: 'none',
- title: res.msg || '请求数据失败!'
- });
- }
- })
- }
- }else{
- uni.showToast({
- icon: 'none',
- title: res.msg || '请求数据失败!'
- });
- }
- })
- }
- $scope.getHcList();
- $scope.changeNum = function(val){
- $scope.dataInfo.num = val?Math.abs(parseFloat(val.toFixed(2), 10)):val;
- }
- $scope.cancel = function() {
- $modalInstance.dismiss('cancel');
- };
- $scope.savercode = function() {
- if ($scope.dataInfo.num && $scope.dataInfo.consumable && $scope.dataInfo.consumable.id){
- $rootScope.isMask = true;
- let postData = {
- "consumableList": [
- {
- "consumablesId": $scope.dataInfo.consumable ? $scope.dataInfo.consumable.id : undefined,
- "consumablesNum": $scope.dataInfo.num
- }
- ],
- "summaryId": $stateParams.id,
- };
- api_bpm.addSummaryDoc(postData).then(function(response) {
- if (response) {
- if (response.status == 200) {
- SweetAlert.swal({
- title: "新增成功!",
- type: "success"
- }, function() {
- scope.getInfo()
- })
- } else {
- SweetAlert.swal({
- title: "新增失败!",
- text: response.msg || '请求数据失败!',
- type: "error"
- }, function() {
- scope.getInfo()
- })
- }
- $modalInstance.close();
- }else{
- SweetAlert.swal({
- title: "新增失败!",
- text: response.msg || '请求数据失败!',
- type: "error"
- }, function() {
- scope.getInfo()
- })
- }
- })
- }else{
- SweetAlert.swal({
- title: "新增失败!",
- text: "请填写必填项!",
- type: "error",
- confirmButtonColor: "#DD6B55"
- }, function () {
- });
- }
- }
- },
- resolve: {
- scope: function() {
- return $scope;
- }
- }
- });
- }
- // 撤销工时
- $scope.removeGs = function(workHourManagement){
- var modalInstance = $modal.open({
- templateUrl: "assets/views/delete.html",
- controller: function ($scope, $modalInstance) {
- $scope.title = "撤销";
- $scope.connect = "您确认要撤销此条工时("+ workHourManagement.workName + "【"+ workHourManagement.workHourNum2 +"】" +")的使用吗?";
- $scope.ok = function () {
- $modalInstance.close("start");
- };
- $scope.cancel = function () {
- $modalInstance.dismiss("cancel");
- };
- },
- size: "sm",
- });
- modalInstance.result.then(
- function (result) {
- if(result == 'start'){
- $rootScope.isMask = true;
- api_bpm_data.removeHc({
- summaryId: +$stateParams.id,
- remove: 'remove',
- workHourManagementList: [
- {
- workHourId: workHourManagement.id,
- workHourNum: workHourManagement.workHourNum,
- }
- ]
- }).then(
- function (response) {
- $rootScope.isMask = false;
- if(response.status == 200){
- SweetAlert.swal({
- title: "撤销成功!",
- type: "success"
- }, function() {
- $scope.getInfo()
- })
- } else {
- SweetAlert.swal({
- title: "撤销失败!",
- text: response.msg || '请求数据失败!',
- type: "error"
- }, function() {
- $scope.getInfo()
- })
- }
- },
- function () {
- $rootScope.isMask = false;
- }
- );
- }
- },
- function () {}
- );
- }
- // 修改工时
- $scope.updateGs = function(workHourManagement){
- console.log(workHourManagement)
- var modalInstance = $modal.open({
- templateUrl: 'assets/views/system/tpl/gsChange.html',
- controller: function($scope, scope, $modalInstance, api_user_data, api_bpm) {
- $scope.title = '工时修改';
- $scope.isAdd = false;
- $scope.dataInfo = {
- num: workHourManagement.workHourNum2,
- workHourManagement: [{id: workHourManagement.id, name: workHourManagement.workName}],
- }
- // --------------------
- $scope.select_treedata = [];
- $rootScope.bala1 = $scope.getGsList = function (s, fn) {
- if (s) {
- var filterKeyword = s.filterKeyword;
- }
- var postData = {
- idx: 0,
- sum: 9999,
- };
- $scope.my_data = [];
- $scope.doing_async = true;
- api_bpm_data
- .fetchDataList("workHourManagement", postData)
- .then(function (response) {
- if (response.status == 200) {
- var data = response.list;
- if (filterKeyword) {
- data.forEach((e) => {
- e.isExpanded = true;
- });
- var li = transform(data).children;
- console.log(li);
- fn(li);
- return;
- } else {
- var objects = [];
- for (var i = 0; i < data.length; i++) {
- var object = {};
- object.id = data[i].id;
- object.parent = data[i].parent;
- object.workName = data[i].workName;
- object.isExpanded = true;
- objects.push(object);
- }
- $scope.my_data = convertParentToChildList(objects);
- $scope.select_treedata = angular.copy($scope.my_data);
- console.log($scope.select_treedata)
- }
- if ($scope.my_data.length > 0) {
- $scope.doing_async = false;
- }
- selectItem(workHourManagement,$scope.select_treedata);
- $scope.dataInfo.workHourManagement = workHourManagement;
- } else {
- SweetAlert.swal({
- title: "系统错误!",
- text: "请刷新重试!",
- type: "error",
- });
- }
- });
- };
- $scope.getGsList();
- // --------------------
- $scope.changeNum = function(val){
- $scope.dataInfo.num = val?Math.abs(parseFloat(val.toFixed(2), 10)):val;
- }
- $scope.cancel = function() {
- $modalInstance.dismiss('cancel');
- };
- $scope.savercode = function() {
- if ($scope.dataInfo.num && $scope.dataInfo.workHourManagement.length){
- $rootScope.isMask = true;
- let postData = {
- "workHourManagementList": $scope.dataInfo.workHourManagement.map(v => ({ workHourId: v.id, workHourNum: $scope.dataInfo.num })),
- "summaryId": $stateParams.id,
- modifyNum: "edit"
- };
- api_bpm.addSummaryDoc(postData).then(function(response) {
- if (response) {
- if (response.status == 200) {
- SweetAlert.swal({
- title: "修改成功!",
- type: "success"
- }, function() {
- scope.getInfo()
- })
- } else {
- SweetAlert.swal({
- title: "修改失败!",
- text: response.msg || '请求数据失败!',
- type: "error"
- }, function() {
- scope.getInfo()
- })
- }
- $modalInstance.close();
- }else{
- SweetAlert.swal({
- title: "修改失败!",
- text: response.msg || '请求数据失败!',
- type: "error"
- }, function() {
- scope.getInfo()
- })
- }
- })
- }else{
- SweetAlert.swal({
- title: "新增失败!",
- text: "请填写必填项!",
- type: "error",
- confirmButtonColor: "#DD6B55"
- }, function () {
- });
- }
- }
- },
- resolve: {
- scope: function() {
- return $scope;
- }
- }
- });
- }
- // 新增工时
- $scope.addGs = function(){
- var modalInstance = $modal.open({
- templateUrl: 'assets/views/system/tpl/gsChange.html',
- controller: function($scope, scope, $modalInstance, api_user_data, api_bpm) {
- $scope.title = '工时新增';
- $scope.isAdd = true;
- $scope.dataInfo = {
- // num: 1,
- workHourManagement: [],
- }
- // --------------------
- $scope.select_treedata = [];
- $rootScope.bala1 = $scope.getGsList = function (s, fn) {
- if (s) {
- var filterKeyword = s.filterKeyword;
- }
- var postData = {
- idx: 0,
- sum: 9999,
- };
- $scope.my_data = [];
- $scope.doing_async = true;
- api_bpm_data
- .fetchDataList("workHourManagement", postData)
- .then(function (response) {
- if (response.status == 200) {
- var data = response.list;
- if (filterKeyword) {
- data.forEach((e) => {
- e.isExpanded = true;
- });
- var li = transform(data).children;
- console.log(li);
- fn(li);
- return;
- } else {
- var objects = [];
- for (var i = 0; i < data.length; i++) {
- var object = {};
- object.id = data[i].id;
- object.parent = data[i].parent;
- object.workName = data[i].workName;
- object.isExpanded = true;
- objects.push(object);
- }
- $scope.my_data = convertParentToChildList(objects);
- $scope.select_treedata = angular.copy($scope.my_data);
- console.log($scope.select_treedata)
- }
- if ($scope.my_data.length > 0) {
- $scope.doing_async = false;
- }
- // selectItem(selectdata.workName,$scope.select_treedata);
- // $scope.deptdata.workName = selectdata.workName;
- } else {
- SweetAlert.swal({
- title: "系统错误!",
- text: "请刷新重试!",
- type: "error",
- });
- }
- });
- };
- $scope.getGsList();
- // --------------------
- $scope.changeNum = function(val){
- $scope.dataInfo.num = val?Math.abs(parseFloat(val.toFixed(2), 10)):val;
- }
- $scope.cancel = function() {
- $modalInstance.dismiss('cancel');
- };
- $scope.savercode = function() {
- console.log($scope.dataInfo);
- // if ($scope.dataInfo.num && $scope.dataInfo.workHourManagement.length){
- if ($scope.dataInfo.workHourManagement.length){
- $rootScope.isMask = true;
- let workHourManagementList = scope.workHourManagementList || []
- let postData = {
- "workHourManagementList": $scope.dataInfo.workHourManagement.concat(workHourManagementList).map(v => ({ workHourId: v.id, workHourNum: v.workHourNum2 || 1 })),
- "summaryId": $stateParams.id,
- };
- api_bpm.addSummaryDoc(postData).then(function(response) {
- if (response) {
- if (response.status == 200) {
- SweetAlert.swal({
- title: "新增成功!",
- type: "success"
- }, function() {
- scope.getInfo()
- })
- } else {
- SweetAlert.swal({
- title: "新增失败!",
- text: response.msg || '请求数据失败!',
- type: "error"
- }, function() {
- scope.getInfo()
- })
- }
- $modalInstance.close();
- }else{
- SweetAlert.swal({
- title: "新增失败!",
- text: response.msg || '请求数据失败!',
- type: "error"
- }, function() {
- scope.getInfo()
- })
- }
- })
- }else{
- SweetAlert.swal({
- title: "新增失败!",
- text: "请填写必填项!",
- type: "error",
- confirmButtonColor: "#DD6B55"
- }, function () {
- });
- }
- }
- },
- resolve: {
- scope: function() {
- return $scope;
- }
- }
- });
- }
- function selectItem(pmodel, childrens) {
- if (angular.isArray(pmodel)) {
- angular.forEach(pmodel, function (index) {
- if (index && index.id) {
- angular.forEach(childrens, function (item) {
- if (item.id == index.id) {
- item.selected = true;
- }
- if (item && item.children) {
- selectItem(pmodel, item.children);
- }
- });
- }
- });
- } else {
- if (pmodel && pmodel.id) {
- angular.forEach(childrens, function (item, index) {
- if (item.id == pmodel.id) {
- item.selected = true;
- }
- if (item && item.children) {
- selectItem(pmodel, item.children);
- }
- });
- }
- }
- }
- function convertParentToChildList(data) {
- var treeMap = {};
- var list = [];
- convertListToTree(data, treeMap);
- angular.forEach(treeMap, function (item) {
- list.push(item);
- });
- return list;
- }
- function convertListToTree(data, treeMap) {
- var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup
- var root = null; //Initially set our loop to null
- var parentNode = null;
- //loop over data
- for (var i = 0; i < data.length; i++) {
- var datum = data[i];
- //each node will have children, so let's give it a "children" poperty
- datum.children = [];
- //add an entry for this node to the map so that any future children can
- //lookup the parent
- idToNodeMap[datum.id] = datum;
- //Does this node have a parent?
- if (typeof datum.parent === "undefined" || datum.parent == null) {
- //Doesn't look like it, so this node is the root of the tree
- root = datum;
- treeMap[datum.id] = root;
- } else {
- //This node has a parent, so let's look it up using the id
- parentNode = idToNodeMap[datum.parent.id];
- //We don't need this property, so let's delete it.
- delete datum.parent;
- //Let's add the current node as a child of the parent node.
- parentNode.children.push(datum);
- }
- }
- return root;
- }
- //树形图
- // 将故障现象搜索结果返回的数据整理成children模式
- function transform(nodes) {
- var treeConverter = {
- result: null, //转化后的结果,是根节点,所有节点都是从根节点长出来的
- attributeName: 'id', //节点唯一标识符
- needFind: true, //是否查询节点在result中已经存在,为了优化效率
- transform: function (node) { //转化递归函数,参数:一个待插入节点
- if (node.parent != null) { //该节点有父节点
- var newNode = this.transform(node.parent); //递归进入,返回值为一个节点,用作父节点,该父节点必然存在于result中,这点由下面的算法可以控制
- if (this.needFind) {
- for (var i = 0; i < newNode.children.length; i++) { //查找要插入的node子节点是否在newNode这个父节点中存在
- if (newNode.children[i][this.attributeName] === node[this.attributeName]) {
- return newNode.children[i]; //存在的话直接返回newNode父节点内的该子节点,该子节点必然存在于result中,作为返回值它将被用作上级递归的newNode,因此newNode必然存在于result中
- }
- }
- }
- this.needFind = false; //不存在的话,关闭之后递归的循环判断,因为待插入node节点不存在于result中,故而它的子节点一定不存在于result中,不用再循环判断
- // delete node.parent; //删除该节点的parent属性,如果有的话
- node.children = []; //因为确定是要新插入的节点,没有children:[]属性,故给该节点增加children:[]属性
- newNode.children.push(node); //将该node节点push进newNode的子节点数组中
- return node; //return该新插入节点,作为递归返回值给上层,用作newNode父节点,node存在于result中故newNode存在于result中
- } else if (node.parent == null) { //该叶节点没有父节点,即为根节点
- // delete node.parent; //删除该节点的parent属性,如果有的话
- if (this.result == null) { //根节点不存在
- node.children = []; //给该节点增加children:[]属性
- return this.result = node; //该节点赋给result,并return根节点,作为返回值它将被用作上级递归的newNode,因此newNode必然存在于result中
- } else {
- node.children = [];
- // 顶级去重
- for (var i = 0; i < this.result.children.length; i++) {
- if (this.result.children[i][this.attributeName] === node[this.attributeName]) {
- return this.result.children[i];
- }
- }
- this.result.children.push(node)
- return node // 直接return根节点,作为返回值它将被用作上级递归的newNode,因此newNode必然存在于result中
- }
- }
- },
- getWhole: function (nodes, attributeName) { //传入整个叶子节点数组,attributeName作为节点唯一标识符属性,返回整个转化结果
- var _node = {};
- _node.children = [];
- this.result = _node; //重置根节点
- this.attributeName = attributeName == null ? 'id' : attributeName; //唯一标识符默认为“id”
- nodes = JSON.parse(JSON.stringify(nodes)); //复制出一个新的节点对象作为参数,保证不改变原有数据
- nodes.forEach(item => { //循环调用转化方法
- this.needFind = true; //重置开启节点是否已存在判断,保证不插入重复节点
- this.transform(item);
- })
- return this.result; //返回根节点
- }
- }
- var result = treeConverter.getWhole(nodes); //调用
- return result;
- }
- },
- ]);
|