index.ts 9.0 KB

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