addAddress.ts 6.9 KB

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