index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4. const QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
  5. const qqmapsdk = new QQMapWX({
  6. key: '36IBZ-VI53O-FJHW2-SV2NP-USSAE-GFBUS'
  7. })
  8. Page({
  9. data: {
  10. userInfo: {},
  11. list: [], //活动列表
  12. BASEIMGURL: app.globalData.BASEIMGURL,
  13. BASEIMGURL1: app.globalData.BASEIMGURL1,
  14. loading: true,
  15. hideGetInfo: true
  16. // hasUserInfo: false,
  17. // canIUse: wx.canIUse('button.open-type.getUserInfo')
  18. },
  19. onLoad(options) {
  20. console.log(options)
  21. //分享
  22. if (options.openid) {
  23. app.globalData.shareOpenId = options.openid;
  24. }
  25. if (app.globalData.userInfo) {
  26. this.setData({
  27. userInfo: app.globalData.userInfo
  28. })
  29. this.getList();
  30. } else {
  31. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  32. // 所以此处加入 callback 以防止这种情况
  33. app.userInfoReadyCallback = res => {
  34. this.setData({
  35. userInfo: res
  36. })
  37. this.getList();
  38. }
  39. };
  40. this.setData({
  41. BASEIMGURL: app.globalData.BASEIMGURL,
  42. });
  43. },
  44. userInfoHandler(e) {
  45. console.log(e)
  46. const that = this;
  47. if (e.detail.errMsg == "getUserInfo:ok") {
  48. let userInfo = e.detail.userInfo;
  49. let params = Object.assign({}, {
  50. openid: app.globalData.userInfo.openid,
  51. nickName: userInfo.nickName,
  52. gender: userInfo.gender,
  53. avatarUrl: userInfo.avatarUrl
  54. });
  55. app.globalData.api.binduserinfo(params).then(res => {
  56. let newUserInfo = app.globalData.userInfo;
  57. newUserInfo.avatar = userInfo.avatarUrl;
  58. newUserInfo.gender = userInfo.gender;
  59. newUserInfo.nickname = userInfo.nickName;
  60. app.globalData.userInfo = newUserInfo;
  61. that.setData({
  62. userInfo: newUserInfo
  63. })
  64. })
  65. }
  66. },
  67. goDetail(e) {
  68. let id = e.currentTarget.dataset.id;
  69. let currentActivity = this.data.list.filter(v => {
  70. return v.id == id;
  71. });
  72. app.globalData.currentActivity = currentActivity[0];
  73. wx.navigateTo({
  74. url: '/pages/detail/index?actid=' + id,
  75. })
  76. },
  77. goEdit(e) {
  78. let actid = e.currentTarget.dataset.id;
  79. wx.navigateTo({
  80. url: '/pages/add/index?actid=' + actid,
  81. })
  82. },
  83. add(e) {
  84. let actid = e.currentTarget.dataset.actid;
  85. //判断是否过期
  86. let currentAct = this.data.list.filter(v => {
  87. return v.id == actid;
  88. })
  89. if (currentAct[0].isoverdue) {
  90. return wx.showToast({
  91. title: '活动时间已截止,您可以参加其它活动哦~',
  92. icon: 'none'
  93. })
  94. }
  95. //参加活动
  96. console.log('add');
  97. app.globalData.api.activity_attend({
  98. phone: app.globalData.userInfo.phone,
  99. actid
  100. }).then(res => {
  101. console.log(res);
  102. this.getList();
  103. wx.showToast({
  104. title: '您已成功参加活动!',
  105. icon: 'none'
  106. })
  107. })
  108. },
  109. async getPhoneNumber(e) {
  110. let actid = e.currentTarget.dataset.actid;
  111. const that = this;
  112. if (e.detail.errMsg == "getPhoneNumber:ok") {
  113. let res = await app.globalData.api.bindphone({
  114. "openid": app.globalData.userInfo.openid,
  115. "encryptedData": e.detail.encryptedData,
  116. "iv": e.detail.iv,
  117. "invitor": app.globalData.shareOpenId
  118. })
  119. app.globalData.userInfo.phone = res.data.phone;
  120. that.setData({
  121. userInfo: {
  122. ...this.data.userInfo,
  123. 'phone': res.data.phone,
  124. },
  125. hideGetInfo: true
  126. });
  127. //跳转到我的页面
  128. wx.switchTab({
  129. url: '/pages/my/index',
  130. })
  131. } else {
  132. wx.showToast({
  133. title: '请授权您的手机号!',
  134. icon: 'none'
  135. })
  136. }
  137. },
  138. touchHandler() {
  139. return;
  140. },
  141. async getList() {
  142. const _this = this;
  143. let result = await app.globalData.api.list_public();
  144. const lngArr = [];
  145. result.data.forEach(v => {
  146. //判断当前时间是否大于活动结束时间
  147. let myDate = new Date();
  148. let now = myDate.valueOf();
  149. let time = new Date(v.endtime).valueOf();
  150. if (app.globalData.systemInfo.platform == 'ios') {
  151. let _date = v.endtime.replace(/\.|\-/g, '/');
  152. time = new Date(_date).valueOf();
  153. }
  154. if (now > time) {
  155. v.isoverdue = true;
  156. } else {
  157. v.isoverdue = false;
  158. }
  159. v.starttime = v.starttime.substr(0, 16);
  160. v.shareMember = 0;
  161. if (v.images) {
  162. try {
  163. v.imgList = JSON.parse(v.images)
  164. } catch (error) {
  165. }
  166. }
  167. if (v.members) {
  168. v.membersList = v.members.split(';').filter(v => {
  169. return v != '';
  170. })
  171. if (v.membersList.indexOf(app.globalData.userInfo.phone || '0') > -1) {
  172. v.shareMember = 1;
  173. }
  174. }
  175. if (!!v.latitude && !!v.longitude) {
  176. lngArr.push({
  177. latitude: v.latitude,
  178. longitude: v.longitude
  179. })
  180. }
  181. });
  182. this.setData({
  183. list: result.data,
  184. loading: false
  185. })
  186. if (lngArr.length) {
  187. this.getDistance(lngArr);
  188. }
  189. },
  190. getDistance(localresult) {
  191. const that = this;
  192. qqmapsdk.calculateDistance({
  193. to: localresult, //终点坐标
  194. success: function (res) { //成功后的回调
  195. let newList = that.data.list;
  196. res.result.elements.map(v => {
  197. newList.map(j => {
  198. if (v.to.lat == j.latitude && v.to.lng == j.longitude) {
  199. j.distance = v.distance;
  200. }
  201. })
  202. });
  203. that.setData({
  204. list: newList
  205. })
  206. //判断经纬度,获取具体门店,付值 distance
  207. // resolve(res.result.elements[0].distance);
  208. },
  209. fail: function (error) {
  210. console.error(error);
  211. },
  212. complete: function (res) {}
  213. });
  214. },
  215. getUserInfo: function (e) {
  216. if (e.detail.userInfo) {
  217. app.globalData.userInfo = e.detail.userInfo
  218. this.setData({
  219. userInfo: e.detail.userInfo,
  220. hasUserInfo: true
  221. })
  222. } else {
  223. this.setData({
  224. userInfo: {},
  225. hasUserInfo: false
  226. })
  227. }
  228. },
  229. goAdd() {
  230. wx.navigateTo({
  231. url: '/pages/add/index',
  232. })
  233. },
  234. onShow() {
  235. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  236. this.getTabBar().setData({
  237. selected: 0
  238. });
  239. }
  240. },
  241. onShareAppMessage() {
  242. return app.onShareAppMessage();
  243. },
  244. closeTap() {
  245. this.setData({
  246. hideGetInfo: true
  247. })
  248. },
  249. onUnload() {
  250. this.setData({
  251. hideGetInfo: true
  252. })
  253. },
  254. })