index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. // hasUserInfo: false,
  14. // canIUse: wx.canIUse('button.open-type.getUserInfo')
  15. },
  16. onLoad(options) {
  17. console.log(options)
  18. //分享
  19. if (options.openid) {
  20. app.globalData.shareOpenId = options.openid;
  21. }
  22. //刷新页面
  23. if(options.update == 1){
  24. this.getList();
  25. }
  26. if (app.globalData.userInfo) {
  27. this.setData({
  28. userInfo: app.globalData.userInfo
  29. })
  30. this.getList();
  31. } else {
  32. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  33. // 所以此处加入 callback 以防止这种情况
  34. app.userInfoReadyCallback = res => {
  35. this.setData({
  36. userInfo: res
  37. })
  38. this.getList();
  39. }
  40. };
  41. this.setData({
  42. BASEIMGURL: app.globalData.BASEIMGURL
  43. });
  44. },
  45. goDetail(e) {
  46. let id = e.currentTarget.dataset.id;
  47. let currentActivity = this.data.list.filter(v => {
  48. return v.id == id;
  49. });
  50. app.globalData.currentActivity = currentActivity[0];
  51. wx.navigateTo({
  52. url: '/pages/detail/index',
  53. })
  54. },
  55. goEdit(){
  56. },
  57. add(e) {
  58. let actid = e.currentTarget.dataset.actid;
  59. //参加活动
  60. console.log('add');
  61. app.globalData.api.activity_attend({
  62. phone: app.globalData.userInfo.phone,
  63. actid
  64. }).then(res=>{
  65. console.log(res);
  66. this.getList();
  67. wx.showToast({
  68. title: '您已成功参加活动!',
  69. icon: 'none'
  70. })
  71. })
  72. },
  73. async getPhoneNumber(e) {
  74. let actid = e.currentTarget.dataset.actid;
  75. const that = this;
  76. if (e.detail.errMsg == "getPhoneNumber:ok") {
  77. let res = await app.globalData.api.bindphone({
  78. "openid": app.globalData.userInfo.openid,
  79. "encryptedData": e.detail.encryptedData,
  80. "iv": e.detail.iv,
  81. "invitor": app.globalData.shareOpenId
  82. })
  83. app.globalData.userInfo.phone = res.data.phone;
  84. that.setData({
  85. userInfo: {
  86. ...this.data.userInfo,
  87. 'phone': res.data.phone
  88. }
  89. });
  90. //参加活动
  91. that.add(e);
  92. // wx.showToast({
  93. // title: '参加成功!',
  94. // icon: 'none'
  95. // })
  96. } else {
  97. wx.showToast({
  98. title: '请授权您的手机号!',
  99. icon: 'none'
  100. })
  101. }
  102. },
  103. touchHandler() {
  104. return;
  105. },
  106. async getList() {
  107. const _this = this;
  108. let result = await app.globalData.api.list_public();
  109. const lngArr = [];
  110. result.data.forEach( v => {
  111. v.shareMember = 0;
  112. if (v.images) {
  113. v.imgList = v.images.split(';').filter(v=>{
  114. return v != '';
  115. })
  116. }
  117. if(v.members){
  118. v.membersList = v.members.split(';').filter(v=>{
  119. return v != '';
  120. })
  121. if(v.membersList.indexOf(app.globalData.userInfo.phone || '0') > -1){
  122. v.shareMember = 1;
  123. }
  124. }
  125. if(v.lat && v.lng){
  126. lngArr.push({
  127. latitude: v.lat,
  128. longitude: v.lng
  129. })
  130. }
  131. });
  132. this.setData({
  133. list: result.data
  134. })
  135. if(lngArr.length){
  136. this.getDistance(lngArr);
  137. }
  138. },
  139. getDistance(localresult) {
  140. qqmapsdk.calculateDistance({
  141. to: localresult, //终点坐标
  142. success: function (res) { //成功后的回调
  143. console.log(res,8888)
  144. //判断经纬度,获取具体门店,付值 distance
  145. // resolve(res.result.elements[0].distance);
  146. },
  147. fail: function (error) {
  148. console.error(error);
  149. },
  150. complete: function (res) {}
  151. });
  152. },
  153. getUserInfo: function (e) {
  154. if (e.detail.userInfo) {
  155. app.globalData.userInfo = e.detail.userInfo
  156. this.setData({
  157. userInfo: e.detail.userInfo,
  158. hasUserInfo: true
  159. })
  160. } else {
  161. this.setData({
  162. userInfo: {},
  163. hasUserInfo: false
  164. })
  165. }
  166. },
  167. goAdd() {
  168. wx.navigateTo({
  169. url: '/pages/add/index',
  170. })
  171. },
  172. onShow() {
  173. },
  174. onShareAppMessage() {
  175. const openid = wx.getStorageSync('openid')
  176. let param = openid && typeof openid === 'string' ? `pages/index/index?openid=${ openid }` : `pages/index/index`
  177. return {
  178. title: 'Coffee Talk',
  179. desc: '快来参加活动吧~',
  180. // imageUrl: 'https://cirraybucket.oss-cn-shanghai.aliyuncs.com/nowwa-h5/aboutUs/share.jpg',
  181. path: param
  182. }
  183. },
  184. })