addAddress.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. this.setData({
  102. province: selectedOptions[0].code,
  103. city: selectedOptions[1].code,
  104. region: selectedOptions[2]?.code || "",
  105. provinceName: selectedOptions[0].name,
  106. cityName: selectedOptions[1].name,
  107. regionName: selectedOptions[2]?.name || "",
  108. showArea: false
  109. })
  110. },
  111. /** 获取省市区 */
  112. async getAreas() {
  113. try {
  114. const { data } = await api_sysAreaQueryAllProvince({})
  115. this.setData({
  116. areaList: this.formateArea(data.data)
  117. })
  118. } catch {
  119. //
  120. }
  121. },
  122. formateArea(area: any[]) {
  123. const province_list: { [_: string]: string } = {};
  124. const city_list: { [_: string]: string } = {};
  125. const county_list: { [_: string]: string } = {};
  126. area.forEach((item: any) => {
  127. province_list[item.code] = item.name;
  128. });
  129. area.forEach((item: any) => {
  130. item.areas && item.areas.forEach((city: any, index: number) => {
  131. let code = city.code + ""
  132. // 某些数据不标准 这里需要转换一下
  133. if (code[4] !== "0" || code[5] !== "0") {
  134. // 现在把区域的数据改为市的
  135. const newCode = code.substring(0, 2) + (index < 10 ? `a${index}` : index < 20 ? `b${index - 10}` : index < 30 ? `c${index - 20}` : `d${index - 30}`) + "00";
  136. this.data.cacheArea.push({
  137. cityCode: code,
  138. shiftCityCode: newCode
  139. })
  140. code = newCode
  141. }
  142. city_list[code] = city.name;
  143. });
  144. });
  145. area.forEach((item: any) => {
  146. item.areas && item.areas.forEach((city: any) => {
  147. city.areas && city.areas.forEach((county: any) => {
  148. county_list[county.code] = county.name;
  149. });
  150. });
  151. });
  152. return {
  153. province_list,
  154. city_list,
  155. county_list
  156. };
  157. },
  158. // 转换
  159. formateCityCode(reverse?: boolean) {
  160. if (!this.data.region && this.data.city) {
  161. const cityCodeObj = this.data.cacheArea.find((item: any) => {
  162. return item[reverse ? "cityCode" : "shiftCityCode"] == this.data.city
  163. })
  164. return cityCodeObj ? cityCodeObj[reverse ? "shiftCityCode" : "cityCode"] : ""
  165. }
  166. return this.data.city
  167. },
  168. /** 最终提交 */
  169. async onSubmit() {
  170. try {
  171. const params = this.data
  172. if (!params.name) {
  173. wx.showToast({
  174. title: '请输入收货人',
  175. icon: "none"
  176. })
  177. return
  178. }
  179. if (!params.phoneNumber || !/^1[3456789]\d{9}$/.test(params.phoneNumber)) {
  180. wx.showToast({
  181. title: '请输入正确的手机号',
  182. icon: "none"
  183. })
  184. return
  185. }
  186. if (!params.province || !params.city) {
  187. wx.showToast({
  188. title: '请选择地区',
  189. icon: "none"
  190. })
  191. return
  192. }
  193. if (!params.detailAddress) {
  194. wx.showToast({
  195. title: '请输入详细地址',
  196. icon: "none"
  197. })
  198. return
  199. }
  200. wx.showLoading({
  201. mask: true,
  202. title: "",
  203. });
  204. // 转换CityCode
  205. const citycode = this.formateCityCode()
  206. // 编辑
  207. let id
  208. if (params.editId) {
  209. id = params.editId
  210. await api_userReceiveAddressUpdate({
  211. id: params.editId,
  212. phoneNumber: params.phoneNumber,
  213. name: params.name,
  214. province: params.province,
  215. city: citycode,
  216. region: params.region,
  217. detailAddress: params.detailAddress,
  218. defaultStatus: false,
  219. postCode: "",
  220. })
  221. wx.showToast({
  222. title: '保存成功',
  223. icon: 'none'
  224. })
  225. } else {
  226. const { data } = await api_userReceiveAddressSave({
  227. phoneNumber: params.phoneNumber,
  228. name: params.name,
  229. province: params.province,
  230. city: citycode,
  231. region: params.region,
  232. detailAddress: params.detailAddress,
  233. defaultStatus: false,
  234. postCode: "",
  235. })
  236. id = data.data
  237. wx.showToast({
  238. title: '保存成功',
  239. icon: 'none'
  240. })
  241. }
  242. wx.hideLoading()
  243. this.triggerEvent('addAddress', { addressInfo: { id, name: params.name, phoneNumber: params.phoneNumber, addressDes: params.provinceName + params.cityName + (params.regionName || "") + params.detailAddress } }, {})
  244. this.onDialogClose()
  245. } catch {
  246. wx.hideLoading()
  247. //
  248. }
  249. },
  250. }
  251. })