editAddress.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. if(that.data.name == '') {
  123. wx.showToast({
  124. title: '请输入姓名',
  125. icon: 'none',
  126. duration: 2000
  127. })
  128. return
  129. }else if(that.data.phone == '') {
  130. wx.showToast({
  131. title: '请输入手机号码',
  132. icon: 'none',
  133. duration: 2000
  134. })
  135. return
  136. }else if(that.data.detailAddress == '') {
  137. wx.showToast({
  138. title: '请输入详细地址',
  139. icon: 'none',
  140. duration: 2000
  141. })
  142. return
  143. }
  144. let addressObject
  145. if(that.data.userAddressId) {
  146. addressObject = {
  147. userAddressId: that.data.userAddressId,
  148. name: that.data.name,
  149. phone: that.data.phone,
  150. districtId: that.data.districtId,
  151. detailAddress: that.data.detailAddress,
  152. defaultAddress: that.data.defaultAddress
  153. }
  154. request.postData(
  155. 'app/user/address/update',
  156. addressObject,
  157. res => {
  158. console.log(res.data.data)
  159. wx.navigateBack({
  160. delta: 1
  161. })
  162. }
  163. )
  164. }else{
  165. addressObject = {
  166. name: that.data.name,
  167. phone: that.data.phone,
  168. districtId: that.data.districtId,
  169. detailAddress: that.data.detailAddress,
  170. defaultAddress: that.data.defaultAddress
  171. }
  172. request.postData(
  173. 'app/user/address/create',
  174. addressObject,
  175. res => {
  176. console.log(res.data.data)
  177. wx.navigateBack({
  178. delta: 1
  179. })
  180. }
  181. )
  182. }
  183. console.log(addressObject)
  184. },
  185. switchDefaultAddressChange(event) {
  186. this.setData({
  187. defaultAddress: event.detail.value
  188. })
  189. },
  190. getDetailAddressInput(event) {
  191. this.setData({
  192. detailAddress: event.detail.value
  193. })
  194. },
  195. getPhoneInput(event) {
  196. this.setData({
  197. phone: event.detail.value
  198. })
  199. },
  200. getNameInput(event) {
  201. this.setData({
  202. name: event.detail.value
  203. })
  204. },
  205. findIndex(districtId, arr) {
  206. for(let index=0; index<=arr.length; index++) {
  207. if(arr[index].districtId == districtId) {
  208. return index
  209. }
  210. }
  211. },
  212. findarr(pid, arr) {
  213. var obj = []
  214. arr.forEach((val)=> {
  215. if(val.pid == pid) {
  216. obj.push(val)
  217. }
  218. })
  219. return obj
  220. },
  221. getDistrictData() {
  222. let that = this;
  223. return new Promise((resolve, reject) => {
  224. var districtData = wx.getStorageSync('districtData')
  225. that.data.multiArray[0] = districtData.provinceList;
  226. that.data.multiArray[1] = that.findarr(that.data.multiArray[0][0].districtId, districtData.cityList);
  227. that.data.multiArray[2] = that.findarr(that.data.multiArray[1][0].districtId, districtData.districtList);
  228. that.setData({
  229. districtData: districtData,
  230. multiArray: that.data.multiArray,
  231. districtArray: that.data.multiArray,
  232. })
  233. resolve(districtData)
  234. })
  235. },
  236. // getDistrictData() {
  237. // let that = this;
  238. // return new Promise((resolve, reject) => {
  239. // request.getData(
  240. // 'app/district/list',{},
  241. // res => {
  242. // that.data.multiArray[0] = res.data.data.provinceList;
  243. // that.data.multiArray[1] = that.findarr(that.data.multiArray[0][0].districtId, res.data.data.cityList);
  244. // that.data.multiArray[2] = that.findarr(that.data.multiArray[1][0].districtId, res.data.data.districtList);
  245. // that.setData({
  246. // districtData: res.data.data,
  247. // multiArray: that.data.multiArray,
  248. // districtArray: that.data.multiArray,
  249. // })
  250. // resolve(res)
  251. // }
  252. // )
  253. // })
  254. // },
  255. bindMultiPickerChange: function (e) {
  256. // console.log('picker发送选择改变,携带值为', e.detail.value)
  257. let districtId = this.data.multiArray[2][e.detail.value[2]].districtId;
  258. this.setData({
  259. districtArray: this.data.multiArray,
  260. districtIndex: e.detail.value,
  261. districtId: districtId
  262. })
  263. },
  264. bindMultiPickerColumnChange: function (e) {
  265. let that = this;
  266. // console.log('修改的列为', e.detail.column, ',值为', e.detail.value);
  267. var data = {
  268. multiArray: that.data.multiArray,
  269. multiIndex: that.data.multiIndex
  270. };
  271. data.multiIndex[e.detail.column] = e.detail.value;
  272. switch (e.detail.column) {
  273. case 0:
  274. data.multiArray[1] = that.findarr(data.multiArray[0][e.detail.value].districtId, that.data.districtData.cityList);
  275. data.multiArray[2] = that.findarr(data.multiArray[1][0].districtId, that.data.districtData.districtList);
  276. data.multiIndex[1] = 0;
  277. data.multiIndex[2] = 0;
  278. break;
  279. case 1:
  280. data.multiArray[2] = that.findarr(data.multiArray[1][e.detail.value].districtId, that.data.districtData.districtList);
  281. data.multiIndex[2] = 0;
  282. break;
  283. }
  284. // console.log(data.multiIndex);
  285. that.setData(data);
  286. },
  287. })