delivery.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. (function () {
  2. /***
  3. * 配送区域设置
  4. * @param options
  5. * @constructor
  6. */
  7. function delivery(options) {
  8. var option = $.extend(true, {
  9. el: '#app',
  10. name: '',
  11. method: 10,
  12. regions: {},
  13. cityCount: 0,
  14. formData: []
  15. }, options);
  16. var app = this.createVueApp(option);
  17. app.initializeForms();
  18. }
  19. delivery.prototype = {
  20. createVueApp: function (option) {
  21. return new Vue({
  22. el: option.el,
  23. data: {
  24. // 模板名称
  25. name: option.name,
  26. // 计费方式
  27. method: option.method,
  28. // 所有地区
  29. regions: option.regions,
  30. // 全选状态
  31. checkAll: false,
  32. // 当前选择的地区id集
  33. checked: {
  34. province: [],
  35. citys: []
  36. },
  37. // 禁止选择的地区id集
  38. disable: {
  39. province: [],
  40. citys: [],
  41. treeData: {}
  42. },
  43. // 已选择的区域和运费form项
  44. forms: []
  45. },
  46. methods: {
  47. // 初始化forms
  48. initializeForms: function () {
  49. var app = this;
  50. if (!option.formData.length) return false;
  51. option.formData.forEach(function (form) {
  52. // 转换为整数型
  53. for (var key in form.citys) {
  54. if (form.citys.hasOwnProperty(key)) {
  55. form.citys[key] = parseInt(form.citys[key]);
  56. }
  57. }
  58. form['treeData'] = app.getTreeData({
  59. province: form.province,
  60. citys: form.citys
  61. });
  62. app.forms.push(form);
  63. });
  64. },
  65. // 添加配送区域
  66. onAddRegionEvent: function () {
  67. var app = this;
  68. // 判断是否选择了全国
  69. var total = 0;
  70. app.forms.forEach(function (item) {
  71. total += item.citys.length;
  72. });
  73. if (total >= option.cityCount) {
  74. layer.msg('已经选择了所有区域~');
  75. return false;
  76. }
  77. // 显示选择可配送区域弹窗
  78. app.onShowCheckBox({
  79. complete: function (checked) {
  80. // 选择完成后新增form项
  81. app.forms.push({
  82. province: checked.province,
  83. citys: checked.citys,
  84. treeData: app.getTreeData(checked)
  85. });
  86. }
  87. });
  88. },
  89. // 全选
  90. onCheckAll: function (checked) {
  91. var app = this;
  92. app.checkAll = checked;
  93. // 遍历能选择的地区
  94. for (var key in app.regions) {
  95. if (app.regions.hasOwnProperty(key)) {
  96. var item = app.regions[key];
  97. if (!app.isPropertyExist(item.id, app.disable.treeData)
  98. || !app.disable.treeData[item.id].isAllCitys) {
  99. var provinceId = parseInt(item.id);
  100. this.checkedProvince(provinceId, app.checkAll);
  101. }
  102. }
  103. }
  104. },
  105. // 标记不可选的地区
  106. onDisableRegion: function (ignoreFormIndex) {
  107. var app = this;
  108. // 清空禁选地区
  109. var disable = {province: [], citys: []};
  110. for (var key in app.forms) {
  111. if (app.forms.hasOwnProperty(key)) {
  112. if (ignoreFormIndex > -1 && ignoreFormIndex === parseInt(key)) continue;
  113. var item = app.forms[key];
  114. disable.province = app.arrayMerge(disable.province, item.province);
  115. disable.citys = app.arrayMerge(disable.citys, item.citys);
  116. }
  117. }
  118. app.disable = {
  119. province: disable.province,
  120. citys: disable.citys,
  121. treeData: app.getTreeData(disable)
  122. };
  123. },
  124. // 将选中的区域id集格式化为树状格式
  125. getTreeData: function (checkedData) {
  126. var app = this;
  127. var treeData = {};
  128. checkedData.province.forEach(function (provinceId) {
  129. var province = app.regions[provinceId]
  130. , citys = []
  131. , cityCount = 0;
  132. for (var cityIndex in province.city) {
  133. if (province.city.hasOwnProperty(cityIndex)) {
  134. var cityItem = province.city[cityIndex];
  135. if (app.inArray(cityItem.id, checkedData.citys)) {
  136. citys.push({id: cityItem.id, name: cityItem.name});
  137. }
  138. cityCount++;
  139. }
  140. }
  141. treeData[province.id] = {
  142. id: province.id,
  143. name: province.name,
  144. citys: citys,
  145. isAllCitys: citys.length === cityCount
  146. };
  147. });
  148. return treeData;
  149. },
  150. // 编辑配送区域
  151. onEditerForm: function (formIndex, formItem) {
  152. var app = this;
  153. // 显示选择可配送区域弹窗
  154. app.onShowCheckBox({
  155. editerFormIndex: formIndex,
  156. checkedData: {
  157. province: formItem.province,
  158. citys: formItem.citys
  159. },
  160. complete: function (data) {
  161. // var formItem = app.forms[formIndex];
  162. formItem.province = data.province;
  163. formItem.citys = data.citys;
  164. formItem.treeData = app.getTreeData(data);
  165. }
  166. });
  167. },
  168. // 删除配送区域
  169. onDeleteForm: function (formIndex) {
  170. var app = this;
  171. layer.confirm('确定要删除吗?'
  172. , {title: '友情提示'}
  173. , function (index) {
  174. app.forms.splice(formIndex, 1);
  175. layer.close(index);
  176. }
  177. );
  178. },
  179. // 显示选择可配送区域弹窗
  180. onShowCheckBox: function (option) {
  181. var app = this;
  182. var options = $.extend(true, {
  183. editerFormIndex: -1,
  184. checkedData: null,
  185. complete: $.noop()
  186. }, option);
  187. // 已选中的数据
  188. app.checked = options.checkedData ? options.checkedData : {
  189. province: [],
  190. citys: []
  191. };
  192. // 标记不可选的地区
  193. app.onDisableRegion(options.editerFormIndex);
  194. // 取消全选按钮
  195. app.checkAll = false;
  196. // 开启弹窗
  197. layer.open({
  198. type: 1,
  199. shade: false,
  200. title: '选择可配送区域',
  201. btn: ['确定', '取消'],
  202. area: ['820px', '520px'], //宽高
  203. content: $(this.$refs['choice']),
  204. yes: function (index) {
  205. if (app.checked.citys.length <= 0) {
  206. layer.msg('请选择区域~');
  207. return false;
  208. }
  209. options.complete(app.checked);
  210. layer.close(index);
  211. }
  212. });
  213. },
  214. // 选择省份
  215. onCheckedProvince: function (e) {
  216. var provinceId = parseInt(e.target.value);
  217. this.checkedProvince(provinceId, e.target.checked);
  218. },
  219. checkedProvince: function (provinceId, checked) {
  220. var app = this;
  221. // 更新省份选择
  222. var index = app.checked.province.indexOf(provinceId);
  223. if (!checked) {
  224. index > -1 && app.checked.province.splice(index, 1);
  225. } else {
  226. index === -1 && app.checked.province.push(provinceId);
  227. }
  228. // 更新城市选择
  229. var cityIds = app.regions[provinceId].city;
  230. for (var cityIndex in cityIds) {
  231. if (cityIds.hasOwnProperty(cityIndex)) {
  232. var cityId = parseInt(cityIndex);
  233. var checkedIndex = app.checked.citys.indexOf(cityId);
  234. if (!checked) {
  235. checkedIndex > -1 && app.checked.citys.splice(checkedIndex, 1)
  236. } else {
  237. checkedIndex === -1 && app.checked.citys.push(cityId);
  238. }
  239. }
  240. }
  241. },
  242. // 选择城市
  243. onCheckedCity: function (e, provinceId) {
  244. var cityId = parseInt(e.target.value);
  245. if (!e.target.checked) {
  246. var index = this.checked.citys.indexOf(cityId);
  247. index > -1 && this.checked.citys.splice(index, 1)
  248. } else {
  249. this.checked.citys.push(cityId);
  250. }
  251. // 更新省份选中状态
  252. this.onUpdateProvinceChecked(parseInt(provinceId));
  253. },
  254. // 更新省份选中状态
  255. onUpdateProvinceChecked: function (provinceId) {
  256. var provinceIndex = this.checked.province.indexOf(provinceId);
  257. var isExist = provinceIndex > -1;
  258. if (!this.onHasCityChecked(provinceId)) {
  259. isExist && this.checked.province.splice(provinceIndex, 1);
  260. } else {
  261. !isExist && this.checked.province.push(provinceId);
  262. }
  263. },
  264. // 是否存在城市被选中
  265. onHasCityChecked: function (provinceId) {
  266. var app = this;
  267. var cityIds = this.regions[provinceId].city;
  268. for (var cityId in cityIds) {
  269. if (cityIds.hasOwnProperty(cityId)
  270. && app.inArray(parseInt(cityId), app.checked.citys))
  271. return true;
  272. }
  273. return false;
  274. },
  275. // 数组中是否存在指定的值
  276. inArray: function (val, array) {
  277. return array.indexOf(val) > -1;
  278. },
  279. // 对象的属性是否存在
  280. isPropertyExist: function (key, obj) {
  281. return obj.hasOwnProperty(key);
  282. },
  283. // 数组合并
  284. arrayMerge: function (arr1, arr2) {
  285. return arr1.concat(arr2);
  286. }
  287. }
  288. });
  289. }
  290. };
  291. window.delivery = delivery;
  292. })(window);