addressAdd.js 7.7 KB

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