addressAdd.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. lat: '',
  20. lng: '',
  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.address = 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. onLoad: function (options) {
  104. // 页面初始化 options为页面跳转所带来的参数
  105. //console.log(options)
  106. if (options.id && options.id != 0) {
  107. this.setData({
  108. addressId: options.id
  109. });
  110. this.getAddressDetail();
  111. }
  112. //初始化area
  113. //area.init();
  114. },
  115. cancelAddress() {
  116. wx.navigateBack();
  117. },
  118. saveAddress() {
  119. console.log(this.data.address)
  120. let address = this.data.address;
  121. if (address.name == '') {
  122. util.showErrorToast('请输入姓名');
  123. return false;
  124. }
  125. if (address.tel == '') {
  126. util.showErrorToast('请输入手机号码');
  127. return false;
  128. }
  129. if (address.areaCode == 0) {
  130. util.showErrorToast('请输入省市区');
  131. return false;
  132. }
  133. if (address.address == '') {
  134. util.showErrorToast('请选择地址');
  135. return false;
  136. }
  137. if (!check.isValidPhone(address.tel)) {
  138. util.showErrorToast('手机号不正确');
  139. return false;
  140. }
  141. let that = this;
  142. let addressString = (address.detail == '' ? address.address : address.address + ',' + address.detail);
  143. util.request(api.AddressSave, {
  144. shopId: app.globalData.shopId ? app.globalData.shopId : '',
  145. id: address.id,
  146. name: address.name,
  147. tel: address.tel,
  148. province: address.province,
  149. city: address.city,
  150. county: address.county,
  151. areaCode: address.areaCode,
  152. addressDetail: addressString,
  153. addressName: address.addressName,
  154. isDefault: address.isDefault,
  155. lat: Number(address.lat).toFixed(5),
  156. lng: Number(address.lng).toFixed(5),
  157. }, 'POST').then(function (res) {
  158. if (res.errno === 0) {
  159. //返回之前,先取出上一页对象,并设置addressId
  160. var pages = getCurrentPages();
  161. var prevPage = pages[pages.length - 2];
  162. console.log(prevPage);
  163. if (prevPage.route == "pages/index/index") {
  164. var city = address.city.indexOf("市") < 0 ? address.province : address.city;
  165. var addresInfo = {
  166. city: city,
  167. lng: res.data.lng,
  168. lat: res.data.lat,
  169. shopId: res.data.shopId,
  170. address: res.data.addressDetail
  171. }
  172. wx.setStorageSync('addressInfo', address);
  173. } else if (prevPage.route == "pages/ucenter/serviceDetail/serviceDetail") {
  174. wx.setStorageSync('addressInfo', addresInfo);
  175. } else if (prevPage.route == "pages/checkout/checkout") {
  176. prevPage.setData({
  177. addressId: res.data.id
  178. })
  179. try {
  180. wx.setStorageSync('addressId', res.data);
  181. } catch (e) {
  182. }
  183. console.log("set address");
  184. }
  185. wx.navigateBack();
  186. } else {
  187. wx.showModal({
  188. title: '提醒',
  189. showCancel: false,
  190. content: res.errmsg
  191. })
  192. }
  193. });
  194. },
  195. deleteAddress(event) {
  196. console.log(event.target)
  197. let that = this;
  198. wx.showModal({
  199. title: '',
  200. content: '确定要删除地址?',
  201. success: function (res) {
  202. if (res.confirm) {
  203. let addressId = event.target.dataset.addressId;
  204. util.request(api.AddressDelete, {
  205. id: addressId
  206. }, 'POST').then(function (res) {
  207. if (res.errno === 0) {
  208. wx.navigateBack({
  209. delta: 1,
  210. success: function () {}
  211. })
  212. // that.getAddressList();
  213. // wx.removeStorage({
  214. // key: 'addressId',
  215. // success: function(res) {},
  216. // })
  217. }
  218. });
  219. console.log('用户点击确定')
  220. }
  221. }
  222. })
  223. return false;
  224. },
  225. goMap() {
  226. // const key = "JA2BZ-XGOEU-UNZVD-46G64-S6MYJ-6BBF4";
  227. //使用在腾讯位置服务申请的key
  228. const key = '6TRBZ-XXC6F-BWWJB-N75ME-BQNOF-YKFQY';
  229. const referer = '鲸致生活LifePlus'; //调用插件的app的名称
  230. const category = '小区,生活服务,娱乐休闲';
  231. if (this.data.address.lat) {
  232. const location = JSON.stringify({
  233. latitude: Number(this.data.address.lat),
  234. longitude: Number(this.data.address.lng)
  235. });
  236. wx.navigateTo({
  237. url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer + '&category=' + category + '&location=' + location
  238. });
  239. } else
  240. wx.navigateTo({
  241. url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer + '&category=' + category
  242. });
  243. },
  244. onShow: function () {
  245. // 页面显示
  246. const location = chooseLocation.getLocation();
  247. console.log(location);
  248. if (location != null) {
  249. let address = this.data.address;
  250. address.province = location.province ? location.province : location.city;
  251. address.city = location.city;
  252. address.county = location.district;
  253. address.addressName = location.name;
  254. address.address = location.address;
  255. address.lat = location.latitude;
  256. address.lng = location.longitude;
  257. this.setData({
  258. address: address,
  259. });
  260. this.getAreaCode()
  261. }
  262. },
  263. getAreaCode() {
  264. let param = {
  265. key: '6TRBZ-XXC6F-BWWJB-N75ME-BQNOF-YKFQY',
  266. location: this.data.address.lat + ',' + this.data.address.lng,
  267. }
  268. wx.request({
  269. url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=',
  270. data: param,
  271. method: 'GET',
  272. header: {
  273. 'Content-Type': 'application/json',
  274. 'X-JZ-Token': wx.getStorageSync('token')
  275. },
  276. success: res => {
  277. console.log(res);
  278. if (res.statusCode == 200) {
  279. if (res.data.status == 0) {
  280. let ad_info = res.data.result.ad_info;
  281. let areaCode = res.data.result.ad_info.adcode;
  282. this.setData({
  283. ['address.areaCode']: areaCode,
  284. ['address.province']: ad_info.province,
  285. ['address.city']: ad_info.city,
  286. ['address.county']: ad_info.district,
  287. });
  288. } else {
  289. wx.showToast({
  290. title: '获取地区码失败',
  291. icon: 'none',
  292. duration: 3000
  293. });
  294. }
  295. } else {
  296. wx.showToast({
  297. title: '获取地区码失败',
  298. icon: 'none',
  299. duration: 3000
  300. });
  301. }
  302. },
  303. fail: function (err) {
  304. reject(err)
  305. }
  306. });
  307. },
  308. onHide: function () {
  309. // 页面隐藏
  310. },
  311. onUnload: function () {
  312. // 页面关闭
  313. chooseLocation.setLocation(null);
  314. }
  315. })