人人商城

trade.js 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. define(['bootstrap'], function($){
  2. var trade = {};
  3. var reg_credit = /^[+-]?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/i;
  4. var reg_int = /^[0-9]\d*$/i;
  5. trade.init = function() {
  6. $('.modal-trade-credit1, .modal-trade-credit2, .modal-trade-consume, .modal-trade-card, .modal-trade-cardsn, .modal-trade-cardconsume').on('click', function(){
  7. $('#consume-Modal, #credit-Modal, #card-Modal, #card-edit-Modal, #group-Modal, #cardconsume-Modal').remove();
  8. var type = $(this).data('type');
  9. var uid = parseInt($(this).data('uid'));
  10. if(type == 'consume') {
  11. trade.consume(uid);
  12. } else if (type == 'credit1' || type == 'credit2') {
  13. trade.credit(type, uid);
  14. } else if (type == 'card') {
  15. trade.card(uid);
  16. } else if (type == 'cardsn') {
  17. trade.card_edit(uid);
  18. } else if (type == 'cardconsume') {
  19. trade.cardconsume();
  20. }
  21. });
  22. };
  23. trade.consume = function(uid) {
  24. var html = '<div class="modal fade" id="consume-Modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">'+
  25. ' <div class="modal-dialog modal-lg" role="document">'+
  26. ' <div class="modal-content">'+
  27. ' <form class="table-responsive form-inline" method="post" action="" id="form-consume">'+
  28. ' <div class="modal-header">'+
  29. ' <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+
  30. ' <h4 class="modal-title" id="myModalLabel">会员消费</h4>'+
  31. ' </div>'+
  32. ' <div class="modal-body">'+
  33. ' <table class="table table-hover table-bordered">'+
  34. ' <tr>'+
  35. ' <th width="150">'+
  36. ' <select name="type" id="type" class="form-control">'+
  37. ' <option value="mobile">手机号</option>'+
  38. ' <option value="uid">会员UID</option>'+
  39. ' </select>'+
  40. ' </th>'+
  41. ' <td>'+
  42. ' <div class="form-group">'+
  43. ' <input type="text" value="" name="username" id="username" class="form-control"/>'+
  44. ' </div>'+
  45. ' </td>'+
  46. ' </tr>'+
  47. ' <tr id="user" style="display: none">'+
  48. ' <th>会员信息</th>'+
  49. ' <td>'+
  50. ' <div class="form-group">'+
  51. ' <div class="input-group">'+
  52. ' <strong class="form-control-static"></strong>'+
  53. ' </div>'+
  54. ' </div>'+
  55. ' </td>'+
  56. ' </tr>'+
  57. ' <tr>'+
  58. ' <th>消费总计</th>'+
  59. ' <td>'+
  60. ' <div class="form-group">'+
  61. ' <div class="input-group">'+
  62. ' <input type="text" value="" name="total" id="total" class="form-control"/>'+
  63. ' <span class="input-group-addon">元</span>'+
  64. ' </div>'+
  65. ' </div>'+
  66. ' </td>'+
  67. ' </tr>'+
  68. ' <tr>'+
  69. ' <th>实收金额</th>'+
  70. ' <td>'+
  71. ' <div class="input-group">'+
  72. ' <input type="text" value="" name="money" id="money" class="form-control" readonly/>'+
  73. ' <span class="input-group-addon">元</span>'+
  74. ' </div>'+
  75. ' </td>'+
  76. ' </tr>'+
  77. ' <tr>'+
  78. ' <th rowspan="3">支付方式</th>'+
  79. ' <td>'+
  80. ' <label class="checkbox-inline">'+
  81. ' <input type="checkbox" name="is_credit2_pay" id="is_credit2_pay" value="1" /> 余额支付'+
  82. ' </label>'+
  83. ' <div class="form-group">'+
  84. ' <div class="input-group">'+
  85. ' <input type="text" value="0" name="credit2" id="credit2" disabled class="form-control"/>'+
  86. ' <span class="input-group-addon">元</span>'+
  87. ' </div>'+
  88. ' </div>'+
  89. ' </td>'+
  90. ' </tr>'+
  91. ' <tr>'+
  92. ' <td>'+
  93. ' <label class="checkbox-inline">'+
  94. ' <input type="checkbox" name="is_credit1_pay" id="is_credit1_pay" value="1" /> 积分抵现'+
  95. ' </label>'+
  96. ' <div class="form-group">'+
  97. ' <div class="input-group">'+
  98. ' <input type="text" value="0" name="credit1" id="credit1" disabled class="form-control"/>'+
  99. ' <span class="input-group-addon">积分 抵消</span>'+
  100. ' <input type="text" value="0" name="offset_money" id="offset_money" disabled class="form-control"/>'+
  101. ' <span class="input-group-addon">元</span>'+
  102. ' </div>'+
  103. ' </div>'+
  104. ' </td>'+
  105. ' </tr>'+
  106. ' <tr>'+
  107. ' <td>'+
  108. ' <label class="checkbox-inline">'+
  109. ' <input type="checkbox" value="1" name="is_cash_pay" id="is_cash_pay" /> 现金支付'+
  110. ' </label>'+
  111. ' <div class="form-group">'+
  112. ' <div class="input-group">'+
  113. ' <input type="text" value="0" name="cash" id="cash" disabled class="form-control"/>'+
  114. ' <span class="input-group-addon">元  找零</span>'+
  115. ' <input type="text" value="0" name="return_cash" id="return_cash" disabled class="form-control"/>'+
  116. ' <span class="input-group-addon">元</span>'+
  117. ' </div>'+
  118. ' </div>'+
  119. ' </td>'+
  120. ' </tr>'+
  121. ' <tr>'+
  122. ' <th>备注</th>'+
  123. ' <td>'+
  124. ' <textarea name="remark" class="form-control" cols="81"></textarea>'+
  125. ' </td>'+
  126. ' </tr>'+
  127. ' </table>'+
  128. ' </div>'+
  129. ' <div class="modal-footer">'+
  130. ' <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>'+
  131. ' <input type="button" class="btn btn-primary" id="submit-form" value="提交">'+
  132. ' </div>'+
  133. ' </form>'+
  134. ' </div>'+
  135. ' </div>'+
  136. '</div>';
  137. require(['validator'], function($){
  138. $('#consume-Modal').remove();
  139. $(document.body).append(html);
  140. var dialog = $('#consume-Modal');
  141. dialog.modal('show');
  142. $('#username').focus();
  143. $('#form-consume').bootstrapValidator({
  144. fields: {
  145. username: {
  146. validators: {
  147. notEmpty: {
  148. message: '请填写用户手机或UID'
  149. }
  150. }
  151. },
  152. total: {
  153. validators: {
  154. notEmpty: {
  155. message: '请填写消费总计'
  156. },
  157. regexp: {
  158. regexp: reg_credit,
  159. message: '最多只保留小数点后2位的正数'
  160. }
  161. }
  162. },
  163. credit2: {
  164. validators: {
  165. notEmpty: {
  166. message: '请填写余额支付金额'
  167. },
  168. regexp: {
  169. regexp: reg_credit,
  170. message: '最多只保留小数点后2位的正数'
  171. },
  172. between: {
  173. min: 0,
  174. max: 100000000,
  175. money: 0,
  176. message : '超出可用余额'
  177. }
  178. }
  179. },
  180. credit1: {
  181. validators: {
  182. notEmpty: {
  183. message: '请填写积分数量'
  184. },
  185. regexp: {
  186. regexp: reg_int,
  187. message: '积分数量只能是正整数'
  188. },
  189. between: {
  190. min: 0,
  191. max: 1000000000,
  192. message : '超出会员所拥有的积分或超出最多可抵消金额'
  193. }
  194. }
  195. },
  196. cash: {
  197. validators: {
  198. notEmpty: {
  199. message: '请填写现金支付金额'
  200. },
  201. regexp: {
  202. regexp: reg_credit,
  203. message: '最多只保留小数点后2位的正数'
  204. }
  205. }
  206. }
  207. }
  208. });
  209. var Validator = $('#form-consume').data('bootstrapValidator');
  210. var consume = {
  211. 'user': {},
  212. 'card': {},
  213. 'total': 0,
  214. 'money': 0,
  215. 'last_money': 0,
  216. 'return_cash':0,
  217. 'is_credit1_pay': false,
  218. 'is_credit2_pay': false,
  219. 'is_cash_pay': false,
  220. 'credit1': 0,
  221. 'credit2': 0,
  222. 'cash': 0,
  223. 'offset_money': 0,
  224. 'password': '',
  225. 'init': function(uid) {
  226. Validator.enableFieldValidators('credit2');
  227. Validator.enableFieldValidators('credit1');
  228. Validator.enableFieldValidators('cash');
  229. this.getuser();
  230. if(uid > 0) {
  231. $('#type').val('uid');
  232. $('#username').val(uid);
  233. $('#username').trigger('blur');
  234. $('#username, #type').attr('disabled', true);
  235. }
  236. this.initmoney();
  237. this.initconsume();
  238. this.submit();
  239. },
  240. 'checkuser': function() {
  241. if(this.user.uid == undefined || !this.user.uid) {
  242. this.user = {};
  243. this.card = {};
  244. $('#username').focus();
  245. Validator.updateMessage('username', 'notEmpty', '未找到对应会员');
  246. Validator.updateStatus('username', 'INVALID', 'notEmpty');
  247. //util.message('未找到对应会员');
  248. return false;
  249. }
  250. return true;
  251. },
  252. 'getuser': function(){
  253. var _this = this;
  254. $('#username').blur(function(){
  255. Validator.validateField('username');
  256. var username = $.trim($('#username').val());
  257. var type = $('#type').val();
  258. if(username) {
  259. $.post('./index.php?c=mc&a=trade&do=user&', {'type':type, 'username':username}, function(data){
  260. var data = $.parseJSON(data);
  261. if(data.error != 'none') {
  262. _this.user = {};
  263. _this.card = {};
  264. $('#user').hide();
  265. Validator.updateMessage('username', 'notEmpty', data.message);
  266. Validator.updateStatus('username', 'INVALID', 'notEmpty');
  267. return false;
  268. } else {
  269. _this.user = data.user;
  270. _this.card = data.card;
  271. $('#user strong').html(data.html);
  272. $('#user').show();
  273. }
  274. });
  275. }
  276. });
  277. },
  278. 'initmoney': function() {
  279. var _this = this;
  280. $('#total').blur(function(){
  281. if(!_this.checkuser()) {
  282. return false;
  283. }
  284. var total = parseFloat($(this).val());
  285. _this.total = total;
  286. money = total;
  287. if(_this.user.discount && total >= _this.user.discount.condition) {
  288. if(_this.card.discount_type == 1) {
  289. money = total - _this.user.discount.discount;
  290. } else {
  291. if(!_this.user.discount.discount) {
  292. _this.user.discount.discount = 1;
  293. }
  294. money = total * _this.user.discount.discount;
  295. money = money.toFixed(1);
  296. }
  297. if(money < 0) {
  298. money = 0;
  299. }
  300. }
  301. $('#money').val(money);
  302. _this.money = money;
  303. _this.last_money = money;
  304. });
  305. },
  306. 'initcredit1': function() {
  307. var _this = this;
  308. $('#credit1').keyup(function(){
  309. if(!_this.checkuser()) {
  310. return false;
  311. }
  312. _this.credit1 = parseInt($(this).val());
  313. if(isNaN(_this.credit1)) {
  314. _this.credit1 = 0;
  315. }
  316. if(_this.credit1 >= 0 && _this.card.offset_rate > 0 && _this.card.offset_max > 0) {
  317. $('#offset_money').val(_this.credit1 / _this.card.offset_rate);
  318. _this.offset_money = _this.credit1/_this.card.offset_rate;
  319. }
  320. var credit1 = parseInt($(this).val());
  321. if(_this.user.credit1 < credit1) {
  322. Validator.updateOption('credit1', 'between', 'max', _this.user.credit1);
  323. Validator.updateMessage('credit1', 'between', '超出会员账户可用积分');
  324. Validator.updateStatus('credit1', 'INVALID', 'between');
  325. } else if(_this.card && (_this.card.offset_rate > 0) && (_this.card.offset_max > 0) && (credit1 > (_this.card.offset_rate * _this.card.offset_max))) {
  326. Validator.updateOption('credit1', 'between', 'max', _this.card.offset_rate * _this.card.offset_max);
  327. Validator.updateMessage('credit1', 'between', '积分最多可抵消'+_this.card.offset_max);
  328. Validator.updateStatus('credit1', 'INVALID', 'between');
  329. }
  330. _this.updatereturn();
  331. });
  332. },
  333. 'initcredit2': function() {
  334. var _this = this;
  335. $('#credit2').keyup(function(){
  336. if(!_this.checkuser()) {
  337. return false;
  338. }
  339. var money = $('#money').val();
  340. if(money > 0) {
  341. var credit2 = parseFloat($('#credit2').val());
  342. if(_this.user.credit2 < credit2) {
  343. Validator.updateOption('credit2', 'between', 'max', _this.user.credit2);
  344. Validator.updateMessage('credit2', 'between', '超出会员账户可用余额');
  345. Validator.updateStatus('credit2', 'INVALID', 'between');
  346. } else if(credit2 > money) {
  347. Validator.updateOption('credit2', 'between', 'max', money);
  348. Validator.updateMessage('credit2', 'between', '使用余额不能大于应付金额');
  349. Validator.updateStatus('credit2', 'INVALID', 'between');
  350. }
  351. _this.credit2 = credit2;
  352. _this.updatereturn();
  353. }
  354. });
  355. },
  356. 'initcash': function() {
  357. var _this = this;
  358. $('#cash').keyup(function(){
  359. if(!_this.checkuser()) {
  360. return false;
  361. }
  362. var money = $('#money').val();
  363. if(money > 0) {
  364. var cash = parseFloat($('#cash').val());
  365. _this.cash = cash;
  366. _this.updatereturn();
  367. }
  368. });
  369. },
  370. 'updatereturn': function() {
  371. var _this = this;
  372. _this.return_cash = _this.money - _this.credit2 - _this.offset_money - _this.cash;
  373. if(_this.return_cash > 0) {
  374. _this.last_money = _this.return_cash;
  375. $('#return_cash').val(0);
  376. } else {
  377. _this.last_money = 0;
  378. $('#return_cash').val(Math.abs(_this.return_cash));
  379. }
  380. },
  381. 'updatecredit1': function(){
  382. var _this = this;
  383. if(_this.card.offset_rate > 0 && _this.card.offset_max > 0 && _this.last_money > 0) {
  384. var min = Math.min.apply(null, [_this.user.credit1, _this.card.offset_rate * _this.card.offset_max, _this.card.offset_rate * _this.last_money]);
  385. $('#credit1').val(min).focus().select();
  386. $('#offset_money').val(min/_this.card.offset_rate);
  387. _this.credit1 = min;
  388. _this.offset_money = min/_this.card.offset_rate;
  389. }
  390. },
  391. 'updatecredit2': function(){
  392. var _this = this;
  393. var min = Math.min(_this.user.credit2, _this.last_money);
  394. $('#credit2').val(min);
  395. _this.credit2 = min;
  396. },
  397. 'updatecash': function(){
  398. var _this = this;
  399. $('#cash').val(_this.last_money);
  400. _this.cash = _this.last_money;
  401. },
  402. 'initconsume': function() {
  403. var _this = this;
  404. _this.is_credit1_pay = $('#is_credit1_pay').prop('checked');
  405. _this.is_credit2_pay = $('#is_credit2_pay').prop('checked');
  406. _this.is_cash_pay = $('#is_cash_pay').prop('checked');
  407. _this.credit1 = parseInt($('#credit1').val());
  408. _this.credit2 = parseFloat($('#credit2').val());
  409. _this.cash = parseFloat($('#cash').val());
  410. _this.initcredit1();
  411. _this.initcredit2();
  412. _this.initcash();
  413. $('#is_credit1_pay').click(function(){
  414. if(!_this.checkuser()) {
  415. return false;
  416. }
  417. if(!_this.money) {
  418. Validator.updateStatus('total', 'INVALID', 'regexp');
  419. $('#total').focus().select();
  420. return false;
  421. }
  422. if(!$(this).prop('checked')) {
  423. //如果是非选中状态
  424. Validator.updateStatus('credit1', 'VALID');
  425. $('#credit1').attr('disabled', true);
  426. Validator.enableFieldValidators('credit1', false);
  427. $('#credit1').val(0);
  428. $('#offset_money').val(0);
  429. $(this).prop('checked', false);
  430. _this.is_credit1_pay = false;
  431. _this.credit1 = 0;
  432. _this.offset_money = 0;
  433. } else if(_this.last_money >= 0) {
  434. if(_this.last_money == 0) {
  435. return false;
  436. }
  437. _this.is_credit1_pay = true;
  438. $('#credit1').removeAttr('disabled');
  439. Validator.enableFieldValidators('credit1', true);
  440. _this.updatecredit1();
  441. }
  442. _this.updatereturn();
  443. });
  444. $('#is_credit2_pay').click(function(){
  445. if(!_this.checkuser()) {
  446. return false;
  447. }
  448. if(!_this.money) {
  449. Validator.updateStatus('total', 'INVALID', 'regexp');
  450. $('#total').focus().select();
  451. return false;
  452. }
  453. if(!$(this).prop('checked')) {
  454. //如果是非选中状态
  455. Validator.updateStatus('credit2', 'VALID');
  456. $('#credit2').attr('disabled', true);
  457. Validator.enableFieldValidators('credit2', false);
  458. $('#credit2').val(0);
  459. $(this).prop('checked', false);
  460. _this.is_credit2_pay = false;
  461. _this.credit2 = 0;
  462. } else if(_this.last_money >= 0) {
  463. if(_this.last_money == 0) {
  464. return false;
  465. }
  466. $('#credit2').removeAttr('disabled');
  467. Validator.enableFieldValidators('credit2', true);
  468. _this.is_credit2_pay = true;
  469. _this.updatecredit2();
  470. }
  471. _this.updatereturn();
  472. });
  473. $('#is_cash_pay').click(function(){
  474. if(!_this.checkuser()) {
  475. return false;
  476. }
  477. if(!_this.money) {
  478. Validator.updateStatus('total', 'INVALID', 'regexp');
  479. $('#total').focus().select();
  480. return false;
  481. }
  482. if(!$(this).prop('checked')) {
  483. //如果是非选中状态
  484. Validator.updateStatus('cash', 'VALID');
  485. $('#cash').attr('disabled', true);
  486. Validator.enableFieldValidators('cash', false);
  487. $('#cash').val(0);
  488. $(this).prop('checked', false);
  489. _this.is_cash_pay = false;
  490. _this.cash = 0;
  491. } else if(_this.last_money >= 0) {
  492. if(_this.last_money == 0) {
  493. return false;
  494. }
  495. $('#cash').removeAttr('disabled');
  496. Validator.enableFieldValidators('cash', true);
  497. _this.is_cash_pay = true;
  498. _this.updatecash();
  499. }
  500. _this.updatereturn();
  501. });
  502. },
  503. 'submit': function() {
  504. var _this = this;
  505. _this.updatereturn();
  506. $('#form-consume #submit-form').click(function(){
  507. Validator.validate();
  508. if(Validator.isValid()) {
  509. if(_this.last_money > 0) {
  510. util.message('支付金额小于实收金额,请检查表单');
  511. return false;
  512. }
  513. _this.password = $.trim($(':input[name="password"]').val());
  514. _this.remark = $.trim($('textarea[name="remark"]').val());
  515. var param = {
  516. 'uid': _this.user.uid,
  517. 'total': _this.total,
  518. 'money': _this.money,
  519. 'credit2': _this.credit2,
  520. 'credit1': _this.credit1,
  521. 'cash': _this.cash,
  522. 'offset_money': _this.offset_money,
  523. 'return_cash': _this.return_cash,
  524. 'password': _this.password,
  525. 'remark': _this.remark
  526. }
  527. $.post('./index.php?c=mc&a=trade&do=consume', param, function(data){
  528. if(data != 'success') {
  529. util.message(data, '', 'error');
  530. return false;
  531. } else {
  532. dialog.modal('hide');
  533. util.message('交易成功', 'refresh', 'success');
  534. return false;
  535. }
  536. });
  537. }
  538. });
  539. }
  540. };
  541. consume.init(uid);
  542. });
  543. };
  544. trade.credit = function(type, uid) {
  545. var types = {'credit1': '积分', 'credit2': '余额'};
  546. var html = '<div class="modal fade" id="credit-Modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">'+
  547. ' <div class="modal-dialog modal-lg" role="document">'+
  548. ' <div class="modal-content">'+
  549. ' <form class="table-responsive form-inline" method="post" action="" id="form-credit">'+
  550. ' <div class="modal-header">'+
  551. ' <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+
  552. ' <h4 class="modal-title" id="myModalLabel">会员'+types[type]+'操作</h4>'+
  553. ' </div>'+
  554. ' <div class="modal-body">'+
  555. ' <table class="table table-hover table-bordered">'+
  556. ' <tr>'+
  557. ' <th width="150">'+
  558. ' <select name="type" id="type" class="form-control">'+
  559. ' <option value="mobile">手机号</option>'+
  560. ' <option value="uid">会员UID</option>'+
  561. ' </select>'+
  562. ' </th>'+
  563. ' <td>'+
  564. ' <div class="form-group">'+
  565. ' <input type="text" value="" name="username" id="username" class="form-control"/>'+
  566. ' </div>'+
  567. ' </td>'+
  568. ' </tr>'+
  569. ' <tr id="user" style="display: none">'+
  570. ' <th>会员信息</th>'+
  571. ' <td>'+
  572. ' <div class="form-group">'+
  573. ' <div class="input-group">'+
  574. ' <strong class="form-control-static"></strong>'+
  575. ' </div>'+
  576. ' </div>'+
  577. ' </td>'+
  578. ' </tr>'+
  579. ' <tr>'+
  580. ' <th>修改'+types[type]+'(增减)</th>'+
  581. ' <td>'+
  582. ' <div class="form-group">'+
  583. ' <input type="text" value="" name="num" id="total" class="form-control"/>'+
  584. ' <div class="help-block">输入500,则标识增加500;输入-500则表示减少500</div>'+
  585. ' </div>'+
  586. ' </td>'+
  587. ' </tr>'+
  588. ' <tr>'+
  589. ' <th>备注</th>'+
  590. ' <td>'+
  591. ' <textarea name="remark" class="form-control" cols="81"></textarea>'+
  592. ' </td>'+
  593. ' </tr>'+
  594. ' </table>'+
  595. ' </div>'+
  596. ' <div class="modal-footer">'+
  597. ' <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>'+
  598. ' <input type="submit" class="btn btn-primary" id="submit" name="提交"value="提交">'+
  599. ' </div>'+
  600. ' </form>'+
  601. ' </div>'+
  602. ' </div>'+
  603. '</div>';
  604. require(['validator'], function($){
  605. $('#credit-Modal').remove();
  606. $(document.body).append(html);
  607. var dialog = $('#credit-Modal');
  608. dialog.modal('show');
  609. $('#form-credit').bootstrapValidator({
  610. fields: {
  611. username: {
  612. validators: {
  613. notEmpty: {
  614. message: '请填写用户手机或UID'
  615. }
  616. }
  617. },
  618. num: {
  619. validators: {
  620. notEmpty: {
  621. message: '请填写数量'
  622. },
  623. regexp: {
  624. regexp: /^[+-]?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/i,
  625. message: '最多只保留小数点后2位的正数'
  626. }
  627. }
  628. }
  629. }
  630. });
  631. var Validator = $('#form-credit').data('bootstrapValidator');
  632. var credit = {
  633. 'user': {},
  634. 'card': {},
  635. 'init': function(type, uid) {
  636. this.type = type,
  637. this.getuser();
  638. if(uid > 0) {
  639. $('#type').val('uid');
  640. $('#username').val(uid);
  641. $('#username').trigger('blur');
  642. $('#username, #type').attr('disabled', true);
  643. }
  644. this.submit();
  645. },
  646. getuser: function() {
  647. var _this = this;
  648. $('#username').blur(function(){
  649. Validator.validateField('username');
  650. var username = $.trim($('#username').val());
  651. var type = $('#type').val();
  652. if(username) {
  653. $.post('./index.php?c=mc&a=trade&do=user&', {'type':type, 'username':username}, function(data){
  654. var data = $.parseJSON(data);
  655. if(data.error != 'none') {
  656. _this.user = {};
  657. _this.card = {};
  658. $('#user').hide();
  659. Validator.updateMessage('username', 'notEmpty', data.message);
  660. Validator.updateStatus('username', 'INVALID', 'notEmpty');
  661. return false;
  662. } else {
  663. _this.user = data.user;
  664. _this.card = data.card;
  665. $('#user strong').html(data.html);
  666. $('#user').show();
  667. }
  668. });
  669. }
  670. });
  671. },
  672. 'checkuser': function() {
  673. if(this.user.uid == undefined || !this.user.uid) {
  674. this.user = {};
  675. this.card = {};
  676. $('#username').focus();
  677. Validator.updateMessage('username', 'notEmpty', '未找到对应会员');
  678. Validator.updateStatus('username', 'INVALID', 'notEmpty');
  679. //util.message('未找到对应会员');
  680. return false;
  681. }
  682. return true;
  683. },
  684. 'submit': function() {
  685. var _this = this;
  686. $('#form-credit .btn-primary').click(function(){
  687. _this.checkuser();
  688. Validator.validate();
  689. if(Validator.isValid()) {
  690. _this.password = $.trim($(':input[name="password"]').val());
  691. _this.num = $.trim($(':text[name="num"]').val());
  692. _this.remark = $.trim($('textarea[name="remark"]').val());
  693. var param = {
  694. 'uid': _this.user.uid,
  695. 'num': _this.num,
  696. 'password': _this.password,
  697. 'remark': _this.remark,
  698. 'type': _this.type
  699. }
  700. $.post('./index.php?c=mc&a=trade&do=credit', param, function(data){
  701. if(data != 'success') {
  702. util.message(data, '', 'error');
  703. return false;
  704. } else {
  705. dialog.modal('hide');
  706. util.message('操作成功', 'refresh', 'success');
  707. return false;
  708. }
  709. });
  710. }
  711. });
  712. }
  713. };
  714. credit.init(type, uid);
  715. });
  716. };
  717. trade.card = function(uid) {
  718. var html = '<div class="modal fade" id="card-Modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">'+
  719. ' <div class="modal-dialog modal-lg" role="document">'+
  720. ' <div class="modal-content">'+
  721. ' <form class="table-responsive form-inline" method="post" action="" id="form-card">'+
  722. ' <div class="modal-header">'+
  723. ' <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+
  724. ' <h4 class="modal-title" id="myModalLabel">发放会员卡</h4>'+
  725. ' </div>'+
  726. ' <div class="modal-body">'+
  727. ' <table class="table table-hover table-bordered">'+
  728. ' <tr>'+
  729. ' <th width="150">'+
  730. ' <select name="type" id="type" class="form-control">'+
  731. ' <option value="mobile">手机号</option>'+
  732. ' <option value="uid">会员UID</option>'+
  733. ' </select>'+
  734. ' </th>'+
  735. ' <td>'+
  736. ' <div class="form-group">'+
  737. ' <input type="text" value="" name="username" id="username" class="form-control"/>'+
  738. ' </div>'+
  739. ' </td>'+
  740. ' </tr>'+
  741. ' <tr id="user" style="display: none">'+
  742. ' <th>会员信息</th>'+
  743. ' <td>'+
  744. ' <div class="form-group">'+
  745. ' <div class="input-group">'+
  746. ' <strong class="form-control-static"></strong>'+
  747. ' </div>'+
  748. ' </div>'+
  749. ' </td>'+
  750. ' </tr>'+
  751. ' </table>'+
  752. ' </div>'+
  753. ' <div class="modal-footer">'+
  754. ' <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>'+
  755. ' <input type="submit" class="btn btn-primary" id="submit" name="提交"value="提交">'+
  756. ' </div>'+
  757. ' </form>'+
  758. ' </div>'+
  759. ' </div>'+
  760. '</div>';
  761. require(['validator'], function($){
  762. $('#card-Modal').remove();
  763. $(document.body).append(html);
  764. var dialog = $('#card-Modal');
  765. dialog.modal('show');
  766. $('#form-card').bootstrapValidator({
  767. fields: {
  768. username: {
  769. validators: {
  770. notEmpty: {
  771. message: '请填写用户手机或UID'
  772. }
  773. }
  774. }
  775. }
  776. });
  777. var Validator = $('#form-card').data('bootstrapValidator');
  778. var card = {
  779. 'user': {},
  780. 'card': {},
  781. 'init': function(uid) {
  782. this.getuser();
  783. if(uid > 0) {
  784. $('#type').val('uid');
  785. $('#username').val(uid);
  786. $('#username').trigger('blur');
  787. if(this.user.uid > 0) {
  788. $('#username, #type').attr('disabled', true);
  789. }
  790. }
  791. this.submit();
  792. },
  793. getuser: function() {
  794. var _this = this;
  795. $('#username').blur(function(){
  796. Validator.validateField('username');
  797. var username = $.trim($('#username').val());
  798. var type = $('#type').val();
  799. if(username) {
  800. $.post('./index.php?c=mc&a=trade&do=user&', {'type':type, 'username':username}, function(data){
  801. var data = $.parseJSON(data);
  802. if(data.error != 'none') {
  803. _this.user = {};
  804. _this.card = {};
  805. $('#user').hide();
  806. Validator.updateMessage('username', 'notEmpty', data.message);
  807. Validator.updateStatus('username', 'INVALID', 'notEmpty');
  808. return false;
  809. } else {
  810. _this.user = data.user;
  811. _this.card = data.card;
  812. $('#user strong').html(data.html);
  813. $('#user').show();
  814. }
  815. });
  816. }
  817. });
  818. },
  819. 'checkuser': function() {
  820. if(this.user.uid == undefined || !this.user.uid) {
  821. this.user = {};
  822. this.card = {};
  823. $('#username').focus();
  824. Validator.updateMessage('username', 'notEmpty', '未找到对应会员');
  825. Validator.updateStatus('username', 'INVALID', 'notEmpty');
  826. return false;
  827. }
  828. if(this.card.id > 0) {
  829. util.message('该会员已领取会员卡');
  830. return false;
  831. }
  832. return true;
  833. },
  834. 'submit': function() {
  835. var _this = this;
  836. $('#form-card .btn-primary').click(function(){
  837. _this.checkuser();
  838. Validator.validate();
  839. if(Validator.isValid()) {
  840. _this.password = $.trim($(':input[name="password"]').val());
  841. _this.username = $.trim($('#username').val());
  842. var param = {
  843. 'uid': _this.user.uid,
  844. 'password': _this.password,
  845. 'username': _this.username
  846. }
  847. $.post('./index.php?c=mc&a=trade&do=card', param, function(data){
  848. if(data != 'success') {
  849. util.message(data, '', 'error');
  850. return false;
  851. } else {
  852. dialog.modal('hide');
  853. util.message('发放会员卡成功', 'refresh', 'success');
  854. return false;
  855. }
  856. });
  857. }
  858. });
  859. }
  860. };
  861. card.init(uid);
  862. });
  863. }
  864. trade.cardconsume = function() {
  865. var html = '<div class="modal fade" id="card-Modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">'+
  866. ' <div class="modal-dialog modal-lg" role="document">'+
  867. ' <div class="modal-content">'+
  868. ' <form class="table-responsive form-inline" method="post" action="" id="form-card">'+
  869. ' <div class="modal-header">'+
  870. ' <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+
  871. ' <h4 class="modal-title" id="myModalLabel">卡券核销</h4>'+
  872. ' </div>'+
  873. ' <div class="modal-body">'+
  874. ' <table class="table table-hover table-bordered">'+
  875. ' <tr height="100">'+
  876. ' <th width="150">'+
  877. ' 卡券code码'+
  878. ' </th>'+
  879. ' <td>'+
  880. ' <div class="form-group">'+
  881. ' <input type="text" value="" name="code" id="code" class="form-control"/>'+
  882. ' <div class="help-block">请输入卡券code码</div>'+
  883. ' </div>'+
  884. ' </div>'+
  885. ' </td>'+
  886. ' </tr>'+
  887. ' </table>'+
  888. ' </div>'+
  889. ' <div class="modal-footer">'+
  890. ' <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>'+
  891. ' <input type="submit" class="btn btn-primary" id="submit" name="核销"value="核销">'+
  892. ' </div>'+
  893. ' </form>'+
  894. ' </div>'+
  895. ' </div>'+
  896. '</div>';
  897. require(['validator'], function($){
  898. $('#card-Modal').remove();
  899. $(document.body).append(html);
  900. var dialog = $('#card-Modal');
  901. dialog.modal('show');
  902. $('#card-Modal').on('shown.bs.modal', function () {
  903. $('#code').focus();
  904. });
  905. $('#form-card').bootstrapValidator({
  906. fields: {
  907. code: {
  908. validators: {
  909. notEmpty: {
  910. message: '请填写卡券code码'
  911. }
  912. }
  913. }
  914. }
  915. });
  916. var Validator = $('#form-card').data('bootstrapValidator');
  917. var card = {
  918. 'user': {},
  919. 'card': {},
  920. 'init': function(uid) {
  921. if(uid > 0) {
  922. $('#type').val('uid');
  923. $('#code').val(uid);
  924. $('#code').trigger('blur');
  925. if(this.user.uid > 0) {
  926. $('#code, #type').attr('disabled', true);
  927. }
  928. }
  929. this.submit();
  930. },
  931. 'submit': function() {
  932. var _this = this;
  933. $('#form-card .btn-primary').click(function(){
  934. Validator.validate();
  935. if(Validator.isValid()) {
  936. _this.code = $.trim($(':input[name="code"]').val());
  937. var param = {
  938. 'code': _this.code,
  939. }
  940. $.post('./index.php?c=mc&a=trade&do=cardconsume', param, function(data){
  941. if(data != 'success') {
  942. util.message(data, '', 'error');
  943. return false;
  944. } else {
  945. dialog.modal('hide');
  946. util.message('核销成功', 'refresh', 'success');
  947. return false;
  948. }
  949. });
  950. }
  951. });
  952. }
  953. };
  954. card.init();
  955. });
  956. }
  957. trade.card_edit = function(uid) {
  958. var html = '<div class="modal fade" id="card-edit-Modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">'+
  959. ' <div class="modal-dialog modal-lg" role="document">'+
  960. ' <div class="modal-content">'+
  961. ' <form class="table-responsive form-inline" method="post" action="" id="form-card">'+
  962. ' <div class="modal-header">'+
  963. ' <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+
  964. ' <h4 class="modal-title" id="myModalLabel">更改会员卡号</h4>'+
  965. ' </div>'+
  966. ' <div class="modal-body">'+
  967. ' <table class="table table-hover table-bordered">'+
  968. ' <tr>'+
  969. ' <th width="150">'+
  970. ' <select name="type" id="type" class="form-control">'+
  971. ' <option value="mobile">手机号</option>'+
  972. ' <option value="uid">会员UID</option>'+
  973. ' </select>'+
  974. ' </th>'+
  975. ' <td>'+
  976. ' <div class="form-group">'+
  977. ' <input type="text" value="" name="username" id="username" class="form-control"/>'+
  978. ' </div>'+
  979. ' </td>'+
  980. ' </tr>'+
  981. ' <tr id="user" style="display: none">'+
  982. ' <th>会员信息</th>'+
  983. ' <td>'+
  984. ' <div class="form-group">'+
  985. ' <div class="input-group">'+
  986. ' <strong class="form-control-static"></strong>'+
  987. ' </div>'+
  988. ' </div>'+
  989. ' </td>'+
  990. ' </tr>'+
  991. ' <tr>'+
  992. ' <th>原卡号</th>'+
  993. ' <td>'+
  994. ' <div class="form-group">'+
  995. ' <p class="label label-danger" id="old_cardsn"></p>'+
  996. ' </div>'+
  997. ' </td>'+
  998. ' </tr>'+
  999. ' <tr>'+
  1000. ' <th>设置新卡号</th>'+
  1001. ' <td>'+
  1002. ' <div class="form-group">'+
  1003. ' <input type="text" name="cardsn" id="cardsn" value="" class="form-control">'+
  1004. ' </div>'+
  1005. ' </td>'+
  1006. ' </tr>'+
  1007. ' </table>'+
  1008. ' </div>'+
  1009. ' <div class="modal-footer">'+
  1010. ' <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>'+
  1011. ' <input type="submit" class="btn btn-primary" id="submit" name="提交"value="提交">'+
  1012. ' </div>'+
  1013. ' </form>'+
  1014. ' </div>'+
  1015. ' </div>'+
  1016. '</div>';
  1017. require(['validator'], function($){
  1018. $('#card-edit-Modal').remove();
  1019. $(document.body).append(html);
  1020. var dialog = $('#card-edit-Modal');
  1021. dialog.modal('show');
  1022. $('#form-card').bootstrapValidator({
  1023. fields: {
  1024. username: {
  1025. validators: {
  1026. notEmpty: {
  1027. message: '请填写用户手机或UID'
  1028. }
  1029. }
  1030. },
  1031. cardsn: {
  1032. validators: {
  1033. notEmpty: {
  1034. message: '新卡号不能为空'
  1035. },
  1036. remote: {
  1037. url: "./index.php?c=mc&a=trade&do=cardsn&type=check",
  1038. data: function(validator) {
  1039. return {
  1040. data: validator.getFieldElements('cardsn').val()
  1041. };
  1042. },
  1043. message: '卡号已经被其他用户使用'
  1044. }
  1045. }
  1046. }
  1047. }
  1048. });
  1049. var Validator = $('#form-card').data('bootstrapValidator');
  1050. var card_edit = {
  1051. 'user': {},
  1052. 'card': {},
  1053. 'init': function(uid) {
  1054. this.getuser();
  1055. if(uid > 0) {
  1056. $('#type').val('uid');
  1057. $('#username').val(uid);
  1058. $('#username').trigger('blur');
  1059. if(this.user.uid > 0) {
  1060. $('#username, #type').attr('disabled', true);
  1061. }
  1062. }
  1063. this.submit();
  1064. },
  1065. getuser: function() {
  1066. var _this = this;
  1067. $('#username').blur(function(){
  1068. Validator.validateField('username');
  1069. var username = $.trim($('#username').val());
  1070. var type = $('#type').val();
  1071. if(username) {
  1072. $.post('./index.php?c=mc&a=trade&do=user&', {'type':type, 'username':username}, function(data){
  1073. var data = $.parseJSON(data);
  1074. if(data.error != 'none') {
  1075. _this.user = {};
  1076. _this.card = {};
  1077. $('#user').hide();
  1078. Validator.updateMessage('username', 'notEmpty', data.message);
  1079. Validator.updateStatus('username', 'INVALID', 'notEmpty');
  1080. return false;
  1081. } else {
  1082. _this.user = data.user;
  1083. _this.card = data.card;
  1084. $('#user strong').html(data.html);
  1085. $('#old_cardsn').html(data.user.cardsn);
  1086. $('#user').show();
  1087. }
  1088. });
  1089. }
  1090. });
  1091. },
  1092. 'checkuser': function() {
  1093. if(this.user.uid == undefined || !this.user.uid) {
  1094. this.user = {};
  1095. this.card = {};
  1096. $('#username').focus();
  1097. Validator.updateMessage('username', 'notEmpty', '未找到对应会员');
  1098. Validator.updateStatus('username', 'INVALID', 'notEmpty');
  1099. return false;
  1100. }
  1101. return true;
  1102. },
  1103. 'submit': function() {
  1104. var _this = this;
  1105. $('#form-card .btn-primary').click(function(){
  1106. _this.checkuser();
  1107. Validator.validate();
  1108. if(Validator.isValid()) {
  1109. var param = {
  1110. 'uid': _this.user.uid,
  1111. 'cardsn': $.trim($('#cardsn').val()),
  1112. 'password': $.trim($(':input[name="password"]').val())
  1113. }
  1114. $.post('./index.php?c=mc&a=trade&do=cardsn', param, function(data){
  1115. if(data != 'success') {
  1116. util.message(data, '', 'error');
  1117. return false;
  1118. } else {
  1119. dialog.modal('hide');
  1120. util.message('更改卡号成功', 'refresh', 'success');
  1121. return false;
  1122. }
  1123. });
  1124. }
  1125. });
  1126. }
  1127. };
  1128. card_edit.init(uid);
  1129. });
  1130. }
  1131. return trade;
  1132. });