addressAdd.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. const chooseLocation = requirePlugin('chooseLocation');
  6. var app = getApp();
  7. Page({
  8. data: {
  9. address: {
  10. id: 0,
  11. areaCode: 0,
  12. address: '',
  13. name: '',
  14. tel: '',
  15. isDefault: 0,
  16. province: '',
  17. city: '',
  18. county: '',
  19. latitude:'',
  20. longitude:'',
  21. addressDetail:'',
  22. detail:'',
  23. addressname:''
  24. },
  25. addressId: 0,
  26. openSelectRegion: false,
  27. selectRegionList: [{
  28. code: 0,
  29. name: '省份'
  30. },
  31. {
  32. code: 0,
  33. name: '城市'
  34. },
  35. {
  36. code: 0,
  37. name: '区县'
  38. }
  39. ],
  40. regionType: 1,
  41. regionList: [],
  42. selectRegionDone: false
  43. },
  44. bindinputMobile(event) {
  45. let address = this.data.address;
  46. address.tel = event.detail.value;
  47. this.setData({
  48. address: address
  49. });
  50. },
  51. bindinputName(event) {
  52. let address = this.data.address;
  53. address.name = event.detail.value;
  54. this.setData({
  55. address: address
  56. });
  57. },
  58. bindinputAddress(event) {
  59. let address = this.data.address;
  60. address.detail = event.detail.value.replace(/[,,]/g,'');
  61. this.setData({
  62. address: address
  63. });
  64. // return value.replace(/[,,]/g,'');
  65. },
  66. bindIsDefault() {
  67. let address = this.data.address;
  68. address.isDefault = !address.isDefault;
  69. this.setData({
  70. address: address
  71. });
  72. },
  73. getAddressDetail() {
  74. let that = this;
  75. util.request(api.AddressDetail, {
  76. id: that.data.addressId
  77. }).then(function (res) {
  78. if (res.errno === 0) {
  79. if (res.data) {
  80. that.setData({
  81. address: res.data
  82. });
  83. let addressDetailList=res.data.addressDetail.split(',');
  84. let address=that.data.address;
  85. address.addressname=addressDetailList[0];
  86. address.detail=(addressDetailList.length>1?addressDetailList[1]:'');
  87. that.setData({
  88. address: address
  89. });
  90. }
  91. }
  92. });
  93. },
  94. setRegionDoneStatus() {
  95. let that = this;
  96. let doneStatus = that.data.selectRegionList.every(item => {
  97. return item.code != 0;
  98. });
  99. that.setData({
  100. selectRegionDone: doneStatus
  101. })
  102. },
  103. chooseRegion() {
  104. let that = this;
  105. this.setData({
  106. openSelectRegion: !this.data.openSelectRegion
  107. });
  108. //设置区域选择数据
  109. let address = this.data.address;
  110. if (address.areaCode > 0) {
  111. let selectRegionList = this.data.selectRegionList;
  112. console.info(selectRegionList);
  113. selectRegionList[0].code = address.areaCode.slice(0, 2) + '0000';
  114. selectRegionList[0].name = address.province;
  115. selectRegionList[1].code = address.areaCode.slice(0, 4) + '00';
  116. selectRegionList[1].name = address.city;
  117. selectRegionList[2].code = address.areaCode;
  118. selectRegionList[2].name = address.county;
  119. let regionList = area.getList('county', address.areaCode.slice(0, 4));
  120. regionList = regionList.map(item => {
  121. //标记已选择的
  122. if (address.areaCode === item.code) {
  123. item.selected = true;
  124. } else {
  125. item.selected = false;
  126. }
  127. return item;
  128. })
  129. this.setData({
  130. selectRegionList: selectRegionList,
  131. regionType: 3,
  132. regionList: regionList
  133. });
  134. } else {
  135. let selectRegionList = [{
  136. code: 0,
  137. name: '省份',
  138. },
  139. {
  140. code: 0,
  141. name: '城市',
  142. },
  143. {
  144. code: 0,
  145. name: '区县',
  146. }
  147. ];
  148. this.setData({
  149. selectRegionList: selectRegionList,
  150. regionType: 1,
  151. regionList: area.getList('province')
  152. });
  153. }
  154. this.setRegionDoneStatus();
  155. },
  156. onLoad: function (options) {
  157. // 页面初始化 options为页面跳转所带来的参数
  158. //console.log(options)
  159. if (options.id && options.id != 0) {
  160. this.setData({
  161. addressId: options.id
  162. });
  163. this.getAddressDetail();
  164. }
  165. //初始化area
  166. //area.init();
  167. },
  168. onReady: function () {
  169. },
  170. selectRegionType(event) {
  171. console.info('selectRegionType');
  172. let that = this;
  173. let regionTypeIndex = event.target.dataset.regionTypeIndex;
  174. let selectRegionList = that.data.selectRegionList;
  175. //判断是否可点击
  176. if (regionTypeIndex + 1 == this.data.regionType || (regionTypeIndex - 1 >= 0 && selectRegionList[regionTypeIndex - 1].code <= 0)) {
  177. return false;
  178. }
  179. let selectRegionItem = selectRegionList[regionTypeIndex];
  180. let code = selectRegionItem.code;
  181. let regionList;
  182. if (regionTypeIndex === 0) {
  183. // 点击省级,取省级
  184. regionList = area.getList('province');
  185. } else if (regionTypeIndex === 1) {
  186. // 点击市级,取市级
  187. regionList = area.getList('city', code.slice(0, 2));
  188. } else {
  189. // 点击县级,取县级
  190. console.info(code);
  191. regionList = area.getList('county', code.slice(0, 4));
  192. console.info(regionList);
  193. }
  194. regionList = regionList.map(item => {
  195. //标记已选择的
  196. if (that.data.selectRegionList[regionTypeIndex].code == item.code) {
  197. item.selected = true;
  198. } else {
  199. item.selected = false;
  200. }
  201. return item;
  202. })
  203. this.setData({
  204. regionList: regionList,
  205. regionType: regionTypeIndex + 1
  206. })
  207. this.setRegionDoneStatus();
  208. },
  209. selectRegion(event) {
  210. let that = this;
  211. let regionIndex = event.target.dataset.regionIndex;
  212. let regionItem = this.data.regionList[regionIndex];
  213. let regionType = this.data.regionType;
  214. let selectRegionList = this.data.selectRegionList;
  215. selectRegionList[regionType - 1] = regionItem;
  216. if (regionType == 3) {
  217. this.setData({
  218. selectRegionList: selectRegionList
  219. })
  220. let regionList = that.data.regionList.map(item => {
  221. //标记已选择的
  222. if (that.data.selectRegionList[that.data.regionType - 1].code == item.code) {
  223. item.selected = true;
  224. } else {
  225. item.selected = false;
  226. }
  227. return item;
  228. })
  229. this.setData({
  230. regionList: regionList
  231. })
  232. this.setRegionDoneStatus();
  233. return
  234. }
  235. //重置下级区域为空
  236. selectRegionList.map((item, index) => {
  237. if (index > regionType - 1) {
  238. item.code = 0;
  239. item.name = index == 1 ? '城市' : '区县';
  240. }
  241. return item;
  242. });
  243. //console.info(selectRegionList);
  244. this.setData({
  245. selectRegionList: selectRegionList,
  246. regionType: regionType + 1
  247. })
  248. let code = regionItem.code;
  249. let regionList = [];
  250. if (regionType === 1) {
  251. // 点击省级,取市级
  252. regionList = area.getList('city', code.slice(0, 2))
  253. } else {
  254. // 点击市级,取县级
  255. regionList = area.getList('county', code.slice(0, 4))
  256. }
  257. this.setData({
  258. regionList: regionList
  259. })
  260. this.setRegionDoneStatus();
  261. },
  262. doneSelectRegion() {
  263. if (this.data.selectRegionDone === false) {
  264. return false;
  265. }
  266. let address = this.data.address;
  267. let selectRegionList = this.data.selectRegionList;
  268. address.province = selectRegionList[0].name;
  269. address.city = selectRegionList[1].name;
  270. address.county = selectRegionList[2].name;
  271. address.areaCode = selectRegionList[2].code;
  272. this.setData({
  273. address: address,
  274. openSelectRegion: false
  275. });
  276. },
  277. cancelSelectRegion() {
  278. this.setData({
  279. openSelectRegion: false,
  280. regionType: this.data.regionDoneStatus ? 3 : 1
  281. });
  282. },
  283. cancelAddress() {
  284. wx.navigateBack();
  285. },
  286. saveAddress() {
  287. console.log(this.data.address)
  288. let address = this.data.address;
  289. if (address.name == '') {
  290. util.showErrorToast('请输入姓名');
  291. return false;
  292. }
  293. if (address.tel == '') {
  294. util.showErrorToast('请输入手机号码');
  295. return false;
  296. }
  297. if (address.areaCode == 0) {
  298. util.showErrorToast('请输入省市区');
  299. return false;
  300. }
  301. if (address.addressname == '') {
  302. util.showErrorToast('请选择地址');
  303. return false;
  304. }
  305. if (!check.isValidPhone(address.tel)) {
  306. util.showErrorToast('手机号不正确');
  307. return false;
  308. }
  309. let that = this;
  310. let addressString=(address.detail==''?address.addressname:address.addressname+','+address.detail);
  311. util.request(api.AddressSave, {
  312. id: address.id,
  313. name: address.name,
  314. tel: address.tel,
  315. province: address.province,
  316. city: address.city,
  317. county: address.county,
  318. areaCode: address.areaCode,
  319. addressDetail:addressString ,
  320. isDefault: address.isDefault,
  321. lat:Number(address.lat).toFixed(5),
  322. lng:Number(address.lng).toFixed(5),
  323. }, 'POST').then(function (res) {
  324. if (res.errno === 0) {
  325. //返回之前,先取出上一页对象,并设置addressId
  326. var pages = getCurrentPages();
  327. var prevPage = pages[pages.length - 2];
  328. console.log(prevPage);
  329. if (prevPage.route == "pages/index/index") {
  330. var city = address.city.indexOf("市") < 0 ? address.province : address.city;
  331. var addresInfo = {
  332. city: city,
  333. lng: res.data.lng,
  334. lat: res.data.lat,
  335. shopId: res.data.shopId,
  336. address: res.data.addressDetail
  337. }
  338. wx.setStorageSync('addressInfo', addresInfo);
  339. } else if (prevPage.route == "pages/checkout/checkout") {
  340. prevPage.setData({
  341. addressId: res.data.id
  342. })
  343. try {
  344. wx.setStorageSync('addressId', res.data);
  345. } catch (e) {
  346. }
  347. console.log("set address");
  348. }
  349. wx.navigateBack();
  350. } else {
  351. wx.showModal({
  352. title: '提醒',
  353. showCancel: false,
  354. content: res.errmsg
  355. })
  356. }
  357. });
  358. },
  359. deleteAddress(event) {
  360. console.log(event.target)
  361. let that = this;
  362. wx.showModal({
  363. title: '',
  364. content: '确定要删除地址?',
  365. success: function (res) {
  366. if (res.confirm) {
  367. let addressId = event.target.dataset.addressId;
  368. util.request(api.AddressDelete, {
  369. id: addressId
  370. }, 'POST').then(function (res) {
  371. if (res.errno === 0) {
  372. wx.navigateBack({
  373. delta: 1,
  374. success: function () {}
  375. })
  376. // that.getAddressList();
  377. // wx.removeStorage({
  378. // key: 'addressId',
  379. // success: function(res) {},
  380. // })
  381. }
  382. });
  383. console.log('用户点击确定')
  384. }
  385. }
  386. })
  387. return false;
  388. },
  389. goMap(){
  390. // const key = "JA2BZ-XGOEU-UNZVD-46G64-S6MYJ-6BBF4";
  391. //使用在腾讯位置服务申请的key
  392. const key = '6TRBZ-XXC6F-BWWJB-N75ME-BQNOF-YKFQY';
  393. const referer = '鲸致生活LifePlus'; //调用插件的app的名称
  394. const category = '小区,生活服务,娱乐休闲';
  395. wx.navigateTo({
  396. url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer + '&category=' + category
  397. });
  398. },
  399. onShow: function () {
  400. // 页面显示
  401. const location = chooseLocation.getLocation();
  402. console.log(location);
  403. if(location!=null){
  404. let address = this.data.address;
  405. address.province = location.province;
  406. address.city = location.city;
  407. address.county = location.district;
  408. address.addressname = location.name;
  409. address.lat=location.latitude;
  410. address.lng=location.longitude;
  411. this.setData({
  412. address: address,
  413. });
  414. this.getAreaCode()
  415. }
  416. },
  417. getAreaCode(){
  418. let param={
  419. key:'6TRBZ-XXC6F-BWWJB-N75ME-BQNOF-YKFQY',
  420. location: this.data.address.lat+','+this.data.address.lng,
  421. }
  422. wx.request({
  423. url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=',
  424. data:param,
  425. method: 'GET',
  426. header: {
  427. 'Content-Type': 'application/json',
  428. 'X-JZ-Token': wx.getStorageSync('token')
  429. },
  430. success: res=>{
  431. console.log(res);
  432. if (res.statusCode == 200) {
  433. if (res.data.status == 0) {
  434. let areaCode=res.data.result.ad_info.adcode;
  435. this.setData({
  436. ['address.areaCode']:areaCode
  437. });
  438. }else{
  439. wx.showToast({
  440. title: '获取地区码失败',
  441. icon: 'none',
  442. duration: 3000
  443. });
  444. }
  445. }else{
  446. wx.showToast({
  447. title: '获取地区码失败',
  448. icon: 'none',
  449. duration: 3000
  450. });
  451. }
  452. },
  453. fail: function(err) {
  454. reject(err)
  455. }
  456. });
  457. },
  458. onHide: function () {
  459. // 页面隐藏
  460. },
  461. onUnload: function () {
  462. // 页面关闭
  463. chooseLocation.setLocation(null);
  464. }
  465. })