123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- //index.js
- //获取应用实例
- const app = getApp()
- const QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
- const qqmapsdk = new QQMapWX({
- key: '36IBZ-VI53O-FJHW2-SV2NP-USSAE-GFBUS'
- })
- Page({
- data: {
- userInfo: {},
- list: [], //活动列表
- BASEIMGURL: app.globalData.BASEIMGURL,
- BASEIMGURL1: app.globalData.BASEIMGURL1,
- loading: true,
- hideGetInfo: true
- // hasUserInfo: false,
- // canIUse: wx.canIUse('button.open-type.getUserInfo')
- },
- onLoad(options) {
- console.log(options)
- //分享
- if (options.openid) {
- app.globalData.shareOpenId = options.openid;
- }
- if (app.globalData.userInfo) {
- this.setData({
- userInfo: app.globalData.userInfo
- })
- this.getList();
- } else {
- // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
- // 所以此处加入 callback 以防止这种情况
- app.userInfoReadyCallback = res => {
- this.setData({
- userInfo: res
- })
- this.getList();
- }
- };
- this.setData({
- BASEIMGURL: app.globalData.BASEIMGURL,
- });
- },
- userInfoHandler(e) {
- console.log(e)
- const that = this;
- if (e.detail.errMsg == "getUserInfo:ok") {
- let userInfo = e.detail.userInfo;
- let params = Object.assign({}, {
- openid: app.globalData.userInfo.openid,
- nickName: userInfo.nickName,
- gender: userInfo.gender,
- avatarUrl: userInfo.avatarUrl
- });
- app.globalData.api.binduserinfo(params).then(res => {
- let newUserInfo = app.globalData.userInfo;
- newUserInfo.avatar = userInfo.avatarUrl;
- newUserInfo.gender = userInfo.gender;
- newUserInfo.nickname = userInfo.nickName;
- app.globalData.userInfo = newUserInfo;
- that.setData({
- userInfo: newUserInfo
- })
- })
- }
- },
- goDetail(e) {
- let id = e.currentTarget.dataset.id;
- let currentActivity = this.data.list.filter(v => {
- return v.id == id;
- });
- app.globalData.currentActivity = currentActivity[0];
- wx.navigateTo({
- url: '/pages/detail/index?actid=' + id,
- })
- },
- goEdit(e) {
- let actid = e.currentTarget.dataset.id;
- wx.navigateTo({
- url: '/pages/add/index?actid=' + actid,
- })
- },
- add(e) {
- let actid = e.currentTarget.dataset.actid;
- //判断是否过期
- let currentAct = this.data.list.filter(v => {
- return v.id == actid;
- })
- if (currentAct[0].isoverdue) {
- return wx.showToast({
- title: '活动时间已截止,您可以参加其它活动哦~',
- icon: 'none'
- })
- }
- //参加活动
- console.log('add');
- app.globalData.api.activity_attend({
- phone: app.globalData.userInfo.phone,
- actid
- }).then(res => {
- console.log(res);
- this.getList();
- wx.showToast({
- title: '您已成功参加活动!',
- icon: 'none'
- })
- })
- },
- async getPhoneNumber(e) {
- let actid = e.currentTarget.dataset.actid;
- const that = this;
- if (e.detail.errMsg == "getPhoneNumber:ok") {
- let res = await app.globalData.api.bindphone({
- "openid": app.globalData.userInfo.openid,
- "encryptedData": e.detail.encryptedData,
- "iv": e.detail.iv,
- "invitor": app.globalData.shareOpenId
- })
- app.globalData.userInfo.phone = res.data.phone;
- that.setData({
- userInfo: {
- ...this.data.userInfo,
- 'phone': res.data.phone,
- },
- hideGetInfo: true
- });
- //跳转到我的页面
- wx.switchTab({
- url: '/pages/my/index',
- })
- } else {
- wx.showToast({
- title: '请授权您的手机号!',
- icon: 'none'
- })
- }
- },
- touchHandler() {
- return;
- },
- async getList() {
- const _this = this;
- let result = await app.globalData.api.list_public();
- const lngArr = [];
- result.data.forEach(v => {
- //判断当前时间是否大于活动结束时间
- let myDate = new Date();
- let now = myDate.valueOf();
- let time = new Date(v.endtime).valueOf();
- if (app.globalData.systemInfo.platform == 'ios') {
- let _date = v.endtime.replace(/\.|\-/g, '/');
- time = new Date(_date).valueOf();
- }
- if (now > time) {
- v.isoverdue = true;
- } else {
- v.isoverdue = false;
- }
- v.starttime = v.starttime.substr(0, 16);
- v.shareMember = 0;
- if (v.images) {
- try {
- v.imgList = JSON.parse(v.images)
- } catch (error) {
- }
- }
- if (v.members) {
- v.membersList = v.members.split(';').filter(v => {
- return v != '';
- })
- if (v.membersList.indexOf(app.globalData.userInfo.phone || '0') > -1) {
- v.shareMember = 1;
- }
- }
- if (!!v.latitude && !!v.longitude) {
- lngArr.push({
- latitude: v.latitude,
- longitude: v.longitude
- })
- }
- });
- this.setData({
- list: result.data,
- loading: false
- })
- if (lngArr.length) {
- this.getDistance(lngArr);
- }
- },
- getDistance(localresult) {
- const that = this;
- qqmapsdk.calculateDistance({
- to: localresult, //终点坐标
- success: function (res) { //成功后的回调
- let newList = that.data.list;
- res.result.elements.map(v => {
- newList.map(j => {
- if (v.to.lat == j.latitude && v.to.lng == j.longitude) {
- j.distance = v.distance;
- }
- })
- });
- that.setData({
- list: newList
- })
- //判断经纬度,获取具体门店,付值 distance
- // resolve(res.result.elements[0].distance);
- },
- fail: function (error) {
- console.error(error);
- },
- complete: function (res) {}
- });
- },
- getUserInfo: function (e) {
- if (e.detail.userInfo) {
- app.globalData.userInfo = e.detail.userInfo
- this.setData({
- userInfo: e.detail.userInfo,
- hasUserInfo: true
- })
- } else {
- this.setData({
- userInfo: {},
- hasUserInfo: false
- })
- }
- },
- goAdd() {
- wx.navigateTo({
- url: '/pages/add/index',
- })
- },
- onShow() {
- if (typeof this.getTabBar === 'function' && this.getTabBar()) {
- this.getTabBar().setData({
- selected: 0
- });
- }
- },
- onShareAppMessage() {
- return app.onShareAppMessage();
- },
- closeTap() {
- this.setData({
- hideGetInfo: true
- })
- },
- onUnload() {
- this.setData({
- hideGetInfo: true
- })
- },
- })
|