addaddress.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. var app = getApp();
  2. Page({
  3. data: {
  4. _num:1,
  5. region: ['省','市','区'],
  6. cartId : '',
  7. pinkId : '',
  8. couponId : '',
  9. id:0,
  10. userAddress:[]
  11. },
  12. onLoad: function (options) {
  13. app.setBarColor();
  14. app.setUserInfo();
  15. if (options.cartId){
  16. this.setData({
  17. cartId: options.cartId,
  18. pinkId: options.pinkId,
  19. couponId: options.couponId,
  20. })
  21. }
  22. if (options.id){
  23. this.setData({
  24. id: options.id
  25. })
  26. this.getUserAddress();
  27. }
  28. },
  29. getUserAddress: function () {//get_user_address
  30. var that = this;
  31. wx.request({
  32. url: app.globalData.url + '/routine/auth_api/get_user_address?uid=' + app.globalData.uid,
  33. method: 'GET',
  34. data: {
  35. addressId : that.data.id
  36. },
  37. success: function (res) {
  38. console.log(res);
  39. var regionOne = "region.0";
  40. var regionTwo = "region.1";
  41. var regionTherr = "region.2";
  42. that.setData({
  43. userAddress: res.data.data,
  44. [regionOne]: res.data.data.province,
  45. [regionTwo]: res.data.data.city,
  46. [regionTherr]: res.data.data.district,
  47. _num: res.data.data.is_default == 1 ? 0 : 1
  48. })
  49. }
  50. })
  51. },
  52. bindRegionChange: function (e) {
  53. this.setData({
  54. region: e.detail.value
  55. })
  56. },
  57. defaulttap:function(e){
  58. var num = this.data._num;
  59. if(num==1){
  60. this.setData({
  61. _num: 0
  62. })
  63. }else{
  64. this.setData({
  65. _num: 1
  66. })
  67. }
  68. },
  69. formSubmit: function (e) {
  70. var warn = "";
  71. var that = this;
  72. var flag = true;
  73. var cartId = '';
  74. var name = e.detail.value.name;
  75. var phone = e.detail.value.phone;
  76. var area = JSON.stringify(this.data.region);
  77. var fulladdress = e.detail.value.fulladdress;
  78. var addressP = {};
  79. if (name == "") {
  80. warn = '请输入姓名';
  81. } else if (!/^1(3|4|5|7|8)\d{9}$/i.test(phone)) {
  82. warn = '您输入的手机号有误'
  83. } else if (area == '["省","市","区"]'){
  84. warn = '请选择地区';
  85. } else if (fulladdress == "") {
  86. warn = "请填写具体地址";
  87. } else{
  88. flag = false;
  89. }
  90. if(flag==true){
  91. wx.showModal({
  92. title: '提示',
  93. content: warn
  94. })
  95. }else{
  96. addressP.province = this.data.region[0];
  97. addressP.city = this.data.region[1];
  98. addressP.district = this.data.region[2];
  99. wx.request({
  100. url: app.globalData.url + '/routine/auth_api/edit_user_address?uid=' + app.globalData.uid,
  101. method: 'POST',
  102. data: {
  103. address: addressP,
  104. is_default : that.data._num == 0 ? 1 : 0,
  105. real_name : name,
  106. post_code : '',
  107. phone : phone,
  108. detail : fulladdress,
  109. id:that.data.id
  110. },
  111. success: function (res) {
  112. if (res.data.code == 200) {
  113. if (that.data.id) {
  114. wx.showToast({
  115. title: '修改成功',
  116. icon: 'success',
  117. duration: 1000
  118. })
  119. }else{
  120. wx.showToast({
  121. title: '添加成功',
  122. icon: 'success',
  123. duration: 1000
  124. })
  125. }
  126. setTimeout(function () {
  127. if (that.data.cartId) {
  128. var cartId = that.data.cartId;
  129. var pinkId = that.data.pinkId;
  130. var couponId = that.data.couponId;
  131. that.setData({
  132. cartId: '',
  133. pinkId: '',
  134. couponId:'',
  135. })
  136. wx.navigateTo({ //跳转至指定页面并关闭其他打开的所有页面(这个最好用在返回至首页的的时候)
  137. url: '/pages/order-confirm/order-confirm?id=' + cartId + '&addressId=' + that.data.id + '&pinkId=' + pinkId + '&couponId=' + couponId
  138. })
  139. } else {
  140. wx.navigateTo({ //跳转至指定页面并关闭其他打开的所有页面(这个最好用在返回至首页的的时候)
  141. url: '/pages/address/address'
  142. })
  143. }
  144. },1200)
  145. }
  146. }
  147. })
  148. }
  149. }
  150. })