index.ts 8.0 KB

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