You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

addAddress.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import Api from "../../../api/index.js"
  2. import { requestFnc } from "../../../utils/request.js"
  3. import { getStore } from "../../../utils/index.js"
  4. Page({
  5. data: {
  6. isLoading: false,
  7. linkman: "",
  8. linkPhone: "",
  9. address: "",
  10. addressId: "",
  11. showCity: false,
  12. provinceCode: "",
  13. provinceName: "",
  14. cityCode: "",
  15. cityName: "",
  16. areaName: "",
  17. areaCode: "",
  18. currentType: "",
  19. sendType: "1",
  20. cityList: [],
  21. searchProvinceCode: "",
  22. searchCityCode: "",
  23. checked: false,
  24. isEdit: false,
  25. },
  26. onLoad(options) {
  27. if (options.address) {
  28. let adressObj = JSON.parse(options.address)
  29. this.setData({
  30. isEdit: true,
  31. ...adressObj,
  32. })
  33. if (adressObj.isDefaultAddress == 1) {
  34. this.setData({
  35. checked: true,
  36. })
  37. }
  38. }
  39. },
  40. onChange({ detail }) {
  41. // 需要手动对 checked 状态进行更新
  42. this.setData({ checked: detail })
  43. },
  44. // 选择省、市、区
  45. onCityClick(e) {
  46. let type = e.currentTarget.dataset.type
  47. this.setData({
  48. currentType: type,
  49. })
  50. if (type === "close") {
  51. this.setData({
  52. showCity: false,
  53. })
  54. return
  55. }
  56. if (type === "provice") {
  57. this.setData({
  58. searchProvinceCode: "",
  59. searchCityCode: "",
  60. })
  61. }
  62. if (type === "city") {
  63. this.setData({
  64. searchProvinceCode: this.data.provinceCode,
  65. searchCityCode: "",
  66. })
  67. }
  68. if (type === "county") {
  69. this.setData({
  70. searchProvinceCode: this.data.provinceCode,
  71. searchCityCode: this.data.cityCode,
  72. })
  73. }
  74. this.getProvice(type)
  75. },
  76. // 获取输入框的值
  77. changeInput(e) {
  78. let key = e.currentTarget.dataset.type
  79. this.data[key] = e.detail
  80. this.setData({
  81. ...this.data,
  82. })
  83. },
  84. // 选择城市
  85. onSelectCity(event) {
  86. console.log("event:", event)
  87. let { value } = event.detail
  88. if (this.data.currentType === "provice") {
  89. this.setData({
  90. provinceCode: value.code,
  91. provinceName: value.name,
  92. searchProvinceCode: value.code,
  93. searchCityCode: "",
  94. cityCode: "",
  95. cityName: "",
  96. areaName: "",
  97. areaCode: "",
  98. showCity: false,
  99. })
  100. }
  101. if (this.data.currentType === "city") {
  102. this.setData({
  103. cityCode: value.code,
  104. cityName: value.name,
  105. searchCityCode: value.code,
  106. areaName: "",
  107. areaCode: "",
  108. showCity: false,
  109. })
  110. }
  111. if (this.data.currentType === "county") {
  112. this.setData({
  113. areaName: value.name,
  114. areaCode: value.code,
  115. showCity: false,
  116. })
  117. }
  118. },
  119. // 获取收货地址
  120. getAddress(cb) {
  121. let params = {
  122. filename: Api.getAddress.filename,
  123. data: {
  124. accountId: getStore("accountId"),
  125. },
  126. }
  127. requestFnc(Api.getAddress.url, params, (res) => {
  128. let adressObj = res.dataModel[0]
  129. this.setData({
  130. ...adressObj,
  131. })
  132. cb && cb()
  133. })
  134. },
  135. // 获取省市区
  136. getProvice(type) {
  137. let params = {
  138. filename: Api.getProvice.filename,
  139. data: {
  140. provinceCode: this.data.searchProvinceCode,
  141. cityCode: this.data.searchCityCode,
  142. },
  143. }
  144. requestFnc(Api.getProvice.url, params, (res) => {
  145. let list = res.data || []
  146. for (let obj of list) {
  147. obj.text = obj.name
  148. }
  149. this.setData({
  150. cityList: list,
  151. })
  152. if (type) {
  153. this.setData({
  154. showCity: true,
  155. })
  156. }
  157. })
  158. },
  159. // 保存地址
  160. saveAddress() {
  161. let title = ""
  162. if (!this.data.linkPhone || !this.data.linkman) {
  163. title = "联系人和联系方式不能为空"
  164. }
  165. if (
  166. !this.data.provinceCode ||
  167. !this.data.cityCode ||
  168. !this.data.areaCode
  169. ) {
  170. title = "省市区不能为空"
  171. }
  172. if (title) {
  173. wx.showToast({
  174. title: title,
  175. icon: "none",
  176. })
  177. return
  178. }
  179. this.setData({
  180. isLoading: true,
  181. })
  182. let params = {
  183. filename: Api.saveAddress.filename,
  184. data: {
  185. accountId: getStore("accountId"),
  186. addressId: this.data.addressId,
  187. linkman: this.data.linkman,
  188. address: this.data.address,
  189. linkPhone: this.data.linkPhone,
  190. provinceCode: this.data.provinceCode,
  191. cityCode: this.data.cityCode,
  192. areaCode: this.data.areaCode,
  193. postcode: this.data.postcode,
  194. sendType: this.data.sendType,
  195. isDefaultAddress: this.data.checked ? "1" : "0",
  196. },
  197. }
  198. requestFnc(
  199. Api.saveAddress.url,
  200. params,
  201. (res) => {
  202. this.setData({
  203. isLoading: false,
  204. })
  205. wx.navigateBack({
  206. delta: 1,
  207. })
  208. },
  209. () => {
  210. this.setData({
  211. isLoading: false,
  212. })
  213. }
  214. )
  215. },
  216. // 删除地址
  217. delAddress(e) {
  218. wx.showModal({
  219. title: "提示",
  220. content: "确定要删除此地址吗?",
  221. success: (res) => {
  222. if (res.confirm) {
  223. let params = {
  224. filename: Api.delAddress.filename,
  225. data: {
  226. accountId: getStore("accountId"),
  227. addressId: this.data.addressId,
  228. },
  229. }
  230. this.setData({
  231. isLoading: true,
  232. })
  233. requestFnc(
  234. Api.delAddress.url,
  235. params,
  236. (res) => {
  237. this.setData({
  238. isLoading: false,
  239. })
  240. wx.navigateBack({
  241. delta: 1,
  242. })
  243. },
  244. () => {
  245. this.setData({
  246. isLoading: false,
  247. })
  248. }
  249. )
  250. }
  251. },
  252. })
  253. },
  254. })