index.js 6.1 KB

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