index.ts 9.3 KB

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