index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. const WXAPI = require('apifm-wxapi')
  2. const AUTH = require('../../utils/auth')
  3. var address_parse = require("../../utils/address_parse")
  4. Page({
  5. data: {
  6. provinces: undefined,// 省份数据数组
  7. pIndex: 0,//选择的省下标
  8. cities: undefined,// 城市数据数组
  9. cIndex: 0,//选择的市下标
  10. areas: undefined,// 区县数数组
  11. aIndex: 0,//选择的区下标
  12. },
  13. async provinces(provinceId, cityId, districtId, streetId) {
  14. const res = await WXAPI.province()
  15. if (res.code == 0) {
  16. const provinces = [{
  17. id: 0,
  18. name: '请选择'
  19. }].concat(res.data)
  20. let pIndex = 0
  21. if (provinceId) {
  22. pIndex = provinces.findIndex(ele => {
  23. return ele.id == provinceId
  24. })
  25. }
  26. this.setData({
  27. pIndex,
  28. provinces: provinces
  29. })
  30. if (provinceId) {
  31. const e = { detail: { value: pIndex}}
  32. this.provinceChange(e, cityId, districtId, streetId)
  33. }
  34. }
  35. },
  36. async provinceChange(e, cityId, districtId, streetId) {
  37. const index = e.detail.value
  38. this.setData({
  39. pIndex: index
  40. })
  41. const pid = this.data.provinces[index].id
  42. if (pid == 0) {
  43. this.setData({
  44. cities: null,
  45. cIndex: 0,
  46. areas: null,
  47. aIndex: 0
  48. })
  49. return
  50. }
  51. const res = await WXAPI.nextRegion(pid);
  52. if (res.code == 0) {
  53. const cities = [{
  54. id: 0,
  55. name: '请选择'
  56. }].concat(res.data)
  57. let cIndex = 0
  58. if (cityId) {
  59. cIndex = cities.findIndex(ele => {
  60. return ele.id == cityId
  61. })
  62. }
  63. this.setData({
  64. cIndex,
  65. cities: cities
  66. })
  67. if (cityId) {
  68. const e = { detail: { value: cIndex } }
  69. this.cityChange(e, districtId, streetId)
  70. }
  71. }
  72. },
  73. async cityChange(e, districtId, streetId) {
  74. const index = e.detail.value
  75. this.setData({
  76. cIndex: index
  77. })
  78. const pid = this.data.cities[index].id
  79. if (pid == 0) {
  80. this.setData({
  81. areas: null,
  82. aIndex: 0
  83. })
  84. return
  85. }
  86. const res = await WXAPI.nextRegion(pid);
  87. if (res.code == 0) {
  88. const areas = [{
  89. id: 0,
  90. name: '请选择'
  91. }].concat(res.data)
  92. let aIndex = 0
  93. if (districtId) {
  94. aIndex = areas.findIndex(ele => {
  95. return ele.id == districtId
  96. })
  97. }
  98. this.setData({
  99. aIndex,
  100. areas: areas
  101. })
  102. if (districtId) {
  103. const e = { detail: { value: aIndex } }
  104. this.areaChange(e, streetId)
  105. }
  106. }
  107. },
  108. async areaChange(e, streetId) {
  109. const index = e.detail.value
  110. this.setData({
  111. aIndex: index
  112. })
  113. const shipping_address_region_level = wx.getStorageSync('shipping_address_region_level')
  114. if (shipping_address_region_level == 3) {
  115. return
  116. }
  117. //
  118. const pid = this.data.areas[index].id
  119. if (pid == 0) {
  120. this.setData({
  121. streets: null,
  122. sIndex: 0
  123. })
  124. return
  125. }
  126. const res = await WXAPI.nextRegion(pid);
  127. if (res.code == 0) {
  128. const streets = [{
  129. id: 0,
  130. name: '请选择'
  131. }].concat(res.data)
  132. let sIndex = 0
  133. if (streetId) {
  134. sIndex = streets.findIndex(ele => {
  135. return ele.id == streetId
  136. })
  137. }
  138. this.setData({
  139. sIndex,
  140. streets
  141. })
  142. if (streetId) {
  143. const e = { detail: { value: sIndex } }
  144. this.streetChange(e)
  145. }
  146. }
  147. },
  148. async streetChange(e) {
  149. const index = e.detail.value
  150. this.setData({
  151. sIndex: index
  152. })
  153. },
  154. async bindSave() {
  155. if (this.data.pIndex == 0 ) {
  156. wx.showToast({
  157. title: '请选择省份',
  158. icon: 'none'
  159. })
  160. return
  161. }
  162. if (this.data.cIndex == 0 ) {
  163. wx.showToast({
  164. title: '请选择城市',
  165. icon: 'none'
  166. })
  167. return
  168. }
  169. if (this.data.aIndex == 0 ) {
  170. wx.showToast({
  171. title: '请选择区县',
  172. icon: 'none'
  173. })
  174. return
  175. }
  176. const shipping_address_region_level = wx.getStorageSync('shipping_address_region_level')
  177. if (shipping_address_region_level == 4) {
  178. if (this.data.sIndex == 0 ) {
  179. wx.showToast({
  180. title: '请选择社区/街道',
  181. icon: 'none'
  182. })
  183. return
  184. }
  185. }
  186. const linkMan = this.data.linkMan;
  187. const address = this.data.address;
  188. const mobile = this.data.mobile;
  189. if (this.data.shipping_address_gps == '1' && !this.data.addressData) {
  190. wx.showToast({
  191. title: '请选择定位',
  192. icon: 'none',
  193. })
  194. return
  195. }
  196. const latitude = this.data.addressData ? this.data.addressData.latitude : null
  197. const longitude = this.data.addressData ? this.data.addressData.longitude : null
  198. if (!linkMan){
  199. wx.showToast({
  200. title: '请填写联系人姓名',
  201. icon: 'none'
  202. })
  203. return
  204. }
  205. if (!mobile){
  206. wx.showToast({
  207. title: '请填写手机号码',
  208. icon: 'none'
  209. })
  210. return
  211. }
  212. const postData = {
  213. token: wx.getStorageSync('token'),
  214. linkMan: linkMan,
  215. address: address,
  216. mobile: mobile,
  217. isDefault: 'true'
  218. }
  219. if (this.data.shipping_address_gps == '1' && !latitude){
  220. wx.showToast({
  221. title: '请选择定位',
  222. icon: 'none',
  223. })
  224. return
  225. }
  226. if (latitude) {
  227. postData.latitude = latitude
  228. }
  229. if (longitude) {
  230. postData.longitude = longitude
  231. }
  232. if (!address){
  233. wx.showToast({
  234. title: '请填写详细地址',
  235. icon: 'none'
  236. })
  237. return
  238. }
  239. if (this.data.pIndex > 0) {
  240. postData.provinceId = this.data.provinces[this.data.pIndex].id
  241. }
  242. if (this.data.cIndex > 0) {
  243. postData.cityId = this.data.cities[this.data.cIndex].id
  244. }
  245. if (this.data.aIndex > 0) {
  246. postData.districtId = this.data.areas[this.data.aIndex].id
  247. }
  248. if (this.data.sIndex > 0) {
  249. postData.streetId = this.data.streets[this.data.sIndex].id
  250. }
  251. let apiResult
  252. if (this.data.id) {
  253. postData.id = this.data.id
  254. apiResult = await WXAPI.updateAddress(postData)
  255. } else {
  256. apiResult = await WXAPI.addAddress(postData)
  257. }
  258. if (apiResult.code != 0) {
  259. // 登录错误
  260. wx.hideLoading();
  261. wx.showToast({
  262. title: apiResult.msg,
  263. icon: 'none'
  264. })
  265. return;
  266. } else {
  267. wx.navigateBack()
  268. }
  269. },
  270. async onLoad(e) {
  271. // this.initFromClipboard('广州市天河区天河东路6号粤电广场北塔2302,徐小姐,18588998859')
  272. const _this = this
  273. if (e.id) { // 修改初始化数据库数据
  274. const res = await WXAPI.addressDetail(wx.getStorageSync('token'), e.id)
  275. if (res.code == 0) {
  276. this.setData({
  277. id: e.id,
  278. ...res.data.info
  279. })
  280. this.provinces(res.data.info.provinceId, res.data.info.cityId, res.data.info.districtId, res.data.info.streetId)
  281. } else {
  282. wx.showModal({
  283. title: '错误',
  284. content: '无法获取快递地址数据',
  285. showCancel: false
  286. })
  287. }
  288. } else {
  289. this.provinces()
  290. wx.getClipboardData({
  291. success (res){
  292. if (res.data) {
  293. _this.initFromClipboard(res.data)
  294. }
  295. }
  296. })
  297. }
  298. this.setData({
  299. shipping_address_gps: wx.getStorageSync('shipping_address_gps')
  300. })
  301. },
  302. async initFromClipboard (str) {
  303. address_parse.smart(str).then(res => {
  304. console.log('ggggggg', res);
  305. if (res.name && res.phone && res.address) {
  306. // 检测到收货地址
  307. this.setData({
  308. addressData: {
  309. provinceId: res.provinceCode,
  310. cityId: res.cityCode,
  311. districtId: res.countyCode,
  312. linkMan: res.name,
  313. mobile: res.phone,
  314. address: res.address,
  315. }
  316. })
  317. this.provinces(res.provinceCode, res.cityCode, res.countyCode)
  318. }
  319. })
  320. },
  321. deleteAddress: function (e) {
  322. const id = e.currentTarget.dataset.id;
  323. wx.showModal({
  324. title: '提示',
  325. content: '确定要删除该收货地址吗?',
  326. success: function (res) {
  327. if (res.confirm) {
  328. WXAPI.deleteAddress(wx.getStorageSync('token'), id).then(function () {
  329. wx.navigateBack({})
  330. })
  331. } else {
  332. console.log('用户点击取消')
  333. }
  334. }
  335. })
  336. },
  337. async readFromWx() {
  338. let that = this;
  339. wx.chooseAddress({
  340. success: function (res) {
  341. // res = {
  342. // cityName: '上海市',
  343. // countyName: '嘉定区',
  344. // detailInfo: '惠民路123号',
  345. // errMsg: 'chooseAddress.ok',
  346. // nationalCode: '310114',
  347. // postalCode: '201800',
  348. // provinceName: '上海市',
  349. // telNumber: '13500000000',
  350. // userName: '测试',
  351. // }
  352. const provinceName = res.provinceName;
  353. const cityName = res.cityName;
  354. const diatrictName = res.countyName;
  355. // 读取省
  356. const pIndex = that.data.provinces.findIndex(ele => {
  357. return ele.name == provinceName
  358. })
  359. if (pIndex != -1) {
  360. const e = {
  361. detail: {
  362. value: pIndex
  363. }
  364. }
  365. that.provinceChange(e, 0, 0).then(() => {
  366. // 读取市
  367. let cIndex = that.data.cities.findIndex(ele => {
  368. return ele.name == cityName
  369. })
  370. if (cIndex == -1) {
  371. cIndex = 1 // 兼容直辖市
  372. }
  373. if (cIndex != -1) {
  374. const e = {
  375. detail: {
  376. value: cIndex
  377. }
  378. }
  379. that.cityChange(e, 0).then(() => {
  380. // 读取区县
  381. const aIndex = that.data.areas.findIndex(ele => {
  382. return ele.name == diatrictName
  383. })
  384. if (aIndex != -1) {
  385. const e = {
  386. detail: {
  387. value: aIndex
  388. }
  389. }
  390. that.areaChange(e)
  391. }
  392. })
  393. }
  394. })
  395. }
  396. that.setData({
  397. linkMan: res.userName,
  398. mobile: res.telNumber,
  399. address: res.detailInfo
  400. });
  401. }
  402. })
  403. },
  404. chooseLocation() {
  405. wx.chooseLocation({
  406. success: (res) => {
  407. const addressData = this.data.addressData ? this.data.addressData : {}
  408. addressData.address = res.address + res.name
  409. addressData.latitude = res.latitude
  410. addressData.longitude = res.longitude
  411. this.setData({
  412. addressData
  413. })
  414. },
  415. fail: (e) => {
  416. console.error(e)
  417. },
  418. })
  419. }
  420. })