addressAdd.js 8.4 KB

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