addOrEditaddress.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // pages/groupmeallist/addOrEditaddress/addOrEditaddress.js
  2. const cache = require('../../../utils/cache.js');
  3. import address from '../../../api/address'
  4. import base from '../../../utils/base'
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. mobile: '',
  11. addressId: '',
  12. name: '',
  13. gender: '2',
  14. phone: '',
  15. address: '',
  16. longitude: '',
  17. latitude: '',
  18. houseNumber: '',
  19. tagName: '家',
  20. tagList: [{
  21. value: '家',
  22. show: true,
  23. id: 0
  24. }, {
  25. value: '公司',
  26. show: false,
  27. id: 1
  28. }, {
  29. value: '学校',
  30. show: false,
  31. id: 2
  32. }, {
  33. value: '其他',
  34. show: false,
  35. id: 3
  36. }],
  37. tagName: '家'
  38. },
  39. name: function (e) {
  40. console.log("进入确定")
  41. let name = e.detail.value;
  42. this.data.lock = false;
  43. this.setData({
  44. name,
  45. })
  46. },
  47. phone: function (e) {
  48. console.log("进入确定")
  49. let phone = e.detail.value;
  50. this.data.lock = false;
  51. this.setData({
  52. phone,
  53. })
  54. },
  55. address: function (e) {
  56. console.log("进入确定")
  57. let address = e.detail.value;
  58. this.data.lock = false;
  59. this.setData({
  60. address,
  61. })
  62. },
  63. houseNumber: function (e) {
  64. console.log("进入确定")
  65. let houseNumber = e.detail.value;
  66. this.data.lock = false;
  67. this.setData({
  68. houseNumber,
  69. })
  70. },
  71. radioChange(e) {
  72. console.log('radio发生change事件,携带value值为:', e.detail.value)
  73. let gender = e.detail.value
  74. this.setData({
  75. gender: gender
  76. })
  77. },
  78. //地址保存
  79. async saveAddress() {
  80. let jw = cache.getNearLocation()
  81. let longitude = jw.longitude
  82. let latitude = jw.latitude
  83. console.log(this.data.addressId)
  84. await address.addoreditaddress({
  85. mobile: cache.getMobile(),
  86. addressId: this.data.addressId,
  87. name: this.data.name,
  88. gender: this.data.gender,
  89. phone: this.data.phone,
  90. address: this.data.address,
  91. longitude: longitude,
  92. latitude: latitude,
  93. houseNumber: this.data.houseNumber,
  94. tagName: this.data.tagName,
  95. }).then(res => {
  96. if (res.code == 200) {
  97. wx.redirectTo({
  98. url: '../address/address',
  99. })
  100. }
  101. })
  102. },
  103. /**
  104. * 生命周期函数--监听页面加载
  105. */
  106. onLoad: function (options) {
  107. let type = options.type;
  108. let addressId = options.addressId;
  109. console.log(type +addressId)
  110. if (type == 'edit') {
  111. address.getddress({
  112. mobile: cache.getMobile(),
  113. addressId:addressId
  114. }).then(res => {
  115. console.log(res.data)
  116. this.data.tagList.forEach(item => {
  117. if (item.value == res.data.tagName) {
  118. item.show = true
  119. } else {
  120. item.show = false
  121. }
  122. })
  123. this.setData({
  124. tagList:this.data.tagList,
  125. mobile:res.data.mobile,
  126. addressId:res.data.id,
  127. name:res.data.name,
  128. gender:res.data.gender,
  129. phone:res.data.phone,
  130. address:res.data.address,
  131. longitude:res.data.longitude,
  132. latitude:res.data.latitude,
  133. houseNumber:res.data.houseNumber,
  134. tagName:res.data.tagName,
  135. })
  136. })
  137. }
  138. },
  139. /**
  140. * 生命周期函数--监听页面初次渲染完成
  141. */
  142. onReady: function () {
  143. },
  144. /**
  145. * 生命周期函数--监听页面显示
  146. */
  147. onShow: function () {
  148. },
  149. //获取标签值
  150. getagValue(e) {
  151. let value = e.currentTarget.dataset.value;
  152. let index = e.currentTarget.dataset.id;
  153. this.data.tagList[index].show = !this.data.tagList[index].show
  154. this.data.tagList.forEach(item => {
  155. if (item.id == index) {
  156. item.show = true
  157. } else {
  158. item.show = false
  159. }
  160. })
  161. this.setData({
  162. tagName: value,
  163. tagList: this.data.tagList
  164. })
  165. },
  166. // 自动获取手机号
  167. getPhone() {
  168. let phone = cache.getMobile()
  169. this.setData({
  170. phone: phone
  171. })
  172. },
  173. // 选择地址
  174. goSelectMap() {
  175. var that = this;
  176. wx.authorize({
  177. scope: 'scope.userLocation',
  178. complete: function (res) {
  179. console.log(res)
  180. wx.chooseLocation({
  181. success(str) {
  182. console.log(str)
  183. var key = 'V7DBZ-K7C22-SXXUJ-CDUE7-AM2LH-AEFCM';
  184. //发送请求通过经纬度反查地址信息
  185. var getAddressUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + str.latitude + "," + str.longitude + "&key=" + key + "&get_poi=1";
  186. wx.request({
  187. url: getAddressUrl,
  188. success: function (result) {
  189. var province = result.data.result.address_component.province; //省
  190. var city = result.data.result.address_component.city; //市
  191. var district = result.data.result.address_component.district; //区
  192. var address = result.data.result.formatted_addresses.recommend; //具体地址范围
  193. console.log('省市县:' + province + city + district)
  194. console.log('地址:' + address)
  195. that.setData({
  196. address: province + city + address
  197. })
  198. }
  199. })
  200. }
  201. })
  202. }
  203. })
  204. },
  205. /**
  206. * 生命周期函数--监听页面隐藏
  207. */
  208. onHide: function () {
  209. },
  210. /**
  211. * 生命周期函数--监听页面卸载
  212. */
  213. onUnload: function () {
  214. },
  215. /**
  216. * 页面相关事件处理函数--监听用户下拉动作
  217. */
  218. onPullDownRefresh: function () {
  219. },
  220. /**
  221. * 页面上拉触底事件的处理函数
  222. */
  223. onReachBottom: function () {
  224. },
  225. /**
  226. * 用户点击右上角分享
  227. */
  228. onShareAppMessage: function () {
  229. }
  230. })