editAddress.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //index.js
  2. //获取应用实例
  3. var tcity = require("../../utils/citys.js");
  4. var app = getApp()
  5. Page({
  6. data: {
  7. provinces: [],
  8. province: "",
  9. citys: [],
  10. city: "",
  11. countys: [],
  12. county: '',
  13. value: [0, 0, 0],
  14. values: [0, 0, 0],
  15. condition: false,
  16. userName: "",//用户名
  17. userAddressArea: "",//省市区
  18. userAddress: "", //详细地址
  19. userTelephone: "", //联系方式
  20. isDefault: 0 , //是否默认
  21. arr : [],
  22. flag : 0,
  23. checked : true,
  24. user_id : ''
  25. },
  26. onLoad: function (options) {
  27. var that = this;
  28. that.data.arr = JSON.parse(options.arr);
  29. that.setData({
  30. arr: JSON.parse(options.arr),
  31. user_id: that.data.arr.id,
  32. userName: that.data.arr.name,
  33. userTelephone: that.data.arr.phone,
  34. userAddress: that.data.arr.adress,
  35. province: that.data.arr.province,
  36. city: that.data.arr.city,
  37. county: that.data.arr.county,
  38. isDefault: that.data.arr.flag,
  39. provinceId: that.data.arr.provinceId,
  40. cityId: that.data.arr.cityId,
  41. countyId: that.data.arr.countyId
  42. })
  43. if (that.data.isDefault == 1) {
  44. that.setData({
  45. checked: true
  46. })
  47. } else {
  48. that.setData({
  49. checked: false
  50. })
  51. }
  52. tcity.init(that);
  53. var cityData = that.data.cityData;
  54. const provinces = [];
  55. const citys = [];
  56. const countys = [];
  57. var provinceId = that.data.arr.provinceId;
  58. var cityId = that.data.arr.cityId;
  59. var countyId = that.data.arr.countyId;
  60. for (let i = 0; i < cityData.length; i++) {
  61. provinces.push(cityData[i].name);
  62. }
  63. console.log('省份完成');
  64. for (let i = 0; i < cityData[provinceId].sub.length; i++) {
  65. citys.push(cityData[provinceId].sub[i].name)
  66. }
  67. console.log('city完成');
  68. for (let i = 0; i < cityData[provinceId].sub[cityId].sub.length; i++) {
  69. countys.push(cityData[provinceId].sub[cityId].sub[i].name)
  70. }
  71. that.setData({
  72. 'provinces': provinces,
  73. 'citys': citys,
  74. 'countys': countys,
  75. value: [provinceId, cityId, countyId]
  76. // 'province': options.province,
  77. // 'city': options.city,
  78. // 'county': options.county
  79. })
  80. },
  81. bindChange: function (e) {
  82. var val = e.detail.value
  83. var t = this.data.values;
  84. var cityData = this.data.cityData;
  85. if (val[0] != t[0]) {
  86. const citys = [];
  87. const countys = [];
  88. for (let i = 0; i < cityData[val[0]].sub.length; i++) {
  89. citys.push(cityData[val[0]].sub[i].name)
  90. }
  91. for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
  92. countys.push(cityData[val[0]].sub[0].sub[i].name)
  93. }
  94. this.setData({
  95. province: this.data.provinces[val[0]],
  96. city: cityData[val[0]].sub[0].name,
  97. citys: citys,
  98. county: cityData[val[0]].sub[0].sub[0].name,
  99. countys: countys,
  100. values: val,
  101. value: [val[0], 0, 0]
  102. })
  103. return;
  104. }
  105. if (val[1] != t[1]) {
  106. const countys = [];
  107. for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
  108. countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
  109. }
  110. this.setData({
  111. city: this.data.citys[val[1]],
  112. county: cityData[val[0]].sub[val[1]].sub[0].name,
  113. countys: countys,
  114. values: val,
  115. value: [val[0], val[1], 0]
  116. })
  117. return;
  118. }
  119. if (val[2] != t[2]) {
  120. this.setData({
  121. county: this.data.countys[val[2]],
  122. values: val
  123. })
  124. return;
  125. }
  126. },
  127. open: function () {
  128. var that = this;
  129. this.setData({
  130. condition: !this.data.condition
  131. })
  132. if (that.data.condition == false) {
  133. var province = that.data.province;
  134. var city = that.data.city;
  135. var county = that.data.county;
  136. var cityData = that.data.cityData;
  137. var provinceId = 0;
  138. var cityId = 0;
  139. var countyId = 0;
  140. var len1 = cityData.length;
  141. for (var i = 0; i < len1; i++) {
  142. if (cityData[i].name == province) {
  143. provinceId = i;
  144. }
  145. }
  146. var len2 = cityData[provinceId].sub.length;
  147. for (var i = 0; i < len2; i++) {
  148. if (cityData[provinceId].sub[i].name == city) {
  149. cityId = i;
  150. }
  151. }
  152. var len3 = cityData[provinceId].sub[cityId].sub.length;
  153. for (var i = 0; i < len3; i++) {
  154. if (cityData[provinceId].sub[cityId].sub[i].name == county) {
  155. countyId = i;
  156. }
  157. }
  158. that.setData({
  159. value: [provinceId, cityId, countyId]
  160. })
  161. }
  162. },
  163. userNameInput: function (e) {
  164. this.setData({
  165. userName: e.detail.value
  166. })
  167. },
  168. userAddressInput: function (e) {
  169. this.setData({
  170. userAddress: e.detail.value
  171. })
  172. },
  173. userTelephoneInput: function (e) {
  174. this.setData({
  175. userTelephone: e.detail.value
  176. })
  177. },
  178. switchChange: function (e) {
  179. if (e.detail.value == false){
  180. this.setData({
  181. isDefault: 0
  182. })
  183. }else{
  184. this.setData({
  185. isDefault: 1
  186. })
  187. }
  188. },
  189. saveInfor: function (e) {
  190. var host = getApp().globalData.servsers;
  191. var un_id = getApp().globalData.un_id;
  192. var that = this;
  193. that = this;
  194. wx.request({
  195. url: host + "adressapi/update",
  196. data: {
  197. id: that.data.user_id,
  198. consignee: that.data.userName,
  199. province: that.data.province,
  200. city: that.data.city,
  201. area: that.data.county,
  202. adress: that.data.userAddress,
  203. phone: that.data.userTelephone,
  204. adress_flag: that.data.isDefault
  205. },
  206. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  207. header: {
  208. 'Accept': 'application/json'
  209. },
  210. success: function (res) {
  211. wx.navigateBack({ delta: 1})
  212. if (res == null || res.data == null) {
  213. console.error('网络请求失败');
  214. return;
  215. }
  216. }
  217. })
  218. }
  219. })
  220. var that;
  221. var Util = require('../../utils/util.js');