123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- define(['$', 'template', 'IScroll', 'recharge', 'native', 'config', 'user'], function ($, template, IScroll, recharge, native, config, user) {
- var backHash;
- function back(event) {
- event.preventDefault();
- $(document).trigger('spa:navigate', {
- hash: backHash,
- pushData: {
- dontNeedReload: true
- }
- });
- }
- return {
- title: '充值规则',
- body: '',
- init: function (pageData, dtd) {
- /* this是什么? */
- var $view = this;
- var $doc = $(document);
- /* 定义按钮事件。
- * 为什么不是$(selector).onclick(function)呢?
- */
- // 返回
- $view.on('swipeRight', function (event) {
- back.call(this, event);
- // history.go(-1);
- });
- $.newTouch('.back-button', function (event) {
- back.call(this, event);
- // history.go(-1);
- }, $view);
- $view.on('tapBackButton', function (event) {
- back.call(this, event);
- // history.go(-1);
- });
- //点击展示协议 vactivity-text
- $.newTouch('.pay-agreement',function (event) {
- event.preventDefault();
- $doc.trigger('spa:navigate', {
- hash: 'activity-text',
- pushData: {
- needReload: true
- }
- });
- }, $view);
- //规则 pay-rule
- $.newTouch('.pay-rule',function (event) {
- event.preventDefault();
- $doc.trigger('spa:navigate', {
- hash: 'activity-rule',
- pushData: {
- needReload: true
- }
- });
- }, $view);
- // 样式切换
- $.newTouch('.media',function (event) {
- event.preventDefault();
- /* on代表这一条的选中状态。如果已经选中了,就return。this的作用域是这一条extra。 */
- if ($(this).hasClass('on')) return;
- /* 如果没有on状态,加上on。 */
- $(this).addClass('on');
- /* 删掉其他选择框的on状态 */
- $(this).siblings().removeClass('on');
- // 使input失去焦点
- $('.rechange-input', $view).blur();
- },$view);
- // 活动说明展示
- $.newTouch('.recharge-details',function (event) {
- event.preventDefault();
- $(this).children('div.arrow').toggleClass('active');
- if($(this).children('div.extended').css('display')=='none'){
- $(this).children('div.extended').css('display','block');
- $(this).siblings().children('div.extended').css('display','none');
- $(this).siblings().children('div.arrow').removeClass('active');
- }else {
- $(this).children('div.extended').css('height','0').css('display','none');
- $(this).children('div.arrow-block').addClass('arrow')
- }
- },$view);
- // 点击充值
- $.newTouch('.btn-add-recharge', function (event) {
- event.preventDefault();
- // 使input失去焦点
- $('.rechange-input', $view).blur();
- // 获取input值
- var inputVal=$('.media.on .rechange-input').val();
- // 判断用户是否已选择充值金额
- if (!($('.media.on').hasClass('on'))) {
- // 弹出提示框
- $doc.trigger('spa:openpanel', ['simpleAlert', {
- message: '请选择金额'
- }]);
- return;
- }
- // 判断是否为数字
- console.log(inputVal);
- if(inputVal !==undefined){
- if (isNaN(inputVal)) {
- // 弹出提示框
- $doc.trigger('spa:openpanel', ['simpleAlert', {
- message: '请输入数字'
- }]);
- return;
- }
- }
- // 是否为空
- if (inputVal==="") {
- // 弹出提示框
- $doc.trigger('spa:openpanel', ['simpleAlert', {
- message: '请输入金额'
- }]);
- return;
- }
- // 加载过度动画
- $doc.trigger('spa:openloader');
- var value = $('.media.on').attr('data-value');
- recharge.addRechargeorder({
- user_id: user.id,
- value:inputVal,
- recharge_id: $('.media.on').attr('data-id')
- }, function (res) {
- $doc.trigger('spa:closeloader');
- if (res.success) {
- // 更新user的订单
- user.orders[1].data.push({
- id: res.data._id.$id,
- products: [{product: {name: '充值' + value + '元'}}],
- final_price: res.data.price
- });
- // 只能用现金支付,跳转到pay页面
- $doc.trigger('spa:navigate', {
- hash: 'pay',
- pushData: {
- isRecharge: true,
- orderID: res.data._id.$id,
- backHash: 'recharge'
- }
- });
- }
- });
- }, $view);
- // 清空input 输入的值与 获得 类名 为 on 的单位
- $.newTouch('.btn-alert.btn-primary',function(event){
- evnet.preventDefault();
- // 使input失去焦点
- $('.rechange-input', $view).blur();
- $('.media.on .recharge-input').val('');
- $('.media.on').removeClass('on');
- },$view);
- dtd.resolve();
- },
- beforeopen: function (pageData, dtd) {
- /*获取上一页跳转的路由名称*/
- var pushData = pageData.pushData;
- backHash = pushData.backHash ? pushData.backHash : '';
- dtd.resolve();
- },
- afteropen: function (pageData, dtd) {
- var $view = this;
- var $doc = $(document);
- if (!pageData.pushData.dontNeedReload) {
- $doc.trigger('spa:openloader');
- recharge.getRechargeList(function (res) {
- $doc.trigger('spa:closeloader');
- if (res.success) {
- var tpl = template('recharge/index', {
- showNavBar: !config.isWX,
- recharges: res.data
- });
- $('.spa-page-body', $view).html(tpl);
- //var value = $(this).attr('data-value');
- var value = $('.btn-add-recharge').attr('data-index');
- for (var i = value; i < 2; i++) {
- }
- setTimeout(function () {
- pageData.scroll = new IScroll('#rechargeScroll');
- }, 500);
- }
- });
- }
- dtd.resolve();
- },
- beforeclose: function (pageData, dtd) {
- dtd.resolve();
- }
- }
- })
|