index.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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) {
  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)
  33. }
  34. }
  35. },
  36. async provinceChange(e, cityId, districtId) {
  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)
  70. }
  71. }
  72. },
  73. async cityChange(e, districtId) {
  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)
  105. }
  106. }
  107. },
  108. async areaChange(e) {
  109. const index = e.detail.value
  110. this.setData({
  111. aIndex: index
  112. })
  113. },
  114. async bindSave(e) {
  115. if (this.data.pIndex == 0 ) {
  116. wx.showToast({
  117. title: '请选择省份',
  118. icon: 'none'
  119. })
  120. return
  121. }
  122. if (this.data.cIndex == 0 ) {
  123. wx.showToast({
  124. title: '请选择城市',
  125. icon: 'none'
  126. })
  127. return
  128. }
  129. const linkMan = e.detail.value.linkMan;
  130. const address = e.detail.value.address;
  131. const mobile = e.detail.value.mobile;
  132. if (!this.data.addressData) {
  133. wx.showToast({
  134. title: '请选择定位',
  135. icon: 'none',
  136. })
  137. return
  138. }
  139. const latitude = this.data.addressData.latitude
  140. const longitude = this.data.addressData.longitude
  141. if (linkMan == ""){
  142. wx.showToast({
  143. title: '请填写联系人姓名',
  144. icon: 'none'
  145. })
  146. return
  147. }
  148. if (mobile == ""){
  149. wx.showToast({
  150. title: '请填写手机号码',
  151. icon: 'none'
  152. })
  153. return
  154. }
  155. if (!latitude){
  156. wx.showToast({
  157. title: '请选择定位',
  158. icon: 'none',
  159. })
  160. return
  161. }
  162. if (address == ""){
  163. wx.showToast({
  164. title: '请填写详细地址',
  165. icon: 'none'
  166. })
  167. return
  168. }
  169. const postData = {
  170. token: wx.getStorageSync('token'),
  171. linkMan: linkMan,
  172. address: address,
  173. mobile: mobile,
  174. isDefault: 'true',
  175. latitude,
  176. longitude
  177. }
  178. if (this.data.pIndex > 0) {
  179. postData.provinceId = this.data.provinces[this.data.pIndex].id
  180. }
  181. if (this.data.cIndex > 0) {
  182. postData.cityId = this.data.cities[this.data.cIndex].id
  183. }
  184. if (this.data.aIndex > 0) {
  185. postData.districtId = this.data.areas[this.data.aIndex].id
  186. }
  187. let apiResult
  188. if (this.data.id) {
  189. postData.id = this.data.id
  190. apiResult = await WXAPI.updateAddress(postData)
  191. } else {
  192. apiResult = await WXAPI.addAddress(postData)
  193. }
  194. if (apiResult.code != 0) {
  195. // 登录错误
  196. wx.hideLoading();
  197. wx.showToast({
  198. title: apiResult.msg,
  199. icon: 'none'
  200. })
  201. return;
  202. } else {
  203. wx.navigateBack()
  204. }
  205. },
  206. async onLoad(e) {
  207. // this.initFromClipboard('广州市天河区天河东路6号粤电广场北塔2302,徐小姐,18588998859')
  208. const _this = this
  209. if (e.id) { // 修改初始化数据库数据
  210. const res = await WXAPI.addressDetail(wx.getStorageSync('token'), e.id)
  211. if (res.code == 0) {
  212. this.setData({
  213. id: e.id,
  214. addressData: res.data.info
  215. })
  216. this.provinces(res.data.info.provinceId, res.data.info.cityId, res.data.info.districtId)
  217. } else {
  218. wx.showModal({
  219. title: '错误',
  220. content: '无法获取快递地址数据',
  221. showCancel: false
  222. })
  223. }
  224. } else {
  225. this.provinces()
  226. wx.getClipboardData({
  227. success (res){
  228. if (res.data) {
  229. _this.initFromClipboard(res.data)
  230. }
  231. }
  232. })
  233. }
  234. },
  235. async initFromClipboard (str) {
  236. address_parse.smart(str).then(res => {
  237. console.log('ggggggg', res);
  238. if (res.name && res.phone && res.address) {
  239. // 检测到收货地址
  240. this.setData({
  241. addressData: {
  242. provinceId: res.provinceCode,
  243. cityId: res.cityCode,
  244. districtId: res.countyCode,
  245. linkMan: res.name,
  246. mobile: res.phone,
  247. address: res.address,
  248. }
  249. })
  250. this.provinces(res.provinceCode, res.cityCode, res.countyCode)
  251. }
  252. })
  253. },
  254. deleteAddress: function (e) {
  255. const id = e.currentTarget.dataset.id;
  256. wx.showModal({
  257. title: '提示',
  258. content: '确定要删除该收货地址吗?',
  259. success: function (res) {
  260. if (res.confirm) {
  261. WXAPI.deleteAddress(wx.getStorageSync('token'), id).then(function () {
  262. wx.navigateBack({})
  263. })
  264. } else {
  265. console.log('用户点击取消')
  266. }
  267. }
  268. })
  269. },
  270. async readFromWx() {
  271. let that = this;
  272. wx.chooseAddress({
  273. success: function (res) {
  274. // res = {
  275. // cityName: '上海市',
  276. // countyName: '嘉定区',
  277. // detailInfo: '惠民路123号',
  278. // errMsg: 'chooseAddress.ok',
  279. // nationalCode: '310114',
  280. // postalCode: '201800',
  281. // provinceName: '上海市',
  282. // telNumber: '13500000000',
  283. // userName: '测试',
  284. // }
  285. const provinceName = res.provinceName;
  286. const cityName = res.cityName;
  287. const diatrictName = res.countyName;
  288. // 读取省
  289. const pIndex = that.data.provinces.findIndex(ele => {
  290. return ele.name == provinceName
  291. })
  292. if (pIndex != -1) {
  293. const e = {
  294. detail: {
  295. value: pIndex
  296. }
  297. }
  298. that.provinceChange(e, 0, 0).then(() => {
  299. // 读取市
  300. let cIndex = that.data.cities.findIndex(ele => {
  301. return ele.name == cityName
  302. })
  303. if (cIndex == -1) {
  304. cIndex = 1 // 兼容直辖市
  305. }
  306. if (cIndex != -1) {
  307. const e = {
  308. detail: {
  309. value: cIndex
  310. }
  311. }
  312. that.cityChange(e, 0).then(() => {
  313. // 读取区县
  314. const aIndex = that.data.areas.findIndex(ele => {
  315. return ele.name == diatrictName
  316. })
  317. if (aIndex != -1) {
  318. const e = {
  319. detail: {
  320. value: aIndex
  321. }
  322. }
  323. that.areaChange(e)
  324. }
  325. })
  326. }
  327. })
  328. }
  329. const addressData = {}
  330. addressData.linkMan = res.userName
  331. addressData.mobile = res.telNumber
  332. addressData.address = res.detailInfo
  333. that.setData({
  334. addressData
  335. });
  336. }
  337. })
  338. },
  339. chooseLocation() {
  340. wx.chooseLocation({
  341. success: (res) => {
  342. const addressData = this.data.addressData ? this.data.addressData : {}
  343. addressData.address = res.address + res.name
  344. addressData.latitude = res.latitude
  345. addressData.longitude = res.longitude
  346. this.setData({
  347. addressData
  348. })
  349. },
  350. fail: (e) => {
  351. console.error(e)
  352. },
  353. })
  354. }
  355. })