addAddress.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { api_sysAreaQueryAllProvince, api_userReceiveAddressUpdate, api_userReceiveAddressSave, api_userReceiveAddressDetail } from "../../api/login";
  2. Component({
  3. properties: {
  4. popupShow: {
  5. type: Boolean,
  6. value: false
  7. },
  8. editId: {
  9. type: String,
  10. value: ""
  11. }
  12. },
  13. data: {
  14. name: "",
  15. phoneNumber: "",
  16. showArea: false,
  17. areaList: [] as any, // 省市区
  18. province: "",
  19. city: "",
  20. region: "",
  21. provinceName: "",
  22. cityName: "",
  23. regionName: "",
  24. detailAddress: ""
  25. },
  26. pageLifetimes: {
  27. show() {
  28. this.getAreas()
  29. },
  30. },
  31. observers: {
  32. async editId(newVal: any) {
  33. if (newVal) {
  34. try {
  35. const { data } = await api_userReceiveAddressDetail(newVal)
  36. if (data.code === 200) {
  37. const params = data.data
  38. this.setData({
  39. phoneNumber: params.phoneNumber,
  40. name: params.name,
  41. province: params.province,
  42. city: params.city,
  43. region: params.region,
  44. provinceName: params.provinceName,
  45. cityName: params.cityName,
  46. regionName: params.regionName,
  47. detailAddress: params.detailAddress
  48. })
  49. }
  50. } catch (e: any) {
  51. console.log(e, 888)
  52. }
  53. }
  54. }
  55. },
  56. methods: {
  57. onDialogClose() {
  58. this.setData({
  59. popupShow: false,
  60. name: "",
  61. phoneNumber: "",
  62. province: "",
  63. city: "",
  64. region: "",
  65. provinceName: "",
  66. cityName: "",
  67. regionName: "",
  68. detailAddress: ""
  69. })
  70. },
  71. /** 显示选择地区 */
  72. onShowAreaList() {
  73. this.setData({
  74. showArea: true
  75. })
  76. },
  77. /** 关闭选择地区 */
  78. onCloseAreaList() {
  79. this.setData({
  80. showArea: false
  81. })
  82. },
  83. /** 确定选择地区 */
  84. submitArea(e: any) {
  85. const selectedOptions: any = e.detail.values
  86. if (!selectedOptions || !selectedOptions[selectedOptions.length - 1]) {
  87. wx.showToast({
  88. title: '未选中值',
  89. icon: 'none'
  90. })
  91. return
  92. }
  93. this.setData({
  94. province: selectedOptions[0].code,
  95. city: selectedOptions[1].code,
  96. region: selectedOptions[2].code,
  97. provinceName: selectedOptions[0].name,
  98. cityName: selectedOptions[1].name,
  99. regionName: selectedOptions[2].name,
  100. showArea: false
  101. })
  102. },
  103. /** 获取省市区 */
  104. async getAreas() {
  105. try {
  106. const { data } = await api_sysAreaQueryAllProvince({})
  107. this.setData({
  108. areaList: this.formateArea(data.data)
  109. })
  110. } catch {
  111. //
  112. }
  113. },
  114. formateArea(area: any[]) {
  115. const province_list: { [_: string]: string } = {};
  116. const city_list: { [_: string]: string } = {};
  117. const county_list: { [_: string]: string } = {};
  118. area.forEach((item: any) => {
  119. province_list[item.code] = item.name;
  120. });
  121. area.forEach((item: any) => {
  122. item.areas && item.areas.forEach((city: any) => {
  123. city_list[city.code] = city.name;
  124. });
  125. });
  126. area.forEach((item: any) => {
  127. item.areas && item.areas.forEach((city: any) => {
  128. city.areas && city.areas.forEach((county: any) => {
  129. county_list[county.code] = county.name;
  130. });
  131. });
  132. });
  133. return {
  134. province_list,
  135. city_list,
  136. county_list
  137. };
  138. },
  139. /** 最终提交 */
  140. async onSubmit() {
  141. try {
  142. const params = this.data
  143. if (!params.name) {
  144. wx.showToast({
  145. title: '请输入收货人',
  146. icon: "none"
  147. })
  148. return
  149. }
  150. if (!params.phoneNumber || !/^1[3456789]\d{9}$/.test(params.phoneNumber)) {
  151. wx.showToast({
  152. title: '请输入正确的手机号',
  153. icon: "none"
  154. })
  155. return
  156. }
  157. if (!params.province || !params.city || !params.region) {
  158. wx.showToast({
  159. title: '请选择地区',
  160. icon: "none"
  161. })
  162. return
  163. }
  164. if (!params.detailAddress) {
  165. wx.showToast({
  166. title: '请输入详细地址',
  167. icon: "none"
  168. })
  169. return
  170. }
  171. // 编辑
  172. let id
  173. if (params.editId) {
  174. id = params.editId
  175. await api_userReceiveAddressUpdate({
  176. id: params.editId,
  177. phoneNumber: params.phoneNumber,
  178. name: params.name,
  179. province: params.province,
  180. city: params.city,
  181. region: params.region,
  182. detailAddress: params.detailAddress,
  183. defaultStatus: false,
  184. postCode: "",
  185. })
  186. wx.showToast({
  187. title: '保存成功',
  188. icon: 'none'
  189. })
  190. } else {
  191. const { data } = await api_userReceiveAddressSave({
  192. phoneNumber: params.phoneNumber,
  193. name: params.name,
  194. province: params.province,
  195. city: params.city,
  196. region: params.region,
  197. detailAddress: params.detailAddress,
  198. defaultStatus: false,
  199. postCode: "",
  200. })
  201. id = data.data
  202. wx.showToast({
  203. title: '保存成功',
  204. icon: 'none'
  205. })
  206. }
  207. this.triggerEvent('addAddress', { addressInfo: { id, name: params.name, phoneNumber: params.phoneNumber, addressDes: params.provinceName + params.cityName + params.regionName + params.detailAddress } }, {})
  208. this.onDialogClose()
  209. } catch {
  210. //
  211. }
  212. },
  213. }
  214. })