addAddress.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. checked : false,
  22. hiddenmodal : 'hidden',
  23. modalCont : ''
  24. },
  25. bindChange: function (e) {
  26. var val = e.detail.value
  27. var t = this.data.values;
  28. var cityData = this.data.cityData;
  29. if (val[0] != t[0]) {
  30. const citys = [];
  31. const countys = [];
  32. for (let i = 0; i < cityData[val[0]].sub.length; i++) {
  33. citys.push(cityData[val[0]].sub[i].name)
  34. }
  35. for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
  36. countys.push(cityData[val[0]].sub[0].sub[i].name)
  37. }
  38. this.setData({
  39. province: this.data.provinces[val[0]],
  40. city: cityData[val[0]].sub[0].name,
  41. citys: citys,
  42. county: cityData[val[0]].sub[0].sub[0].name,
  43. countys: countys,
  44. values: val,
  45. value: [val[0], 0, 0]
  46. })
  47. return;
  48. }
  49. if (val[1] != t[1]) {
  50. const countys = [];
  51. for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
  52. countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
  53. }
  54. this.setData({
  55. city: this.data.citys[val[1]],
  56. county: cityData[val[0]].sub[val[1]].sub[0].name,
  57. countys: countys,
  58. values: val,
  59. value: [val[0], val[1], 0]
  60. })
  61. return;
  62. }
  63. if (val[2] != t[2]) {
  64. this.setData({
  65. county: this.data.countys[val[2]],
  66. values: val
  67. })
  68. return;
  69. }
  70. },
  71. open: function () {
  72. var that = this;
  73. this.setData({
  74. condition: !this.data.condition
  75. })
  76. if (that.data.condition == false){
  77. var province = that.data.province;
  78. var city = that.data.city;
  79. var county = that.data.county;
  80. var cityData = that.data.cityData;
  81. var provinceId = 0;
  82. var cityId = 0;
  83. var countyId = 0;
  84. var len1 = cityData.length;
  85. for (var i = 0; i < len1; i++) {
  86. if (cityData[i].name == province) {
  87. provinceId = i;
  88. }
  89. }
  90. var len2 = cityData[provinceId].sub.length;
  91. for (var i = 0; i < len2; i++) {
  92. if (cityData[provinceId].sub[i].name == city) {
  93. cityId = i;
  94. }
  95. }
  96. var len3 = cityData[provinceId].sub[cityId].sub.length;
  97. for (var i = 0; i < len3; i++) {
  98. if (cityData[provinceId].sub[cityId].sub[i].name == county) {
  99. countyId = i;
  100. }
  101. }
  102. that.setData({
  103. value: [provinceId, cityId, countyId]
  104. })
  105. }
  106. },
  107. userNameInput: function (e) {
  108. this.setData({
  109. userName: e.detail.value
  110. })
  111. },
  112. userAddressInput: function (e) {
  113. this.setData({
  114. userAddress: e.detail.value
  115. })
  116. },
  117. userTelephoneInput: function (e) {
  118. this.setData({
  119. userTelephone: e.detail.value
  120. })
  121. },
  122. switchChange: function (e) {
  123. if (e.detail.value == false){
  124. this.setData({
  125. isDefault: 0
  126. })
  127. }else{
  128. this.setData({
  129. isDefault: 1
  130. })
  131. }
  132. },
  133. saveInfor: function (e) {
  134. var host = getApp().globalData.servsers;
  135. var un_id = getApp().globalData.un_id;
  136. var that = this;
  137. var consignee = this.data.userName;
  138. if (consignee == undefined || consignee == '') {
  139. that.setData({
  140. hiddenmodal: false,
  141. modalCont: '请填写收货人姓名'
  142. })
  143. setTimeout(function () {
  144. that.setData({
  145. hiddenmodal: true
  146. })
  147. }, 1000)
  148. return false;
  149. }
  150. var userAddress = this.data.userAddress;
  151. if (userAddress == undefined || userAddress == '') {
  152. that.setData({
  153. hiddenmodal: false,
  154. modalCont: '请填写详细地址'
  155. })
  156. setTimeout(function () {
  157. that.setData({
  158. hiddenmodal: true
  159. })
  160. }, 1000)
  161. return false;
  162. }
  163. var userTelephone = this.data.userTelephone;
  164. if (userTelephone == undefined || userTelephone == '') {
  165. that.setData({
  166. hiddenmodal: false,
  167. modalCont: '请填写电话号码'
  168. })
  169. setTimeout(function () {
  170. that.setData({
  171. hiddenmodal: true
  172. })
  173. }, 1000)
  174. return false;
  175. }
  176. var pages = getCurrentPages();
  177. var currPage = pages[pages.length - 1]; //当前页面
  178. var prevPage = pages[pages.length - 2]; //上一个页面
  179. var prevPage3 = pages[pages.length - 3];
  180. var prevPageUrl = prevPage.route;
  181. var prevPageUrl3 = pages[pages.length - 3].route;
  182. var num = pages.length;
  183. if (un_id != undefined){
  184. wx.request({
  185. url: host + "adressapi/insertadd",
  186. data: {
  187. user_id: un_id,
  188. consignee: that.data.userName,
  189. province: that.data.province,
  190. city: that.data.city,
  191. area: that.data.county,
  192. adress: that.data.userAddress,
  193. phone: that.data.userTelephone,
  194. adress_flag: that.data.isDefault
  195. },
  196. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  197. header: {
  198. 'Accept': 'application/json'
  199. },
  200. success: function (res) {
  201. if (prevPageUrl3 == 'pages/confirmOrder/confirmOrder') {
  202. prevPage3.setData({
  203. province: that.data.province,
  204. city: that.data.city,
  205. area: that.data.county,
  206. adress: that.data.userAddress,
  207. consignee: that.data.userName,
  208. phone: that.data.userTelephone
  209. });
  210. wx.navigateBack({
  211. delta: 2
  212. })
  213. } else {
  214. wx.redirectTo({
  215. url: '/' + prevPageUrl
  216. })
  217. }
  218. if (res == null || res.data == null) {
  219. console.error('网络请求失败');
  220. return;
  221. }
  222. }
  223. })
  224. }
  225. },
  226. onLoad: function () {
  227. var that = this;
  228. tcity.init(that);
  229. var cityData = that.data.cityData;
  230. const provinces = [];
  231. const citys = [];
  232. const countys = [];
  233. for (let i = 0; i < cityData.length; i++) {
  234. provinces.push(cityData[i].name);
  235. }
  236. console.log('省份完成');
  237. for (let i = 0; i < cityData[0].sub.length; i++) {
  238. citys.push(cityData[0].sub[i].name)
  239. }
  240. console.log('city完成');
  241. for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
  242. countys.push(cityData[0].sub[0].sub[i].name)
  243. }
  244. that.setData({
  245. 'provinces': provinces,
  246. 'citys': citys,
  247. 'countys': countys,
  248. 'province': cityData[0].name,
  249. 'city': cityData[0].sub[0].name,
  250. 'county': cityData[0].sub[0].sub[0].name
  251. })
  252. console.log('初始化完成');
  253. }
  254. })
  255. var that;
  256. var Util = require('../../utils/util.js');