addAddress.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import { api_sysAreaQueryAllProvince, api_userReceiveAddressUpdate, api_userReceiveAddressSave, api_userReceiveAddressDetail } from "../../api/login";
  2. Component({
  3. options: {
  4. styleIsolation: 'shared'
  5. },
  6. properties: {
  7. popupShow: {
  8. type: Boolean,
  9. value: false
  10. },
  11. editId: {
  12. type: String,
  13. value: ""
  14. }
  15. },
  16. data: {
  17. name: "",
  18. phoneNumber: "",
  19. cacheArea: [] as { cityCode: string, shiftCityCode: string }[], // 临时存储的对应关系
  20. showArea: false,
  21. areaList: [] as any, // 省市区
  22. province: "",
  23. city: "",
  24. region: "",
  25. provinceName: "",
  26. cityName: "",
  27. regionName: "",
  28. detailAddress: ""
  29. },
  30. pageLifetimes: {
  31. show() {
  32. this.getAreas()
  33. },
  34. },
  35. observers: {
  36. async editId(newVal: any) {
  37. if (newVal) {
  38. try {
  39. const { data } = await api_userReceiveAddressDetail(newVal)
  40. if (data.code === 200) {
  41. const params = data.data
  42. this.setData({
  43. phoneNumber: params.phoneNumber,
  44. name: params.name,
  45. province: params.province,
  46. city: params.city,
  47. region: params.region || "",
  48. provinceName: params.provinceName,
  49. cityName: params.cityName,
  50. regionName: params.regionName,
  51. detailAddress: params.detailAddress
  52. })
  53. // 回显市区
  54. this.setData({
  55. city: this.formateCityCode(true)
  56. })
  57. }
  58. } catch (e: any) {
  59. console.log(e, 888)
  60. }
  61. }
  62. }
  63. },
  64. methods: {
  65. onDialogClose() {
  66. this.setData({
  67. popupShow: false,
  68. name: "",
  69. phoneNumber: "",
  70. province: "",
  71. city: "",
  72. region: "",
  73. provinceName: "",
  74. cityName: "",
  75. regionName: "",
  76. detailAddress: ""
  77. })
  78. },
  79. /** 显示选择地区 */
  80. onShowAreaList() {
  81. this.setData({
  82. showArea: true
  83. })
  84. },
  85. /** 关闭选择地区 */
  86. onCloseAreaList() {
  87. this.setData({
  88. showArea: false
  89. })
  90. },
  91. /** 确定选择地区 */
  92. submitArea(e: any) {
  93. const selectedOptions: any = e.detail.values
  94. if (!selectedOptions || !selectedOptions[0]) {
  95. wx.showToast({
  96. title: '未选中值',
  97. icon: 'none'
  98. })
  99. return
  100. }
  101. if (selectedOptions[0].code == this.data.province && selectedOptions[1].code == this.data.city && (selectedOptions[2]?.code || "") == this.data.region) {
  102. this.setData({
  103. showArea: false
  104. })
  105. return
  106. }
  107. this.setData({
  108. province: selectedOptions[0].code,
  109. city: selectedOptions[1].code,
  110. region: selectedOptions[2]?.code || "",
  111. provinceName: selectedOptions[0].name,
  112. cityName: selectedOptions[1].name,
  113. regionName: selectedOptions[2]?.name || "",
  114. showArea: false
  115. })
  116. },
  117. /** 获取省市区 */
  118. async getAreas() {
  119. try {
  120. const { data } = await api_sysAreaQueryAllProvince({})
  121. this.setData({
  122. areaList: this.formateArea(data.data)
  123. })
  124. } catch {
  125. //
  126. }
  127. },
  128. formateArea(area: any[]) {
  129. const province_list: { [_: string]: string } = {};
  130. const city_list: { [_: string]: string } = {};
  131. const county_list: { [_: string]: string } = {};
  132. area.forEach((item: any) => {
  133. province_list[item.code] = item.name;
  134. });
  135. area.forEach((item: any) => {
  136. item.areas && item.areas.forEach((city: any, index: number) => {
  137. let code = city.code + ""
  138. // 某些数据不标准 这里需要转换一下
  139. if (code[4] !== "0" || code[5] !== "0") {
  140. // 现在把区域的数据改为市的
  141. const newCode = code.substring(0, 2) + (index < 10 ? `a${index}` : index < 20 ? `b${index - 10}` : index < 30 ? `c${index - 20}` : `d${index - 30}`) + "00";
  142. this.data.cacheArea.push({
  143. cityCode: code,
  144. shiftCityCode: newCode
  145. })
  146. code = newCode
  147. }
  148. city_list[code] = city.name;
  149. });
  150. });
  151. area.forEach((item: any) => {
  152. item.areas && item.areas.forEach((city: any) => {
  153. city.areas && city.areas.forEach((county: any) => {
  154. county_list[county.code] = county.name;
  155. });
  156. });
  157. });
  158. return {
  159. province_list,
  160. city_list,
  161. county_list
  162. };
  163. },
  164. // 转换
  165. formateCityCode(reverse?: boolean) {
  166. if (!this.data.region && this.data.city) {
  167. const cityCodeObj = this.data.cacheArea.find((item: any) => {
  168. return item[reverse ? "cityCode" : "shiftCityCode"] == this.data.city
  169. })
  170. return cityCodeObj ? cityCodeObj[reverse ? "shiftCityCode" : "cityCode"] : ""
  171. }
  172. return this.data.city
  173. },
  174. /** 最终提交 */
  175. async onSubmit() {
  176. try {
  177. const params = this.data
  178. if (!params.name) {
  179. wx.showToast({
  180. title: '请输入收货人',
  181. icon: "none"
  182. })
  183. return
  184. }
  185. if (!params.phoneNumber || !/^1[3456789]\d{9}$/.test(params.phoneNumber)) {
  186. wx.showToast({
  187. title: '请输入正确的手机号',
  188. icon: "none"
  189. })
  190. return
  191. }
  192. if (!params.province || !params.city) {
  193. wx.showToast({
  194. title: '请选择地区',
  195. icon: "none"
  196. })
  197. return
  198. }
  199. if (!params.detailAddress) {
  200. wx.showToast({
  201. title: '请输入详细地址',
  202. icon: "none"
  203. })
  204. return
  205. }
  206. wx.showLoading({
  207. mask: true,
  208. title: "",
  209. });
  210. // 转换CityCode
  211. const citycode = this.formateCityCode()
  212. // 编辑
  213. let id
  214. if (params.editId) {
  215. id = params.editId
  216. await api_userReceiveAddressUpdate({
  217. id: params.editId,
  218. phoneNumber: params.phoneNumber,
  219. name: params.name,
  220. province: params.province,
  221. city: citycode,
  222. region: params.region,
  223. detailAddress: params.detailAddress,
  224. defaultStatus: false,
  225. postCode: "",
  226. })
  227. } else {
  228. const { data } = await api_userReceiveAddressSave({
  229. phoneNumber: params.phoneNumber,
  230. name: params.name,
  231. province: params.province,
  232. city: citycode,
  233. region: params.region,
  234. detailAddress: params.detailAddress,
  235. defaultStatus: false,
  236. postCode: "",
  237. })
  238. id = data.data
  239. }
  240. wx.hideLoading()
  241. wx.showToast({
  242. title: '保存成功',
  243. icon: 'none'
  244. })
  245. this.triggerEvent('addAddress', { addressInfo: { id, name: params.name, phoneNumber: params.phoneNumber, addressDes: params.provinceName + params.cityName + (params.regionName || "") + params.detailAddress } }, {})
  246. this.onDialogClose()
  247. } catch {
  248. wx.hideLoading()
  249. //
  250. }
  251. },
  252. }
  253. })