index.ts 7.9 KB

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