editAddress.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // pages/editAddress/editAddress.js
  2. const request = require("../../utils/request.js");
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. districtData: {},
  9. multiArray: [[], [], []],//picker窗口显示数据
  10. multiIndex: [0, 0, 0],
  11. districtArray: [[], [], []],//实际显示数据
  12. districtIndex: [0, 0, 0],
  13. districtId: null,
  14. name: '',
  15. phone: '',
  16. detailAddress: '',
  17. defaultAddress: false,
  18. userAddressId: null,//修改
  19. editAdressData: {},
  20. delViewStatus: false,
  21. },
  22. /**
  23. * 生命周期函数--监听页面加载
  24. */
  25. onLoad(options) {
  26. this.getDistrictData().then(res=>{
  27. if(options.userAddressId) {
  28. wx.setNavigationBarTitle({
  29. title: '编辑收货地址'
  30. })
  31. this.setData({
  32. userAddressId: options.userAddressId
  33. })
  34. this.getAddressDetail();
  35. }
  36. })
  37. },
  38. /**
  39. * 生命周期函数--监听页面初次渲染完成
  40. */
  41. onReady() {
  42. },
  43. /**
  44. * 生命周期函数--监听页面显示
  45. */
  46. onShow() {
  47. },
  48. /**
  49. * 生命周期函数--监听页面隐藏
  50. */
  51. onHide() {
  52. },
  53. /**
  54. * 生命周期函数--监听页面卸载
  55. */
  56. onUnload() {
  57. },
  58. /**
  59. * 页面相关事件处理函数--监听用户下拉动作
  60. */
  61. onPullDownRefresh() {
  62. },
  63. /**
  64. * 页面上拉触底事件的处理函数
  65. */
  66. onReachBottom() {
  67. },
  68. /**
  69. * 用户点击右上角分享
  70. */
  71. onShareAppMessage() {
  72. },
  73. deleteFun() {
  74. this.setData({
  75. delViewStatus: true
  76. })
  77. },
  78. cancelDel() {
  79. this.setData({
  80. delViewStatus: false
  81. })
  82. },
  83. sureDel() {
  84. let that = this;
  85. request.postData(
  86. 'app/user/address/remove',
  87. {userAddressId: that.data.userAddressId},
  88. res => {
  89. wx.navigateBack({
  90. delta: 1
  91. })
  92. }
  93. )
  94. },
  95. getAddressDetail() {
  96. let that = this;
  97. request.getData(
  98. 'app/user/address/detail',{userAddressId: that.data.userAddressId},
  99. res => {
  100. that.data.districtArray[1] = that.data.multiArray[1] = that.findarr(res.data.data.districtList[0].districtId, that.data.districtData.cityList);
  101. that.data.districtArray[2] = that.data.multiArray[2] = that.findarr(res.data.data.districtList[1].districtId, that.data.districtData.districtList);
  102. that.data.districtIndex[0] = that.data.multiIndex[0] = that.findIndex(res.data.data.districtList[0].districtId, that.data.districtArray[0]);
  103. that.data.districtIndex[1] = that.data.multiIndex[1] = that.findIndex(res.data.data.districtList[1].districtId, that.data.districtArray[1]);
  104. that.data.districtIndex[2] = that.data.multiIndex[2] = that.findIndex(res.data.data.districtList[2].districtId, that.data.districtArray[2]);
  105. let data = {
  106. name: res.data.data.name,
  107. phone: res.data.data.phone,
  108. districtId: res.data.data.districtList[2].districtId,
  109. detailAddress: res.data.data.detailAddress,
  110. defaultAddress: res.data.data.defaultAddress,
  111. multiArray: that.data.multiArray,
  112. districtArray: that.data.districtArray,
  113. districtIndex: that.data.districtIndex,
  114. multiIndex: that.data.multiIndex
  115. };
  116. that.setData(data)
  117. }
  118. )
  119. },
  120. toSave() {
  121. let that = this;
  122. let addressObject
  123. if(that.data.userAddressId) {
  124. addressObject = {
  125. userAddressId: that.data.userAddressId,
  126. name: that.data.name,
  127. phone: that.data.phone,
  128. districtId: that.data.districtId,
  129. detailAddress: that.data.detailAddress,
  130. defaultAddress: that.data.defaultAddress
  131. }
  132. request.postData(
  133. 'app/user/address/update',
  134. addressObject,
  135. res => {
  136. console.log(res.data.data)
  137. wx.navigateBack({
  138. delta: 1
  139. })
  140. }
  141. )
  142. }else{
  143. addressObject = {
  144. name: that.data.name,
  145. phone: that.data.phone,
  146. districtId: that.data.districtId,
  147. detailAddress: that.data.detailAddress,
  148. defaultAddress: that.data.defaultAddress
  149. }
  150. request.postData(
  151. 'app/user/address/create',
  152. addressObject,
  153. res => {
  154. console.log(res.data.data)
  155. wx.navigateBack({
  156. delta: 1
  157. })
  158. }
  159. )
  160. }
  161. console.log(addressObject)
  162. },
  163. switchDefaultAddressChange(event) {
  164. this.setData({
  165. defaultAddress: event.detail.value
  166. })
  167. },
  168. getDetailAddressInput(event) {
  169. this.setData({
  170. detailAddress: event.detail.value
  171. })
  172. },
  173. getPhoneInput(event) {
  174. this.setData({
  175. phone: event.detail.value
  176. })
  177. },
  178. getNameInput(event) {
  179. this.setData({
  180. name: event.detail.value
  181. })
  182. },
  183. findIndex(districtId, arr) {
  184. for(let index=0; index<=arr.length; index++) {
  185. if(arr[index].districtId == districtId) {
  186. return index
  187. }
  188. }
  189. },
  190. findarr(pid, arr) {
  191. var obj = []
  192. arr.forEach((val)=> {
  193. if(val.pid == pid) {
  194. obj.push(val)
  195. }
  196. })
  197. return obj
  198. },
  199. getDistrictData() {
  200. let that = this;
  201. return new Promise((resolve, reject) => {
  202. request.getData(
  203. 'app/district/list',{},
  204. res => {
  205. that.data.multiArray[0] = res.data.data.provinceList;
  206. that.data.multiArray[1] = that.findarr(that.data.multiArray[0][0].districtId, res.data.data.cityList);
  207. that.data.multiArray[2] = that.findarr(that.data.multiArray[1][0].districtId, res.data.data.districtList);
  208. that.setData({
  209. districtData: res.data.data,
  210. multiArray: that.data.multiArray,
  211. districtArray: that.data.multiArray,
  212. })
  213. resolve(res)
  214. }
  215. )
  216. })
  217. },
  218. bindMultiPickerChange: function (e) {
  219. // console.log('picker发送选择改变,携带值为', e.detail.value)
  220. let districtId = this.data.multiArray[2][e.detail.value[2]].districtId;
  221. this.setData({
  222. districtArray: this.data.multiArray,
  223. districtIndex: e.detail.value,
  224. districtId: districtId
  225. })
  226. },
  227. bindMultiPickerColumnChange: function (e) {
  228. let that = this;
  229. // console.log('修改的列为', e.detail.column, ',值为', e.detail.value);
  230. var data = {
  231. multiArray: that.data.multiArray,
  232. multiIndex: that.data.multiIndex
  233. };
  234. data.multiIndex[e.detail.column] = e.detail.value;
  235. switch (e.detail.column) {
  236. case 0:
  237. data.multiArray[1] = that.findarr(data.multiArray[0][e.detail.value].districtId, that.data.districtData.cityList);
  238. data.multiArray[2] = that.findarr(data.multiArray[1][0].districtId, that.data.districtData.districtList);
  239. data.multiIndex[1] = 0;
  240. data.multiIndex[2] = 0;
  241. break;
  242. case 1:
  243. data.multiArray[2] = that.findarr(data.multiArray[1][e.detail.value].districtId, that.data.districtData.districtList);
  244. data.multiIndex[2] = 0;
  245. break;
  246. }
  247. // console.log(data.multiIndex);
  248. that.setData(data);
  249. },
  250. })