activity.js 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {
  2. var Controller = {
  3. index: function () {
  4. // 初始化表格参数
  5. Table.api.init({
  6. extend: {
  7. index_url: 'lottery/activity/index' + location.search,
  8. add_url: 'lottery/activity/add',
  9. edit_url: 'lottery/activity/edit',
  10. del_url: 'lottery/activity/del',
  11. multi_url: 'lottery/activity/multi',
  12. import_url: 'lottery/activity/import',
  13. table: 'shop_lottery_activity',
  14. }
  15. });
  16. var table = $("#table");
  17. // 初始化表格
  18. table.bootstrapTable({
  19. url: $.fn.bootstrapTable.defaults.extend.index_url,
  20. pk: 'id',
  21. sortName: 'id',
  22. fixedColumns: true,
  23. fixedRightNumber: 1,
  24. columns: [
  25. [
  26. {checkbox: true},
  27. {field: 'id', title: __('Id'), operate: false},
  28. {field: 'name', title: __('活动名称'), operate: 'LIKE'},
  29. {field: 'type', title: __('活动类型'), searchList: Controller.api.parseConfigJson("typeList"), formatter: Table.api.formatter.normal},
  30. {field: 'status', title: __('状态'), searchList: Controller.api.parseConfigJson("statusList"), formatter: Table.api.formatter.status},
  31. {field: 'lottery_type', title: __('开奖方式'), searchList: Controller.api.parseConfigJson("lotteryTypeList"), formatter: Table.api.formatter.normal},
  32. {field: 'start_time', title: __('开始时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
  33. {field: 'end_time', title: __('结束时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
  34. {field: 'total_people_count', title: __('参与人数'), operate: false},
  35. {field: 'total_draw_count', title: __('抽奖次数'), operate: false},
  36. {field: 'total_win_count', title: __('中奖次数'), operate: false},
  37. {field: 'createtime', title: __('Create time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
  38. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
  39. buttons: [
  40. // {
  41. // name: 'prize',
  42. // title: __('奖品管理'),
  43. // classname: 'btn btn-xs btn-success btn-magic btn-ajax',
  44. // icon: 'fa fa-gift',
  45. // url: 'lottery/activity/prize',
  46. // callback: function (data) {
  47. // Layer.alert("接收到回传数据:" + JSON.stringify(data), {title: "回传数据"});
  48. // },
  49. // visible: function (row) {
  50. // //返回true时按钮显示,返回false隐藏
  51. // return true;
  52. // }
  53. // },
  54. // {
  55. // name: 'condition',
  56. // title: __('参与条件'),
  57. // classname: 'btn btn-xs btn-info btn-magic btn-ajax',
  58. // icon: 'fa fa-tasks',
  59. // url: 'lottery/activity/condition'
  60. // },
  61. // {
  62. // name: 'records',
  63. // title: __('抽奖记录'),
  64. // classname: 'btn btn-xs btn-warning btn-magic btn-ajax',
  65. // icon: 'fa fa-list',
  66. // url: 'lottery/activity/records'
  67. // },
  68. // {
  69. // name: 'statistics',
  70. // title: __('统计数据'),
  71. // classname: 'btn btn-xs btn-primary btn-magic btn-ajax',
  72. // icon: 'fa fa-bar-chart',
  73. // url: 'lottery/activity/statistics'
  74. // }
  75. ],
  76. formatter: Table.api.formatter.operate
  77. }
  78. ]
  79. ]
  80. });
  81. // 为表格绑定事件
  82. Table.api.bindevent(table);
  83. },
  84. add: function () {
  85. Controller.api.bindevent();
  86. Controller.api.initAddPage();
  87. },
  88. edit: function () {
  89. Controller.api.bindevent();
  90. Controller.api.initEditPage();
  91. },
  92. api: {
  93. // 解析Config中的JSON字符串的辅助函数
  94. parseConfigJson: function(configKey, defaultValue) {
  95. var configValue = Config[configKey] || defaultValue || {};
  96. // 如果是字符串,尝试解析JSON
  97. if (typeof configValue === 'string') {
  98. try {
  99. return JSON.parse(configValue);
  100. } catch (e) {
  101. return defaultValue || {};
  102. }
  103. }
  104. return configValue;
  105. },
  106. bindevent: function () {
  107. Form.api.bindevent($("form[role=form]"));
  108. },
  109. // 初始化添加页面
  110. initAddPage: function() {
  111. // 初始化基础UI组件
  112. Controller.api.initBasicUI();
  113. // 初始化奖品数据管理器
  114. Controller.api.prizesDataManager.init();
  115. // 初始化规则数据管理器
  116. Controller.api.rulesDataManager.init();
  117. // 同步DOM中现有的奖品数据到数据管理器
  118. Controller.api.syncExistingPrizesToDataManager();
  119. // 初始化奖品管理
  120. Controller.api.initPrizeManagement();
  121. // 初始化任务规则管理
  122. Controller.api.initTaskRuleManagement();
  123. // 确保系统默认项有正确的概率值
  124. Controller.api.initDefaultPrizeRate();
  125. // 确保DOM渲染完成后再计算奖品数量
  126. setTimeout(function() {
  127. console.log('页面初始化 - 开始更新奖品计数');
  128. Controller.api.updatePrizeCount();
  129. }, 50);
  130. // 再次延迟调用,确保所有DOM都已渲染
  131. setTimeout(function() {
  132. console.log('页面初始化 - 二次更新奖品计数');
  133. Controller.api.updatePrizeCount();
  134. }, 500);
  135. // 初始化表单验证
  136. Controller.api.initFormValidation();
  137. },
  138. // 初始化基础UI组件
  139. initBasicUI: function() {
  140. // 活动名称字数统计
  141. $('#c-name').on('input', function(){
  142. var len = $(this).val().length;
  143. $('#activity-name-count').text(len + '/24');
  144. });
  145. },
  146. // 初始化任务规则管理
  147. initTaskRuleManagement: function() {
  148. // 任务类型切换(复选框)
  149. $(document).on('change', 'input[name="row[task_type][]"]', function() {
  150. Controller.api.handleTaskTypeChange();
  151. });
  152. // 商品规则切换
  153. $(document).on('change', 'input[name^="condition"][name$="[goods_rule]"]', function() {
  154. Controller.api.updateGoodsRuleDisplay($(this));
  155. // 更新规则数据管理器
  156. Controller.api.updateRulesDataFromDOM();
  157. });
  158. // 条件值输入框变化事件
  159. $(document).on('input change', 'input[name^="condition"][name$="[condition_value]"]', function() {
  160. // 更新规则数据管理器
  161. Controller.api.updateRulesDataFromDOM();
  162. });
  163. // 初始化任务类型状态
  164. Controller.api.initTaskTypeState();
  165. },
  166. // 处理任务类型变化
  167. handleTaskTypeChange: function() {
  168. var checkedTypes = $('input[name="row[task_type][]"]:checked');
  169. // 更新复选框样式
  170. $('input[name="row[task_type][]"]').each(function() {
  171. var label = $(this).closest('label');
  172. if ($(this).is(':checked')) {
  173. label.addClass('checked');
  174. } else {
  175. label.removeClass('checked');
  176. }
  177. });
  178. // 隐藏所有任务设置项
  179. $('.task-setting-item').hide();
  180. // 显示选中的任务设置项
  181. checkedTypes.each(function() {
  182. var type = parseInt($(this).val());
  183. $('#task-setting-' + type).show();
  184. });
  185. // 更新验证规则
  186. Controller.api.updateTaskValidation();
  187. // 更新规则数据管理器
  188. Controller.api.updateRulesDataFromDOM();
  189. },
  190. // 初始化任务类型状态
  191. initTaskTypeState: function() {
  192. var checkedTasks = $('input[name="row[task_type][]"]:checked');
  193. // 初始化复选框样式
  194. $('input[name="row[task_type][]"]').each(function() {
  195. var label = $(this).closest('label');
  196. if ($(this).is(':checked')) {
  197. label.addClass('checked');
  198. } else {
  199. label.removeClass('checked');
  200. }
  201. });
  202. // 初始化任务设置区域显示
  203. $('.task-setting-item').hide();
  204. checkedTasks.each(function() {
  205. var type = parseInt($(this).val());
  206. $('#task-setting-' + type).show();
  207. });
  208. },
  209. // 更新商品规则显示
  210. updateGoodsRuleDisplay: function($radio) {
  211. var container = $radio.closest('.task-setting-item');
  212. var rule = $radio.val();
  213. var helpText = container.find('.goods-description-box');
  214. if (rule == '1') {
  215. helpText.html('<span class="text-muted">指定商品下单后将自动触发抽奖活动且发放一次抽奖机会,若用户产生维权退款,已发放的奖励自动回收。</span>');
  216. } else {
  217. helpText.html('<span class="text-muted">除指定商品外的其他商品下单后将自动触发抽奖活动且发放一次抽奖机会。</span>');
  218. }
  219. },
  220. // 更新任务验证规则
  221. updateTaskValidation: function() {
  222. var checkedTypes = $('input[name="row[task_type][]"]:checked');
  223. // 移除旧的验证规则
  224. $('[name^="condition"]').removeAttr('data-rule');
  225. // 为选中的任务类型添加验证规则
  226. checkedTypes.each(function() {
  227. var type = parseInt($(this).val());
  228. switch(type) {
  229. case 1: // 购买指定商品
  230. // 商品选择验证会在选择商品时处理
  231. break;
  232. case 2: // 单笔订单消费满额
  233. case 3: // 单次充值满额
  234. case 4: // 活动期间累计消费满额
  235. $('[name="condition[' + type + '][condition_value]"]').attr('data-rule', 'required;number(0.01~999999.99)');
  236. break;
  237. }
  238. });
  239. },
  240. // 初始化表单验证
  241. initFormValidation: function() {
  242. // 添加自定义验证规则 - 验证奖品JSON数据
  243. if (typeof Nice !== 'undefined' && Nice.Validator) {
  244. Nice.Validator.addRule('prizesJson', function(val, el) {
  245. try {
  246. var prizes = JSON.parse(val || '[]');
  247. // 验证是否有奖品
  248. if (!prizes || prizes.length === 0) {
  249. return false;
  250. }
  251. // 验证每个奖品的基本字段
  252. for (var i = 0; i < prizes.length; i++) {
  253. var prize = prizes[i];
  254. if (!prize.name || !prize.type || !prize.image) {
  255. return false;
  256. }
  257. // 验证概率值
  258. if (typeof prize.rate !== 'number' || prize.rate < 0 || prize.rate > 100) {
  259. return false;
  260. }
  261. }
  262. return true;
  263. } catch (e) {
  264. return false;
  265. }
  266. }, '奖品数据不完整或格式错误');
  267. // 添加自定义验证规则 - 验证规则JSON数据
  268. Nice.Validator.addRule('rulesJson', function(val, el) {
  269. try {
  270. var rules = JSON.parse(val || '{}');
  271. // 验证是否有规则
  272. if (!rules || Object.keys(rules).length === 0) {
  273. return false;
  274. }
  275. // 验证每个规则的基本字段
  276. for (var type in rules) {
  277. var rule = rules[type];
  278. if (!rule.type) {
  279. return false;
  280. }
  281. // 根据类型验证特定字段
  282. switch(parseInt(type)) {
  283. case 1: // 购买指定商品
  284. if (!rule.goods_ids || !Array.isArray(JSON.parse(rule.goods_ids || '[]'))) {
  285. return false;
  286. }
  287. break;
  288. case 2: // 单笔订单消费满额
  289. case 3: // 单次充值满额
  290. case 4: // 活动期间累计消费满额
  291. if (!rule.condition_value || parseFloat(rule.condition_value) <= 0) {
  292. return false;
  293. }
  294. break;
  295. }
  296. }
  297. return true;
  298. } catch (e) {
  299. return false;
  300. }
  301. }, '规则设置不完整或格式错误');
  302. }
  303. // 监听表单提交事件
  304. $('form[role="form"]').on('submit', function(e) {
  305. console.log('表单提交开始');
  306. // 验证任务规则设置
  307. if (!Controller.api.validateTaskRules()) {
  308. e.preventDefault();
  309. return false;
  310. }
  311. // 收集规则数据并保存到隐藏域
  312. Controller.api.collectAndSaveRulesData();
  313. return true;
  314. });
  315. },
  316. // 初始化编辑页面
  317. initEditPage: function() {
  318. // 初始化基础UI组件
  319. Controller.api.initBasicUI();
  320. // 初始化奖品数据管理器
  321. Controller.api.prizesDataManager.init();
  322. // 初始化规则数据管理器
  323. Controller.api.rulesDataManager.init();
  324. // 初始化奖品管理
  325. Controller.api.initPrizeManagement();
  326. // 初始化任务规则管理
  327. Controller.api.initTaskRuleManagement();
  328. // 初始化已有的奖品数据
  329. Controller.api.initExistingPrizes();
  330. // 初始化已有的规则数据
  331. Controller.api.initExistingRules();
  332. // 初始化已有的条件数据
  333. Controller.api.initExistingConditions();
  334. // 确保所有概率输入框有正确的值
  335. setTimeout(function() {
  336. Controller.api.initDefaultPrizeRate();
  337. }, 100);
  338. // 初始化表单验证
  339. Controller.api.initFormValidation();
  340. console.log('编辑页面初始化完成');
  341. },
  342. // 同步DOM中现有的奖品数据到数据管理器
  343. syncExistingPrizesToDataManager: function() {
  344. console.log('开始同步DOM中现有的奖品数据到数据管理器');
  345. var existingPrizes = [];
  346. // 遍历DOM中现有的奖品项
  347. $('.prize-item').each(function(index) {
  348. var $item = $(this);
  349. var prizeData = Controller.api.extractPrizeDataFromDOM($item, index);
  350. if (prizeData) {
  351. existingPrizes.push(prizeData);
  352. console.log('提取到奖品数据:', prizeData);
  353. }
  354. });
  355. // 如果有奖品数据,同步到数据管理器
  356. if (existingPrizes.length > 0) {
  357. Controller.api.prizesDataManager.prizesData = existingPrizes;
  358. Controller.api.prizesDataManager.saveToHiddenField();
  359. console.log('已同步奖品数据到数据管理器,共', existingPrizes.length, '个奖品');
  360. } else {
  361. console.log('DOM中没有找到现有的奖品数据');
  362. }
  363. },
  364. // 从DOM中提取奖品数据
  365. extractPrizeDataFromDOM: function($item, index) {
  366. try {
  367. var isNoPrize = $item.attr('data-type') === 'no-prize';
  368. var prizeData = {
  369. id: $item.attr('data-prize-id') || ('prize_' + Date.now() + '_' + index),
  370. name: '',
  371. type: 1,
  372. image: '',
  373. quantity: 0,
  374. rate: 0
  375. };
  376. // 提取奖品名称
  377. if (isNoPrize) {
  378. // 系统默认未中奖项
  379. prizeData.name = $item.find('.prize-name-input').val() || $item.find('td:eq(1)').text().trim() || '谢谢参与';
  380. prizeData.type = parseInt($item.find('.prize-type-input').val() || 1);
  381. prizeData.image = $item.find('.prize-image-input').val() || '';
  382. // 如果隐藏域中没有图片,尝试从img标签获取(去掉CDN前缀)
  383. if (!prizeData.image) {
  384. var imgSrc = $item.find('.prize-image').attr('src') || '';
  385. if (imgSrc && typeof Fast !== 'undefined' && Fast.api && Fast.api.cdnurl) {
  386. // 去掉CDN前缀,获取相对路径
  387. prizeData.image = imgSrc.replace(/^https?:\/\/[^\/]+/, '');
  388. } else {
  389. prizeData.image = imgSrc;
  390. }
  391. }
  392. prizeData.quantity = 0; // 未中奖项数量固定为0
  393. prizeData.rate = parseFloat($item.find('.prize-rate').val() || $item.find('#no-prize-rate-input').val() || 50);
  394. } else {
  395. // 用户添加的奖品
  396. prizeData.name = $item.find('.prize-name').text().trim();
  397. prizeData.type = parseInt($item.find('.prize-type-input').val() || 2);
  398. prizeData.image = $item.find('.prize-image-input').val() || $item.find('.prize-image').attr('src') || '';
  399. prizeData.quantity = parseInt($item.find('.prize-quantity').val() || 1);
  400. prizeData.rate = parseFloat($item.find('.prize-rate').val() || 0);
  401. }
  402. // 根据奖品类型提取特定数据
  403. switch(prizeData.type) {
  404. case 3: // 积分
  405. prizeData.points = parseInt($item.find('.prize-points-input').val() || 0);
  406. break;
  407. case 4: // 余额
  408. prizeData.balance = parseFloat($item.find('.prize-balance-input').val() || 0);
  409. break;
  410. case 5: // 优惠券
  411. prizeData.coupon_id = $item.find('.prize-coupon-id-input').val();
  412. prizeData.couponId = prizeData.coupon_id;
  413. prizeData.coupon_name = $item.find('.prize-coupon-name').text() || '';
  414. break;
  415. case 6: // 红包
  416. prizeData.redpack_amount = parseFloat($item.find('.prize-redpack-input').val() || 0);
  417. break;
  418. case 7: // 兑换码
  419. prizeData.code_type = $item.find('.prize-code-type-input').val() || 'auto';
  420. prizeData.manual_codes = $item.find('.prize-manual-codes-input').val() || '';
  421. break;
  422. case 8: // 商城奖品
  423. prizeData.goods_id = $item.find('.prize-goods-id-input').val();
  424. prizeData.goodsId = prizeData.goods_id;
  425. prizeData.goods_name = $item.find('.prize-goods-name').text() || '';
  426. break;
  427. }
  428. console.log('提取奖品数据:', {
  429. index: index,
  430. isNoPrize: isNoPrize,
  431. extractedData: prizeData
  432. });
  433. return prizeData;
  434. } catch (e) {
  435. console.warn('提取奖品数据时出错:', e, $item);
  436. return null;
  437. }
  438. },
  439. // 初始化已有的奖品数据(重构版 - 使用数据管理器)
  440. initExistingPrizes: function() {
  441. // 检查是否有后台传递的奖品数据
  442. if (typeof window.existingPrizes !== 'undefined' && window.existingPrizes.length > 0) {
  443. console.log('正在初始化已有奖品数据:', window.existingPrizes);
  444. // 将已有奖品数据加载到数据管理器
  445. Controller.api.prizesDataManager.prizesData = window.existingPrizes;
  446. Controller.api.prizesDataManager.saveToHiddenField();
  447. // 重新构建奖品列表
  448. Controller.api.rebuildPrizeList();
  449. // 更新奖品计数和概率统计
  450. Controller.api.updatePrizeCount();
  451. Controller.api.updateTotalRate();
  452. console.log('奖品数据初始化完成,数据管理器已同步');
  453. } else {
  454. console.log('没有找到已有的奖品数据,尝试从DOM同步');
  455. // 如果没有后台数据,尝试从DOM同步现有数据
  456. Controller.api.syncExistingPrizesToDataManager();
  457. }
  458. },
  459. // 初始化已有的规则数据
  460. initExistingRules: function() {
  461. // 检查是否有后台传递的规则数据
  462. if (typeof window.existingRulesData !== 'undefined' && window.existingRulesData) {
  463. console.log('正在初始化已有规则数据:', window.existingRulesData);
  464. // 将已有规则数据加载到数据管理器
  465. Controller.api.rulesDataManager.rulesData = window.existingRulesData;
  466. Controller.api.rulesDataManager.saveToHiddenField();
  467. console.log('规则数据初始化完成');
  468. } else {
  469. console.log('没有找到已有的规则数据');
  470. }
  471. },
  472. // 初始化已有的条件数据
  473. initExistingConditions: function() {
  474. // 检查是否有后台传递的条件数据
  475. if (typeof window.existingConditions !== 'undefined' && window.existingConditions.length > 0) {
  476. console.log('正在初始化已有条件数据:', window.existingConditions);
  477. // 根据条件数据勾选任务类型
  478. window.existingConditions.forEach(function(condition) {
  479. var checkbox = $('input[name="row[task_type][]"][value="' + condition.type + '"]');
  480. if (checkbox.length > 0) {
  481. checkbox.prop('checked', true);
  482. // 显示对应的任务设置
  483. $('#task-setting-' + condition.type).show();
  484. // 根据条件类型设置具体的数据
  485. switch (parseInt(condition.type)) {
  486. case 1: // 购买指定商品
  487. if (condition.goods_ids_array && condition.goods_ids_array.length > 0) {
  488. // 需要调用商品选择API来获取商品详情
  489. Controller.api.loadConditionGoods(condition.type, condition.goods_ids_array);
  490. }
  491. break;
  492. case 2: // 单笔订单消费满额
  493. case 3: // 单次充值满额
  494. case 4: // 活动期间累计消费满额
  495. var amountInput = $('input[name="condition[' + condition.type + '][condition_value]"]');
  496. if (amountInput.length > 0) {
  497. amountInput.val(condition.condition_value);
  498. }
  499. break;
  500. }
  501. }
  502. });
  503. console.log('条件数据初始化完成');
  504. } else {
  505. console.log('没有找到已有的条件数据');
  506. }
  507. },
  508. // 加载条件中的商品数据
  509. loadConditionGoods: function(taskType, goodsIds) {
  510. if (!goodsIds || goodsIds.length === 0) return;
  511. // 调用商品信息API
  512. $.ajax({
  513. url: 'shop/goods/info',
  514. type: 'POST',
  515. data: {
  516. ids: goodsIds.join(',')
  517. },
  518. dataType: 'json',
  519. success: function(ret) {
  520. if (ret.code === 1 && ret.data && ret.data.length > 0) {
  521. // 更新商品显示
  522. Controller.api.updateSelectedGoods(ret.data, {
  523. container: '#task-setting-' + taskType + ' #selected-goods',
  524. template: 'goods-list-template'
  525. });
  526. // 更新隐藏字段
  527. $('#task-goods-ids-' + taskType).val(JSON.stringify(goodsIds));
  528. console.log('任务商品数据加载完成:', ret.data);
  529. }
  530. },
  531. error: function(xhr, status, error) {
  532. console.error('加载任务商品数据失败:', error);
  533. }
  534. });
  535. },
  536. // 验证任务规则设置
  537. validateTaskRules: function() {
  538. var checkedTypes = $('input[name="row[task_type][]"]:checked');
  539. // 检查是否选择了任务类型
  540. if (checkedTypes.length === 0) {
  541. Toastr.error('请选择至少一种参与条件');
  542. return false;
  543. }
  544. // 验证每个选中的任务类型
  545. var isValid = true;
  546. checkedTypes.each(function() {
  547. var type = parseInt($(this).val());
  548. var container = $('#task-setting-' + type);
  549. switch(type) {
  550. case 1: // 购买指定商品
  551. var goodsContainer = container.find('#selected-goods');
  552. if (goodsContainer.find('.goods-list-item').length === 0) {
  553. Toastr.error('请选择参与的商品');
  554. isValid = false;
  555. return false;
  556. }
  557. break;
  558. case 2: // 单笔订单消费满额
  559. case 3: // 单次充值满额
  560. case 4: // 活动期间累计消费满额
  561. var amountInput = container.find('input[name*="[condition_value]"]');
  562. var amount = parseFloat(amountInput.val()) || 0;
  563. if (amount <= 0) {
  564. var typeName = type === 2 ? '订单消费' : (type === 3 ? '充值' : '累计消费');
  565. Toastr.error('请填写' + typeName + '金额');
  566. amountInput.focus();
  567. isValid = false;
  568. return false;
  569. }
  570. break;
  571. }
  572. });
  573. return isValid;
  574. },
  575. // 初始化奖品管理 - 重构版
  576. initPrizeManagement: function() {
  577. console.log('初始化奖品管理模块');
  578. // 初始化拖拽排序
  579. Controller.api.initPrizeSort();
  580. // 初始化概率计算
  581. Controller.api.updateTotalRate();
  582. // 绑定奖品相关事件
  583. Controller.api.bindPrizeEvents();
  584. // 初始化表单验证
  585. Controller.api.initPrizeFormValidation();
  586. console.log('奖品管理模块初始化完成');
  587. },
  588. // 添加奖品
  589. addPrize: function() {
  590. var totalCount = $('.prize-item').length; // 总奖品数(包含未中奖项)
  591. var userCount = totalCount - 1; // 用户添加的奖品数(减去未中奖项)
  592. var maxUserCount = 7; // 用户最多只能添加7个奖品(总共8个,包括系统默认的未中奖项)
  593. if (userCount >= maxUserCount) {
  594. Toastr.error('最多只能添加' + maxUserCount + '个奖品(不包括系统默认的未中奖项)');
  595. return;
  596. }
  597. // 使用通用表单,添加模式
  598. Controller.api.openPrizeForm({
  599. mode: 'add',
  600. title: '添加奖品'
  601. });
  602. },
  603. // 初始化奖品表单验证
  604. initPrizeFormValidation: function() {
  605. console.log('初始化奖品表单验证');
  606. // FastAdmin使用Nice-validator,验证规则通过data-rule属性定义
  607. // 无需额外的自定义验证方法,表单验证会在Form.api.bindevent中自动处理
  608. console.log('FastAdmin表单验证已通过data-rule属性配置');
  609. },
  610. // 通用奖品表单 - 重构版
  611. openPrizeForm: function(options) {
  612. var defaults = {
  613. mode: 'add', // 'add' 或 'edit'
  614. title: '奖品表单',
  615. prizeItem: null,
  616. isSystemNoPrize: false,
  617. data: {
  618. name: '',
  619. image: '',
  620. type: '2', // 默认实物奖品
  621. quantity: '1',
  622. rate: '0.00'
  623. }
  624. };
  625. var config = $.extend({}, defaults, options);
  626. var isEditMode = config.mode === 'edit';
  627. // 获取奖品类型数据
  628. var prizeTypeMap = Controller.api.parseConfigJson('prizeTypeList', {});
  629. var prizeTypes = [];
  630. for (var key in prizeTypeMap) {
  631. if (prizeTypeMap.hasOwnProperty(key)) {
  632. // 添加模式和编辑模式都包含所有类型
  633. prizeTypes.push({
  634. value: parseInt(key),
  635. text: prizeTypeMap[key]
  636. });
  637. }
  638. }
  639. // 确定是否显示奖励设置区域
  640. var selectedType = parseInt(config.data.type) || (isEditMode ? 1 : 2);
  641. var rewardVisible = selectedType > 2;
  642. var rewardType = Controller.api.getRewardTypeByPrizeType(selectedType);
  643. // 生成表单HTML - 使用重构的模板
  644. var formHtml = Template('prize-form-template', {
  645. mode: config.mode,
  646. prizeTypes: prizeTypes,
  647. selectedType: selectedType
  648. });
  649. // 弹出表单 - 学习提供的样式代码
  650. Layer.open({
  651. type: 1,
  652. title: config.title,
  653. area: isEditMode ? ['80%', '80%'] : ['80%', '80%'],
  654. content: formHtml,
  655. btn: [isEditMode ? '保存' : '确定', '取消'],
  656. success: function(layero, index) {
  657. var $form = layero.find('.prize-form');
  658. // 初始化FastAdmin表单组件
  659. Form.api.bindevent($form);
  660. // 编辑模式需要设置表单数据
  661. if (isEditMode) {
  662. Controller.api.setFormData($form, config);
  663. // 系统默认未中奖项的特殊处理
  664. if (config.isSystemNoPrize) {
  665. // 隐藏奖品数量字段(未中奖不需要数量)
  666. $form.find('.prize-quantity-group').hide();
  667. $form.find('[data-favisible="prize_type!=1"]').hide();
  668. // 禁用奖品类型选择(固定为未中奖)
  669. $form.find('input[name="row[prize_type]"]').prop('disabled', true);
  670. }
  671. } else {
  672. // 添加模式:设置默认奖品类型并触发相应逻辑
  673. var defaultType = selectedType || 2; // 默认实物奖品
  674. $form.find('input[name="row[prize_type]"][value="' + defaultType + '"]').prop('checked', true);
  675. // 设置默认图片
  676. Controller.api.setPrizeDefaultImage($form, defaultType);
  677. // 触发奖品类型改变事件
  678. $form.find('input[name="row[prize_type]"]').trigger('change');
  679. }
  680. // 设置正确的字数统计初始值
  681. var currentName = $form.find('input[name="prize_name"]').val();
  682. var currentLength = currentName ? currentName.length : 0;
  683. $form.find('#prize-name-count').text(currentLength + '/20');
  684. // 绑定自定义事件
  685. Controller.api.bindPrizeFormEvents($form, config);
  686. // 初始化字数统计
  687. Controller.api.initNameCounter($form);
  688. // 优化表单样式,使其更紧凑
  689. Controller.api.optimizePrizeFormStyle($form);
  690. console.log('奖品表单初始化完成');
  691. },
  692. yes: function(index, layero) {
  693. var $form = layero.find('.prize-form');
  694. var success = false;
  695. if (isEditMode) {
  696. success = Controller.api.updatePrize($form, config);
  697. } else {
  698. success = Controller.api.savePrize($form);
  699. }
  700. if (success) {
  701. Layer.close(index);
  702. }
  703. // 如果验证失败,success会是false,Layer不会关闭
  704. return success;
  705. }
  706. });
  707. },
  708. // 设置默认表单数据(添加模式)
  709. setDefaultFormData: function($form, config) {
  710. console.log('设置默认表单数据');
  711. // 设置默认奖品类型
  712. var defaultType = config.data.type || 2;
  713. $form.find('input[name="row[prize_type]"][value="' + defaultType + '"]').prop('checked', true);
  714. // 设置默认图片
  715. Controller.api.setPrizeDefaultImage($form, defaultType);
  716. // 初始化图片上传组件
  717. Controller.api.initImageUpload($form);
  718. // 触发奖品类型改变事件
  719. $form.find('input[name="row[prize_type]"]').trigger('change');
  720. console.log('默认表单数据设置完成');
  721. },
  722. // 初始化名称字符计数
  723. initNameCounter: function($form) {
  724. var $nameInput = $form.find('input[name="prize_name"]');
  725. var $counter = $form.find('#prize-name-count');
  726. // 初始化计数
  727. var currentLength = $nameInput.val().length;
  728. $counter.text(currentLength + '/20');
  729. // 绑定输入事件
  730. $nameInput.on('input', function() {
  731. var length = $(this).val().length;
  732. $counter.text(length + '/20');
  733. });
  734. },
  735. // 优化奖品表单样式,使其更紧凑
  736. optimizePrizeFormStyle: function($form) {
  737. // 减少表单组的间距
  738. $form.find('.form-group').css({
  739. 'margin-bottom': '15px'
  740. });
  741. // 减少奖励设置区域的间距
  742. $form.find('.reward-group').css({
  743. 'margin-bottom': '10px'
  744. });
  745. // 减少表单区块的间距
  746. $form.find('.form-section').css({
  747. 'margin-bottom': '20px'
  748. });
  749. // 减少帮助文本的上边距
  750. $form.find('.help-block').css({
  751. 'margin-top': '5px',
  752. 'margin-bottom': '0px'
  753. });
  754. // 优化警告框的间距
  755. $form.find('.alert').css({
  756. 'margin-top': '10px',
  757. 'margin-bottom': '10px',
  758. 'padding': '10px'
  759. });
  760. // 减少图片预览区域的间距
  761. $form.find('.plupload-preview').css({
  762. 'margin-top': '10px'
  763. });
  764. console.log('表单样式优化完成');
  765. },
  766. // 初始化图片上传组件
  767. initImageUpload: function($form) {
  768. console.log('初始化图片上传组件');
  769. // 使用FastAdmin的Form组件来重新初始化表单内的上传组件
  770. Form.api.bindevent($form);
  771. // 绑定图片输入框的变化事件,实现预览功能
  772. $form.find('input[name="prize_image"]').on('change', function() {
  773. var imageUrl = $(this).val();
  774. if (imageUrl) {
  775. // 生成预览HTML
  776. var previewHtml = '<li class="col-xs-3">' +
  777. '<img src="' + (typeof Fast !== 'undefined' && Fast.api ? Fast.api.cdnurl(imageUrl) : imageUrl) + '" ' +
  778. 'class="img-responsive img-thumbnail" style="max-width: 100px; max-height: 100px;">' +
  779. '</li>';
  780. // 更新预览区域
  781. $form.find('#p-prize-image').html(previewHtml);
  782. } else {
  783. // 清空预览
  784. $form.find('#p-prize-image').empty();
  785. }
  786. });
  787. console.log('图片上传组件初始化完成');
  788. },
  789. // 设置表单数据(编辑模式) - 重构版
  790. setFormData: function($form, config) {
  791. console.log('设置编辑表单数据', config.data);
  792. var data = config.data;
  793. // 设置基本字段
  794. $form.find('input[name="prize_name"]').val(data.name);
  795. $form.find('input[name="prize_image"]').val(data.image);
  796. $form.find('input[name="prize_quantity"]').val(data.quantity || '1');
  797. $form.find('input[name="prize_rate"]').val(parseFloat(data.rate || 0).toFixed(2));
  798. // 设置奖品类型
  799. $form.find('input[name="row[prize_type]"][value="' + data.type + '"]').prop('checked', true);
  800. // 根据奖品类型处理特殊字段
  801. switch(parseInt(data.type)) {
  802. case 3: // 积分
  803. if (data.points) {
  804. $form.find('input[name="reward_value_integral"]').val(data.points);
  805. }
  806. break;
  807. case 4: // 余额
  808. if (data.balance) {
  809. $form.find('input[name="reward_value_balance"]').val(data.balance);
  810. }
  811. break;
  812. case 5: // 优惠券
  813. // 支持两种字段名格式
  814. var couponId = data.couponId || data.coupon_id;
  815. if (couponId) {
  816. $form.find('input[name="coupon_id"]').val(couponId);
  817. Controller.api.loadCouponInfo($form, couponId);
  818. }
  819. break;
  820. case 6: // 红包
  821. if (data.redpack_amount) {
  822. $form.find('input[name="reward_value_redpack"]').val(data.redpack_amount);
  823. }
  824. break;
  825. case 7: // 兑换码
  826. if (data.code_type) {
  827. $form.find('input[name="code_type"][value="' + data.code_type + '"]').prop('checked', true);
  828. }
  829. if (data.manual_codes) {
  830. $form.find('textarea[name="manual_codes"]').val(data.manual_codes);
  831. }
  832. break;
  833. case 8: // 商城奖品
  834. // 支持两种字段名格式
  835. var goodsId = data.goodsId || data.goods_id;
  836. if (goodsId) {
  837. $form.find('input[name="goods_id"]').val(goodsId);
  838. Controller.api.loadGoodsInfo($form, goodsId);
  839. }
  840. break;
  841. }
  842. // 触发奖品类型改变事件以显示对应的奖励设置
  843. $form.find('input[name="row[prize_type]"]').trigger('change');
  844. // 初始化图片上传组件
  845. Controller.api.initImageUpload($form);
  846. // 触发图片预览更新
  847. if (data.image) {
  848. $form.find('input[name="prize_image"]').trigger('change');
  849. }
  850. console.log('编辑表单数据设置完成');
  851. },
  852. // 绑定奖品表单事件 - 重构版
  853. bindPrizeFormEvents: function($form, config) {
  854. console.log('绑定奖品表单事件');
  855. var isEditMode = config && config.mode === 'edit';
  856. // 奖品类型切换事件
  857. $form.find('input[name="row[prize_type]"]').off('change.prizeType').on('change.prizeType', function() {
  858. var type = parseInt($(this).val());
  859. console.log('奖品类型切换至:', type);
  860. // 显示/隐藏对应的奖励设置
  861. Controller.api.toggleRewardSettings($form, type);
  862. // 非编辑模式时设置默认图片
  863. if (!isEditMode) {
  864. Controller.api.setPrizeDefaultImage($form, type);
  865. }
  866. });
  867. // 商品选择按钮
  868. $form.find('#select-goods-btn').off('click.selectGoods').on('click.selectGoods', function() {
  869. Controller.api.openGoodsSelector($form);
  870. });
  871. // 优惠券选择按钮
  872. $form.find('#select-coupon-btn').off('click.selectCoupon').on('click.selectCoupon', function() {
  873. Controller.api.openCouponSelector($form);
  874. });
  875. // 兑换码类型切换
  876. $form.find('input[name="code_type"]').off('change.codeType').on('change.codeType', function() {
  877. var codeType = $(this).val();
  878. Controller.api.toggleManualCodesInput($form, codeType);
  879. });
  880. // 概率输入框格式化
  881. $form.find('input[name="prize_rate"]').off('blur.rateFormat').on('blur.rateFormat', function() {
  882. var value = parseFloat($(this).val()) || 0;
  883. $(this).val(value.toFixed(2));
  884. });
  885. // 删除选中项事件
  886. $form.find('.selected-display').off('click.removeItem').on('click.removeItem', '.remove-item', function() {
  887. var $item = $(this).closest('.selected-item');
  888. var $hiddenInput = $item.siblings('input[type="hidden"]');
  889. $item.remove();
  890. $hiddenInput.val('');
  891. });
  892. console.log('奖品表单事件绑定完成');
  893. },
  894. // 切换奖励设置显示
  895. toggleRewardSettings: function($form, type) {
  896. console.log('切换奖励设置显示:', type);
  897. // 隐藏所有奖励设置
  898. $form.find('.reward-group').removeClass('active').hide();
  899. // 根据类型显示对应的设置
  900. var rewardType = '';
  901. switch(parseInt(type)) {
  902. case 3:
  903. rewardType = 'integral';
  904. break;
  905. case 4:
  906. rewardType = 'balance';
  907. break;
  908. case 5:
  909. rewardType = 'coupon';
  910. break;
  911. case 6:
  912. rewardType = 'redpack';
  913. break;
  914. case 7:
  915. rewardType = 'code';
  916. break;
  917. case 8:
  918. rewardType = 'goods';
  919. break;
  920. }
  921. if (rewardType) {
  922. $form.find('.reward-group[data-type="' + rewardType + '"]').addClass('active').show();
  923. }
  924. },
  925. // 切换手动兑换码输入
  926. toggleManualCodesInput: function($form, codeType) {
  927. var $container = $form.find('.manual-codes-container');
  928. if (codeType === 'manual') {
  929. $container.show();
  930. } else {
  931. $container.hide();
  932. }
  933. },
  934. // 设置奖品默认图片
  935. setPrizeDefaultImage: function(form, type) {
  936. // 从后台配置获取默认图片
  937. var defaultImages = Controller.api.parseConfigJson('prizeDefaultImages', {});
  938. var defaultImage = defaultImages[type] || '/assets/img/default.png';
  939. // 设置图片路径并触发change事件以更新预览
  940. form.find('input[name="prize_image"]').val(defaultImage).trigger('change');
  941. },
  942. // 加载商品信息用于编辑模式回显
  943. loadGoodsInfo: function(form, goodsId, callback) {
  944. if (!goodsId) {
  945. console.warn('loadGoodsInfo: 商品ID为空');
  946. if (callback) callback();
  947. return;
  948. }
  949. console.log('正在加载商品信息,ID:', goodsId);
  950. // 通过Ajax获取商品信息
  951. $.ajax({
  952. url: Fast.api.fixurl('shop/goods/info'),
  953. type: 'POST',
  954. data: {id: goodsId},
  955. dataType: 'json',
  956. success: function(data) {
  957. console.log('商品信息API响应:', data);
  958. if (data.code === 1 && data.data) {
  959. var goods = data.data; // 标准FastAdmin响应格式
  960. console.log('成功获取商品信息:', goods);
  961. // 处理商品数据
  962. var processedGoods = {
  963. id: goods.id,
  964. name: goods.name || goods.title,
  965. title: goods.title || goods.name,
  966. image: goods.image ? Fast.api.cdnurl(goods.image) : '',
  967. price: goods.price || goods.sellprice || '',
  968. goods_sn: goods.goods_sn || '',
  969. spec_type: goods.spec_type !== undefined ? goods.spec_type : null,
  970. category: goods.category || null,
  971. type_name: Controller.api.getGoodsTypeName(goods),
  972. type_text: Controller.api.getGoodsTypeText(goods)
  973. };
  974. // 使用商品模板显示
  975. var html = Template('prize-goods-template', processedGoods);
  976. form.find('#selected-goods-display').html(html).show();
  977. // 绑定删除事件
  978. form.find('.remove-prize-goods').off('click').on('click', function() {
  979. $(this).closest('.prize-goods-item').remove();
  980. form.find('input[name="goods_id"]').val('');
  981. });
  982. console.log('商品信息已成功显示');
  983. // 执行回调
  984. if (callback) callback();
  985. } else {
  986. console.warn('商品信息API返回异常:', data);
  987. // 显示友好的提示信息
  988. var errorMsg = data.msg || '获取商品信息失败';
  989. form.find('#selected-goods-display').html(
  990. '<div class="alert alert-warning" style="margin-top: 10px;">' +
  991. '<i class="fa fa-warning"></i> ' + errorMsg +
  992. ' (商品ID: ' + goodsId + ')' +
  993. '</div>'
  994. ).show();
  995. // 即使失败也执行回调
  996. if (callback) callback();
  997. }
  998. },
  999. error: function(xhr, status, error) {
  1000. console.error('加载商品信息失败 - 详细信息:');
  1001. console.error('- 商品ID:', goodsId);
  1002. console.error('- HTTP状态:', xhr.status);
  1003. console.error('- 错误信息:', error);
  1004. console.error('- 响应内容:', xhr.responseText);
  1005. // 显示友好的错误提示
  1006. var errorHtml = '<div class="alert alert-danger" style="margin-top: 10px;">' +
  1007. '<i class="fa fa-exclamation-circle"></i> 加载商品信息失败<br>' +
  1008. '<small>商品ID: ' + goodsId + '</small><br>' +
  1009. '<small>错误: ' + (xhr.status === 404 ? '商品不存在或已删除' :
  1010. xhr.status === 403 ? '无权限访问' :
  1011. '网络错误,请稍后重试') + '</small>' +
  1012. '</div>';
  1013. form.find('#selected-goods-display').html(errorHtml).show();
  1014. // 错误时也执行回调
  1015. if (callback) callback();
  1016. }
  1017. });
  1018. },
  1019. // 加载优惠券信息用于编辑模式回显
  1020. loadCouponInfo: function(form, couponId) {
  1021. if (!couponId) {
  1022. console.warn('loadCouponInfo: 优惠券ID为空');
  1023. return;
  1024. }
  1025. console.log('正在加载优惠券信息,ID:', couponId);
  1026. // 暂时显示一个简单的占位信息,提醒需要重新选择
  1027. var placeholderHtml = '<div class="alert alert-info" style="margin-top: 10px;">' +
  1028. '<i class="fa fa-info-circle"></i> 编辑模式下需要重新选择优惠券<br>' +
  1029. '<small>优惠券ID: ' + couponId + '</small>' +
  1030. '</div>';
  1031. form.find('#selected-coupon-display').html(placeholderHtml).show();
  1032. console.log('优惠券占位信息已显示,用户需要重新选择');
  1033. },
  1034. // 打开商品选择器
  1035. openGoodsSelector: function(form) {
  1036. Fast.api.open('shop/goods/select?selectMode=single', '选择商品', {
  1037. callback: function(data) {
  1038. if (data.length > 0) {
  1039. var goods = data[0]; // 取第一个选中的商品
  1040. // 处理商品数据,确保格式完整
  1041. var processedGoods = {
  1042. id: goods.id,
  1043. name: goods.name || goods.title,
  1044. title: goods.title || goods.name,
  1045. image: goods.image ? (typeof Fast !== 'undefined' && Fast.api ? Fast.api.cdnurl(goods.image) : goods.image) : '',
  1046. price: goods.price || goods.sellprice || '',
  1047. goods_sn: goods.goods_sn || '',
  1048. spec_type: goods.spec_type !== undefined ? goods.spec_type : null,
  1049. category: goods.category || null,
  1050. type_name: Controller.api.getGoodsTypeName(goods),
  1051. type_text: Controller.api.getGoodsTypeText(goods)
  1052. };
  1053. // 使用奖品商品模板
  1054. var html = Template('prize-goods-template', processedGoods);
  1055. form.find('#selected-goods-display').html(html).show();
  1056. // 设置隐藏域的值以触发验证
  1057. form.find('input[name="goods_id"]').val(goods.id);
  1058. // 设置商品图片为奖品图片并触发预览更新
  1059. if (goods.image) {
  1060. form.find('input[name="prize_image"]').val(goods.image).trigger('change');
  1061. }
  1062. // 绑定删除事件
  1063. form.find('.remove-prize-goods').off('click').on('click', function() {
  1064. $(this).closest('.prize-goods-item').remove();
  1065. form.find('input[name="goods_id"]').val(''); // 清空隐藏域
  1066. });
  1067. }
  1068. }
  1069. });
  1070. },
  1071. // 打开优惠券选择器
  1072. openCouponSelector: function(form) {
  1073. Fast.api.open('shop/coupon/select', '选择优惠券', {
  1074. callback: function(data) {
  1075. if (data.length > 0) {
  1076. var coupon = data[0]; // 取第一个选中的优惠券
  1077. // 直接使用模板ID
  1078. var html = Template('selected-coupon-template', {
  1079. name: coupon.name,
  1080. type_text: coupon.type_text || '优惠券',
  1081. id: coupon.id
  1082. });
  1083. form.find('#selected-coupon-display').html(html).show();
  1084. // 设置隐藏域的值以触发验证
  1085. form.find('input[name="coupon_id"]').val(coupon.id);
  1086. }
  1087. }
  1088. });
  1089. },
  1090. // 保存奖品 - 重构版(使用数据管理器)
  1091. savePrize: function($form) {
  1092. console.log('开始保存奖品');
  1093. // 收集表单数据
  1094. var prizeData = Controller.api.collectPrizeFormData($form);
  1095. if (!prizeData) {
  1096. return false; // 验证失败
  1097. }
  1098. // 使用数据管理器添加奖品
  1099. var prizeId = Controller.api.prizesDataManager.addPrize(prizeData);
  1100. // 添加奖品到显示列表(追加模式,不会覆盖现有奖品)
  1101. Controller.api.appendPrizeToList(prizeData);
  1102. // 更新相关统计
  1103. Controller.api.updatePrizeCount();
  1104. Controller.api.updateTotalRate();
  1105. console.log('奖品保存成功:', prizeData, '奖品ID:', prizeId);
  1106. return true;
  1107. },
  1108. // 收集奖品表单数据
  1109. collectPrizeFormData: function($form) {
  1110. console.log('收集奖品表单数据');
  1111. var name = $form.find('input[name="prize_name"]').val().trim();
  1112. var type = parseInt($form.find('input[name="row[prize_type]"]:checked').val());
  1113. var image = $form.find('input[name="prize_image"]').val();
  1114. var quantity = parseInt($form.find('input[name="prize_quantity"]').val()) || 1;
  1115. var rate = parseFloat($form.find('input[name="prize_rate"]').val()) || 0;
  1116. // 基础验证 - FastAdmin的data-rule已经处理了大部分验证
  1117. // 这里只做简单的空值检查,具体验证错误会由Nice-validator显示
  1118. if (!name) {
  1119. Toastr.error('请输入奖品名称');
  1120. return null;
  1121. }
  1122. if (!type) {
  1123. Toastr.error('请选择奖品类型');
  1124. return null;
  1125. }
  1126. if (!image) {
  1127. Toastr.error('请选择奖品图片');
  1128. return null;
  1129. }
  1130. // 构建奖品数据
  1131. var prizeData = {
  1132. name: name,
  1133. type: type,
  1134. image: image,
  1135. quantity: Controller.api.isNoPrizeType(type) ? 0 : quantity,
  1136. rate: rate
  1137. };
  1138. // 根据奖品类型收集特定数据
  1139. switch(type) {
  1140. case 3: // 积分
  1141. prizeData.points = parseInt($form.find('input[name="reward_value_integral"]').val()) || 0;
  1142. break;
  1143. case 4: // 余额
  1144. prizeData.balance = parseFloat($form.find('input[name="reward_value_balance"]').val()) || 0;
  1145. break;
  1146. case 5: // 优惠券
  1147. prizeData.coupon_id = $form.find('input[name="coupon_id"]').val();
  1148. prizeData.couponId = prizeData.coupon_id; // 为模板提供一致的字段名
  1149. // 从优惠券显示区域获取名称
  1150. prizeData.coupon_name = $form.find('#selected-coupon-display').text().trim() ||
  1151. $form.find('.selected-coupon-item span:first').text() || '';
  1152. break;
  1153. case 6: // 红包
  1154. prizeData.redpack_amount = parseFloat($form.find('input[name="reward_value_redpack"]').val()) || 0;
  1155. break;
  1156. case 7: // 兑换码
  1157. prizeData.code_type = $form.find('input[name="code_type"]:checked').val() || 'auto';
  1158. if (prizeData.code_type === 'manual') {
  1159. prizeData.manual_codes = $form.find('textarea[name="manual_codes"]').val() || '';
  1160. }
  1161. break;
  1162. case 8: // 商城奖品
  1163. prizeData.goods_id = $form.find('input[name="goods_id"]').val();
  1164. prizeData.goodsId = prizeData.goods_id; // 为模板提供一致的字段名
  1165. // 从商品显示区域获取名称
  1166. prizeData.goods_name = $form.find('#selected-goods-display .goods-name-display').text() ||
  1167. $form.find('#selected-goods-display .prize-goods-item .goods-name-display').text() ||
  1168. $form.find('.prize-goods-item .goods-name-display').text() || '';
  1169. break;
  1170. }
  1171. console.log('收集到的奖品数据:', prizeData);
  1172. return prizeData;
  1173. },
  1174. // 添加奖品到列表(编辑模式用)
  1175. addPrizeToList: function(prize, index) {
  1176. // 获取奖品类型信息
  1177. var typeName = Controller.api.getPrizeTypeName(prize.type);
  1178. var labelClass = Controller.api.getPrizeLabelClass(prize.type);
  1179. var rewardText = Controller.api.getPrizeRewardText(prize);
  1180. // 使用传入的索引
  1181. var prizeIndex = typeof index !== 'undefined' ? index : $('.prize-list .prize-item').length;
  1182. // 处理图片URL
  1183. var imageRelative = prize.image; // 相对路径,用于表单提交
  1184. var imageDisplay = prize.image ? (typeof Fast !== 'undefined' && Fast.api ? Fast.api.cdnurl(prize.image) : prize.image) : ''; // CDN URL,用于显示
  1185. // 判断是否为系统奖品(未中奖类型)
  1186. var isSystemPrize = Controller.api.isNoPrizeType(prize.type);
  1187. // 生成唯一的奖品ID
  1188. var prizeId = prize.id || ('prize_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9));
  1189. // 使用外部模板 - 重构为仅展示用途
  1190. var prizeHtml = Template('prize-item-template', {
  1191. index: prizeIndex,
  1192. prizeId: prizeId,
  1193. prizeType: isSystemPrize ? 'no-prize' : prize.type, // 系统默认项使用特殊标识
  1194. name: prize.name, // 奖品名称
  1195. description: prize.description || '', // 奖品描述
  1196. type: prize.type, // 奖品类型
  1197. typeName: typeName, // 类型显示名称
  1198. labelClass: labelClass, // 标签样式类
  1199. rewardText: rewardText, // 奖励文本
  1200. imageUrl: imageDisplay, // CDN URL,用于显示
  1201. quantity: prize.quantity || 0, // 奖品数量
  1202. showQuantity: !Controller.api.isNoPrizeType(prize.type), // 是否显示数量
  1203. rate: parseFloat(prize.rate || 0).toFixed(2), // 确保概率值格式化为两位小数
  1204. isSystemPrize: isSystemPrize // 是否为系统奖品
  1205. });
  1206. $('.prize-list').append(prizeHtml);
  1207. console.log('已添加奖品到列表:', prize.name, '索引:', prizeIndex, '系统奖品:', isSystemPrize, '数据:', prize);
  1208. },
  1209. // 添加奖品到列表(新增模式用)- 重构版
  1210. appendPrizeToList: function(prize) {
  1211. // 获取奖品类型信息
  1212. var typeName = Controller.api.getPrizeTypeName(prize.type);
  1213. var labelClass = Controller.api.getPrizeLabelClass(prize.type);
  1214. var rewardText = Controller.api.getPrizeRewardText(prize);
  1215. // 获取当前奖品索引,确保与数据管理器中的索引一致
  1216. var prizeIndex = Controller.api.prizesDataManager.prizesData.length - 1;
  1217. // 处理图片URL
  1218. var imageDisplay = prize.image ? (typeof Fast !== 'undefined' && Fast.api ? Fast.api.cdnurl(prize.image) : prize.image) : '';
  1219. // 生成唯一的奖品ID
  1220. var prizeId = prize.id || ('prize_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9));
  1221. // 使用外部模板 - 重构版(仅用于展示)
  1222. var prizeHtml = Template('prize-item-template', {
  1223. index: prizeIndex,
  1224. prizeId: prizeId,
  1225. prizeType: prize.type, // 奖品类型
  1226. name: prize.name, // 奖品名称
  1227. description: '', // 奖品描述(新增模式通常为空)
  1228. type: prize.type, // 奖品类型
  1229. typeName: typeName, // 类型显示名称
  1230. labelClass: labelClass, // 标签样式类
  1231. rewardText: rewardText, // 奖励文本
  1232. imageUrl: imageDisplay, // CDN URL,用于显示
  1233. quantity: prize.quantity || 0, // 奖品数量
  1234. showQuantity: !Controller.api.isNoPrizeType(prize.type), // 是否显示数量
  1235. rate: parseFloat(prize.rate || 0).toFixed(2), // 确保概率值格式化为两位小数
  1236. isSystemPrize: false // 用户添加的奖品非系统奖品
  1237. });
  1238. $('.prize-list').append(prizeHtml);
  1239. // 重新初始化拖拽排序
  1240. Controller.api.initPrizeSort();
  1241. console.log('奖品已追加到列表,索引:', prizeIndex, '数据:', prize);
  1242. },
  1243. // 初始化默认奖品概率值
  1244. initDefaultPrizeRate: function() {
  1245. console.log('=== 开始初始化奖品概率值 ===');
  1246. // 检查整个表格结构
  1247. var prizeTable = $('.prize-list');
  1248. console.log('奖品表格查找结果:', prizeTable.length > 0);
  1249. if (prizeTable.length > 0) {
  1250. console.log('奖品表格HTML:', prizeTable.html().substring(0, 500) + '...');
  1251. }
  1252. // 检查所有概率输入框
  1253. var allRateInputs = $('.prize-rate');
  1254. console.log('找到的所有概率输入框数量:', allRateInputs.length);
  1255. // 使用更精确的选择器
  1256. var noPrizeInput = $('#no-prize-rate-input');
  1257. console.log('通过ID查找默认未中奖项输入框:', {
  1258. found: noPrizeInput.length > 0,
  1259. value: noPrizeInput.val(),
  1260. visible: noPrizeInput.is(':visible'),
  1261. display: noPrizeInput.css('display'),
  1262. visibility: noPrizeInput.css('visibility')
  1263. });
  1264. // 通过class查找
  1265. var noPrizeInputByClass = $('.prize-item[data-type="no-prize"] .prize-rate');
  1266. console.log('通过class查找默认未中奖项输入框:', {
  1267. found: noPrizeInputByClass.length > 0,
  1268. value: noPrizeInputByClass.val(),
  1269. visible: noPrizeInputByClass.is(':visible')
  1270. });
  1271. // 检查所有奖品行
  1272. var prizeRows = $('.prize-item');
  1273. console.log('找到的奖品行数量:', prizeRows.length);
  1274. prizeRows.each(function(index) {
  1275. var $row = $(this);
  1276. var dataType = $row.attr('data-type');
  1277. var rateInput = $row.find('.prize-rate');
  1278. console.log('奖品行 ' + index + ':', {
  1279. dataType: dataType,
  1280. hasRateInput: rateInput.length > 0,
  1281. rateInputValue: rateInput.val(),
  1282. rateInputVisible: rateInput.is(':visible'),
  1283. rowHTML: $row.html().substring(0, 200) + '...'
  1284. });
  1285. });
  1286. // 确保系统默认未中奖项有正确的概率值
  1287. var defaultPrizeRateInput = noPrizeInput.length > 0 ? noPrizeInput : noPrizeInputByClass;
  1288. if (defaultPrizeRateInput.length > 0) {
  1289. var currentValue = defaultPrizeRateInput.val();
  1290. console.log('当前默认概率值:', currentValue);
  1291. if (!currentValue || currentValue === '' || parseFloat(currentValue) === 0) {
  1292. defaultPrizeRateInput.val('50.00');
  1293. console.log('设置系统默认未中奖项概率为50.00%');
  1294. }
  1295. // 强制确保可见性
  1296. defaultPrizeRateInput.show().css({
  1297. 'display': 'inline-block',
  1298. 'visibility': 'visible',
  1299. 'opacity': '1'
  1300. });
  1301. } else {
  1302. console.log('⚠️ 警告: 未找到系统默认未中奖项的概率输入框!');
  1303. // 尝试创建缺失的输入框
  1304. var noPrizeRow = $('tr[data-type="no-prize"]');
  1305. if (noPrizeRow.length > 0) {
  1306. console.log('找到未中奖行,检查其结构...');
  1307. var rateTd = noPrizeRow.find('td').eq(5); // 概率列
  1308. console.log('概率列内容:', rateTd.html());
  1309. // 如果概率列只有%符号,重新构建
  1310. if (rateTd.text().trim() === '%') {
  1311. console.log('发现概率列只有%符号,重新构建input...');
  1312. rateTd.html(
  1313. '<div class="input-group" style="width: 100px; margin: 0 auto;">' +
  1314. '<input type="number" class="form-control input-sm prize-rate text-center" name="prizes[0][rate]" id="no-prize-rate-input" value="50.00" min="0" max="100" step="0.01">' +
  1315. '<span class="input-group-addon" style="padding: 4px 6px;">%</span>' +
  1316. '</div>'
  1317. );
  1318. console.log('重新构建完成');
  1319. }
  1320. }
  1321. }
  1322. // 确保所有奖品概率输入框都有默认值
  1323. $('.prize-rate').each(function() {
  1324. var $this = $(this);
  1325. var currentValue = $this.val();
  1326. if (!currentValue || currentValue === '') {
  1327. if ($this.attr('id') === 'no-prize-rate-input') {
  1328. $this.val('50.00');
  1329. } else {
  1330. $this.val('0.00');
  1331. }
  1332. }
  1333. });
  1334. // 初始化后更新总概率显示
  1335. Controller.api.updateTotalRate();
  1336. console.log('=== 奖品概率值初始化完成 ===');
  1337. },
  1338. // 绑定奖品相关事件(使用事件委托)
  1339. bindPrizeEvents: function() {
  1340. // 使用事件委托,避免重复绑定
  1341. $(document).off('input.prizeManagement change.prizeManagement click.prizeManagement');
  1342. // 概率输入框事件
  1343. $(document).on('input.prizeManagement', '.prize-rate', function() {
  1344. var $item = $(this).closest('.prize-item');
  1345. var index = parseInt($item.attr('data-index'));
  1346. var rate = parseFloat($(this).val()) || 0;
  1347. // 更新数据管理器中的数据
  1348. Controller.api.prizesDataManager.updatePrizeRate(index, rate);
  1349. // 实时更新总概率
  1350. Controller.api.updateTotalRate();
  1351. });
  1352. $(document).on('change.prizeManagement', '.prize-rate', function() {
  1353. // 失去焦点时格式化为两位小数
  1354. var value = parseFloat($(this).val()) || 0;
  1355. $(this).val(value.toFixed(2));
  1356. Controller.api.updateTotalRate();
  1357. });
  1358. // 奖品数量输入框事件
  1359. $(document).on('input.prizeManagement change.prizeManagement', '.prize-quantity', function() {
  1360. var $item = $(this).closest('.prize-item');
  1361. var index = parseInt($item.attr('data-index'));
  1362. var quantity = parseInt($(this).val()) || 0;
  1363. // 更新数据管理器中的数据
  1364. Controller.api.prizesDataManager.updatePrizeQuantity(index, quantity);
  1365. });
  1366. // 删除奖品按钮事件
  1367. $(document).on('click.prizeManagement', '.delete-prize', function() {
  1368. var $item = $(this).closest('.prize-item');
  1369. var index = parseInt($item.attr('data-index'));
  1370. // 检查是否是系统默认项
  1371. if ($item.attr('data-type') === 'no-prize') {
  1372. Toastr.error('系统默认的未中奖项不能删除');
  1373. return;
  1374. }
  1375. Controller.api.deletePrizeByIndex(index);
  1376. });
  1377. // 编辑奖品按钮事件
  1378. $(document).on('click.prizeManagement', '.edit-prize', function() {
  1379. var $item = $(this).closest('.prize-item');
  1380. var index = parseInt($item.attr('data-index'));
  1381. Controller.api.editPrizeByIndex(index);
  1382. });
  1383. },
  1384. // 通过索引删除奖品
  1385. deletePrizeByIndex: function(index) {
  1386. // 从数据管理器中删除
  1387. if (Controller.api.prizesDataManager.deletePrize(index)) {
  1388. // 重新构建列表
  1389. Controller.api.rebuildPrizeList();
  1390. // 更新统计
  1391. Controller.api.updatePrizeCount();
  1392. Controller.api.updateTotalRate();
  1393. console.log('奖品已删除,索引:', index);
  1394. }
  1395. },
  1396. // 通过索引编辑奖品
  1397. editPrizeByIndex: function(index) {
  1398. var prizeData = Controller.api.prizesDataManager.getPrize(index);
  1399. if (!prizeData) {
  1400. Toastr.error('奖品数据不存在');
  1401. return;
  1402. }
  1403. // 判断是否为系统默认未中奖项
  1404. var isSystemNoPrize = Controller.api.isNoPrizeType(prizeData.type);
  1405. var title = isSystemNoPrize ? '编辑未中奖项' : '编辑奖品';
  1406. // 使用通用表单,编辑模式
  1407. Controller.api.openPrizeForm({
  1408. mode: 'edit',
  1409. title: title,
  1410. prizeIndex: index,
  1411. isSystemNoPrize: isSystemNoPrize,
  1412. data: prizeData
  1413. });
  1414. },
  1415. // 重新构建奖品列表
  1416. rebuildPrizeList: function() {
  1417. // 清空现有列表
  1418. $('.prize-list').empty();
  1419. // 获取所有奖品数据
  1420. var allPrizes = Controller.api.prizesDataManager.getAllPrizes();
  1421. // 重新渲染每个奖品
  1422. for (var i = 0; i < allPrizes.length; i++) {
  1423. Controller.api.addPrizeToList(allPrizes[i], i);
  1424. }
  1425. // 重新初始化拖拽排序
  1426. Controller.api.initPrizeSort();
  1427. console.log('奖品列表已重新构建');
  1428. },
  1429. // 奖品数据管理 - 使用单一JSON隐藏域
  1430. prizesDataManager: {
  1431. // 奖品数据数组
  1432. prizesData: [],
  1433. // 初始化奖品数据管理器
  1434. init: function() {
  1435. // 创建奖品数据隐藏域
  1436. if ($('#prizes-data-json').length === 0) {
  1437. $('<input>').attr({
  1438. type: 'hidden',
  1439. id: 'prizes-data-json',
  1440. name: 'prizes_json',
  1441. value: '[]',
  1442. 'data-rule': 'required;prizesJson',
  1443. 'data-msg-required': '请添加至少一个奖品',
  1444. 'data-msg-prizesJson': '奖品数据格式不正确'
  1445. }).appendTo('form[role="form"]');
  1446. }
  1447. // 初始化数据
  1448. this.loadFromHiddenField();
  1449. },
  1450. // 从隐藏域加载数据
  1451. loadFromHiddenField: function() {
  1452. var jsonData = $('#prizes-data-json').val();
  1453. try {
  1454. this.prizesData = JSON.parse(jsonData || '[]');
  1455. } catch (e) {
  1456. this.prizesData = [];
  1457. console.warn('奖品数据格式错误,已重置为空数组');
  1458. }
  1459. },
  1460. // 保存数据到隐藏域
  1461. saveToHiddenField: function() {
  1462. var jsonData = JSON.stringify(this.prizesData);
  1463. $('#prizes-data-json').val(jsonData);
  1464. console.log('奖品数据已保存到隐藏域:', this.prizesData);
  1465. },
  1466. // 添加奖品
  1467. addPrize: function(prizeData) {
  1468. // 生成唯一ID
  1469. prizeData.id = 'prize_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9);
  1470. this.prizesData.push(prizeData);
  1471. this.saveToHiddenField();
  1472. return prizeData.id;
  1473. },
  1474. // 更新奖品
  1475. updatePrize: function(index, prizeData) {
  1476. if (index >= 0 && index < this.prizesData.length) {
  1477. // 保持原有ID
  1478. prizeData.id = this.prizesData[index].id;
  1479. this.prizesData[index] = prizeData;
  1480. this.saveToHiddenField();
  1481. return true;
  1482. }
  1483. return false;
  1484. },
  1485. // 删除奖品
  1486. deletePrize: function(index) {
  1487. if (index >= 0 && index < this.prizesData.length) {
  1488. this.prizesData.splice(index, 1);
  1489. this.saveToHiddenField();
  1490. return true;
  1491. }
  1492. return false;
  1493. },
  1494. // 获取奖品数据
  1495. getPrize: function(index) {
  1496. if (index >= 0 && index < this.prizesData.length) {
  1497. return this.prizesData[index];
  1498. }
  1499. return null;
  1500. },
  1501. // 获取所有奖品数据
  1502. getAllPrizes: function() {
  1503. return this.prizesData;
  1504. },
  1505. // 更新奖品数量
  1506. updatePrizeQuantity: function(index, quantity) {
  1507. if (index >= 0 && index < this.prizesData.length) {
  1508. this.prizesData[index].quantity = parseInt(quantity) || 0;
  1509. this.saveToHiddenField();
  1510. return true;
  1511. }
  1512. return false;
  1513. },
  1514. // 更新奖品概率
  1515. updatePrizeRate: function(index, rate) {
  1516. if (index >= 0 && index < this.prizesData.length) {
  1517. this.prizesData[index].rate = parseFloat(rate) || 0;
  1518. this.saveToHiddenField();
  1519. return true;
  1520. }
  1521. return false;
  1522. },
  1523. // 重新排序奖品
  1524. reorderPrizes: function(newOrder) {
  1525. var reorderedData = [];
  1526. for (var i = 0; i < newOrder.length; i++) {
  1527. var oldIndex = newOrder[i];
  1528. if (oldIndex >= 0 && oldIndex < this.prizesData.length) {
  1529. reorderedData.push(this.prizesData[oldIndex]);
  1530. }
  1531. }
  1532. this.prizesData = reorderedData;
  1533. this.saveToHiddenField();
  1534. }
  1535. },
  1536. // 实时更新奖品隐藏域数据(重构版 - 使用奖品数据管理器)
  1537. updatePrizeHiddenFields: function() {
  1538. // 使用奖品数据管理器保存数据
  1539. Controller.api.prizesDataManager.saveToHiddenField();
  1540. },
  1541. // 初始化奖品拖拽排序 - 重构版
  1542. initPrizeSort: function() {
  1543. require(['../libs/Sortable/Sortable'], function(Sortable) {
  1544. var prizeListEl = document.querySelector('.prize-list');
  1545. if (prizeListEl) {
  1546. // 如果已经初始化过,先销毁
  1547. if (prizeListEl.sortableInstance) {
  1548. prizeListEl.sortableInstance.destroy();
  1549. }
  1550. // 创建新的 Sortable 实例
  1551. prizeListEl.sortableInstance = new Sortable(prizeListEl, {
  1552. handle: '.prize-sort-handle',
  1553. animation: 150,
  1554. onUpdate: function(evt) {
  1555. // 拖拽排序后重新排列数据管理器中的数据
  1556. Controller.api.updatePrizeOrderFromDOM();
  1557. }
  1558. });
  1559. }
  1560. });
  1561. },
  1562. // 从DOM更新奖品顺序到数据管理器
  1563. updatePrizeOrderFromDOM: function() {
  1564. var newOrder = [];
  1565. $('.prize-list .prize-item').each(function(index) {
  1566. var oldIndex = parseInt($(this).attr('data-index'));
  1567. if (!isNaN(oldIndex)) {
  1568. newOrder.push(oldIndex);
  1569. }
  1570. });
  1571. // 重新排序数据管理器中的数据
  1572. Controller.api.prizesDataManager.reorderPrizes(newOrder);
  1573. // 重新构建列表以更新索引
  1574. Controller.api.rebuildPrizeList();
  1575. console.log('奖品拖拽排序完成,新顺序:', newOrder);
  1576. },
  1577. // 获取奖品类型名称
  1578. getPrizeTypeName: function(type) {
  1579. var prizeTypeMap = Controller.api.parseConfigJson('prizeTypeList', {});
  1580. return prizeTypeMap[type] || '未知';
  1581. },
  1582. // 获取奖品标签样式类
  1583. getPrizeLabelClass: function(type) {
  1584. var classMap = {
  1585. 1: 'default', // 未中奖 - 灰色
  1586. 2: 'primary', // 实物奖品 - 蓝色
  1587. 3: 'warning', // 积分 - 橙色
  1588. 4: 'info', // 余额 - 浅蓝色
  1589. 5: 'success', // 优惠券 - 绿色
  1590. 6: 'danger', // 红包 - 红色
  1591. 7: 'default', // 兑换码 - 灰色
  1592. 8: 'primary' // 商城奖品 - 蓝色
  1593. };
  1594. return classMap[type] || 'default';
  1595. },
  1596. // 获取奖品奖励文本
  1597. getPrizeRewardText: function(prize) {
  1598. if (!prize.type || prize.type == 1) {
  1599. return ''; // 未中奖不显示奖励文本
  1600. }
  1601. switch(parseInt(prize.type)) {
  1602. case 2: // 实物奖品
  1603. return '';
  1604. case 3: // 积分
  1605. return prize.points ? (prize.points + ' 积分') : '';
  1606. case 4: // 余额
  1607. return prize.balance ? (prize.balance + ' 余额') : '';
  1608. case 5: // 优惠券
  1609. return prize.coupon_name ? prize.coupon_name : '';
  1610. case 6: // 红包
  1611. return prize.redpack_amount ? (prize.redpack_amount + ' 元红包') : '';
  1612. case 7: // 兑换码
  1613. return prize.code_type == 'manual' ? '手动兑换码' : '自动兑换码';
  1614. case 8: // 商城奖品
  1615. return prize.goods_name ? prize.goods_name : '';
  1616. default:
  1617. return '';
  1618. }
  1619. },
  1620. // 从表单获取奖品奖励文本
  1621. getPrizeRewardTextFromForm: function(form, type) {
  1622. if (!type || type == 1) {
  1623. return ''; // 未中奖不显示奖励文本
  1624. }
  1625. switch(parseInt(type)) {
  1626. case 2: // 实物奖品
  1627. return '';
  1628. case 3: // 积分
  1629. var points = form.find('input[name="reward_value_integral"]').val();
  1630. return points ? (points + ' 积分') : '';
  1631. case 4: // 余额
  1632. var balance = form.find('input[name="reward_value_balance"]').val();
  1633. return balance ? (balance + ' 余额') : '';
  1634. case 5: // 优惠券
  1635. // 从优惠券显示区域获取名称
  1636. var couponName = form.find('#selected-coupon-display').text().trim() ||
  1637. form.find('.selected-coupon-item span:first').text();
  1638. return couponName || '';
  1639. case 6: // 红包
  1640. var amount = form.find('input[name="reward_value_redpack"]').val();
  1641. return amount ? (amount + ' 元红包') : '';
  1642. case 7: // 兑换码
  1643. var codeType = form.find('input[name="code_type"]:checked').val();
  1644. return codeType == 'manual' ? '手动兑换码' : '自动兑换码';
  1645. case 8: // 商城奖品
  1646. // 从商品显示区域获取名称
  1647. return form.find('#selected-goods-display .goods-name-display').text() ||
  1648. form.find('#selected-goods-display .prize-goods-item .goods-name-display').text() ||
  1649. form.find('.prize-goods-item .goods-name-display').text() || '';
  1650. default:
  1651. return '';
  1652. }
  1653. },
  1654. // 根据奖品类型获取奖励类型
  1655. getRewardTypeByPrizeType: function(prizeType) {
  1656. switch(parseInt(prizeType)) {
  1657. case 3: return 'integral'; // 积分
  1658. case 4: return 'balance'; // 余额
  1659. case 5: return 'coupon'; // 优惠券
  1660. case 6: return 'redbag'; // 红包
  1661. case 7: return 'code'; // 兑换码
  1662. case 8: return 'goods'; // 商城奖品
  1663. default: return 'none';
  1664. }
  1665. },
  1666. // 判断是否为未中奖类型
  1667. isNoPrizeType: function(type, element) {
  1668. var typeValue = parseInt(type);
  1669. var isNoPrizeByType = typeValue === 1;
  1670. var isNoPrizeByAttr = element && $(element).attr('data-type') === 'no-prize';
  1671. return isNoPrizeByType || isNoPrizeByAttr;
  1672. },
  1673. // 更新奖品数量统计
  1674. updatePrizeCount: function() {
  1675. var totalCount = $('.prize-item').length; // 总奖品数(包含未中奖项)
  1676. var userCount = totalCount - 1; // 用户添加的奖品数(减去未中奖项)
  1677. console.log('updatePrizeCount - 总奖品数:', totalCount, '用户添加数:', userCount);
  1678. // 显示总奖品数(包含系统默认的未中奖项)
  1679. $('#prize-count').text(totalCount);
  1680. // 更新添加奖品按钮状态(基于用户可添加的奖品数)
  1681. Controller.api.updateAddPrizeButtonState(userCount);
  1682. },
  1683. // 更新添加奖品按钮状态
  1684. updateAddPrizeButtonState: function(userCount) {
  1685. var maxUserCount = 7; // 用户最大可添加奖品数量(总共8个,包括系统默认的未中奖项)
  1686. var addButton = $('#add-prize-btn');
  1687. var remainingCount = maxUserCount - userCount;
  1688. console.log('updateAddPrizeButtonState - 用户奖品数:', userCount, '最大允许:', maxUserCount, '剩余:', remainingCount);
  1689. if (userCount >= maxUserCount) {
  1690. // 达到最大数量,禁用按钮
  1691. addButton.prop('disabled', true).addClass('disabled');
  1692. addButton.find('.btn-text').text('已达最大数量');
  1693. $('#prize-count-tip').text('已添加最大数量的奖品').removeClass('text-muted').addClass('text-danger');
  1694. console.log('按钮已禁用 - 达到最大数量');
  1695. } else {
  1696. // 未达到最大数量,启用按钮
  1697. addButton.prop('disabled', false).removeClass('disabled');
  1698. addButton.find('.btn-text').text('添加奖品');
  1699. if (remainingCount <= 2) {
  1700. $('#prize-count-tip').text('还可添加 ' + remainingCount + ' 个奖品').removeClass('text-muted text-danger').addClass('text-warning');
  1701. } else {
  1702. // 当用户还没添加奖品时,显示还需添加多少个才能满足最低要求
  1703. var minRequired = Math.max(0, maxUserCount - userCount); // 需要添加的奖品数量
  1704. if (minRequired > 0) {
  1705. $('#prize-count-tip').text('还需添加 ' + minRequired + ' 个奖品').removeClass('text-warning text-danger').addClass('text-muted');
  1706. } else {
  1707. $('#prize-count-tip').text('还可添加 ' + remainingCount + ' 个奖品').removeClass('text-warning text-danger').addClass('text-muted');
  1708. }
  1709. }
  1710. console.log('按钮已启用 - 剩余数量:', remainingCount);
  1711. }
  1712. },
  1713. // 计算总概率
  1714. calculateTotalRate: function() {
  1715. var total = 0;
  1716. $('.prize-rate').each(function() {
  1717. total += parseFloat($(this).val()) || 0;
  1718. });
  1719. // 保留两位小数
  1720. return Math.round(total * 100) / 100;
  1721. },
  1722. // 更新总概率显示
  1723. updateTotalRate: function() {
  1724. var total = Controller.api.calculateTotalRate();
  1725. // 格式化显示为两位小数
  1726. $('#total-rate').text(total.toFixed(2) + '%');
  1727. // 根据概率设置颜色
  1728. if (total > 100) {
  1729. $('#total-rate').removeClass('text-success text-warning').addClass('text-danger');
  1730. } else if (total === 100) {
  1731. $('#total-rate').removeClass('text-danger text-warning').addClass('text-success');
  1732. } else {
  1733. $('#total-rate').removeClass('text-danger text-success').addClass('text-warning');
  1734. }
  1735. },
  1736. // 编辑奖品(统一处理所有类型) - 已弃用,请使用 editPrizeByIndex
  1737. editPrize: function(prizeItem) {
  1738. // 获取奖品索引
  1739. var prizeIndex = parseInt(prizeItem.attr('data-index'));
  1740. if (isNaN(prizeIndex)) {
  1741. Toastr.error('无法获取奖品索引');
  1742. return;
  1743. }
  1744. // 调用新的编辑方法
  1745. Controller.api.editPrizeByIndex(prizeIndex);
  1746. },
  1747. // 更新奖品信息(重构版 - 使用数据管理器)
  1748. updatePrize: function($form, config) {
  1749. console.log('updatePrize 调用参数:', {form: $form, config: config});
  1750. // 验证config参数
  1751. if (!config || typeof config !== 'object') {
  1752. console.error('updatePrize: config参数无效:', config);
  1753. Toastr.error('配置参数错误');
  1754. return false;
  1755. }
  1756. // 验证prizeIndex
  1757. if (typeof config.prizeIndex === 'undefined' || config.prizeIndex === null) {
  1758. console.error('updatePrize: prizeIndex参数缺失:', config);
  1759. Toastr.error('奖品索引参数缺失');
  1760. return false;
  1761. }
  1762. // 收集表单数据
  1763. var prizeData = Controller.api.collectPrizeFormData($form);
  1764. if (!prizeData) {
  1765. return false; // 验证失败
  1766. }
  1767. var prizeIndex = parseInt(config.prizeIndex);
  1768. if (isNaN(prizeIndex) || prizeIndex < 0) {
  1769. console.error('updatePrize: prizeIndex无效:', config.prizeIndex);
  1770. Toastr.error('奖品索引无效');
  1771. return false;
  1772. }
  1773. // 使用数据管理器更新奖品
  1774. if (Controller.api.prizesDataManager.updatePrize(prizeIndex, prizeData)) {
  1775. // 重新构建列表以反映更新
  1776. Controller.api.rebuildPrizeList();
  1777. // 更新统计
  1778. Controller.api.updatePrizeCount();
  1779. Controller.api.updateTotalRate();
  1780. console.log('奖品更新成功:', prizeData, '索引:', prizeIndex);
  1781. return true;
  1782. } else {
  1783. console.error('updatePrize: 数据管理器更新失败');
  1784. Toastr.error('更新奖品失败');
  1785. return false;
  1786. }
  1787. },
  1788. // 在表单提交前收集条件数据 - 已弃用,改为使用规则数据管理器
  1789. collectConditionData: function() {
  1790. // 这个方法已经不再使用,改为使用规则数据管理器
  1791. console.warn('collectConditionData方法已弃用,请使用规则数据管理器');
  1792. var conditionData = {};
  1793. var taskTypes = [];
  1794. // 获取选中的任务类型
  1795. $('input[name="row[task_type][]"]:checked').each(function() {
  1796. taskTypes.push($(this).val());
  1797. });
  1798. // 为每个任务类型收集条件数据
  1799. taskTypes.forEach(function(taskType) {
  1800. var type = parseInt(taskType);
  1801. var condition = {};
  1802. switch(type) {
  1803. case 1: // 购买指定商品
  1804. condition.type = type;
  1805. condition.goods_rule = $('input[name="condition[' + type + '][goods_rule]"]:checked').val() || 1;
  1806. condition.goods_ids = $('#task-goods-ids-' + type).val() || '[]';
  1807. break;
  1808. case 2: // 单笔订单消费满额
  1809. case 3: // 单次充值满额
  1810. case 4: // 活动期间累计消费满额
  1811. condition.type = type;
  1812. condition.condition_value = $('input[name="condition[' + type + '][condition_value]"]').val() || 0;
  1813. break;
  1814. }
  1815. conditionData[type] = condition;
  1816. });
  1817. return {
  1818. conditionData: conditionData,
  1819. taskTypes: taskTypes
  1820. };
  1821. },
  1822. // 选择商品 - 通用方法,支持单选和多选
  1823. selectGoods: function(options) {
  1824. var defaults = {
  1825. mode: 'single', // 'single' 单选, 'multiple' 多选
  1826. title: '选择商品',
  1827. container: '#selected-goods',
  1828. template: 'goods-list-template', // 统一使用goods-list-template
  1829. maxSelect: 0, // 最大选择数量,0表示无限制
  1830. callback: null
  1831. };
  1832. var config = $.extend({}, defaults, options);
  1833. // 构建URL参数
  1834. var urlParams = [];
  1835. // 添加选择模式参数
  1836. if (config.mode === 'single') {
  1837. urlParams.push('selectMode=single');
  1838. } else {
  1839. urlParams.push('selectMode=multiple');
  1840. // 如果有最大选择限制,添加参数
  1841. if (config.maxSelect > 0) {
  1842. urlParams.push('maxSelect=' + config.maxSelect);
  1843. }
  1844. }
  1845. // 构建完整URL
  1846. var url = 'shop/goods/select';
  1847. if (urlParams.length > 0) {
  1848. url += '?' + urlParams.join('&');
  1849. }
  1850. Fast.api.open(url, config.title, {
  1851. callback: function(data) {
  1852. if (data && data.length > 0) {
  1853. // 单选模式只取第一个
  1854. if (config.mode === 'single' && data.length > 1) {
  1855. data = [data[0]];
  1856. }
  1857. Controller.api.updateSelectedGoods(data, config);
  1858. // 执行回调
  1859. if (typeof config.callback === 'function') {
  1860. config.callback(data);
  1861. }
  1862. } else {
  1863. Toastr.error('请选择商品');
  1864. }
  1865. }
  1866. });
  1867. },
  1868. // 选择任务商品(多选模式)
  1869. selectTaskGoods: function(container) {
  1870. Controller.api.selectGoods({
  1871. mode: 'multiple',
  1872. title: '选择参与商品',
  1873. container: container || '#task-goods-container',
  1874. template: 'task-goods-template',
  1875. callback: function(data) {
  1876. // 如果是购买指定商品任务,更新隐藏字段
  1877. if (container === '#selected-goods' && data && data.length > 0) {
  1878. var goodsIds = data.map(function(item) {
  1879. return item.id;
  1880. });
  1881. // 更新隐藏字段,存储JSON格式的商品IDs
  1882. $('#task-goods-ids-1').val(JSON.stringify(goodsIds));
  1883. console.log('购买指定商品任务 - 商品IDs已更新:', goodsIds);
  1884. // 绑定删除事件
  1885. Controller.api.bindTaskGoodsEvents();
  1886. // 更新规则数据管理器
  1887. Controller.api.updateRulesDataFromDOM();
  1888. }
  1889. }
  1890. });
  1891. },
  1892. // 选择奖品商品(单选模式)
  1893. selectPrizeGoods: function(container) {
  1894. Controller.api.selectGoods({
  1895. mode: 'single',
  1896. title: '选择奖品商品',
  1897. container: container || '#prize-goods-container',
  1898. template: 'goods-list-template' // 使用统一的商品选择模板
  1899. });
  1900. },
  1901. // 更新已选商品
  1902. updateSelectedGoods: function(goods, config) {
  1903. config = config || {};
  1904. var container = config.container || '#selected-goods';
  1905. var template = config.template || 'goods-list-template';
  1906. // 预处理商品数据
  1907. var processedGoods = Controller.api.processGoodsData(goods);
  1908. var html = '';
  1909. if (template === 'goods-list-template') {
  1910. // 商品列表(支持单选和多选)
  1911. html = Template(template, {
  1912. goods: processedGoods
  1913. });
  1914. $(container).html(html);
  1915. // 绑定删除事件
  1916. $('.remove-goods-item').off('click').on('click', function() {
  1917. $(this).closest('.goods-list-item').remove();
  1918. });
  1919. } else if (template === 'prize-goods-template') {
  1920. // 奖品商品(单选)
  1921. if (processedGoods.length > 0) {
  1922. html = Template(template, processedGoods[0]);
  1923. $(container).html(html);
  1924. // 绑定删除事件
  1925. $('.remove-prize-goods').off('click').on('click', function() {
  1926. $(this).closest('.prize-goods-item').remove();
  1927. });
  1928. }
  1929. } else {
  1930. // 其他模板(向后兼容)
  1931. html = Template(template, {
  1932. goods: processedGoods
  1933. });
  1934. $(container).html(html);
  1935. }
  1936. },
  1937. // 处理商品数据
  1938. processGoodsData: function(goods) {
  1939. var processedGoods = [];
  1940. if (goods && goods.length) {
  1941. for (var i = 0; i < goods.length; i++) {
  1942. var item = goods[i];
  1943. var processedItem = {
  1944. id: item.id,
  1945. name: item.name || item.title,
  1946. title: item.title || item.name,
  1947. image: item.image ? (typeof Fast !== 'undefined' && Fast.api ? Fast.api.cdnurl(item.image) : item.image) : '',
  1948. price: item.price || item.sellprice || '',
  1949. goods_sn: item.goods_sn || '',
  1950. spec_type: item.spec_type !== undefined ? item.spec_type : null,
  1951. category: item.category || null,
  1952. type_name: Controller.api.getGoodsTypeName(item),
  1953. type_text: Controller.api.getGoodsTypeText(item)
  1954. };
  1955. processedGoods.push(processedItem);
  1956. }
  1957. }
  1958. return processedGoods;
  1959. },
  1960. // 获取商品类型文本(用于显示标签)
  1961. getGoodsTypeText: function(goods) {
  1962. // 根据商品类型返回相应的标签文本
  1963. if (goods.type == 2) {
  1964. return '密'; // 虚拟商品显示"密"
  1965. }
  1966. if (goods.isrecom == 1) {
  1967. return '荐'; // 推荐商品显示"荐"
  1968. }
  1969. if (goods.ishot == 1) {
  1970. return '热'; // 热门商品显示"热"
  1971. }
  1972. if (goods.isnew == 1) {
  1973. return '新'; // 新品显示"新"
  1974. }
  1975. return ''; // 默认无标签
  1976. },
  1977. // 获取商品类型名称
  1978. getGoodsTypeName: function(goods) {
  1979. // 根据商品类型返回类型名称
  1980. if (goods.type == 1) {
  1981. return '普通商品';
  1982. } else if (goods.type == 2) {
  1983. return '虚拟商品';
  1984. } else if (goods.type == 3) {
  1985. return '服务商品';
  1986. }
  1987. return '普通商品'; // 默认类型
  1988. },
  1989. // 绑定任务商品删除事件
  1990. bindTaskGoodsEvents: function() {
  1991. $('.remove-task-goods').off('click').on('click', function() {
  1992. var goodsId = $(this).data('id');
  1993. var $item = $(this).closest('.goods-list-item');
  1994. // 从DOM中移除商品项
  1995. $item.remove();
  1996. // 更新隐藏字段中的商品IDs
  1997. Controller.api.updateTaskGoodsIds();
  1998. // 更新规则数据管理器
  1999. Controller.api.updateRulesDataFromDOM();
  2000. });
  2001. },
  2002. // 更新任务商品IDs隐藏字段
  2003. updateTaskGoodsIds: function() {
  2004. var goodsIds = [];
  2005. $('#selected-goods .goods-list-item').each(function() {
  2006. var goodsId = $(this).data('id');
  2007. if (goodsId) {
  2008. goodsIds.push(goodsId);
  2009. }
  2010. });
  2011. // 更新隐藏字段
  2012. $('#task-goods-ids-1').val(JSON.stringify(goodsIds));
  2013. console.log('任务商品IDs已更新:', goodsIds);
  2014. },
  2015. // 规则数据管理器 - 类似奖品数据管理器
  2016. rulesDataManager: {
  2017. // 规则数据对象
  2018. rulesData: {},
  2019. // 初始化规则数据管理器
  2020. init: function() {
  2021. // 创建规则数据隐藏域
  2022. if ($('#rules-data-json').length === 0) {
  2023. $('<input>').attr({
  2024. type: 'hidden',
  2025. id: 'rules-data-json',
  2026. name: 'rules_json',
  2027. value: '{}',
  2028. 'data-rule': 'required;rulesJson',
  2029. 'data-msg-required': '请设置至少一种参与条件',
  2030. 'data-msg-rulesJson': '规则设置数据格式不正确'
  2031. }).appendTo('form[role="form"]');
  2032. }
  2033. // 初始化数据
  2034. this.loadFromHiddenField();
  2035. },
  2036. // 从隐藏域加载数据
  2037. loadFromHiddenField: function() {
  2038. var jsonData = $('#rules-data-json').val();
  2039. try {
  2040. this.rulesData = JSON.parse(jsonData || '{}');
  2041. } catch (e) {
  2042. this.rulesData = {};
  2043. console.warn('规则数据格式错误,已重置为空对象');
  2044. }
  2045. },
  2046. // 保存数据到隐藏域
  2047. saveToHiddenField: function() {
  2048. var jsonData = JSON.stringify(this.rulesData);
  2049. $('#rules-data-json').val(jsonData);
  2050. console.log('规则数据已保存到隐藏域:', this.rulesData);
  2051. },
  2052. // 更新规则数据
  2053. updateRule: function(type, ruleData) {
  2054. this.rulesData[type] = ruleData;
  2055. this.saveToHiddenField();
  2056. },
  2057. // 删除规则
  2058. deleteRule: function(type) {
  2059. delete this.rulesData[type];
  2060. this.saveToHiddenField();
  2061. },
  2062. // 获取规则数据
  2063. getRule: function(type) {
  2064. return this.rulesData[type] || null;
  2065. },
  2066. // 获取所有规则数据
  2067. getAllRules: function() {
  2068. return this.rulesData;
  2069. }
  2070. },
  2071. // 收集规则数据并保存到隐藏域
  2072. collectAndSaveRulesData: function() {
  2073. var rulesData = {};
  2074. var taskTypes = [];
  2075. // 获取选中的任务类型
  2076. $('input[name="row[task_type][]"]:checked').each(function() {
  2077. taskTypes.push($(this).val());
  2078. });
  2079. // 为每个任务类型收集规则数据
  2080. taskTypes.forEach(function(taskType) {
  2081. var type = parseInt(taskType);
  2082. var rule = {
  2083. type: type
  2084. };
  2085. switch(type) {
  2086. case 1: // 购买指定商品
  2087. rule.goods_rule = $('input[name="condition[' + type + '][goods_rule]"]:checked').val() || 1;
  2088. rule.goods_ids = $('#task-goods-ids-' + type).val() || '[]';
  2089. break;
  2090. case 2: // 单笔订单消费满额
  2091. case 3: // 单次充值满额
  2092. case 4: // 活动期间累计消费满额
  2093. rule.condition_value = $('input[name="condition[' + type + '][condition_value]"]').val() || 0;
  2094. break;
  2095. }
  2096. rulesData[type] = rule;
  2097. });
  2098. // 保存到规则数据管理器
  2099. Controller.api.rulesDataManager.rulesData = rulesData;
  2100. Controller.api.rulesDataManager.saveToHiddenField();
  2101. console.log('规则数据已收集并保存:', rulesData);
  2102. },
  2103. // 从DOM更新规则数据
  2104. updateRulesDataFromDOM: function() {
  2105. // 在任务类型变化时同步更新规则数据
  2106. Controller.api.collectAndSaveRulesData();
  2107. }
  2108. }
  2109. };
  2110. // 全局函数,供HTML调用
  2111. window.addPrize = function() {
  2112. Controller.api.addPrize();
  2113. };
  2114. // 默认商品选择(针对购买指定商品任务,使用多选模式)
  2115. window.selectGoods = function() {
  2116. Controller.api.selectGoods({
  2117. mode: 'multiple',
  2118. title: '选择参与商品',
  2119. container: '#selected-goods',
  2120. template: 'goods-list-template'
  2121. });
  2122. };
  2123. // 任务商品选择(多选)
  2124. window.selectTaskGoods = function(container) {
  2125. Controller.api.selectTaskGoods(container);
  2126. };
  2127. // 奖品商品选择(单选)
  2128. window.selectPrizeGoods = function(container) {
  2129. Controller.api.selectPrizeGoods(container);
  2130. };
  2131. // 暴露奖品数据管理器到全局
  2132. window.Controller = Controller;
  2133. return Controller;
  2134. });