userCenterMyAddress.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <template>
  2. <div class="userCenterMyAddress">
  3. <div class="title">
  4. {{ $t("common.myAddress") }}
  5. {{
  6. $i18n.locale == "zh"
  7. ? `(您已创建 ${page.total} 个收货地址,最多可创建 25 个)`
  8. : `(You have created ${page.total} shipping addresses, up to 25)`
  9. }}
  10. </div>
  11. <div class="table">
  12. <div style="text-align: right">
  13. <el-button
  14. type="primary"
  15. style="margin: 20px 0 8px"
  16. @click="addAddress"
  17. >
  18. <i class="el-icon-plus"></i>
  19. {{ $i18n.locale == "zh" ? "新建地址" : "Create New Address" }}
  20. </el-button>
  21. </div>
  22. <div>
  23. <el-table
  24. :data="umsUserCommodityAddress"
  25. v-loading="loading"
  26. style="width: 100%"
  27. height="588"
  28. :header-cell-style="{ backgroundColor: '#f8f8f8' }"
  29. >
  30. <el-table-column
  31. prop="consignee"
  32. align="center"
  33. :label="$i18n.locale === 'zh' ? '收货人' : 'Consignee'"
  34. width="150"
  35. >
  36. </el-table-column>
  37. <el-table-column
  38. v-if="$i18n.locale == 'zh'"
  39. align="center"
  40. :label="$i18n.locale === 'zh' ? '所在地区' : 'Region'"
  41. width="150"
  42. >
  43. <template slot-scope="scope">
  44. {{ scope.row.province }}{{ scope.row.city }}{{ scope.row.area }}
  45. </template>
  46. </el-table-column>
  47. <el-table-column
  48. align="center"
  49. :label="$i18n.locale === 'zh' ? '详细地址' : 'Address'"
  50. min-width="160"
  51. >
  52. <template slot-scope="scope">
  53. {{ scope.row.address }}
  54. </template>
  55. </el-table-column>
  56. <el-table-column
  57. prop="postalCode"
  58. align="center"
  59. :label="$i18n.locale === 'zh' ? '邮编' : 'Postcode'"
  60. width="150"
  61. >
  62. </el-table-column>
  63. <el-table-column
  64. prop="mobile"
  65. align="center"
  66. :label="$i18n.locale === 'zh' ? '联系方式' : 'Contact information'"
  67. width="160"
  68. >
  69. </el-table-column>
  70. <el-table-column
  71. align="center"
  72. :label="$i18n.locale === 'zh' ? '操作' : 'Operate'"
  73. width="160"
  74. >
  75. <template slot-scope="scope">
  76. <el-button
  77. type="text"
  78. size="small"
  79. @click="alterAddress(scope.row)"
  80. >
  81. {{ $i18n.locale === "zh" ? "编辑" : "Edit" }}
  82. </el-button>
  83. <el-button
  84. type="text"
  85. size="small"
  86. @click="deleteAddress(scope.row.id)"
  87. >
  88. {{ $i18n.locale === "zh" ? "删除" : "Delete" }}
  89. </el-button>
  90. <el-button
  91. disabled
  92. type="text"
  93. size="small"
  94. v-if="scope.row.status != '1'"
  95. @click="alterAddressStatus(scope.row)"
  96. >
  97. {{ $i18n.locale === "zh" ? "设为默认地址" : "Set Default Address" }}
  98. </el-button>
  99. </template>
  100. </el-table-column>
  101. </el-table>
  102. <el-pagination
  103. background
  104. @size-change="
  105. (val) => {
  106. page.pageSize = val;
  107. }
  108. "
  109. @current-change="
  110. (val) => {
  111. page.pageNo = val;
  112. }
  113. "
  114. layout="prev, pager, next"
  115. :total="page.total"
  116. :current-page="page.pageNo"
  117. :page-sizes="[10, 20, 50, 100]"
  118. :page-size="page.pageSize"
  119. style="text-align: center; margin-top: 20px"
  120. >
  121. </el-pagination>
  122. </div>
  123. </div>
  124. <el-dialog
  125. destroy-on-close
  126. :modal="false"
  127. custom-class="userCenterMyAddressDialog"
  128. :visible.sync="booleanFlag.addressFlag"
  129. width="700px"
  130. @close="userCenterMyAddressDialogClose"
  131. >
  132. <span slot="title">
  133. <span v-if="addressDialogConfig.type == '新建地址'">
  134. {{ $i18n.locale == "zh" ? "新建地址" : "New address" }}
  135. </span>
  136. <span v-if="addressDialogConfig.type == '修改地址'">
  137. {{ $i18n.locale == "zh" ? "修改地址" : "Modification address" }}
  138. </span>
  139. </span>
  140. <div class="content">
  141. <el-form
  142. :rules="this.isZh ? rules : rulesEn"
  143. ref="addressForm"
  144. :model="addressForm"
  145. label-width="150px"
  146. >
  147. <el-form-item
  148. :label="$i18n.locale == 'zh' ? '收货人' : 'Consignee'"
  149. prop="consignee"
  150. >
  151. <el-input
  152. maxlength="30"
  153. v-model="addressForm.consignee"
  154. :placeholder="
  155. $i18n.locale == 'zh'
  156. ? '长度不超过30个字符'
  157. : '30 characters or less in length'
  158. "
  159. ></el-input>
  160. </el-form-item>
  161. <el-form-item
  162. :label="$i18n.locale == 'zh' ? '电话号码' : 'Mobile'"
  163. prop="mobile"
  164. >
  165. <el-input
  166. v-model="addressForm.mobile"
  167. :placeholder="
  168. $i18n.locale == 'zh'
  169. ? '电话号码、手机号码必须填一项'
  170. : 'Phone number, mobile phone number must fill in one item'
  171. "
  172. ></el-input>
  173. </el-form-item>
  174. <el-form-item
  175. v-if="$i18n.locale == 'zh'"
  176. :label="$i18n.locale == 'zh' ? '地址信息' : 'Division'"
  177. required
  178. prop="division"
  179. >
  180. <el-cascader
  181. v-model="addressForm.division"
  182. :options="sysAreaList"
  183. :props="{ value: 'name', label: 'name', children: 'child' }"
  184. placeholder="请选择省/市/区/街道"
  185. >
  186. </el-cascader>
  187. </el-form-item>
  188. <el-form-item
  189. :label="$i18n.locale == 'zh' ? '详细地址' : 'Detail'"
  190. prop="address"
  191. >
  192. <el-input
  193. v-model="addressForm.address"
  194. :placeholder="
  195. $i18n.locale == 'zh' ? '请输入详细地址' : 'Please enter address'
  196. "
  197. ></el-input>
  198. </el-form-item>
  199. <el-form-item :label="$i18n.locale == 'zh' ? '邮政编码' : 'Postcode'">
  200. <el-input v-model="addressForm.postal_code"></el-input>
  201. </el-form-item>
  202. <el-form-item label=" ">
  203. <el-checkbox
  204. v-model="addressForm.status"
  205. true-label="1"
  206. false-label="0"
  207. >{{
  208. $i18n.locale == "zh" ? "设为默认" : "Set as Default"
  209. }}</el-checkbox
  210. >
  211. </el-form-item>
  212. <el-form-item>
  213. <el-button type="primary" @click="saveAddress('addressForm')"
  214. >{{
  215. $i18n.locale == "zh" ? "保存" : "Save"
  216. }}</el-button
  217. >
  218. </el-form-item>
  219. </el-form>
  220. </div>
  221. </el-dialog>
  222. </div>
  223. </template>
  224. <script>
  225. import {
  226. addUmsUserCommodityAddresss,
  227. deleteUmsUserCommodityAddresss,
  228. getSysAreaList,
  229. getUmsUserCommodityAddresss,
  230. } from "@/api/user";
  231. import { getToken } from "@/api/token";
  232. export default {
  233. name: "userCenterMyAddress",
  234. data() {
  235. return {
  236. isZh: true,
  237. booleanFlag: {
  238. addressFlag: false,
  239. },
  240. user: {
  241. umsUser: {},
  242. },
  243. loading: true,
  244. page: {
  245. pageNo: 1,
  246. pageSize: 10,
  247. total: 0,
  248. },
  249. sysAreaList: [],
  250. umsUserCommodityAddress: [],
  251. addressDialogConfig: {
  252. type: "新建地址",
  253. },
  254. addressForm: {
  255. consignee: "",
  256. mobile: "",
  257. division: [],
  258. address: "",
  259. postal_code: "",
  260. status: "0",
  261. },
  262. rules: {
  263. consignee: [
  264. {
  265. required: true,
  266. message: "请输入收货人",
  267. trigger: "blur",
  268. },
  269. ],
  270. mobile: [
  271. {
  272. required: true,
  273. message: "请输入电话号码或手机号码",
  274. trigger: "blur",
  275. },
  276. ],
  277. division: [
  278. {
  279. required: true,
  280. message: "请选择地址信息",
  281. trigger: "change",
  282. },
  283. ],
  284. address: [
  285. {
  286. required: true,
  287. message: "请输入详细地址信息",
  288. trigger: "blur",
  289. },
  290. ],
  291. },
  292. rulesEn: {
  293. consignee: [
  294. {
  295. required: true,
  296. message: "Please enter the consignee",
  297. trigger: "blur",
  298. },
  299. ],
  300. mobile: [
  301. {
  302. required: true,
  303. message: "Please enter a phone number or mobile number",
  304. trigger: "blur",
  305. },
  306. ],
  307. address: [
  308. {
  309. required: true,
  310. message: "Please enter detailed address information",
  311. trigger: "blur",
  312. },
  313. ],
  314. },
  315. };
  316. },
  317. watch: {
  318. page: {
  319. handler: function () {
  320. this.loading = true;
  321. this.getUmsUserCommodityAddresss();
  322. },
  323. deep: true,
  324. },
  325. "$i18n.locale": {
  326. handler: function () {
  327. if (this.$i18n.locale == "zh") {
  328. this.isZh = true;
  329. } else {
  330. this.isZh = false;
  331. }
  332. },
  333. deep: true,
  334. },
  335. },
  336. mounted() {
  337. let test = JSON.parse(this.$Cookies.get('userInfo'));
  338. console.log(test)
  339. let language = window.localStorage.getItem("locale");
  340. if (language == "zh") {
  341. this.isZh = true;
  342. } else {
  343. this.isZh = false;
  344. }
  345. this.getSysAreaList();
  346. this.init();
  347. },
  348. methods: {
  349. init() {
  350. this.user = JSON.parse(window.localStorage.getItem("user"));
  351. if (this.user.umsUser.userPic == undefined) {
  352. this.user.umsUser.userPic = "";
  353. }
  354. this.getUmsUserCommodityAddresss();
  355. },
  356. // 获取省市区
  357. getSysAreaList() {
  358. function clearLeaf(data) {
  359. data.forEach((item, index) => {
  360. if (item.child.length != 0) {
  361. clearLeaf(item.child);
  362. } else {
  363. item.child = undefined;
  364. }
  365. });
  366. }
  367. getSysAreaList().then((res) => {
  368. if (res) {
  369. let data = res.data;
  370. clearLeaf(data);
  371. this.sysAreaList = data;
  372. console.log(this.sysAreaList)
  373. }
  374. });
  375. },
  376. // 获取地址列表
  377. getUmsUserCommodityAddresss() {
  378. let param = {
  379. pageSize: this.page.pageSize,
  380. pageNo: this.page.pageNo,
  381. // userId: this.user.userId,
  382. };
  383. getUmsUserCommodityAddresss(param)
  384. .then((res) => {
  385. if (res) {
  386. console.log(res);
  387. this.umsUserCommodityAddress = res.data.umsUserCommodityAddresss;
  388. this.page.total = Number(res.data.page.totalCount);
  389. }
  390. })
  391. .then(() => {
  392. this.loading = false;
  393. })
  394. .catch(() => {
  395. this.loading = false;
  396. });
  397. },
  398. addAddress() {
  399. this.addressDialogConfig.type = "新建地址";
  400. this.booleanFlag.addressFlag = true;
  401. },
  402. saveAddress(addressForm) {
  403. let params = {
  404. id: this.addressForm.id,
  405. // userId: this.user.userId,
  406. consignee: this.addressForm.consignee,
  407. mobile: this.addressForm.mobile,
  408. province: this.addressForm.division[0],
  409. city: this.addressForm.division[1],
  410. area: this.addressForm.division[2],
  411. address: this.addressForm.address,
  412. postalCode: this.addressForm.postal_code,
  413. status: this.addressForm.status,
  414. };
  415. this.$refs[addressForm].validate((valid) => {
  416. if (valid) {
  417. this.booleanFlag.addressFlag = false;
  418. getToken().then((res) => {
  419. addUmsUserCommodityAddresss(params, res.data).then((res) => {
  420. this.init();
  421. });
  422. });
  423. } else {
  424. return false;
  425. }
  426. });
  427. },
  428. // 编辑
  429. alterAddress(data) {
  430. this.addressForm = {
  431. consignee: data.consignee,
  432. mobile: data.mobile,
  433. division: [data.province, data.city, data.area],
  434. address: data.address,
  435. postal_code: data.postalCode,
  436. status: data.status,
  437. id: data.id,
  438. };
  439. console.log(this.addressForm);
  440. this.addressDialogConfig.type = "修改地址";
  441. this.booleanFlag.addressFlag = true;
  442. },
  443. // 设为默认地址
  444. alterAddressStatus(data) {
  445. let params = {
  446. // user_id: this.user.userId,
  447. consignee: data.consignee,
  448. mobile: data.mobile,
  449. province: data.province,
  450. city: data.city,
  451. area: data.area,
  452. address: data.address,
  453. postal_code: data.postalCode,
  454. status: "1",
  455. id: data.id,
  456. };
  457. getToken().then((res) => {
  458. addUmsUserCommodityAddresss(params, res.data).then((res) => {
  459. this.init();
  460. });
  461. });
  462. },
  463. deleteAddress(id) {
  464. getToken().then((res) => {
  465. // res.data 为token
  466. deleteUmsUserCommodityAddresss(id, res.data)
  467. .then((res) => {
  468. this.init();
  469. })
  470. .catch((error) => {
  471. console.log(error);
  472. });
  473. });
  474. },
  475. userCenterMyAddressDialogClose() {
  476. this.addressForm = this.$options.data.call(this).addressForm;
  477. },
  478. },
  479. };
  480. </script>
  481. <style scoped lang="less">
  482. .userCenterMyAddress {
  483. height: 700px;
  484. overflow:hidden .title {
  485. height: 60px;
  486. line-height: 60px;
  487. padding-top: 30px;
  488. margin-left: 30px;
  489. margin-right: 30px;
  490. border-bottom: 1px solid rgba(228, 228, 228, 1);
  491. }
  492. > .table {
  493. padding: 0 5px;
  494. }
  495. /deep/ .userCenterMyAddressDialog {
  496. margin-top: 300px;
  497. }
  498. }
  499. </style>