rechargee.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. define(['$', 'template', 'IScroll', 'recharge', 'native', 'config', 'user'], function ($, template, IScroll, recharge, native, config, user) {
  2. var backHash;
  3. function back(event) {
  4. event.preventDefault();
  5. $(document).trigger('spa:navigate', {
  6. hash: backHash,
  7. pushData: {
  8. dontNeedReload: true
  9. }
  10. });
  11. }
  12. return {
  13. title: '充值规则',
  14. body: '',
  15. init: function (pageData, dtd) {
  16. /* this是什么? */
  17. var $view = this;
  18. var $doc = $(document);
  19. /* 定义按钮事件。
  20. * 为什么不是$(selector).onclick(function)呢?
  21. */
  22. // 返回
  23. $view.on('swipeRight', function (event) {
  24. back.call(this, event);
  25. // history.go(-1);
  26. });
  27. $.newTouch('.back-button', function (event) {
  28. back.call(this, event);
  29. // history.go(-1);
  30. }, $view);
  31. $view.on('tapBackButton', function (event) {
  32. back.call(this, event);
  33. // history.go(-1);
  34. });
  35. //点击展示协议 vactivity-text
  36. $.newTouch('.pay-agreement',function (event) {
  37. event.preventDefault();
  38. $doc.trigger('spa:navigate', {
  39. hash: 'activity-text',
  40. pushData: {
  41. needReload: true
  42. }
  43. });
  44. }, $view);
  45. //规则 pay-rule
  46. $.newTouch('.pay-rule',function (event) {
  47. event.preventDefault();
  48. $doc.trigger('spa:navigate', {
  49. hash: 'activity-rule',
  50. pushData: {
  51. needReload: true
  52. }
  53. });
  54. }, $view);
  55. // 样式切换
  56. $.newTouch('.media',function (event) {
  57. event.preventDefault();
  58. /* on代表这一条的选中状态。如果已经选中了,就return。this的作用域是这一条extra。 */
  59. if ($(this).hasClass('on')) return;
  60. /* 如果没有on状态,加上on。 */
  61. $(this).addClass('on');
  62. /* 删掉其他选择框的on状态 */
  63. $(this).siblings().removeClass('on');
  64. // 使input失去焦点
  65. $('.rechange-input', $view).blur();
  66. },$view);
  67. // 活动说明展示
  68. $.newTouch('.recharge-details',function (event) {
  69. event.preventDefault();
  70. $(this).children('div.arrow').toggleClass('active');
  71. if($(this).children('div.extended').css('display')=='none'){
  72. $(this).children('div.extended').css('display','block');
  73. $(this).siblings().children('div.extended').css('display','none');
  74. $(this).siblings().children('div.arrow').removeClass('active');
  75. }else {
  76. $(this).children('div.extended').css('height','0').css('display','none');
  77. $(this).children('div.arrow-block').addClass('arrow')
  78. }
  79. },$view);
  80. // 点击充值
  81. $.newTouch('.btn-add-recharge', function (event) {
  82. event.preventDefault();
  83. // 使input失去焦点
  84. $('.rechange-input', $view).blur();
  85. // 获取input值
  86. var inputVal=$('.media.on .rechange-input').val();
  87. // 判断用户是否已选择充值金额
  88. if (!($('.media.on').hasClass('on'))) {
  89. // 弹出提示框
  90. $doc.trigger('spa:openpanel', ['simpleAlert', {
  91. message: '请选择金额'
  92. }]);
  93. return;
  94. }
  95. // 判断是否为数字
  96. console.log(inputVal);
  97. if(inputVal !==undefined){
  98. if (isNaN(inputVal)) {
  99. // 弹出提示框
  100. $doc.trigger('spa:openpanel', ['simpleAlert', {
  101. message: '请输入数字'
  102. }]);
  103. return;
  104. }
  105. }
  106. // 是否为空
  107. if (inputVal==="") {
  108. // 弹出提示框
  109. $doc.trigger('spa:openpanel', ['simpleAlert', {
  110. message: '请输入金额'
  111. }]);
  112. return;
  113. }
  114. // 加载过度动画
  115. $doc.trigger('spa:openloader');
  116. var value = $('.media.on').attr('data-value');
  117. recharge.addRechargeorder({
  118. user_id: user.id,
  119. value:inputVal,
  120. recharge_id: $('.media.on').attr('data-id')
  121. }, function (res) {
  122. $doc.trigger('spa:closeloader');
  123. if (res.success) {
  124. // 更新user的订单
  125. user.orders[1].data.push({
  126. id: res.data._id.$id,
  127. products: [{product: {name: '充值' + value + '元'}}],
  128. final_price: res.data.price
  129. });
  130. // 只能用现金支付,跳转到pay页面
  131. $doc.trigger('spa:navigate', {
  132. hash: 'pay',
  133. pushData: {
  134. isRecharge: true,
  135. orderID: res.data._id.$id,
  136. backHash: 'recharge'
  137. }
  138. });
  139. }
  140. });
  141. }, $view);
  142. // 清空input 输入的值与 获得 类名 为 on 的单位
  143. $.newTouch('.btn-alert.btn-primary',function(event){
  144. evnet.preventDefault();
  145. // 使input失去焦点
  146. $('.rechange-input', $view).blur();
  147. $('.media.on .recharge-input').val('');
  148. $('.media.on').removeClass('on');
  149. },$view);
  150. dtd.resolve();
  151. },
  152. beforeopen: function (pageData, dtd) {
  153. /*获取上一页跳转的路由名称*/
  154. var pushData = pageData.pushData;
  155. backHash = pushData.backHash ? pushData.backHash : '';
  156. dtd.resolve();
  157. },
  158. afteropen: function (pageData, dtd) {
  159. var $view = this;
  160. var $doc = $(document);
  161. if (!pageData.pushData.dontNeedReload) {
  162. $doc.trigger('spa:openloader');
  163. recharge.getRechargeList(function (res) {
  164. $doc.trigger('spa:closeloader');
  165. if (res.success) {
  166. var tpl = template('recharge/index', {
  167. showNavBar: !config.isWX,
  168. recharges: res.data
  169. });
  170. $('.spa-page-body', $view).html(tpl);
  171. //var value = $(this).attr('data-value');
  172. var value = $('.btn-add-recharge').attr('data-index');
  173. for (var i = value; i < 2; i++) {
  174. }
  175. setTimeout(function () {
  176. pageData.scroll = new IScroll('#rechargeScroll');
  177. }, 500);
  178. }
  179. });
  180. }
  181. dtd.resolve();
  182. },
  183. beforeclose: function (pageData, dtd) {
  184. dtd.resolve();
  185. }
  186. }
  187. })