You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

295 lines
8.7 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
  1. <template>
  2. <view class="container">
  3. <view class="address-info ">
  4. <u--form labelPosition="left" ref="uForm">
  5. <u-form-item required label="联系人" prop="addressInfo.name" labelWidth="120" borderBottom>
  6. <u--input v-model="addressInfo.name" placeholder="请输入联系人姓名" placeholderStyle="text-align:right;color:#AAA" border="none" inputAlign="right"></u--input>
  7. </u-form-item>
  8. <u-form-item required label="手机号码" prop="addressInfo.phone" labelWidth="120" borderBottom>
  9. <u--input v-model="addressInfo.phone" placeholder="请输入手机号码" placeholderStyle="text-align:right;color:#AAA" border="none" inputAlign="right"></u--input>
  10. </u-form-item>
  11. <u-form-item label="应急联络" prop="addressInfo.emergencyPhone" labelWidth="120" borderBottom>
  12. <u--input v-model="addressInfo.emergencyPhone" placeholder="请输入应急联络号码" placeholderStyle="text-align:right;color:#AAA" border="none" inputAlign="right"></u--input>
  13. </u-form-item>
  14. <!-- <u-form-item label="所在城市" prop="addressInfo.city" labelWidth="120" borderBottom>
  15. <picker mode="multiSelector" @columnchange="columnchange" @change="bindMultiPickerColumnChange" :value="multiIndex"
  16. :range="multiArray">
  17. <view style="display: flex;justify-content: space-between;">
  18. <u--input :value="addressInfo.city? (addressInfo.city +' '+ addressInfo.district):''"
  19. disabled disabledColor="#ffffff" placeholder="请选择所在城市"
  20. placeholderStyle="text-align:right;color:#AAA" border="none" inputAlign="right">
  21. </u--input>
  22. <u-icon slot="right" name="arrow-right" color="#AAA"></u-icon>
  23. </view>
  24. </picker>
  25. </u-form-item> -->
  26. <u-form-item label="所在地区" prop="addressInfo.city" labelWidth="120" borderBottom>
  27. <view style="display: flex;justify-content: space-between;" @click="selectAddress">
  28. <u--input :value="addressInfo.city"
  29. disabled disabledColor="#ffffff" placeholder="请选择所在地区"
  30. placeholderStyle="text-align:right;color:#AAA" border="none" inputAlign="right">
  31. </u--input>
  32. <u-icon slot="right" name="arrow-right" color="#AAA"></u-icon>
  33. </view>
  34. </u-form-item>
  35. <u-form-item label="详细地址" prop="addressInfo.detailAddress" labelWidth="120" borderBottom>
  36. <u--input v-model="addressInfo.detailAddress" placeholder="如楼栋、门牌号" placeholderStyle="text-align:right;color:#AAA;" border="none" inputAlign="right"></u--input>
  37. </u-form-item>
  38. <u-form-item label="设为默认地址" prop="addressInfo.isDefault" labelWidth="120">
  39. <view>
  40. <u-radio-group :value="addressInfo.isDefault?'默认':''" placement="column" @change="groupChange">
  41. <u-radio name="默认" activeColor="#ffbf60" labelColor="#ffbf60" label="默认"></u-radio>
  42. </u-radio-group>
  43. </view>
  44. </u-form-item>
  45. </u--form>
  46. </view>
  47. <view class="address-save">
  48. <button class="address-save-btn" @click="save" :loading="loading">保存并使用</button>
  49. </view>
  50. </view>
  51. </template>
  52. <script>
  53. import {
  54. getCity
  55. } from '@/api/system/user.js'
  56. import {
  57. getAddressDetails,addAddress,updateAddress
  58. } from '@/api/system/address.js'
  59. export default{
  60. data(){
  61. return{
  62. loading:false,
  63. addressInfo:{
  64. name:"",
  65. phone:"",
  66. emergencyPhone:"",
  67. province:"",
  68. city:"",
  69. district:'',
  70. detailAddress:"",
  71. openId:"",
  72. isDefault:0
  73. },
  74. multiArray: [],
  75. cityList:[],
  76. multiIndex: [0, 0],
  77. addressId:'',
  78. }
  79. },
  80. onLoad(option) {
  81. this.optionType=option.optionType;
  82. if(this.optionType=='edit'){
  83. this.addressId=option.addressId;
  84. this.getAddressDetails(option.addressId);
  85. }
  86. },
  87. mounted() {
  88. this.getCityList()
  89. },
  90. methods:{
  91. getAddressDetails(id){
  92. getAddressDetails(id).then(res=>{
  93. if(res&& res.id){
  94. const {
  95. name,
  96. phone,
  97. emergencyPhone,
  98. province,
  99. city,
  100. district,
  101. detailAddress,
  102. openId,
  103. isDefault
  104. } = res;
  105. this.addressInfo = {
  106. name,
  107. phone,
  108. emergencyPhone,
  109. province,
  110. city,
  111. district,
  112. detailAddress,
  113. openId,
  114. isDefault
  115. };
  116. }else{
  117. this.$modal.showToast('获取地址详情失败')
  118. }
  119. })
  120. },
  121. // 选择地区
  122. selectAddress(){
  123. uni.chooseLocation({
  124. success: res => {
  125. this.addressInfo.latitude = res.latitude
  126. this.addressInfo.longitude = res.longitude
  127. this.addressInfo.district = ''
  128. this.addressInfo.province = ''
  129. if (!res.address && res.name) { //用户直接选择城市的逻辑
  130. return this.addressInfo.city = res.name
  131. }
  132. if (res.address || res.name) {
  133. return this.addressInfo.city = res.address + res.name
  134. }
  135. this.addressInfo.city = '' //用户啥都没选就点击勾选
  136. },
  137. fail(e) {
  138. console.log("获取位置信息失败!", e)
  139. }
  140. })
  141. },
  142. save(){
  143. let params = this.addressInfo
  144. if(params.phone){
  145. if(!(/^1[3456789]\d{9}$/.test(params.phone))){
  146. this.$modal.showToast('手机号码有误')
  147. return false;
  148. }
  149. } else {
  150. this.$modal.showToast('请输入手机号')
  151. return false;
  152. }
  153. if(params.emergencyPhone){
  154. if(!(/^1[3456789]\d{9}$/.test(params.emergencyPhone))){
  155. this.$modal.showToast('手机号码有误')
  156. return false;
  157. }
  158. }
  159. if(!params.name){
  160. this.$modal.showToast('请输入联系人')
  161. return false;
  162. }
  163. this.loading=true
  164. if(this.optionType=='edit'){
  165. params.id = this.addressId;
  166. updateAddress(params).then(res=>{
  167. if(res&&res==1){
  168. this.$globalData.newOrderData.currentAddress={
  169. id:params.id,
  170. name:params.name,
  171. phone:params.phone,
  172. province:params.province,
  173. city:params.city,
  174. district:params.district,
  175. detailAddress:params.detailAddress,
  176. }
  177. this.loading=false
  178. uni.redirectTo({
  179. url: `/pages/newOrder/serviceNew`
  180. });
  181. }else {
  182. this.loading=false
  183. this.$modal.showToast('更新地址失败')
  184. }
  185. })
  186. } else if(this.optionType=='add'){
  187. addAddress(params).then(res=>{
  188. if(res&&res>0){
  189. this.$globalData.newOrderData.currentAddress={
  190. id:res,
  191. name:params.name,
  192. phone:params.phone,
  193. province:params.province,
  194. city:params.city,
  195. district:params.district,
  196. detailAddress:params.detailAddress,
  197. }
  198. this.loading=false
  199. uni.redirectTo({
  200. url: `/pages/newOrder/serviceNew`
  201. });
  202. }else {
  203. this.loading=false
  204. this.$modal.showToast('新增地址失败')
  205. }
  206. })
  207. }
  208. },
  209. getCityList() {
  210. getCity().then(res => {
  211. if (res.code == 200) {
  212. console.log('city', JSON.parse(res.msg));
  213. this.cityList = JSON.parse(res.msg)
  214. const cityLabels = this.cityList.map(item =>item.city)
  215. this.multiArray[0] = cityLabels
  216. this.multiArray[1] = this.cityList[0].region.map(e=> e.region)
  217. } else {
  218. this.$modal.showToast('获取城市失败,请联系系统管理员!');
  219. }
  220. })
  221. },
  222. columnchange(e){
  223. console.log(e)
  224. // 当滚动切换一级分类时,为当前的一级分类添加它的子类
  225. if (e.detail.column == 0) {
  226. const currentCity = this.cityList[e.detail.value]
  227. // #ifdef H5
  228. // 在小程序中直接赋值无效 H5 可直接赋值
  229. this.multiArray[1] = currentCity.region.map(e=>e.region)
  230. // #endif
  231. // #ifdef MP-WEIXIN
  232. // 在 H5 环境下 $set 会导致一级分类无法滚动, 小程序正常运行
  233. this.$set(this.multiArray, 1, currentCity.region.map(e=>e.region))
  234. // #endif
  235. this.multiIndex=[e.detail.value,0]
  236. }
  237. },
  238. bindMultiPickerColumnChange(e) {
  239. console.log('值为:' + e.detail.value)
  240. this.multiIndex = e.detail.value
  241. this.addressInfo.province = this.multiArray[0][this.multiIndex[0]]
  242. this.addressInfo.city = this.multiArray[0][this.multiIndex[0]]
  243. this.addressInfo.district = this.multiArray[1][this.multiIndex[1]]
  244. this.isSelected = true
  245. this.$forceUpdate()
  246. },
  247. groupChange(n) {
  248. console.log('groupChange', n);
  249. this.addressInfo.isDefault=+(!this.addressInfo.isDefault)
  250. },
  251. }
  252. }
  253. </script>
  254. <style lang="scss">
  255. .container {
  256. position: relative;
  257. height: 100%;
  258. padding-bottom: 90px;
  259. .address-save {
  260. background-color: #FFFFFF;
  261. padding: 10px 20px 40px;
  262. width: 100%;
  263. height: 90px;
  264. position: fixed;
  265. bottom: 0;
  266. z-index: 100;
  267. .address-save-btn {
  268. width: 100%;
  269. border-radius: 6px;
  270. background: #FFB13F;
  271. font-size: 16px;
  272. color: #FFFFFF;
  273. }
  274. }
  275. }
  276. .address-info{
  277. background-color: #fff;
  278. padding: 10px 20px;
  279. .u-radio-group{
  280. float: right;
  281. }
  282. }
  283. </style>