use-address.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. (function(global,factory){
  2. typeof define == 'function' && define(['store','helper'],factory);
  3. })(this,function(storeApi,$h){
  4. var template = '<div><div><div class="model-bg":class="{on:show == true}" @touchmove.prevent @click="close"></div><div class="card-model addres-select":class="{up:show == true}"><div class="cm-header">收货地址</div><div class="empty" v-show="addressList.length == 0" @touchmove.prevent> <img src="/public/wap/first/crmeb/images/empty_address.png"><p>&nbsp;</p></div><div id="selects-wrapper"class="selects-info"><ul><li class="flex"v-for="item in addressList":class="{on:checkedAddressId == item.id}"@click="selectAddress(item)"><div class="select-icon"></div><div class="user-info"><p><span class="name"v-text="item.real_name"> </span> <span class="tel"v-text="item.phone"></span></p><p class="address-info"v-text="addressText(item)"></p></div><a class="edit"@click="goEdit(item.id)"href="javascript:void(0);"></a></li></ul></div><div class="model-footer"><a class="footer-buy"href="javascript:void(0);" @click="goAdd">添加新地址</a></div></div></div></div>';
  5. return {
  6. factory:function(Vue){
  7. return Vue.extend({
  8. template:template,
  9. props:{
  10. checkedAddressId:{
  11. type:Number,
  12. default:function(){return 0;}
  13. },
  14. onSelect:{
  15. type:Function
  16. },
  17. onClose:{
  18. type:Function
  19. },
  20. onShow:{
  21. type:Function
  22. },
  23. show:Boolean
  24. },
  25. data:function(){
  26. return {
  27. addressList:[]
  28. }
  29. },
  30. methods:{
  31. goEdit:function(addressId){
  32. location.href = $h.U({
  33. c:'my',
  34. a:'edit_address',
  35. p:{addressId:addressId}
  36. });
  37. },
  38. goAdd:function(){
  39. location.href = $h.U({
  40. c:'my',
  41. a:'edit_address'
  42. });
  43. },
  44. getUserAddress:function(){
  45. var that = this;
  46. storeApi.getUserAddress(function(res){
  47. that.addressList = res.data.data;
  48. });
  49. },
  50. addressText:function(address){
  51. return address.province+address.city+address.district+address.detail
  52. },
  53. close:function(){
  54. this.show = false;
  55. this.onClose && this.onClose();
  56. },
  57. remove:function(){
  58. var that = this;
  59. setTimeout(function(){
  60. that.$el.remove();
  61. },600);
  62. },
  63. active:function(){
  64. this.show = true;
  65. this.onShow && this.onShow();
  66. },
  67. selectAddress:function(address){
  68. this.close();
  69. this.onSelect && this.onSelect(address.id,address);
  70. },
  71. init:function(opt){
  72. if(!opt) opt = {};
  73. if(typeof opt.onClose == 'function') this.onClose = opt.onClose;
  74. if(typeof opt.onSelect == 'function') this.onSelect = opt.onSelect;
  75. if(typeof opt.onShow == 'function') this.onShow = opt.onShow;
  76. if(opt.checked != undefined) this.checkedAddressId = opt.checked;
  77. }
  78. },
  79. mounted:function(){
  80. vm = this;
  81. this.getUserAddress();
  82. }
  83. });
  84. },
  85. install:function(Vue){
  86. var useAddress = this.factory(Vue);
  87. var $vm = new useAddress().$mount(),$el = $vm.$el;
  88. document.body.appendChild($el);
  89. Vue.prototype.$useAddress = function(opt){
  90. $vm.init(opt);
  91. $vm.active();
  92. };
  93. }
  94. }
  95. });