index.ts 9.7 KB

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