address-detail.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import { api_sysAreaQueryAllProvince, api_userReceiveAddressDetail, api_userReceiveAddressSave, api_userReceiveAddressUpdate } from "../../api/new";
  2. // pages/address/address-detail.ts
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. addressShow: false,
  9. addressAfterLeave: false,
  10. showDialog: false,
  11. cacheArea: [] as { cityCode: string, shiftCityCode: string }[], // 临时存储的对应关系
  12. showArea: false,
  13. showAreaAfterLeave: false,
  14. areaList: [] as any,
  15. currentValues: [] as any,
  16. // 添加地址表单信息
  17. id: "",
  18. name: '',
  19. phoneNumber: '',
  20. detailAddress: '',
  21. cityCode: null,
  22. cityName: "",
  23. provinceCode: null,
  24. provinceName: "",
  25. regionCode: '',
  26. regionName: "",
  27. btnLoading: false
  28. },
  29. /**
  30. * 生命周期函数--监听页面加载
  31. */
  32. async onLoad(options: any) {
  33. await this.getAreas()
  34. console.log(options, "options")
  35. if (options.receiveAddress) {
  36. this.setData({
  37. id: options.receiveAddress
  38. }, () => {
  39. this.getDetail()
  40. })
  41. }
  42. },
  43. /** 获取详情 */
  44. async getDetail() {
  45. try {
  46. if (!this.data.id) return
  47. const { data } = await api_userReceiveAddressDetail({ id: this.data.id })
  48. const result = data.data
  49. this.setData({
  50. name: result.name,
  51. phoneNumber: result.phoneNumber,
  52. detailAddress: result.detailAddress,
  53. cityCode: result.city,
  54. cityName: result.cityName,
  55. provinceCode: result.province,
  56. provinceName: result.provinceName,
  57. regionCode: result.region,
  58. regionName: result.regionName,
  59. }, () => {
  60. const cityCode: any = this.formateCityCode(true)
  61. this.setData({
  62. cityCode
  63. })
  64. })
  65. } catch {
  66. //
  67. }
  68. },
  69. /** 获取省市区 */
  70. async getAreas() {
  71. try {
  72. const { data } = await api_sysAreaQueryAllProvince({})
  73. const areaList: any = this.formateArea(data.data)
  74. const currentValues = []
  75. if (areaList?.province_list) {
  76. // 获取第一个键值对
  77. const firstKey = Object.keys(areaList?.province_list)[0];
  78. // 通过键获取值
  79. const firstValue = areaList?.province_list[firstKey];
  80. currentValues.push({
  81. code: firstKey,
  82. name: firstValue
  83. })
  84. }
  85. if (areaList?.city_list) {
  86. // 获取第一个键值对
  87. const firstKey = Object.keys(areaList?.city_list)[0];
  88. // 通过键获取值
  89. const firstValue = areaList?.city_list[firstKey];
  90. currentValues.push({
  91. code: firstKey,
  92. name: firstValue
  93. })
  94. }
  95. if (areaList?.county_list) {
  96. // 获取第一个键值对
  97. const firstKey = Object.keys(areaList?.county_list)[0];
  98. // 通过键获取值
  99. const firstValue = areaList?.county_list[firstKey];
  100. currentValues.push({
  101. code: firstKey,
  102. name: firstValue
  103. })
  104. }
  105. console.log(areaList,
  106. currentValues)
  107. this.setData({
  108. areaList,
  109. currentValues
  110. })
  111. } catch {
  112. //
  113. }
  114. },
  115. formateArea(area: any[]) {
  116. const province_list: { [_: string]: string } = {};
  117. const city_list: { [_: string]: string } = {};
  118. const county_list: { [_: string]: string } = {};
  119. area.forEach((item: any) => {
  120. province_list[item.code] = item.name;
  121. });
  122. area.forEach((item: any) => {
  123. item.areas && item.areas.forEach((city: any, index: number) => {
  124. let code = city.code + ""
  125. // 某些数据不标准 这里需要转换一下
  126. if (code[4] !== "0" || code[5] !== "0") {
  127. // 现在把区域的数据改为市的
  128. const newCode = code.substring(0, 2) + (index < 10 ? `a${index}` : index < 20 ? `b${index - 10}` : index < 30 ? `c${index - 20}` : `d${index - 30}`) + "00";
  129. this.data.cacheArea.push({
  130. cityCode: code,
  131. shiftCityCode: newCode
  132. })
  133. code = newCode
  134. }
  135. city_list[code] = city.name;
  136. });
  137. });
  138. area.forEach((item: any) => {
  139. item.areas && item.areas.forEach((city: any) => {
  140. city.areas && city.areas.forEach((county: any) => {
  141. county_list[county.code] = county.name;
  142. });
  143. });
  144. });
  145. return {
  146. province_list,
  147. city_list,
  148. county_list
  149. };
  150. },
  151. // 转换
  152. formateCityCode(reverse?: boolean) {
  153. if (!this.data.regionCode && this.data.cityCode) {
  154. const cityCodeObj = this.data.cacheArea.find((item: any) => {
  155. return item[reverse ? "cityCode" : "shiftCityCode"] == this.data.cityCode
  156. })
  157. return cityCodeObj ? cityCodeObj[reverse ? "shiftCityCode" : "cityCode"] : ""
  158. }
  159. return this.data.cityCode
  160. },
  161. /** 显示选择地区 */
  162. onShowAreaList() {
  163. this.setData({
  164. showArea: true
  165. })
  166. },
  167. /** 关闭选择地区 */
  168. onCloseAreaList() {
  169. this.setData({
  170. showArea: false
  171. })
  172. },
  173. onAreaBeforeEnter() {
  174. this.setData({
  175. showAreaAfterLeave: false
  176. })
  177. },
  178. onAreaAfterLeave() {
  179. this.setData({
  180. showAreaAfterLeave: true
  181. })
  182. },
  183. /** 确定选择地区 */
  184. submitArea(e: any) {
  185. const selectedOptions: any = e.detail.values
  186. this.setData({
  187. provinceCode: selectedOptions[0].code,
  188. cityCode: selectedOptions[1].code,
  189. regionCode: selectedOptions[2]?.code || null,
  190. provinceName: selectedOptions[0].name || '',
  191. cityName: selectedOptions[1].name || '',
  192. regionName: selectedOptions[2]?.name || '',
  193. showArea: false,
  194. })
  195. },
  196. /** 创建/修改收货地址 */
  197. async onOperationAddress() {
  198. const addressForm = this.data
  199. try {
  200. if (!addressForm.name) {
  201. wx.showToast({
  202. title: '请输入收货人姓名',
  203. icon: "none"
  204. })
  205. return
  206. }
  207. if (!addressForm.phoneNumber || !/^1[3456789]\d{9}$/.test(addressForm.phoneNumber)) {
  208. wx.showToast({
  209. title: '请输入正确的手机号码',
  210. icon: "none"
  211. })
  212. return
  213. }
  214. if (!addressForm.provinceCode || !addressForm.cityCode) {
  215. wx.showToast({
  216. title: '请选择地区',
  217. icon: "none"
  218. })
  219. return
  220. }
  221. if (!addressForm.detailAddress) {
  222. wx.showToast({
  223. title: '请输入详细地址',
  224. icon: "none"
  225. })
  226. return
  227. }
  228. const citycode = this.formateCityCode()
  229. const params = {
  230. name: addressForm.name,
  231. phoneNumber: addressForm.phoneNumber,
  232. province: addressForm.provinceCode,
  233. city: citycode,
  234. region: addressForm.regionCode || '',
  235. detailAddress: addressForm.detailAddress
  236. }
  237. this.setData({
  238. btnLoading: true
  239. })
  240. const pages = getCurrentPages();
  241. const prevPage = pages[pages.length - 2]; // 获取上一个页面实例
  242. console.log(addressForm.id, "addressForm.id")
  243. if (addressForm.id) {
  244. await api_userReceiveAddressUpdate({
  245. id: addressForm.id,
  246. ...params
  247. })
  248. wx.showToast({
  249. title: '保存成功',
  250. icon: 'none'
  251. })
  252. prevPage?.setData({
  253. backParams: {
  254. receiveAddress: this.data.id,
  255. receiveAddressInfo: {
  256. addressDetail: this.data.id ? (addressForm.provinceName || '') + (addressForm.cityName || '') + (addressForm.regionName || '') + addressForm.detailAddress : '',
  257. name: addressForm?.name,
  258. phoneNumber: addressForm?.phoneNumber
  259. }
  260. }
  261. });
  262. } else {
  263. const { data } = await api_userReceiveAddressSave({
  264. ...params
  265. })
  266. wx.showToast({
  267. title: '保存成功',
  268. icon: 'none'
  269. })
  270. prevPage?.setData({
  271. backParams: {
  272. receiveAddress: data.data,
  273. receiveAddressInfo: {
  274. addressDetail: data.data ? (addressForm.provinceName || '') + (addressForm.cityName || '') + (addressForm.regionName || '') + addressForm.detailAddress : '',
  275. name: addressForm?.name,
  276. phoneNumber: addressForm?.phoneNumber
  277. }
  278. }
  279. });
  280. }
  281. setTimeout(() => {
  282. this.setData({
  283. btnLoading: false
  284. })
  285. wx.navigateBack()
  286. }, 1000);
  287. } catch (e) {
  288. //
  289. console.log(e, '1212')
  290. this.setData({
  291. btnLoading: false
  292. })
  293. }
  294. }
  295. })