editAddress.js 9.3 KB

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