index.ts 10 KB

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