addEditAddress.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <template>
  2. <view class="page">
  3. <view class="content">
  4. <view class="title">
  5. <text class="title-left">{{ i18n.contacts }}</text>
  6. <view class="title-right">
  7. <image
  8. src="../../static/mine/331.png"
  9. class="title-icon"
  10. mode=""
  11. ></image>
  12. <text>{{ i18n.wxAddress }}</text>
  13. </view>
  14. </view>
  15. <!-- 注意,如果需要兼容微信小程序,最好通过setRules方法设置rules规则 -->
  16. <u--form labelPosition="left" :model="model1" ref="uForm">
  17. <u-form-item
  18. labelWidth="150rpx"
  19. :label="i18n.name"
  20. borderBottom
  21. ref="item1"
  22. >
  23. <u--input
  24. v-model="model1.userInfo.name"
  25. :placeholder="i18n.goods"
  26. border="none"
  27. ></u--input>
  28. </u-form-item>
  29. <u-form-item
  30. labelWidth="150rpx"
  31. :label="i18n.phone"
  32. borderBottom
  33. ref="item1"
  34. >
  35. <u--input
  36. v-model="model1.userInfo.phone"
  37. :placeholder="i18n.pleasePhoneNumber"
  38. border="none"
  39. ></u--input>
  40. </u-form-item>
  41. <u-form-item
  42. labelWidth="150rpx"
  43. :label="i18n.city"
  44. borderBottom
  45. @click="show = true"
  46. ref="item1"
  47. >
  48. <u--input
  49. v-model="model1.userInfo.city"
  50. disabled
  51. disabledColor="#ffffff"
  52. :placeholder="i18n.township"
  53. border="none"
  54. ></u--input>
  55. <u-icon slot="right" name="arrow-right"></u-icon>
  56. </u-form-item>
  57. <u-form-item
  58. labelWidth="150rpx"
  59. :label="i18n.detailed"
  60. borderBottom
  61. ref="item1"
  62. >
  63. <u--input
  64. v-model="model1.userInfo.detailed"
  65. :placeholder="i18n.sect"
  66. border="none"
  67. ></u--input>
  68. </u-form-item>
  69. <view class="company">
  70. <u--input
  71. v-model="model1.userInfo.company"
  72. :placeholder="i18n.company"
  73. border="none"
  74. ></u--input>
  75. </view>
  76. <view
  77. style="
  78. display: flex;
  79. justify-content: space-between;
  80. align-items: center;
  81. "
  82. >
  83. <view
  84. class="item-left"
  85. @click="model1.userInfo.is_default = !model1.userInfo.is_default"
  86. >
  87. <image
  88. src="../../static/mine/327.png"
  89. class="address-icon"
  90. mode=""
  91. v-if="!model1.userInfo.is_default"
  92. ></image>
  93. <image
  94. v-else
  95. src="../../static/mine/330.png"
  96. class="address-icon"
  97. mode=""
  98. ></image>
  99. <text>{{ i18n.initial }}</text>
  100. </view>
  101. <text style="font-size: 24rpx; color: #333" @click="empty">{{
  102. i18n.empty
  103. }}</text>
  104. </view>
  105. </u--form>
  106. </view>
  107. <SelectCity :show="show" @close="close" :cityId="cityId" />
  108. <view class="footer">
  109. <button class="save" @click="submit">{{ i18n.preserve }}</button>
  110. </view>
  111. </view>
  112. </template>
  113. <script>
  114. import SelectCity from "./component/selectCity.vue";
  115. export default {
  116. components: {
  117. SelectCity,
  118. },
  119. computed: {
  120. i18n() {
  121. return this.$t("index");
  122. },
  123. },
  124. data() {
  125. return {
  126. show: false,
  127. cityId: [],
  128. cityPid: {},
  129. addressId: "",
  130. model1: {
  131. userInfo: {
  132. name: "",
  133. sex: "",
  134. phone: "",
  135. city: "",
  136. detailed: "",
  137. company: "",
  138. is_default: 0,
  139. country_id: 0,
  140. },
  141. },
  142. rules: {
  143. "userInfo.name": {
  144. type: "string",
  145. required: true,
  146. message: "请填写姓名",
  147. trigger: ["blur", "change"],
  148. },
  149. "userInfo.sex": {
  150. type: "string",
  151. max: 1,
  152. required: true,
  153. message: "请选择男或女",
  154. trigger: ["blur", "change"],
  155. },
  156. },
  157. };
  158. },
  159. onLoad(options) {
  160. if (options.id) {
  161. console.log(options.id);
  162. this.addressId = options.id;
  163. this.getAddressDetail(options.id);
  164. }
  165. },
  166. methods: {
  167. //编辑 or 新增
  168. submit() {
  169. const obj = {
  170. mobile: this.model1.userInfo.phone,
  171. name: this.model1.userInfo.name,
  172. is_default: this.model1.userInfo.is_default ? 1 : 0,
  173. company_name: this.model1.userInfo.company,
  174. address: this.model1.userInfo.detailed,
  175. country_id: this.model1.userInfo.country_id,
  176. };
  177. //有id为编辑,没有id为新增
  178. if (this.addressId) {
  179. uni.$u.http
  180. .put(`/api/address/${this.addressId}`, { ...obj, ...this.cityPid })
  181. .then((res) => {
  182. console.log(res);
  183. const eventchannel = this.getOpenerEventChannel();
  184. eventchannel.emit("topadd", res);
  185. uni.navigateBack({
  186. delta: 1,
  187. });
  188. })
  189. .catch(() => {});
  190. } else {
  191. uni.$u.http
  192. .post(`/api/address`, { ...obj, ...this.cityPid })
  193. .then((res) => {
  194. // console.log(res);
  195. const eventchannel = this.getOpenerEventChannel();
  196. eventchannel.emit("topadd", res);
  197. uni.navigateBack({
  198. delta: 1,
  199. });
  200. })
  201. .catch(() => {});
  202. }
  203. },
  204. //清空输入
  205. empty() {
  206. this.model1.userInfo = {
  207. name: "",
  208. sex: "",
  209. phone: "",
  210. city: "",
  211. detailed: "",
  212. company: "",
  213. is_default: 0,
  214. country_id: 0,
  215. };
  216. },
  217. //关闭选择城市弹窗
  218. close(obj) {
  219. this.show = false;
  220. if (obj && obj.name) {
  221. this.model1.userInfo.city =
  222. obj.name + obj.children.name + obj.children.children.name;
  223. this.cityId = [obj.id, obj.children.id, obj.children.children.id];
  224. this.cityPid.province_id = obj.id;
  225. this.cityPid.city_id = obj.children.id;
  226. this.cityPid.area_id = obj.children.children.id;
  227. }
  228. },
  229. //回显数据
  230. getAddressDetail(id) {
  231. uni.$u.http.get(`/api/address/${id}`).then((res) => {
  232. this.model1.userInfo.city =
  233. res.province_name + res.city_name + res.area_name;
  234. this.model1.userInfo.name = res.name;
  235. this.model1.userInfo.phone = res.mobile;
  236. this.model1.userInfo.detailed = res.address;
  237. this.model1.userInfo.company = res.company_name;
  238. this.model1.userInfo.is_default = res.is_default;
  239. this.model1.userInfo.country_id = res.country_id;
  240. if (res.province_id && res.city_id && res.area_id) {
  241. this.cityId = [res.province_id, res.city_id, res.area_id];
  242. }
  243. });
  244. },
  245. },
  246. mounted() {
  247. uni.setNavigationBarTitle({
  248. title: this.i18n.newAddress,
  249. });
  250. },
  251. onReady() {
  252. //如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则。
  253. this.$refs.uForm.setRules(this.rules);
  254. },
  255. };
  256. </script>
  257. <style lang="scss" scoped>
  258. .page {
  259. padding: 20rpx 24rpx;
  260. .content {
  261. background-color: #fff;
  262. border-radius: 16rpx;
  263. padding: 0 24rpx;
  264. .title {
  265. display: flex;
  266. justify-content: space-between;
  267. align-items: center;
  268. height: 100rpx;
  269. border-bottom: 2rpx solid rgba(151, 151, 151, 0.1);
  270. .title-left {
  271. font-size: 32rpx;
  272. color: #333;
  273. font-weight: 600;
  274. }
  275. .title-icon {
  276. width: 26rpx;
  277. height: 26rpx;
  278. margin-right: 10rpx;
  279. }
  280. .title-right {
  281. display: flex;
  282. align-items: center;
  283. font-size: 24rpx;
  284. color: rgba(51, 51, 51, 0.6);
  285. }
  286. }
  287. .company {
  288. height: 102rpx;
  289. display: flex;
  290. align-items: center;
  291. border-bottom: 2rpx solid rgba(151, 151, 151, 0.1);
  292. }
  293. }
  294. .item-left {
  295. font-size: 24rpx;
  296. color: #333;
  297. display: flex;
  298. align-items: center;
  299. height: 86rpx;
  300. .address-icon {
  301. width: 26rpx;
  302. height: 26rpx;
  303. margin-right: 10rpx;
  304. }
  305. }
  306. .footer {
  307. background-color: #fff;
  308. position: fixed;
  309. bottom: 0;
  310. height: 146rpx;
  311. width: 750rpx;
  312. left: 0;
  313. padding-top: 20rpx;
  314. .save {
  315. width: 702rpx;
  316. margin: 0 auto;
  317. background-color: #f83224;
  318. color: #fff;
  319. border-radius: 40rpx;
  320. }
  321. }
  322. }
  323. </style>