addressManagement.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <template>
  2. <div class="address-management">
  3. <div class="printer-list">
  4. <div class="item" v-for="(v,i) in addressList" :key=i @click.stop="showAddress(1,v)">
  5. <div class="name">{{v.contact}} {{v.phone}}</div>
  6. <div class="shop-name">{{v.address}}</div>
  7. <div class="btns">
  8. <img @click.stop="showAddress(1,v)" src="../../../static/image/edit-icon.png" alt="">
  9. <img @click.stop="deleteAddress(v)" src="../../../static/image/delete-icon.png" alt="">
  10. </div>
  11. </div>
  12. <div class="item1" @click.stop="showAddress(0,v)">
  13. <img class="img" src="../../../static/image/icon-add.png" alt="">
  14. <div class="shop-name">添加常用地址</div>
  15. </div>
  16. </div>
  17. <!-- //新增编辑地址 -->
  18. <el-dialog :title="isEdit ? '编辑地址': '新增地址'" :visible.sync="centerDialogVisible" width="600px" center append-to-body>
  19. <el-form ref="form" :model="form" label-width="100px" label-position="left" size="small">
  20. <el-form-item label="位置" required>
  21. <el-input v-model="form.address" placeholder="请选择位置!" disabled>
  22. <img @click="showMap" slot="append" class="map" src="../../../static/image/icon_map.png" />
  23. </el-input>
  24. </el-form-item>
  25. <el-form-item label="门牌号">
  26. <el-input v-model="form.street" placeholder="请填写楼层、单元、门牌号!"></el-input>
  27. </el-form-item>
  28. <el-form-item label="联系人姓名" required>
  29. <el-input v-model="form.contact" placeholder="请填写联系人姓名!"></el-input>
  30. </el-form-item>
  31. <el-form-item label="联系人电话" required>
  32. <el-input v-model="form.phone" placeholder="请填写联系人电话!"></el-input>
  33. </el-form-item>
  34. <el-form-item label="设为默认地址">
  35. <el-switch v-model="form.isDefault" :active-value="1" :inactive-value="0" active-color="#FC7200" inactive-color="#999"></el-switch>
  36. </el-form-item>
  37. </el-form>
  38. <span slot="footer" class="dialog-footer">
  39. <el-button size="small" @click="cancel">取 消</el-button>
  40. <el-button size="small" type="primary" @click="addAddress">确 定</el-button>
  41. </span>
  42. </el-dialog>
  43. <!-- 通过地图选择地理位置 -->
  44. <order-map @getAddressDetail="getAddressDetail" ref="orderMap"></order-map>
  45. </div>
  46. </template>
  47. <script>
  48. import orderMap from "../orderComponents/orderAMap.vue";
  49. import {
  50. getAddressList,
  51. deleteAddress,
  52. addAddress,
  53. updateAddress,
  54. getAddressDetail,
  55. defaultAddress,
  56. } from "../../api/setting.js";
  57. export default {
  58. name: "addressManagement",
  59. data() {
  60. return {
  61. addressList: [],
  62. centerDialogVisible: false,
  63. isEdit: false,
  64. form: {
  65. id: "",
  66. address: "",
  67. cityCode: "",
  68. cityName: "",
  69. contact: "",
  70. districtName: "",
  71. isDefault: 0,
  72. lat: "",
  73. lng: "",
  74. phone: "",
  75. provinceName: "",
  76. street: "",
  77. },
  78. };
  79. },
  80. components: {
  81. orderMap,
  82. },
  83. // 监听属性 类似于data概念
  84. computed: {},
  85. // 监控data中的数据变化
  86. watch: {},
  87. // 生命周期 - 创建完成(可以访问当前this实例)
  88. created() {
  89. this.getAddressList();
  90. },
  91. mounted() {},
  92. // 方法集合
  93. methods: {
  94. cancel() {
  95. this.centerDialogVisible = false;
  96. this.getAddressList();
  97. },
  98. changeDefault() {
  99. defaultAddress({ addressId: this.form.id }).then((res) => {
  100. if (res.code === 200) {
  101. this.getAddressList();
  102. } else {
  103. this.$message({
  104. type: "error",
  105. message: res.msg,
  106. });
  107. }
  108. });
  109. },
  110. addAddress() {
  111. if (!this.form.address) {
  112. return this.$message({
  113. type: "error",
  114. message: "请选择地址的位置!",
  115. });
  116. }
  117. if (!this.form.contact.trim()) {
  118. return this.$message({
  119. type: "error",
  120. message: "请输入联系人姓名!",
  121. });
  122. }
  123. this.form.phone = this.form.phone.replace(/\s/g, "");
  124. if (!this.form.phone.trim()) {
  125. return this.$message({
  126. type: "error",
  127. message: "请输入联系人电话!",
  128. });
  129. }
  130. let reg2 = new RegExp(/^1[3456789]\d{9}$/);
  131. if (!reg2.test(this.form.phone)) {
  132. return this.$message({
  133. type: "error",
  134. message: "联系人电话格式不正确!",
  135. });
  136. }
  137. if (this.isEdit) {
  138. updateAddress(this.form).then((res) => {
  139. if (res.code === 200) {
  140. this.response();
  141. } else {
  142. this.$message({
  143. type: "success",
  144. message: res.msg,
  145. });
  146. }
  147. });
  148. } else {
  149. addAddress(this.form).then((res) => {
  150. if (res.code === 200) {
  151. this.response();
  152. } else {
  153. this.$message({
  154. type: "success",
  155. message: res.msg,
  156. });
  157. }
  158. });
  159. }
  160. },
  161. response() {
  162. this.$message({
  163. type: "success",
  164. message: `${this.isEdit ? "编辑" : "新增"}地址成功!`,
  165. });
  166. this.centerDialogVisible = false;
  167. this.getAddressList();
  168. },
  169. deleteAddress(row) {
  170. this.$confirm("此操作将删除该地址, 是否继续?", "提示", {
  171. confirmButtonText: "确定",
  172. cancelButtonText: "取消",
  173. center: true,
  174. type: "warning",
  175. }).then(() => {
  176. deleteAddress({ addressId: row.id }).then((res) => {
  177. if (res.code === 200) {
  178. this.$message({
  179. type: "success",
  180. message: "删除成功!",
  181. });
  182. this.getAddressList();
  183. } else {
  184. this.$message({
  185. type: "error",
  186. message: res.msg,
  187. });
  188. }
  189. });
  190. });
  191. },
  192. showMap() {
  193. this.$refs.orderMap.chooseLocation();
  194. },
  195. getAddressDetail(v, lng, lat) {
  196. this.form.address = v.address;
  197. this.form.provinceName = v.province;
  198. this.form.cityName = v.city;
  199. this.form.cityCode = v.citycode;
  200. this.form.districtName = v.district;
  201. this.form.lng = lng;
  202. this.form.lat = lat;
  203. },
  204. showAddress(flag, row) {
  205. this.isEdit = flag;
  206. if (flag) {
  207. this.form = row;
  208. } else {
  209. this.form = {
  210. id: "",
  211. address: "",
  212. cityCode: "",
  213. cityName: "",
  214. contact: "",
  215. districtName: "",
  216. isDefault: 0,
  217. lat: "",
  218. lng: "",
  219. phone: "",
  220. provinceName: "",
  221. street: "",
  222. };
  223. }
  224. this.centerDialogVisible = true;
  225. },
  226. getAddressList() {
  227. getAddressList().then((res) => {
  228. if (res.code === 200) {
  229. this.addressList = res.data.personal;
  230. } else {
  231. this.$message({
  232. type: "error",
  233. message: res.msg,
  234. });
  235. }
  236. });
  237. },
  238. },
  239. };
  240. </script>
  241. <style lang="scss" scoped type="text/css">
  242. .btn {
  243. background: #fc7200;
  244. color: #fff;
  245. border: #fc7200;
  246. }
  247. .map {
  248. width: 20px;
  249. height: 20px;
  250. cursor: pointer;
  251. }
  252. .address-management {
  253. .printer-list {
  254. display: flex;
  255. flex-wrap: wrap;
  256. .item {
  257. position: relative;
  258. display: flex;
  259. flex-direction: column;
  260. width: 280px;
  261. height: 120px;
  262. background: #ffffff;
  263. box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
  264. border-radius: 8px;
  265. margin: 10px 20px 0 0;
  266. padding: 35px 20px 0;
  267. cursor: pointer;
  268. .img {
  269. width: 80px;
  270. height: 80px;
  271. }
  272. .name {
  273. font-size: 18px;
  274. font-weight: 500;
  275. line-height: 20px;
  276. color: #333333;
  277. }
  278. .shop-name {
  279. font-size: 14px;
  280. font-weight: 400;
  281. line-height: 20px;
  282. color: #999999;
  283. margin-top: 16px;
  284. }
  285. .btns {
  286. position: absolute;
  287. top: 10px;
  288. right: 0;
  289. img {
  290. width: 20px;
  291. height: 20px;
  292. margin-right: 10px;
  293. }
  294. }
  295. }
  296. .item1 {
  297. display: flex;
  298. flex-direction: column;
  299. justify-content: center;
  300. align-items: center;
  301. background: #ffffff;
  302. width: 320px;
  303. height: 155px;
  304. box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
  305. border-radius: 8px;
  306. margin: 10px 20px 0 0;
  307. cursor: pointer;
  308. .shop-name {
  309. font-size: 14px;
  310. font-weight: 400;
  311. line-height: 20px;
  312. color: #999999;
  313. }
  314. }
  315. }
  316. }
  317. </style>