address.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // pages/address/address.js
  2. var app = getApp();
  3. Page({
  4. data: {
  5. _num:'',
  6. cartId: '',
  7. pinkId: '',
  8. couponId: '',
  9. addressArray:[]
  10. },
  11. onLoad: function (options) {
  12. app.setBarColor();
  13. app.setUserInfo();
  14. if (options.cartId) {
  15. this.setData({
  16. cartId: options.cartId,
  17. pinkId: options.pinkId,
  18. couponId: options.couponId,
  19. })
  20. }
  21. this.getAddress();
  22. },
  23. getWxAddress:function(){
  24. var that = this;
  25. wx.authorize({
  26. scope: 'scope.address',
  27. success: function(res) {
  28. wx.chooseAddress({
  29. success: function(res) {
  30. console.log(res);
  31. var addressP = {};
  32. addressP.province = res.provinceName;
  33. addressP.city = res.cityName;
  34. addressP.district = res.countyName;
  35. wx.request({
  36. url: app.globalData.url + '/routine/auth_api/edit_user_address?uid=' + app.globalData.uid + '&openid=' + app.globalData.openid,
  37. method: 'POST',
  38. data: {
  39. address: addressP,
  40. is_default: 1,
  41. real_name: res.userName,
  42. post_code: res.postalCode,
  43. phone: res.telNumber,
  44. detail: res.detailInfo,
  45. id: 0
  46. },
  47. success: function (res) {
  48. if (res.data.code == 200) {
  49. wx.showToast({
  50. title: '添加成功',
  51. icon: 'success',
  52. duration: 1000
  53. })
  54. that.getAddress();
  55. }
  56. }
  57. })
  58. },
  59. fail: function(res) {
  60. if (res.errMsg == 'chooseAddress:cancel'){
  61. wx.showToast({
  62. title: '取消选择',
  63. icon: 'none',
  64. duration: 1500
  65. })
  66. }
  67. },
  68. complete: function(res) {},
  69. })
  70. },
  71. fail: function(res) {
  72. console.log(res);
  73. },
  74. complete: function(res) {},
  75. })
  76. },
  77. getAddress: function () {
  78. var that = this;
  79. var header = {
  80. 'content-type': 'application/x-www-form-urlencoded',
  81. };
  82. wx.request({
  83. url: app.globalData.url + '/routine/auth_api/user_address_list?uid=' + app.globalData.uid,
  84. method: 'POST',
  85. header: header,
  86. success: function (res) {
  87. if (res.data.code == 200) {
  88. that.setData({
  89. addressArray: res.data.data
  90. })
  91. for (var i in res.data.data) {
  92. if (res.data.data[i].is_default) {
  93. that.setData({
  94. _num: res.data.data[i].id
  95. })
  96. }
  97. }
  98. }
  99. }
  100. })
  101. },
  102. addAddress:function(){
  103. var cartId = this.data.cartId;
  104. var pinkId = this.data.pinkId;
  105. var couponId = this.data.couponId;
  106. this.setData({
  107. cartId: '',
  108. pinkId:'',
  109. couponId:'',
  110. })
  111. wx.navigateTo({ //跳转至指定页面并关闭其他打开的所有页面(这个最好用在返回至首页的的时候)
  112. url: '/pages/addaddress/addaddress?cartId=' + cartId + '&pinkId=' + pinkId + '&couponId=' + couponId
  113. })
  114. },
  115. goOrder:function(e){
  116. var id = e.currentTarget.dataset.id;
  117. var cartId = '';
  118. var pinkId = '';
  119. var couponId = '';
  120. if (this.data.cartId && id){
  121. cartId = this.data.cartId;
  122. pinkId = this.data.pinkId;
  123. couponId = this.data.couponId;
  124. this.setData({
  125. cartId : '',
  126. pinkId : '',
  127. couponId : '',
  128. })
  129. wx.navigateTo({ //跳转至指定页面并关闭其他打开的所有页面(这个最好用在返回至首页的的时候)
  130. url: '/pages/order-confirm/order-confirm?id=' + cartId + '&addressId=' + id + '&pinkId=' + pinkId + '&couponId=' + couponId
  131. })
  132. }
  133. },
  134. delAddress:function(e){
  135. var id = e.currentTarget.dataset.id;
  136. var that = this;
  137. var header = {
  138. 'content-type': 'application/x-www-form-urlencoded',
  139. };
  140. wx.request({
  141. url: app.globalData.url + '/routine/auth_api/remove_user_address?uid=' + app.globalData.uid,
  142. method: 'GET',
  143. header: header,
  144. data:{
  145. addressId:id
  146. },
  147. success: function (res) {
  148. if (res.data.code == 200) {
  149. wx.showToast({
  150. title: '删除成功',
  151. icon: 'success',
  152. duration: 1000,
  153. })
  154. that.getAddress();
  155. } else {
  156. wx.showToast({
  157. title: res.data.msg,
  158. icon: 'none',
  159. duration: 1000,
  160. })
  161. }
  162. }
  163. })
  164. },
  165. editAddress: function (e) {
  166. var cartId = this.data.cartId;
  167. var pinkId = this.data.pinkId;
  168. var couponId = this.data.couponId;
  169. this.setData({
  170. cartId: '',
  171. pinkId: '',
  172. couponId: '',
  173. })
  174. wx.navigateTo({ //跳转至指定页面并关闭其他打开的所有页面(这个最好用在返回至首页的的时候)
  175. url: '/pages/addaddress/addaddress?id=' + e.currentTarget.dataset.id + '&cartId=' + cartId + '&pinkId=' + pinkId + '&couponId=' + couponId
  176. })
  177. },
  178. activetap:function(e){
  179. var id = e.target.dataset.idx;
  180. var that = this;
  181. var header = {
  182. 'content-type': 'application/x-www-form-urlencoded',
  183. };
  184. wx.request({
  185. url: app.globalData.url + '/routine/auth_api/set_user_default_address?uid=' + app.globalData.uid,
  186. method: 'GET',
  187. header: header,
  188. data:{
  189. addressId:id
  190. },
  191. success: function (res) {
  192. if (res.data.code == 200) {
  193. wx.showToast({
  194. title: '设置成功',
  195. icon: 'success',
  196. duration: 1000,
  197. })
  198. that.setData({
  199. _num: id
  200. })
  201. } else {
  202. wx.showToast({
  203. title: res.data.msg,
  204. icon: 'none',
  205. duration: 1000,
  206. })
  207. }
  208. }
  209. })
  210. }
  211. })