index.js 5.2 KB

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