index.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. //index.js
  2. var amapFile = require('../../libs/amap-wx.js');
  3. var config = require('../../libs/config.js');
  4. var server = require('../../libs/config.js');
  5. //获取应用实例
  6. var app = getApp()
  7. var myAmapFun = null;
  8. var that = null;
  9. Page({
  10. data: {
  11. scale: 18,
  12. latitude: 0,
  13. longitude: 0,
  14. placeName: '',
  15. userId: '',
  16. positionInfo: {},
  17. latitude2: 0,
  18. longitude2: 0,
  19. },
  20. // 页面加载
  21. onLoad: function (options) {
  22. var key = config.Config.key;
  23. that = this;
  24. myAmapFun = new amapFile.AMapWX({ key: key });
  25. // 保持屏幕常亮
  26. wx.setKeepScreenOn({
  27. keepScreenOn: true
  28. })
  29. // 1.获取定时器,用于判断是否已经在计费
  30. this.timer = options.timer;
  31. this.setData({
  32. userId: options.userId
  33. })
  34. app.globalData.nwUserid = options.userId
  35. // 2.获取并设置当前位置经纬度
  36. wx.getLocation({
  37. type: "gcj02",
  38. success: (res) => {
  39. that.setData({
  40. longitude: res.longitude,
  41. latitude: res.latitude
  42. });
  43. }
  44. });
  45. myAmapFun.getRegeo({
  46. success: function (data) {
  47. that.setData({
  48. placeName: data[0].name + ' ' + data[0].desc
  49. })
  50. },
  51. fail: function (info) {
  52. // wx.showModal({title:info.errMsg})
  53. }
  54. })
  55. // 3.设置地图控件的位置及大小,通过设备宽高定位
  56. wx.getSystemInfo({
  57. success: (res) => {
  58. this.setData({
  59. controls: [{
  60. id: 1,
  61. iconPath: '/img/location.png',
  62. position: {
  63. left: 20,
  64. top: res.windowHeight - 120,
  65. width: 50,
  66. height: 50
  67. },
  68. clickable: true
  69. },
  70. // {
  71. // id: 6,
  72. // iconPath: '/img/avatar.png',
  73. // position: {
  74. // left: res.windowWidth - 68,
  75. // top: 200,
  76. // width: 45,
  77. // height: 45
  78. // },
  79. // clickable: true
  80. // },
  81. {
  82. id: 3,
  83. iconPath: '/img/weather.png',
  84. position: {
  85. left: res.windowWidth - 68,
  86. top: 45,
  87. width: 45,
  88. height: 45
  89. },
  90. clickable: true
  91. },
  92. {
  93. id: 4,
  94. iconPath: '/img/marker.png',
  95. position: {
  96. left: res.windowWidth / 2 - 11,
  97. top: res.windowHeight / 2 - 45,
  98. width: 22,
  99. height: 35
  100. },
  101. clickable: true
  102. },
  103. {
  104. id: 5,
  105. iconPath: '/img/avatar.png',
  106. position: {
  107. left: res.windowWidth - 68,
  108. top: 120,
  109. width: 45,
  110. height: 45
  111. },
  112. clickable: true
  113. },
  114. {
  115. id: 6,
  116. iconPath: '/img/upload.png',
  117. position: {
  118. left: res.windowWidth - 68,
  119. top: 195,
  120. width: 45,
  121. height: 45
  122. },
  123. clickable: true
  124. }]
  125. })
  126. }
  127. });
  128. // 4.请求服务器,显示附近的单车,用marker标记
  129. wx.request({
  130. url: server.Server.url + '/api/user/getTeamUsers/' + app.globalData.nwUserid,
  131. data: {},
  132. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  133. // header: {}, // 设置请求的 header
  134. success: (res) => {
  135. this.setData({
  136. markers: res.data
  137. })
  138. },
  139. fail: function (res) {
  140. // fail
  141. },
  142. complete: function (res) {
  143. // complete
  144. }
  145. })
  146. this.timer2 = setInterval(() => {
  147. that = this
  148. wx.getLocation({
  149. type: "gcj02",
  150. success: function (res) {
  151. that.setData({
  152. longitude2: res.longitude,
  153. latitude2: res.latitude
  154. });
  155. },
  156. })
  157. myAmapFun.getRegeo({
  158. success: function (data) {
  159. that.setData({
  160. positionInfo: {
  161. positionId: app.globalData.nwUserid,
  162. longitude: that.data.longitude2,
  163. latitude: that.data.latitude2,
  164. locateName: data[0].name,
  165. locateDesc: data[0].desc
  166. }
  167. })
  168. console.log(that.data.positionInfo.latitude+' '+that.data.positionInfo.longitude)
  169. wx.request({
  170. url: server.Server.url + '/api/position/update',
  171. data: { pos: that.data.positionInfo },
  172. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  173. // header: {}, // 设置请求的 header
  174. success: (res) => {
  175. },
  176. fail: function (res) {
  177. // fail
  178. },
  179. complete: function (res) {
  180. // complete
  181. }
  182. })
  183. },
  184. fail: function (info) {
  185. // wx.showModal({title:info.errMsg})
  186. }
  187. })
  188. }, 3000)
  189. },
  190. // 页面显示
  191. onShow: function () {
  192. // 1.创建地图上下文,移动当前位置到地图中心
  193. this.mapCtx = wx.createMapContext("ofoMap");
  194. this.movetoPosition()
  195. },
  196. // 地图控件点击事件
  197. bindcontroltap: function (e) {
  198. // 判断点击的是哪个控件 e.controlId代表控件的id,在页面加载时的第3步设置的id
  199. switch (e.controlId) {
  200. // 点击定位控件
  201. case 1: this.movetoPosition();
  202. myAmapFun.getRegeo({
  203. success: function (data) {
  204. that.setData({
  205. placeName: data[0].name + ' ' + data[0].desc
  206. })
  207. },
  208. fail: function (info) {
  209. // wx.showModal({title:info.errMsg})
  210. }
  211. })
  212. break;
  213. // 点击立即用车,判断当前是否正在计费
  214. case 2: if (this.timer === "" || this.timer === undefined) {
  215. // 没有在计费就扫码
  216. wx.scanCode({
  217. success: (res) => {
  218. // 正在获取密码通知
  219. wx.showLoading({
  220. title: '正在获取密码',
  221. mask: true
  222. })
  223. // 请求服务器获取密码和车号
  224. wx.request({
  225. url: 'https://www.easy-mock.com/mock/59098d007a878d73716e966f/ofodata/password',
  226. data: {},
  227. method: 'GET',
  228. success: function (res) {
  229. // 请求密码成功隐藏等待框
  230. wx.hideLoading();
  231. // 携带密码和车号跳转到密码页
  232. wx.redirectTo({
  233. url: '../scanresult/index?password=' + res.data.data.password + '&number=' + res.data.data.number,
  234. success: function (res) {
  235. wx.showToast({
  236. title: '获取密码成功',
  237. duration: 1000
  238. })
  239. }
  240. })
  241. }
  242. })
  243. }
  244. })
  245. // 当前已经在计费就回退到计费页
  246. } else {
  247. wx.navigateBack({
  248. delta: 1
  249. })
  250. }
  251. break;
  252. // 点击保障控件,跳转到报障页
  253. case 3: wx.navigateTo({
  254. url: '../weather/weather'
  255. });
  256. break;
  257. // 点击头像控件,跳转到个人中心
  258. case 5: wx.navigateTo({
  259. url: '../my/index'
  260. });
  261. break;
  262. case 6: wx.navigateTo({
  263. url: '../upload/index'
  264. });
  265. default: break;
  266. }
  267. },
  268. // 地图视野改变事件
  269. bindregionchange: function (e) {
  270. // 拖动地图,获取附件单车位置
  271. if (e.type == "begin") {
  272. wx.request({
  273. url: server.Server.url + '/api/user/getTeamUsers/' + app.globalData.nwUserid,
  274. data: {},
  275. method: 'GET',
  276. success: (res) => {
  277. this.setData({
  278. _markers: res.data
  279. })
  280. }
  281. })
  282. // 停止拖动,显示单车位置
  283. } else if (e.type == "end") {
  284. console.log(e.currentTarget)
  285. this.data.markers=[];
  286. this.setData({
  287. markers: this.data._markers
  288. })
  289. }
  290. },
  291. // 地图标记点击事件,连接用户位置和点击的单车位置
  292. bindmarkertap: function (e) {
  293. console.log(e);
  294. let _markers = this.data.markers;
  295. let markerId = e.markerId;
  296. let currMaker = _markers[markerId];
  297. wx.makePhoneCall({
  298. phoneNumber: markerId //仅为示例,并非真实的电话号码
  299. })
  300. },
  301. // 定位函数,移动位置到地图中心
  302. movetoPosition: function () {
  303. this.mapCtx.moveToLocation();
  304. }
  305. })