mineAddress.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // index.js 我的地址
  2. var tcity = require("../../utils/citys.js");
  3. // addressDefault :设为默认地址 1为默认
  4. var addressList = [
  5. ];
  6. Page({
  7. data: {
  8. addressList: addressList,
  9. isSelect: false,
  10. nickName: '',
  11. userInfoAvatar: '',
  12. sex: '',
  13. province: '',
  14. city: '',
  15. provinces: [],
  16. province: "",
  17. citys: [],
  18. city: "",
  19. countys: [],
  20. county: '',
  21. value: [0, 0, 0],
  22. values: [0, 0, 0],
  23. condition: false,
  24. allowDelete : true, //默认可以删除
  25. allowEdit: true, //默认可以编辑
  26. arr : {},
  27. flag : 0,
  28. hidden: 'hidden'
  29. },
  30. onLoad: function () {
  31. wx.showNavigationBarLoading()
  32. var that = this;
  33. var un_id = getApp().globalData.un_id;
  34. var host = getApp().globalData.servsers;
  35. wx.login({
  36. success: function (res) {
  37. var code = res.code;
  38. wx.request({
  39. url: host + "/adressapi/adressview",
  40. data: {
  41. user_id: un_id
  42. },
  43. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  44. header: {
  45. 'Accept': 'application/json'
  46. },
  47. success: function (res) {
  48. that.setData({
  49. addressList: res.data.rows
  50. });
  51. if (res == null || res.data == null) {
  52. console.error('网络请求失败');
  53. return;
  54. }
  55. wx.hideNavigationBarLoading(
  56. that.setData({
  57. hidden: ''
  58. })
  59. )
  60. }
  61. });
  62. var pages = getCurrentPages();
  63. var currPage = pages[pages.length - 1]; //当前页面
  64. var prevPage = pages[pages.length - 2]; //上一个页面
  65. if (prevPage.route == 'pages/confirmOrder/confirmOrder') {
  66. that.setData({
  67. allowDelete: false
  68. })
  69. }
  70. }
  71. })
  72. },
  73. onShow:function(e){
  74. var that = this;
  75. that.onLoad();
  76. var listLength = addressList.length;
  77. var defaultData = [];
  78. var count = 0;
  79. for (var i = 1; i < listLength; i++) {//判断是否有默认地址
  80. defaultData[i] = addressList[i].addressDefault;
  81. if (defaultData[i] == 1) {
  82. this.setData({
  83. 'selectnum': i
  84. })
  85. count++;
  86. }
  87. }
  88. if (listLength == 1){
  89. this.setData({
  90. 'selectnum': 0
  91. })
  92. }else{
  93. if (count == 0){
  94. this.setData({
  95. 'selectnum': 0
  96. })
  97. }
  98. }
  99. },
  100. selectAddress: function (options){//勾选地址
  101. var that = this
  102. var id = options.currentTarget.dataset.id;
  103. var province = options.currentTarget.dataset.province;
  104. var city = options.currentTarget.dataset.city;
  105. var area = options.currentTarget.dataset.area;
  106. var consignee = options.currentTarget.dataset.consignee;
  107. var phone = options.currentTarget.dataset.phone;
  108. var adress = options.currentTarget.dataset.adress;
  109. var pages = getCurrentPages();
  110. var currPage = pages[pages.length - 1]; //当前页面
  111. var prevPage = pages[pages.length - 2]; //上一个页面
  112. if (prevPage.route == 'pages/confirmOrder/confirmOrder'){
  113. prevPage.setData({
  114. province: province,
  115. city: city,
  116. area: area,
  117. adress: adress,
  118. consignee: consignee,
  119. phone: phone
  120. });
  121. wx.navigateBack();
  122. }
  123. //设置当前样式
  124. that.setData({
  125. 'selectnum': id
  126. })
  127. },
  128. editAddress:function(e){//编辑地址
  129. var that = this;
  130. var id = e.currentTarget.dataset.id;
  131. var dataId = that.data.addressList[id].id;
  132. var name = that.data.addressList[id].consignee;
  133. var phone = that.data.addressList[id].phone;
  134. var adress = that.data.addressList[id].adress;
  135. var province = that.data.addressList[id].province;
  136. var city = that.data.addressList[id].city;
  137. var county = that.data.addressList[id].area;
  138. var cityData = that.data.cityData;
  139. var provinceId = 0;
  140. var cityId = 0;
  141. var countyId = 0;
  142. var flag = that.data.addressList[id].adress_flag;
  143. tcity.init(that);
  144. var cityData = that.data.cityData;
  145. var len1 = that.data.cityData.length;
  146. for (var i = 0; i < len1; i++) {
  147. if (cityData[i].name == province) {
  148. provinceId = i;
  149. }
  150. }
  151. var len2 = cityData[provinceId].sub.length;
  152. for (var i = 0; i < len2; i++) {
  153. if (cityData[provinceId].sub[i].name == city) {
  154. cityId = i;
  155. }
  156. }
  157. var len3 = cityData[provinceId].sub[cityId].sub.length;
  158. for (var i = 0; i < len3; i++) {
  159. if (cityData[provinceId].sub[cityId].sub[i].name == county) {
  160. countyId = i;
  161. }
  162. }
  163. var arr = {
  164. id : dataId,
  165. name : name,
  166. phone : phone,
  167. adress : adress,
  168. province : province,
  169. city: city,
  170. county: county,
  171. flag : flag,
  172. provinceId: provinceId,
  173. cityId: cityId,
  174. countyId: countyId
  175. };
  176. this.setData({
  177. arr: arr
  178. })
  179. wx.navigateTo({
  180. url: '/pages/editAddress/editAddress?arr=' + JSON.stringify(that.data.arr),
  181. success: function(res) {},
  182. fail: function(res) {},
  183. complete: function(res) {},
  184. })
  185. },
  186. deleteAddress:function(e){//删除地址
  187. var that = this;
  188. var host = getApp().globalData.servsers;
  189. var id = e.currentTarget.dataset.deleteid;
  190. var index = e.currentTarget.dataset.index;
  191. var addressList = that.data.addressList;
  192. wx.request({
  193. url: host + 'adressapi/delete',
  194. data: {
  195. id : id
  196. },
  197. method: 'GET',
  198. success: function (res) {
  199. addressList.splice(index, 1);
  200. that.setData({
  201. addressList: addressList
  202. });
  203. if (res == null || res.data == null) {
  204. console.error('网络请求失败');
  205. return;
  206. }
  207. }
  208. })
  209. }
  210. })