editAddress.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // pages/editAddress/editAddress.js
  2. var http = require("../../utils/http.js");
  3. var util = require("../../utils/util.js");
  4. var show = false;
  5. var moveY = 200;
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. value: [0, 0, 0],
  12. provArray: [],
  13. cityArray: [],
  14. areaArray: [],
  15. province: "",
  16. city: "",
  17. area: "",
  18. provinceId: 0,
  19. cityId: 0,
  20. areaId: 0,
  21. receiver: "",
  22. mobile: "",
  23. addr: "",
  24. addrId: 0,
  25. sel_address:{},
  26. is_select:false,
  27. },
  28. onLoad: function (options) {
  29. if (options.addrId) {
  30. wx.showLoading();
  31. var params = {
  32. url: "/p/address/addrInfo/" + options.addrId,
  33. method: "GET",
  34. callBack: res => {
  35. this.setData({
  36. is_select:true,
  37. sel_address:res,
  38. province: res.province,
  39. city: res.city,
  40. area: res.area,
  41. provinceId: res.provinceId,
  42. cityId: res.cityId,
  43. areaId: res.areaId,
  44. receiver: res.receiver,
  45. mobile: res.mobile,
  46. addr: res.addrDetail,
  47. addrId: options.addrId
  48. });
  49. // this.initCityData(res.provinceId, res.cityId, res.areaId);
  50. wx.hideLoading();
  51. }
  52. }
  53. http.request(params);
  54. } else {
  55. // this.initCityData(this.data.provinceId, this.data.cityId, this.data.areaId);
  56. }
  57. },
  58. // initCityData: function (provinceId, cityId, areaId) {
  59. // var ths = this;
  60. // wx.showLoading();
  61. // var params = {
  62. // url: "/p/area/listByPid",
  63. // method: "GET",
  64. // data: {
  65. // pid: 0
  66. // },
  67. // callBack: function (res) {
  68. // ths.setData({
  69. // provArray: res
  70. // });
  71. // if (provinceId) {
  72. // for (var index in res) {
  73. // if (res[index].areaId == provinceId) {
  74. // ths.setData({
  75. // value: [index, ths.data.value[1], ths.data.value[2]]
  76. // });
  77. // }
  78. // }
  79. // }
  80. // ths.getCityArray(provinceId ? provinceId : res[0].areaId, cityId, areaId);
  81. // wx.hideLoading();
  82. // }
  83. // }
  84. // http.request(params);
  85. // },
  86. /**
  87. * 生命周期函数--监听页面显示
  88. */
  89. onShow: function () {
  90. },
  91. onReady: function () {
  92. this.animation = wx.createAnimation({
  93. transformOrigin: "50% 50%",
  94. duration: 0,
  95. timingFunction: "ease",
  96. delay: 0
  97. })
  98. this.animation.translateY(200 + 'vh').step();
  99. this.setData({
  100. animation: this.animation.export(),
  101. show: show
  102. })
  103. },
  104. //移动按钮点击事件
  105. translate: function (e) {
  106. let that = this
  107. wx.chooseLocation({
  108. success: function (res) {
  109. that.setData({
  110. sel_address:res,
  111. is_select:true
  112. })
  113. },
  114. fail: function (err) {
  115. console.log(err, '---err');
  116. },
  117. complete: function (e) {
  118. // console.log(e, '-----callback');
  119. }
  120. })
  121. },
  122. //隐藏弹窗浮层
  123. hiddenFloatView(e) {
  124. //console.log(e);
  125. moveY = 200;
  126. show = true;
  127. t = 0;
  128. this.animationEvents(this, moveY, show);
  129. },
  130. //动画事件
  131. animationEvents: function (that, moveY, show) {
  132. //console.log("moveY:" + moveY + "\nshow:" + show);
  133. that.animation = wx.createAnimation({
  134. transformOrigin: "50% 50%",
  135. duration: 400,
  136. timingFunction: "ease",
  137. delay: 0
  138. })
  139. that.animation.translateY(moveY + 'vh').step()
  140. that.setData({
  141. animation: that.animation.export()
  142. })
  143. },
  144. /**
  145. * 根据省份ID获取 城市数据
  146. */
  147. // getCityArray: function (provinceId, cityId, areaId) {
  148. // var ths = this;
  149. // var params = {
  150. // url: "/p/area/listByPid",
  151. // method: "GET",
  152. // data: {
  153. // pid: provinceId
  154. // },
  155. // callBack: function (res) {
  156. // //console.log(res)
  157. // ths.setData({
  158. // cityArray: res
  159. // });
  160. // if (cityId) {
  161. // for (var index in res) {
  162. // if (res[index].areaId == cityId) {
  163. // ths.setData({
  164. // value: [ths.data.value[0], index, ths.data.value[2]]
  165. // });
  166. // }
  167. // }
  168. // }
  169. // ths.getAreaArray(cityId ? cityId : res[0].areaId, areaId);
  170. // wx.hideLoading();
  171. // }
  172. // }
  173. // http.request(params);
  174. // },
  175. /**
  176. * 根据城市ID获取 区数据
  177. */
  178. // getAreaArray: function (cityId, areaId) {
  179. // var ths = this;
  180. // var params = {
  181. // url: "/p/area/listByPid",
  182. // method: "GET",
  183. // data: {
  184. // pid: cityId
  185. // },
  186. // callBack: function (res) {
  187. // //console.log(res)
  188. // ths.setData({
  189. // areaArray: res
  190. // });
  191. // if (areaId) {
  192. // for (var _index in res) {
  193. // if (res[_index].areaId == areaId) {
  194. // ths.setData({
  195. // value: [ths.data.value[0], ths.data.value[1], _index]
  196. // });
  197. // }
  198. // }
  199. // index = ths.data.value;
  200. // ths.setData({
  201. // province: ths.data.province,
  202. // city: ths.data.city,
  203. // area: ths.data.area,
  204. // provinceId: ths.data.provinceId,
  205. // cityId: ths.data.cityId,
  206. // areaId: ths.data.areaId
  207. // })
  208. // } else {
  209. // ths.setData({
  210. // // province: ths.data.provArray[ths.data.value[0]].areaName,
  211. // // city: ths.data.cityArray[ths.data.value[1]].areaName,
  212. // // area: ths.data.areaArray[ths.data.value[2]].areaName,
  213. // // provinceId: ths.data.provArray[ths.data.value[0]].areaId,
  214. // // cityId: ths.data.cityArray[ths.data.value[1]].areaId,
  215. // // areaId: ths.data.areaArray[ths.data.value[2]].areaId
  216. // })
  217. // }
  218. // wx.hideLoading();
  219. // }
  220. // }
  221. // http.request(params);
  222. // },
  223. bindRegionChange: function (e) {
  224. //console.log('picker发送选择改变,携带值为', e.detail.value)
  225. this.setData({
  226. region: e.detail.value
  227. })
  228. },
  229. /**
  230. * 保存地址
  231. */
  232. onSaveAddr: function () {
  233. var ths = this;
  234. var receiver = ths.data.receiver;
  235. var mobile = ths.data.mobile;
  236. var addr = ths.data.addr;
  237. if (!receiver) {
  238. wx.showToast({
  239. title: '请输入收货人姓名',
  240. icon: "none"
  241. })
  242. return;
  243. }
  244. if (!mobile) {
  245. wx.showToast({
  246. title: '请输入手机号码',
  247. icon: "none"
  248. })
  249. return;
  250. }
  251. if (!util.checkPhoneNumber(this.data.mobile)) {
  252. wx.showToast({
  253. title: '请输入正确的手机号码',
  254. icon: "none"
  255. })
  256. return;
  257. }
  258. if (!addr) {
  259. wx.showToast({
  260. title: '请输入详细地址',
  261. icon: "none"
  262. })
  263. return;
  264. }
  265. wx.showLoading();
  266. var url = "/p/address/addAddr";
  267. var method = "POST";
  268. if (ths.data.addrId != 0) {
  269. url = "/p/address/updateAddr";
  270. method = "PUT";
  271. }
  272. //添加或修改地址
  273. var params = {
  274. url: url,
  275. method: method,
  276. data: {
  277. receiver: ths.data.receiver,
  278. mobile: ths.data.mobile,
  279. addrDetail: ths.data.addr,
  280. province: ths.data.province,
  281. provinceId: ths.data.provinceId,
  282. city: ths.data.city,
  283. cityId: ths.data.cityId,
  284. areaId: ths.data.areaId,
  285. area: ths.data.area,
  286. userType: 0,
  287. addrId: ths.data.addrId,
  288. addressName:ths.data.sel_address.addressName||ths.data.sel_address.name,
  289. address:ths.data.sel_address.address,
  290. longitude:ths.data.sel_address.longitude,
  291. latitude:ths.data.sel_address.latitude
  292. },
  293. callBack: function (res) {
  294. wx.hideLoading();
  295. wx.navigateBack({
  296. delta: 1
  297. })
  298. }
  299. }
  300. http.request(params);
  301. },
  302. onReceiverInput: function (e) {
  303. this.setData({
  304. receiver: e.detail.value
  305. });
  306. },
  307. onMobileInput: function (e) {
  308. this.setData({
  309. mobile: e.detail.value
  310. });
  311. },
  312. onAddrInput: function (e) {
  313. this.setData({
  314. addr: e.detail.value
  315. });
  316. },
  317. //删除配送地址
  318. onDeleteAddr: function (e) {
  319. var ths = this;
  320. wx.showModal({
  321. title: '',
  322. content: '确定要删除此收货地址吗?',
  323. confirmColor: "#04358D",
  324. success(res) {
  325. if (res.confirm) {
  326. var addrId = ths.data.addrId;
  327. wx.showLoading();
  328. var params = {
  329. url: "/p/address/deleteAddr/" + addrId,
  330. method: "DELETE",
  331. data: {},
  332. callBack: function (res) {
  333. wx.hideLoading();
  334. wx.navigateBack({
  335. delta: 1
  336. })
  337. }
  338. }
  339. http.request(params);
  340. } else if (res.cancel) {
  341. console.log('用户点击取消')
  342. }
  343. }
  344. })
  345. },
  346. })