1 /**
  2  * 快速划账
  3  */
  4 define(["core", "component/process_components","component/form","component/autocomplete"], function() {
  5 
  6     var _html = [
  7     '<div class="we_tran_acc">',
  8         '<ul class="we_tran_acc_list">',
  9           '<li class="we_trans_out">',
 10               '<div class="we_tran_item1">',
 11                   '<label><i class="we_icon_tran_out"></i>转出账号:</label><span>$outAccount$</span>',
 12                 '</div>',
 13                 '<div class="we_tran_item2">',
 14                    '<label>点券余额:</label><span>$outAccountAmount$</span> ',
 15                 '</div>',
 16             '</li>',
 17             '<li class="we_trans_in">',
 18               '<div class="we_tran_item">',
 19                   '<div class="we_tran_item1">',
 20                     '<label><i class="we_icon_tran_in"></i>转入账号:</label>',
 21 
 22                       '<div class="we_cell_select">',
 23                           '<input class="we_input"  type="text" value="选择一个账号" id="account_selected"/>',
 24                       '</div>',
 25 
 26                     '</div>',
 27                     '<div class="we_tran_item2" attr="inner:volume_info">',
 28                     '</div>',
 29                 '</div>',
 30 
 31                 '<div class="we_cell_form_box" attr="inner:transfer_form"></div>',
 32 
 33             '</li>',
 34         '</ul>',
 35     '</div>',
 36     ].join('');
 37 
 38     var _btmTips = [
 39     '1.快速划账功能仅支持在手机账号管理中绑定账号间的点券转账<br>',
 40     '2.今日可划账额度<b>$maxTransferAmount$</b>点,已使用<b class="we_red_dark">$alreadyTransferAmount$</b>点,剩余额度<b class="we_green">$remainTransferAmount$</b>点'
 41     ].join('');
 42 
 43     var api = {
 44       /**
 45        * 获取帐户列表
 46        * @param  {[type]} sdid [description]
 47        * @return {[type]}      [description]
 48        */
 49       groupList:function(sdid,handler){
 50         // handler(
 51         //   [{"email":null,"alias":"qqqqwssxs","sdid":"3153265953"},{"email":null,"alias":"leotest1003","sdid":"3153292848"},{"email":null,"alias":"leotest1004","sdid":"3153308765"},{"email":null,"alias":"leotest1005","sdid":"3153312918"},{"email":null,"alias":"","sdid":"3154830272"}]
 52         //   );
 53         // return ;
 54         var url = $we.conf.ENV.__API + "/AjaxBill/GroupList";
 55         $we.utils.request(url, {sdid:sdid}, function(data) {
 56             handler(data.data);
 57         }, function(data) {
 58             $we.comp.alert(data.msg);
 59         }, "POST", true);
 60       },
 61 
 62       /**
 63        * 查询帐户余额
 64        * @param  {[type]} sdid    [description]
 65        * @param  {[type]} handler [description]
 66        * @return {[type]}         [description]
 67        */
 68       userVolume:function(sdid,handler){
 69         // handler({"volume":9999});
 70         // return;
 71         var url = $we.conf.ENV.__API + "/AjaxBill/UserVolume";
 72         $we.utils.request(url, {sdid:sdid}, function(data) {
 73             handler(data.data);
 74         }, function(data) {
 75             $we.comp.alert(data.msg);
 76         }, "POST", true);
 77       },
 78 
 79       /**
 80        * 帐户转帐
 81        * @param  {[type]} userIdDest [description]
 82        * @param  {[type]} amount     [description]
 83        * @param  {[type]} handler    [description]
 84        * @return {[type]}            [description]
 85        */
 86       accountTransfer:function(userIdDest,amount,handler){
 87         var url = $we.conf.ENV.__API + "/AjaxBill/AccountTransfer";
 88         $we.utils.request(url, {userIdDest:userIdDest,amount:amount}, function(data) {
 89             handler(data.data);
 90         }, function(data) {
 91             $we.comp.alert(data.msg);
 92         }, "POST", true);
 93       }
 94     };
 95 
 96     /**
 97      * 快速划账
 98      */
 99     $we.widget.reg("rapid_transfer", {
100         interfaces:{
101             /**
102              * 获取转入账号
103              * @return {[type]} [description]
104              */
105             getDestAccount:function(){
106               var destAlias=$('#account_selected').val();
107               var accountList = this.accountList;
108 
109               var destAccount = null;
110               $.each(accountList,function(i,account){
111                 if(account['alias']==destAlias){
112                   destAccount = account;
113                 }
114               });
115               return destAccount;
116             },
117 
118             /**
119              * render函数,AAA的接口函数,对于业务流组件来说,它完全可以转变为一个内置函数,
120              * 业务流组件在大多数情况下,不会调用interfaces
121              */
122             render:function ()
123             {
124                 var t = this;
125                 //当前SDID
126                 var sdid = $we.process.getData('sdid');
127                 //转出账号
128                 var outAccount = $we.process.getData('alias');
129                 //转出账号余额
130                 var outAccountAmount = $we.process.getData('volume');
131                 //最高可转金额
132                 var maxTransferAmount = $we.process.getData('group_transfer_upper');
133                 //剩余额度
134                 var remainTransferAmount = $we.process.getData('group_transfer_quota');
135                 //已转金额
136                 var alreadyTransferAmount = maxTransferAmount - remainTransferAmount;
137 
138                 this.append(this.el, _html,{outAccount:outAccount,outAccountAmount:outAccountAmount});
139 
140                 //组装帐户列表
141                 api.groupList(sdid,function(list){
142                   t.accountList = list;
143                   var ds =[];
144                   $.each(list,function(i,l){
145                     ds.push(l['alias']);
146                   });
147                   $we.widget.add('Autocomplete',$('#account_selected'),
148                     {dropDown:true,ds:ds,onAfterSet:function(s){
149                       var volumeInfo = $(t.node['volume_info']);
150                       volumeInfo.hide();
151 
152                       var destAccount =t.getDestAccount();
153                       if(destAccount){
154                         api.userVolume(destAccount['sdid'],function(data){
155                           var volume = data['volume'];
156                           volumeInfo.html('<label>点券余额:</label><span>'+volume+'</span>');
157                           volumeInfo.show();
158                         });
159                       }
160                     }});
161                 });
162 
163                 //转帐表单
164                 this.transferForm = $we.widget.add("Form", $(this.node['transfer_form']), {
165                     form_elements:[
166                       {label:'转入点券',name:'transferAmount',id:'transferAmount',
167                       events:'blur,focus',require:true,
168                       expression:function(ele){
169                         var value = ele.val();
170                         if (value.length > 0) {
171                             if (!/(^-?\d\d*$)/.test(value)) {
172                                 return {valid:false, error_msg:"必须输入整数!"};
173                             }
174 
175                             value = parseInt(value);
176                             if(value > outAccountAmount){
177                                 return {valid:false, error_msg:"已超出账号点券余额!"}; 
178                             }
179                             if(value > remainTransferAmount){
180                                 return {valid:false, error_msg:"已超出今日剩余可划账额度!"}; 
181                             }
182 
183                             return {valid:true};
184                         }else{
185                           return {valid:true};
186                         }
187                       }}
188                     ],
189                     commit:function(){
190                         $we.comp.alert("提交表单喽");
191                     }
192                 });
193 
194                 this.notify("renderBottom", $we.string.replaceTmpl(_btmTips,{maxTransferAmount:maxTransferAmount,alreadyTransferAmount:alreadyTransferAmount,remainTransferAmount:remainTransferAmount}));
195                 this.notify("setButton", {cancel:true});
196             }
197         },
198         /**
199          * 初始化函数
200          * @param  {element} el    通过业务流组件传递过来的参数,该业务流组件的business节点
201          * @param  {object} params 初始化业务流组件时,每个组件传递的参数
202          */
203         init:function (el, params)
204         {
205             this.el = el;
206             this.params = params;
207         },
208         /**
209          * 每个组件,如果需要使用业务流的话,需要声明这个
210          */
211         process:{
212             /**
213              * 组件开始调用,业务流组件会调用这个接口来启用组件
214              */
215             start:function ()
216             {
217                 this.render();
218             },
219             /**
220              * 组件结束调用,当这个业务结束的时候,业务流组件会调用它
221              */
222             end:function ()
223             {
224                 $(this.el).empty();
225             },
226             /**
227              * 组件检测是否可以进行下一步
228              * 当组件需要进入下一步时,业务流组件将会调用这个接口,
229              * 这个接口需要通过 this.notify("goNext") 来通知业务流组件它检测通过,
230              * 否则业务流组件在这个地方会停滞下来 通过 this.notify("pause")来通知
231              */
232             checkSucc:function ()
233             {
234                 var t = this;
235                 var destAccount = this.getDestAccount();
236                 if(destAccount==null){
237                   $we.comp.alert('请选择转入账号!');
238                   return;
239                 }
240                 var valid = this.transferForm.valid();
241                 if(valid){
242                   var transferAmount = this.transferForm.getValue("transferAmount");
243                   var confirmMsg = "确认从账号"+$we.process.getData('alias')+"转出"+transferAmount+"点券给账号"+destAccount['alias']+"吗?";
244                   $we.comp.confirm(confirmMsg,function(){
245                     api.accountTransfer(destAccount['sdid'],transferAmount,function(){
246                       t.notify("goNext");
247                     });
248                   },function(){});
249                 }
250             }
251         },
252         events:{
253         }
254     });
255 
256 });