paymentAuthCtrl.js 4.67 KB
(function(angular) {
	function paymentAuthCtrl($scope, DTOptionsBuilder, DTColumnBuilder, wechatauthService, $uibModal, $q, $state,ext) {
		$scope.binding = {
			versions:[
				{userVersion:'请选择',templateId:''}
			],
			objUpdateTime:null
		}

		$scope.model ={
			type:'merchantName',
			updateTime:'',
			search,
			searchTxt:'',
			beginDate:'',
			endDate:'',
			specialMerchant:'specialMerchantId',
			specialMerchant_txt:'',
			takeOptions:{
				eventHandlers: {
					'apply.daterangepicker': function (ev, picker) {
						$scope.model.beginDate = `${picker.startDate.format('YYYY-MM-DD')} 00:00:00`;
						$scope.model.endDate = `${picker.endDate.format('YYYY-MM-DD')} 23:59:59`;
					},
					'cancel.daterangepicker':function (ev, picker) {
						$scope.model.beginDate = ''
						$scope.model.endDate = ''
						$scope.binding.objUpdateTime={}
						$scope.binding.objUpdateTime.startDate =null
						$scope.binding.objUpdateTime.endDate =null
						$scope.$apply()
					}
				}
			},
			confirm,
			bind
		};


		function search(){
			var __params= getParams()
			$scope.dtInstance.query(__params);
		}

		//统一处理请求
		function __request(action,params){
			// return new Promise((resolve,reject)=>{
				var __def = $q.defer()
				wechatauthService[action](params,function(result){
					if(result.returnCode == '0'){
						__def.resolve(result.data)
					}else{
						alerter(result.message)
						__def.reject(false)
					}
				})
				return __def.promise
			// })
		}
		$scope.dtColumns = [
			DTColumnBuilder.newColumn('merchantName').withOption('width',140).withTitle('门店名称'),
            DTColumnBuilder.newColumn('appLogo').withOption('width',140).withTitle('小程序图标')
			.renderWith(function(data) { return `<img src="${data}" width="50px" height="50px">`}),
			DTColumnBuilder.newColumn('appId').withOption('width',200).withTitle('AppID'),
			DTColumnBuilder.newColumn('specialMerchantId').withOption('width',200).withTitle('特约商户号'),
			DTColumnBuilder.newColumn('specialMerchantName').withOption('width',200).withTitle('特约商户全称'),
			DTColumnBuilder.newColumn('specialMerchantType').withOption('width',200).withTitle('特约商户类型')
			.renderWith(function(data){ return {'COMMON':'普通商户','PLATFORM':'平台商户'}[data]}),
			DTColumnBuilder.newColumn('updatedDate').withOption('width',200).withTitle('更新时间'),
            DTColumnBuilder.newColumn(null).withTitle(()=>'操作')
				.withOption('width', 100)
				.renderWith(function(data) { return `<a class="btn btn-link" ng-click="model.confirm(${data.id})">解除绑定</a>`})
        	];
		$scope.dtOptions = DTOptionsBuilder.newOptions()
		.withOption('id','id')
		.withFixedColumns({
            leftColumns: 0,
            rightColumns: 1
        })
        .withOption('select', {
        	type: 'checkbox'
        })
		// .withDataProp('data.list')
		.withButtons([{
			text: '新增绑定',
			className: 'btn btn-primary',
			action: function (e, dt, node, config) {
				$scope.model.bind()
			}
		}])
		$scope.dtInstance = {
			serverData: wechatauthService.searchSpecialMerchant
		};
		function getParams(){
			var obj = {};
			obj[$scope.model.type]=$scope.model.searchTxt
			obj[$scope.model.specialMerchant]=$scope.model.specialMerchant_txt
			const {beginDate,endDate} =$scope.model
			var condition = angular.extend({},obj,{beginDate,endDate});
			condition = removeEmptyParam(condition)
			if($.isEmptyObject(condition)){
				return {}
			}
			return {condition}
		}

		function removeEmptyParam(params){
			for(var key in params){
				!params[key] && delete params[key]
			}
			return params
		}
	
		function open(templateUrl,controller,defaultData,result,size){
			$uibModal.open({
				templateUrl,
				controller,
				size: size || 'md',
				resolve: {
					data: ()=> {
						return {defaultData}
					}
				}
			}).result.then(result)
		}
		function alerter(message,type){
			swal({
				type: type||'error',
				title: '',
				text: message||'',
				showConfirmButton:false,
				timer: 1500
			},function(){
				swal.close();
			});
		}
		function bind(){
			open('biz/wechatauth/view/operation/pa-merchant-bind.html','paMerchantBind',{request:__request},function(data){
				alerter('新增绑定成功!','success')
				search()
			},'md')
		}
		function confirm(id){
			open('biz/wechatauth/view/operation/confirm.html','confirm',{title:'解除绑定',content:'是否解除绑定商户号?'},function(data){
				__request('unbindMerchant',{id}).then(function(result){
					// alerter('解除绑定成功!','success')
					result && search()
				})
			},'sm')
		}
		;(function init (){
			// loadVersions()
		})()
	}
	angular.module('wechatauth').controller('paymentAuthCtrl', paymentAuthCtrl)
})(angular);