community.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // lionfish_comshop/pages/position/community.js
  2. var location = require("../../utils/Location")
  3. var QQMapWX = require("../../utils/qqmap-wx-jssdk.min.js")
  4. var util = require('../../utils/util.js');
  5. var status = require('../../utils/index.js');
  6. var app = getApp();
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. loadMore: true,
  13. canGetGPS: true,
  14. tip: "加载中...",
  15. community: {},
  16. communities: [],
  17. historyCommunity: [],
  18. city: {
  19. districtName: ""
  20. },
  21. latitude: '',
  22. longitude: '',
  23. hasRefeshin: false,
  24. pageNum: 1,
  25. isNotHistory: true,
  26. city_id: 0,
  27. needAuth: false,
  28. common_header_backgroundimage: '',
  29. groupInfo: {
  30. group_name: '社区',
  31. owner_name: '团长'
  32. },
  33. isEmpty: false
  34. },
  35. linkSearch: function() {
  36. wx.navigateTo({
  37. url: "/lionfish_comshop/pages/position/search?city=" + JSON.stringify(this.data.city)
  38. });
  39. },
  40. isFirst: true,
  41. /**
  42. * 生命周期函数--监听页面加载
  43. */
  44. onLoad: function(options) {
  45. var that = this;
  46. status.setNavBgColor();
  47. this.setData({ common_header_backgroundimage: app.globalData.common_header_backgroundimage });
  48. status.setGroupInfo().then((groupInfo) => { that.setData({ groupInfo }) });
  49. util.check_login() ? this.setData({ needAuth: false }) : this.setData({ needAuth: true});
  50. var token = wx.getStorageSync('token');
  51. this.loadpage();
  52. },
  53. loadpage: function(){
  54. let that = this;
  55. var current_community = wx.getStorageSync('community');
  56. if (current_community) that.setData({ community: current_community })
  57. var tx_map_key = wx.getStorageSync('tx_map_key');
  58. if (tx_map_key) {
  59. var shopname = wx.getStorageSync('shopname');
  60. wx.setNavigationBarTitle({
  61. title: shopname
  62. });
  63. that.load_gps_community();
  64. } else {
  65. app.util.request({
  66. 'url': 'entry/wxapp/index',
  67. 'data': {
  68. controller: 'index.get_community_config'
  69. },
  70. dataType: 'json',
  71. success: function (res) {
  72. if (res.data.code == 0) {
  73. wx.setStorage({
  74. key: "shopname",
  75. data: res.data.shoname
  76. })
  77. if (res.data.shoname) {
  78. wx.setNavigationBarTitle({
  79. title: res.data.shoname
  80. });
  81. }
  82. wx.setStorage({
  83. key: "tx_map_key",
  84. data: res.data.tx_map_key
  85. })
  86. that.setData({
  87. tx_map_key: res.data.tx_map_key
  88. })
  89. wx.setStorage({
  90. key: "shop_index_share_title",
  91. data: res.data.shop_index_share_title
  92. })
  93. that.load_gps_community();
  94. }
  95. }
  96. })
  97. }
  98. },
  99. /**
  100. * 授权成功回调
  101. */
  102. authSuccess: function () {
  103. this.setData({ needAuth: false })
  104. this.loadpage()
  105. },
  106. /**
  107. * 获取定位城市
  108. */
  109. load_gps_community: function() {
  110. var token = wx.getStorageSync('token');
  111. var tx_map_key = wx.getStorageSync('tx_map_key');
  112. if(tx_map_key == undefined || tx_map_key =='')
  113. {
  114. tx_map_key = this.data.tx_map_key
  115. }
  116. var that = this;
  117. app.util.request({
  118. 'url': 'entry/wxapp/index',
  119. 'data': {
  120. controller: 'index.load_history_community',
  121. token: token
  122. },
  123. dataType: 'json',
  124. success: function(res) {
  125. if (res.data.code == 0) {
  126. let history_communities = res.data.list;
  127. let isNotHistory = false;
  128. if (Object.keys(history_communities).length == 0 || history_communities.communityId==0) isNotHistory = true;
  129. if (that.data.community) {
  130. app.globalData.community = history_communities;
  131. }
  132. that.setData({
  133. historyCommunity: history_communities,
  134. isNotHistory
  135. })
  136. }
  137. }
  138. })
  139. var demo = new QQMapWX({
  140. key: tx_map_key
  141. });
  142. wx.getLocation({
  143. type: 'gcj02', //编码方式,
  144. success: function(res) {
  145. var latitude = res.latitude;
  146. var longitude = res.longitude;
  147. that.setData({
  148. latitude: res.latitude,
  149. longitude: res.longitude
  150. })
  151. wx.setStorage({
  152. key: "latitude",
  153. data: res.latitude
  154. })
  155. wx.setStorage({
  156. key: "longitude",
  157. data: res.longitude
  158. })
  159. demo.reverseGeocoder({
  160. //腾讯地图api 逆解析方法 首先设计经纬度
  161. location: {
  162. latitude: res.latitude,
  163. longitude: res.longitude
  164. }, //逆解析成功回调函数
  165. success: function(res) {
  166. let cityName = res.result.address_component.city;
  167. that.setData({
  168. city: {
  169. districtName: cityName
  170. }
  171. })
  172. wx.showLoading({
  173. title: "加载中...",
  174. mask: true,
  175. icon: "none"
  176. })
  177. that.load_gps_community_list();
  178. }
  179. })
  180. },
  181. fail: function (error) {
  182. that.isFirst = true;
  183. location.checkGPS(app, ()=>{
  184. if(app.globalData.canGetGPS){
  185. console.log('checkGPS sucess')
  186. let gpos = app.globalData.location;
  187. if (gpos && gpos.lat){
  188. that.setData({
  189. latitude: gpos.latitude,
  190. longitude: gpos.longitude
  191. })
  192. wx.setStorage({
  193. key: "latitude",
  194. data: gpos.latitude
  195. })
  196. wx.setStorage({
  197. key: "longitude",
  198. data: gpos.longitude
  199. })
  200. demo.reverseGeocoder({
  201. //腾讯地图api 逆解析方法 首先设计经纬度
  202. location: {
  203. latitude: gpos.latitude,
  204. longitude: gpos.longitude
  205. }, //逆解析成功回调函数
  206. success: function (res) {
  207. let cityName = res.result.address_component.city;
  208. that.setData({
  209. city: {
  210. districtName: cityName
  211. }
  212. })
  213. that.load_gps_community_list();
  214. }
  215. })
  216. }
  217. }else{
  218. location.openSetting(app).then(function (pos) {
  219. that.setData({
  220. latitude: pos.latitude,
  221. longitude: pos.longitude
  222. })
  223. wx.setStorage({
  224. key: "latitude",
  225. data: pos.latitude
  226. })
  227. wx.setStorage({
  228. key: "longitude",
  229. data: pos.longitude
  230. })
  231. demo.reverseGeocoder({
  232. //腾讯地图api 逆解析方法 首先设计经纬度
  233. location: {
  234. latitude: pos.latitude,
  235. longitude: pos.longitude
  236. }, //逆解析成功回调函数
  237. success: function (res) {
  238. let cityName = res.result.address_component.city;
  239. that.setData({
  240. city: {
  241. districtName: cityName
  242. }
  243. })
  244. that.load_gps_community_list();
  245. }
  246. })
  247. }).catch(function () {
  248. that.setData({
  249. isEmpty: true,
  250. loadMore: false,
  251. hasRefeshin: true,
  252. tip: '',
  253. canGetGPS: false
  254. })
  255. });
  256. }
  257. });
  258. }
  259. })
  260. },
  261. /**
  262. * 获取社区列表
  263. */
  264. load_gps_community_list: function (city_id) {
  265. var token = wx.getStorageSync('token');
  266. var that = this;
  267. //tip: "加载中...",
  268. console.log('come gpslist');
  269. if (!that.data.hasRefeshin) {
  270. that.setData({
  271. hasRefeshin: true,
  272. loadMore: true
  273. });
  274. app.util.request({
  275. 'url': 'entry/wxapp/index',
  276. 'data': {
  277. controller: 'index.load_gps_community',
  278. token: token,
  279. pageNum: that.data.pageNum,
  280. longitude: that.data.longitude,
  281. latitude: that.data.latitude,
  282. city_id: that.data.city_id || 0
  283. },
  284. dataType: 'json',
  285. success: function(res) {
  286. wx.hideLoading();
  287. if (res.data.code == 0) {
  288. let communities = that.data.communities.concat(res.data.list);
  289. if (that.data.pageNum == 1 && communities.length == 0) { that.setData({ isEmpty: true, loadMore: false, tip: '', hasRefeshin: true });return false; }
  290. that.setData({
  291. communities: communities,
  292. pageNum: that.data.pageNum + 1,
  293. loadMore: false,
  294. hasRefeshin: false,
  295. tip: '',
  296. index_hide_headdetail_address: res.data.index_hide_headdetail_address || 0
  297. });
  298. } else if (res.data.code == 1) {
  299. //go data
  300. that.setData({
  301. loadMore: false,
  302. tip: '^_^已经到底了'
  303. })
  304. } else if (res.data.code == 2) {
  305. //no login
  306. wx.hideLoading();
  307. console.log(that.data.needAuth)
  308. that.setData({ needAuth: true, hasRefeshin: false })
  309. }
  310. }
  311. })
  312. }
  313. },
  314. /**
  315. * 打开设置
  316. */
  317. openSetting: function() {
  318. let that = this;
  319. that.setData({
  320. isEmpty: false,
  321. loadMore: true,
  322. hasRefeshin: false,
  323. tip: '加载中'
  324. },()=>{
  325. that.load_gps_community();
  326. })
  327. // location.openSetting(app);
  328. },
  329. /**
  330. * 生命周期函数--监听页面初次渲染完成
  331. */
  332. onReady: function() {
  333. },
  334. /**
  335. * 生命周期函数--监听页面显示
  336. */
  337. onShow: function() {
  338. let that = this;
  339. // let community = wx.getStorageSync('community');
  340. // this.setData({ community })
  341. console.log("show")
  342. if (!this.isFirst){
  343. console.log('nofirst');
  344. util.check_login() ? this.setData({ needAuth: false }) : this.setData({ needAuth: true });
  345. var city = wx.getStorageSync('city'), city_id = wx.getStorageSync('city_id');
  346. console.log(city_id)
  347. city && that.setData({ city, city_id, pageNum: 1, hasRefeshin: false, communities: [] }), wx.showLoading({
  348. title: "加载中...",
  349. mask: true,
  350. icon: "none"
  351. }), this.load_gps_community_list();
  352. } else {
  353. this.isFirst = false
  354. }
  355. },
  356. /**
  357. * 生命周期函数--监听页面隐藏
  358. */
  359. onHide: function() {
  360. },
  361. /**
  362. * 生命周期函数--监听页面卸载
  363. */
  364. onUnload: function() {
  365. },
  366. /**
  367. * 页面相关事件处理函数--监听用户下拉动作
  368. */
  369. onPullDownRefresh: function() {
  370. },
  371. /**
  372. * 页面上拉触底事件的处理函数
  373. */
  374. onReachBottom: function() {
  375. this.load_gps_community_list();
  376. }
  377. })