addEditAddress.vue 9.1 KB

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