addressAdd.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.js');
  3. var check = require('../../../utils/check.js');
  4. var area = require('../../../utils/area.js');
  5. var app = getApp();
  6. Page({
  7. data: {
  8. address: {
  9. id: 0,
  10. areaCode: 0,
  11. address: '',
  12. name: '',
  13. tel: '',
  14. isDefault: 0,
  15. province: '',
  16. city: '',
  17. county: ''
  18. },
  19. addressId: 0,
  20. openSelectRegion: false,
  21. selectRegionList: [{
  22. code: 0,
  23. name: '省份'
  24. },
  25. {
  26. code: 0,
  27. name: '城市'
  28. },
  29. {
  30. code: 0,
  31. name: '区县'
  32. }
  33. ],
  34. regionType: 1,
  35. regionList: [],
  36. selectRegionDone: false
  37. },
  38. bindinputMobile(event) {
  39. let address = this.data.address;
  40. address.tel = event.detail.value;
  41. this.setData({
  42. address: address
  43. });
  44. },
  45. bindinputName(event) {
  46. let address = this.data.address;
  47. address.name = event.detail.value;
  48. this.setData({
  49. address: address
  50. });
  51. },
  52. bindinputAddress(event) {
  53. let address = this.data.address;
  54. address.addressDetail = event.detail.value;
  55. this.setData({
  56. address: address
  57. });
  58. },
  59. bindIsDefault() {
  60. let address = this.data.address;
  61. address.isDefault = !address.isDefault;
  62. this.setData({
  63. address: address
  64. });
  65. },
  66. getAddressDetail() {
  67. let that = this;
  68. util.request(api.AddressDetail, {
  69. id: that.data.addressId
  70. }).then(function(res) {
  71. if (res.errno === 0) {
  72. if (res.data) {
  73. that.setData({
  74. address: res.data
  75. });
  76. }
  77. }
  78. });
  79. },
  80. setRegionDoneStatus() {
  81. let that = this;
  82. let doneStatus = that.data.selectRegionList.every(item => {
  83. return item.code != 0;
  84. });
  85. that.setData({
  86. selectRegionDone: doneStatus
  87. })
  88. },
  89. chooseRegion() {
  90. let that = this;
  91. this.setData({
  92. openSelectRegion: !this.data.openSelectRegion
  93. });
  94. //设置区域选择数据
  95. let address = this.data.address;
  96. if (address.areaCode > 0) {
  97. let selectRegionList = this.data.selectRegionList;
  98. selectRegionList[0].code = address.areaCode.slice(0, 2) + '0000';
  99. selectRegionList[0].name = address.province;
  100. selectRegionList[1].code = address.areaCode.slice(0, 4) + '00';
  101. selectRegionList[1].name = address.city;
  102. selectRegionList[2].code = address.areaCode;
  103. selectRegionList[2].name = address.county;
  104. let regionList = area.getList('county', address.areaCode.slice(0, 4));
  105. regionList = regionList.map(item => {
  106. //标记已选择的
  107. if (address.areaCode === item.code) {
  108. item.selected = true;
  109. } else {
  110. item.selected = false;
  111. }
  112. return item;
  113. })
  114. this.setData({
  115. selectRegionList: selectRegionList,
  116. regionType: 3,
  117. regionList: regionList
  118. });
  119. } else {
  120. let selectRegionList = [{
  121. code: 0,
  122. name: '省份',
  123. },
  124. {
  125. code: 0,
  126. name: '城市',
  127. },
  128. {
  129. code: 0,
  130. name: '区县',
  131. }
  132. ];
  133. this.setData({
  134. selectRegionList: selectRegionList,
  135. regionType: 1,
  136. regionList: area.getList('province')
  137. });
  138. }
  139. this.setRegionDoneStatus();
  140. },
  141. onLoad: function(options) {
  142. // 页面初始化 options为页面跳转所带来的参数
  143. console.log(options)
  144. if (options.id && options.id != 0) {
  145. this.setData({
  146. addressId: options.id
  147. });
  148. this.getAddressDetail();
  149. }
  150. },
  151. onReady: function() {
  152. },
  153. selectRegionType(event) {
  154. let that = this;
  155. let regionTypeIndex = event.target.dataset.regionTypeIndex;
  156. let selectRegionList = that.data.selectRegionList;
  157. //判断是否可点击
  158. if (regionTypeIndex + 1 == this.data.regionType || (regionTypeIndex - 1 >= 0 && selectRegionList[regionTypeIndex - 1].code <= 0)) {
  159. return false;
  160. }
  161. let selectRegionItem = selectRegionList[regionTypeIndex];
  162. let code = selectRegionItem.code;
  163. let regionList;
  164. if (regionTypeIndex === 0) {
  165. // 点击省级,取省级
  166. regionList = area.getList('province');
  167. }
  168. else if (regionTypeIndex === 1) {
  169. // 点击市级,取市级
  170. regionList = area.getList('city', code.slice(0, 2));
  171. }
  172. else{
  173. // 点击县级,取县级
  174. regionList = area.getList('county', code.slice(0, 4));
  175. }
  176. regionList = regionList.map(item => {
  177. //标记已选择的
  178. if (that.data.selectRegionList[regionTypeIndex].code == item.code) {
  179. item.selected = true;
  180. } else {
  181. item.selected = false;
  182. }
  183. return item;
  184. })
  185. this.setData({
  186. regionList: regionList,
  187. regionType: regionTypeIndex + 1
  188. })
  189. this.setRegionDoneStatus();
  190. },
  191. selectRegion(event) {
  192. let that = this;
  193. let regionIndex = event.target.dataset.regionIndex;
  194. let regionItem = this.data.regionList[regionIndex];
  195. let regionType = this.data.regionType;
  196. let selectRegionList = this.data.selectRegionList;
  197. selectRegionList[regionType - 1] = regionItem;
  198. if (regionType == 3) {
  199. this.setData({
  200. selectRegionList: selectRegionList
  201. })
  202. let regionList = that.data.regionList.map(item => {
  203. //标记已选择的
  204. if (that.data.selectRegionList[that.data.regionType - 1].code == item.code) {
  205. item.selected = true;
  206. } else {
  207. item.selected = false;
  208. }
  209. return item;
  210. })
  211. this.setData({
  212. regionList: regionList
  213. })
  214. this.setRegionDoneStatus();
  215. return
  216. }
  217. //重置下级区域为空
  218. selectRegionList.map((item, index) => {
  219. if (index > regionType - 1) {
  220. item.code = 0;
  221. item.name = index == 1 ? '城市' : '区县';
  222. }
  223. return item;
  224. });
  225. this.setData({
  226. selectRegionList: selectRegionList,
  227. regionType: regionType + 1
  228. })
  229. let code = regionItem.code;
  230. let regionList = [];
  231. if (regionType === 1) {
  232. // 点击省级,取市级
  233. regionList= area.getList('city', code.slice(0, 2))
  234. }
  235. else {
  236. // 点击市级,取县级
  237. regionList= area.getList('county', code.slice(0, 4))
  238. }
  239. this.setData({
  240. regionList: regionList
  241. })
  242. this.setRegionDoneStatus();
  243. },
  244. doneSelectRegion() {
  245. if (this.data.selectRegionDone === false) {
  246. return false;
  247. }
  248. let address = this.data.address;
  249. let selectRegionList = this.data.selectRegionList;
  250. address.province = selectRegionList[0].name;
  251. address.city = selectRegionList[1].name;
  252. address.county = selectRegionList[2].name;
  253. address.areaCode = selectRegionList[2].code;
  254. this.setData({
  255. address: address,
  256. openSelectRegion: false
  257. });
  258. },
  259. cancelSelectRegion() {
  260. this.setData({
  261. openSelectRegion: false,
  262. regionType: this.data.regionDoneStatus ? 3 : 1
  263. });
  264. },
  265. cancelAddress() {
  266. wx.navigateBack();
  267. },
  268. saveAddress() {
  269. console.log(this.data.address)
  270. let address = this.data.address;
  271. if (address.name == '') {
  272. util.showErrorToast('请输入姓名');
  273. return false;
  274. }
  275. if (address.tel == '') {
  276. util.showErrorToast('请输入手机号码');
  277. return false;
  278. }
  279. if (address.areaCode == 0) {
  280. util.showErrorToast('请输入省市区');
  281. return false;
  282. }
  283. if (address.addressDetail == '') {
  284. util.showErrorToast('请输入详细地址');
  285. return false;
  286. }
  287. let that = this;
  288. util.request(api.AddressSave, {
  289. id: address.id,
  290. name: address.name,
  291. tel: address.tel,
  292. province: address.province,
  293. city: address.city,
  294. county: address.county,
  295. areaCode: address.areaCode,
  296. addressDetail: address.addressDetail,
  297. isDefault: address.isDefault
  298. }, 'POST').then(function(res) {
  299. if (res.errno === 0) {
  300. //返回之前,先取出上一页对象,并设置addressId
  301. var pages = getCurrentPages();
  302. var prevPage = pages[pages.length - 2];
  303. console.log(prevPage);
  304. if (prevPage.route == "pages/checkout/checkout") {
  305. prevPage.setData({
  306. addressId: res.data
  307. })
  308. try {
  309. wx.setStorageSync('addressId', res.data);
  310. } catch (e) {
  311. }
  312. console.log("set address");
  313. }
  314. wx.navigateBack();
  315. }
  316. });
  317. },
  318. onShow: function() {
  319. // 页面显示
  320. },
  321. onHide: function() {
  322. // 页面隐藏
  323. },
  324. onUnload: function() {
  325. // 页面关闭
  326. }
  327. })