addressAdd.js 13 KB

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