community.js 11 KB

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