123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
- var max_goods_count = 6;
- $(document).on('click', '.js_add_goods', function () {
- selectedIds = getSelectedIds();
- if (selectedIds.length >= max_goods_count) {
- Layer.alert('最多可以选择' + max_goods_count + '个商品');
- return false;
- }
- _goodsSelectUrl = getGoodsSelectUrl();
- Fast.api.open(_goodsSelectUrl, '选择商品', {
- callback: function (data) {
- createGoodsItem(data[0]);
- }
- })
- });
- $(document).on('click', '.js_edit', function (e) {
- $editBtn = $(this);
- _goodsSelectUrl = getGoodsSelectUrl();
- Fast.api.open(_goodsSelectUrl, '选择商品', {
- callback: function (data) {
- var goodsItem = data[0];
- htmlstr = formatGoodsItem(goodsItem);
- $editBtn.parents('.js_goods_item').html(htmlstr);
- }
- })
- });
- $(document).on('click', '.js_del', function () {
- $delBtn = $(this);
- layer.confirm('您确认移除商品吗?', {title: "移除商品"}, function (index) {
- layer.close(index);
- $delBtn.parents('.js_goods_item').remove();
- });
- });
- $(document).on('click', '.js_custom_goods_submit', function () {
- var data = $('.js_form_custom_goods').serialize();
- $.ajax({
- url: '/admin/viprecharge/index',
- data: data,
- type: 'POST',
- timeout: 35000,
- success: function (data) {
- if (data.code == 1) {//成功
- Toastr.success(data.msg);
- } else {
- Toastr.error(data.msg);
- }
- },
- error: function (XMLHttpRequest, textStatus, errorThrown) {
- Toastr.error(XMLHttpRequest.message);
- },
- complete: function () {
- }
- });
- });
- $(document).on('click','.js_custom_goods_reset',function () {
- $.ajax({
- url: '/admin/viprecharge/reset',
- data: null,
- type: 'POST',
- timeout: 35000,
- success: function (data) {
- if (data.code == 1) {//成功
- Toastr.success(data.msg);
- $('.js_goods_item').each(function (i, e) {
- var cc = $(e).hasClass('js_add_goods_item');
- if (!cc) {
- e.remove();
- }
- });
- initGoodsList(data.data);
- } else {
- Toastr.error(data.msg);
- }
- },
- error: function (XMLHttpRequest, textStatus, errorThrown) {
- Toastr.error(XMLHttpRequest.message);
- },
- complete: function () {
- }
- });
- });
- //已充值/未充值tab切换
- $(document).on('click','#goods-list-tabs li',function () {
- //清除原有的充值列表节点
- $('.js_goods_list li').each(function () {
- if(!$(this).hasClass('js_add_goods_item')){
- $(this).remove();
- }
- });
- customGoodsListStr = Config.customGoodsList;
- customGoodsList = JSON.parse(customGoodsListStr);
- initGoodsList(customGoodsList);
- });
- $(document).on('click','.js_custom_goods_sync',function () {
- $.ajax({
- url: '/admin/vip/admin/recharge/syncvipRecharge',
- type: 'POST',
- timeout: 35000,
- success: function (data) {
- if (data.code == 0) {//成功
- layer.alert('同步成功');
- } else {
- layer.alert('同步失败');
- }
- },
- error: function (XMLHttpRequest, textStatus, errorThrown) {
- Toastr.error(XMLHttpRequest.message);
- },
- complete: function () {
- }
- });
- })
- /**
- * 获取已选中的商品id
- * @returns {Array}
- */
- function getSelectedIds() {
- // formValueList = $('form').serializeArray();
- // selectedIds = [];
- // for (var i = 0; i < formValueList.length; i++) {
- // var item = formValueList[i];
- // if (item.name == 'row[goods_id][]') {
- // selectedIds.push(item.value);
- // }
- // }
- selectedIds = [];
- $('form li').each(function (i) {
- if(!$(this).hasClass('hide') && $(this).find('input').val()){
- selectedIds.push($(this).find('input').val());
- }
- });
- console.log(selectedIds)
- return selectedIds;
- }
- /**
- * 获取商品选择框的加载列表的API地址
- * @returns {string}
- */
- function getGoodsSelectUrl() {
- selectedIds = getSelectedIds();
- var show_type = getShowTypeValue();
- goodsSelectUrl = 'goods/select?business_line=0&recharge_show_type='+show_type;
- if (selectedIds.length > 0) {
- goodsSelectUrl += '&selected_ids=' + selectedIds.join('_');
- }
- return goodsSelectUrl;
- }
- /**
- * 初始化商品列表
- * @param list
- */
- function initGoodsList(list) {
- var data = {'row':{'fun':'get_custom_goods_list'}};
- $.ajax({
- url: '/admin/viprecharge/index',
- data: data,
- type: 'POST',
- timeout: 35000,
- success: function (data) {
- if (data.code == 1) {//成功
- var goodsList = data.data;
- for (i = 0; i < goodsList.length; i++) {
- if (goodsList[i].business_line == '0') {
- createGoodsItem(goodsList[i]);
- }
- }
- } else {
- Toastr.error(data.msg);
- }
- },
- error: function (XMLHttpRequest, textStatus, errorThrown) {
- Toastr.error(XMLHttpRequest.message);
- },
- complete: function () {
- }
- });
- }
- /**
- * 格式化商品单元格
- * @param goodsItem 商品对象
- * @returns {string}
- */
- function formatGoodsItem(goodsItem,hide) {
- var checked = goodsItem.default == '1' ? 'checked' : '';
- var htmlstr =
- '<div class="recl_info js_edit" '+hide+'>' +
- '<div class="recl_box">' +
- '<div class="recl_mark">' + goodsItem.icon + '</div>' +
- '<div class="recl_tit">' + goodsItem.show_type_text + '</div>' +
- '<div class="recl_money">' + goodsItem.title + '</div>' +
- '<div class="recl_des">' +
- '<strong>' + goodsItem.first_description + '</strong>' +
- '<span>' + goodsItem.second_description + '</span>' +
- '</div></div><input type="hidden" name="row[goods_id][]" value="' + goodsItem.id + '"></div> ' +
- '<div class="recl_operate">' +
- '<span style="float: left;line-height: 30px;"><input type="radio" name="row[default]" value="'+goodsItem.id+'" '+checked+'>用户默认勾选</span>'+
- '<a href="javascript:;" class="btn btn-xs btn-danger btn-delone js_del" title=""' +
- 'data-table-id="table" data-field-index="4" data-row-index="0"' +
- 'data-button-index="2"><i class="fa fa-trash"></i></a>' +
- '<a href="javascript:;" class="btn btn-xs btn-success js_move" title=""' +
- 'data-table-id="table" data-field-index="4" data-row-index="0"' +
- 'data-button-index="2"><i class="fa fa-arrows"></i></a>' +
- '</div>'
- ;
- return htmlstr;
- }
- /**
- * 创建商品
- * @param goodsItem
- */
- function createGoodsItem(goodsItem) {
- var show_type = getShowTypeValue();
- //已充值用户(全部用户/已充值用户/新用户/老用户)
- if(show_type == -1 && goodsItem.show_type == 2){
- htmlstr = formatGoodsItem(goodsItem,'hidden');
- htmlstr = '<li class="js_goods_item hide">' + htmlstr + '</li>';
- $('.js_add_goods_item').before(htmlstr);
- return;
- }
- //未充值用户(全部用户/未充值用户/新用户/老用户)
- else if(show_type == -2 && goodsItem.show_type == 1){
- htmlstr = formatGoodsItem(goodsItem,'hidden');
- htmlstr = '<li class="js_goods_item hide">' + htmlstr + '</li>';
- $('.js_add_goods_item').before(htmlstr);
- return;
- }
- else{
- htmlstr = formatGoodsItem(goodsItem,'');
- htmlstr = '<li class="js_goods_item">' + htmlstr + '</li>';
- $('.js_add_goods_item').before(htmlstr);
- return;
- }
- }
- /**
- * 获取当前tab标签选中value
- * @return (-1:已充值用户/-2:未充值用户)
- */
- function getShowTypeValue(){
- var show_type = -1;
- $("#goods-list-tabs li").each(function (obj) {
- if($(this).hasClass("active")){
- show_type = $(this).attr("data-value");
- return false;
- }
- });
- return show_type;
- }
- $(document).delegate('.js_move', 'click', function () {
- $(this).closest('.js_goods_item').addClass('js_goods_border');
- if ($('.js_goods_border').length == 2) {
- var list = $('.js_goods_item').toArray();
- var from = $('.js_goods_border').eq(0).index();
- var to = $('.js_goods_border').eq(1).index();
- var vfrom = list[from]
- var vto = list[to]
- list.splice(from,1,vto);
- list.splice(to,1,vfrom);
- $('.js_goods_list').html(list);
- $('.js_goods_border').removeClass('js_goods_border');
- }
- })
- // $(document).delegate('.js_goods_item', 'drop', function(event){
- // var list = $('.js_goods_item').toArray();
- // var to = $(event.currentTarget).index();
- // var from = event.originalEvent.dataTransfer.getData("index");
- // var vfrom = list[from]
- // var vto = list[to]
- // list.splice(from,1,vto);
- // list.splice(to,1,vfrom);
- // $('.js_goods_list').html(list);
- // })
- customGoodsListStr = Config.customGoodsList;
- customGoodsList = JSON.parse(customGoodsListStr);
- initGoodsList(customGoodsList);
- var Controller = {
- index: function () {
- }
- };
- return Controller;
- });
|