addressAdd.js 8.3 KB

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