| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- // index.js
- const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
- const homeApi_empower = "https://aipush.aidsleep.cn";
- const token_empower = "b74fd5754c5ef24cf600c39194abdaeb";
- Page({
- data: {
- motto: 'Hello World',
- defaultAvatarUrlD: defaultAvatarUrl,
- userInfo: {
- avatarUrl: defaultAvatarUrl,
- nickName: '',
- },
- phoneNumber: '',//手机号
- hasUserInfo: false,
- canIUseGetUserProfile: wx.canIUse('getUserProfile'),
- canIUseNicknameComp: wx.canIUse('input.type.nickname'),
- intervalId: null,
- loading: false ,// 标记是否正在加载
- isLoading: false,
- disableButtom: false,
- unionid: '',
- openid: '',
- session_key: '',
- encryptphone: '',//加密的手机号
- },
- bindViewTap() {
- // wx.navigateTo({
- // url: '../logs/logs'
- // })
- },
- onChooseAvatar(e) {
- const { avatarUrl } = e.detail
- const { nickName } = this.data.userInfo
- var that = this
- this.setData({
- "userInfo.avatarUrl": avatarUrl,
- hasUserInfo: nickName && this.data.phoneNumber && avatarUrl && avatarUrl !== defaultAvatarUrl,
- })
- wx.setStorageSync('userInfo_em', this.data.userInfo);
- wx.setStorageSync('phoneNumber', this.data.phoneNumber);
- },
- onInputChange(e) {
- console.log('e=='+JSON.stringify(e));
- console.log('e.detail=='+JSON.stringify(e.detail));
- const nickName = e.detail.value
- const { avatarUrl } = this.data.userInfo
- this.setData({
- "userInfo.nickName": nickName,
- hasUserInfo: nickName && this.data.phoneNumber && avatarUrl && avatarUrl !== defaultAvatarUrl,
- })
- wx.setStorageSync('userInfo_em', this.data.userInfo);
- wx.setStorageSync('phoneNumber', this.data.phoneNumber);
- },
- getUserProfile(e) {
- // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
- wx.getUserProfile({
- desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
- success: (res) => {
- console.log(res)
- this.setData({
- userInfo: res.userInfo,
- hasUserInfo: true
- })
- }
- })
- },
- onLoad: function(options) {
- const userInfo_em = wx.getStorageSync('userInfo_em');
- const phoneNumber = wx.getStorageSync('phoneNumber');
- console.log('userInfo_em====='+JSON.stringify(userInfo_em));
- if(userInfo_em && phoneNumber){
- const { avatarUrl,nickName } = userInfo_em;
- this.setData({
- userInfo: userInfo_em,
- phoneNumber: phoneNumber,
- hasUserInfo: nickName && phoneNumber && avatarUrl && avatarUrl !== defaultAvatarUrl,
- disableButtom: wx.getStorageSync('userInfo_em') && wx.getStorageSync('unionid') && wx.getStorageSync('phoneNumber') && wx.getStorageSync('hasAuth')
- })
- }
- const unionid = wx.getStorageSync('unionid');
- const hasAuth = wx.getStorageSync('hasAuth');
- if(userInfo_em && unionid && hasAuth){
- this.setData({
- disableButtom: true,
- });
- }else{
- this.setData({
- disableButtom: false,
- });
- // setTimeout(function() {
- // wx.showModal({
- // title: '提示',
- // content: '请进行授权',
- // showCancel: false
- // });
- // }, 2000) // 设置延时时间,单位为毫秒
-
- // wx.showToast({
- // title: '请进行授权',
- // icon: 'info',
- // duration: 3000
- // });
- // wx.showModal({
- // title: '提示',
- // content: '请进行授权',
- // showCancel: false,
- // duration: 3000
- // });
-
- }
- },
-
- handleUserInfo: function(e) {
- if(!this.data.hasUserInfo){
- // setTimeout(function() {
- wx.showToast({
- title: '请设置头像、昵称和手机号',
- icon: 'none'
- });
- // }, 2000) // 设置延时时间,单位为毫秒
- return;
- }
- var that = this;
- if (e.detail.userInfo) {
- // 用户点击允许,获取到用户信息
- this.setData({
- // userInfo: e.detail.userInfo,
- // loading: true,
- disableButtom: true,
- isLoading: true,
- });
- wx.login({
- success: res => {
- if (res.code) {
- // 发送 code 到服务器换取 session_key, openid
- console.log('res.code=='+res.code);
-
- wx.request({
- url: `${homeApi_empower}/wxlogin`,
- data: {
- code: res.code,
- token: token_empower,
- nickname: that.data.userInfo.nickName,
- encryptphone: that.data.encryptphone
- },
- method: 'POST',
- success: function(loginRes) {
- console.log('loginRes='+JSON.stringify(loginRes));
- console.log('loginRes.data='+JSON.stringify(loginRes.data));
- if (loginRes.data.codes && loginRes.data.codes == 'success') {
- console.log('123123123');
- console.log('loginRes.data.back.unionid='+loginRes.data.back.unionid);
- console.log('loginRes.data.back.openid='+loginRes.data.back.openid);
- wx.setStorageSync('unionid', loginRes.data.back.unionid);
- wx.setStorageSync('openid', loginRes.data.back.openid);
- // 获取后台授权
- that.startInterval();
- // 可以在这里提示用户进行手机号授权
- that.setData({
- canGetPhoneNumber: true,
- isLoading: true,
- unionid: loginRes.data.back.unionid,
- openid: loginRes.data.back.openid,
- session_key: loginRes.data.back.session_key,
- });
- }
- }
- });
- }
- }
- });
-
- // 假设有异步操作(如发送数据到服务器),则放在这里
-
-
- } else {
- // 用户拒绝授权
- wx.showToast({
- title: '您拒绝了授权',
- icon: 'none'
- });
-
- // 可以选择在这里处理用户拒绝授权后的逻辑,如跳转到其他页面或显示提示信息
- }
- },
- onGetPhoneNumber: function(e) {
- console.log('onGetPhoneNumber='+JSON.stringify(e));
- var that = this;
- if (e.detail.errMsg === "getPhoneNumber:ok") {
- // 发送 encryptedData, iv, 和 sessionKey 到你的后台服务器
- wx.login({
- success: function (res) {
- if (res.code) {
- // 发送 res.code 到后台换取 openId, sessionKey, unionId
- wx.request({
- url: `${homeApi_empower}/wxtel`,
- method: 'POST',
- data: {
- encryptedData: e.detail.encryptedData,
- iv: e.detail.iv,
- code: res.code,
- token: token_empower,
- },
- success: function(res) {
- // 处理服务器返回的结果
- console.log('手机号获取结果:', res.data);
- console.log('手机号获取结果:', res.data.encryptphone);
- console.log('手机号获取结果:', res.data.shieldphone);
- if(!res.data || !res.data.encryptphone || !res.data.shieldphone){
- // setTimeout(function() {
- wx.showToast({
- title: '无法获取用户手机号',
- icon: 'none'
- });
- // }, 2000) // 设置延时时间,单位为毫秒
- return;
- }
- console.log('e=='+JSON.stringify(e));
- console.log('e.detail=='+JSON.stringify(e.detail));
- const { avatarUrl,nickName } = that.data.userInfo
- that.setData({
- encryptphone: res.data.encryptphone,
- phoneNumber: res.data.shieldphone,
- hasUserInfo: nickName && res.data.shieldphone && avatarUrl && avatarUrl !== defaultAvatarUrl,
- })
- wx.setStorageSync('userInfo_em', that.data.userInfo);
- wx.setStorageSync('phoneNumber',res.data.shieldphone);
- }
- })
- }
- }
- })
- }
- },
- startInterval: function () {
- var that = this;
- this.data.intervalId = setInterval(() => {
- console.log('后台授权中...');
- console.log('startInterval-unionid='+that.data.unionid);
- console.log('startInterval-openid='+that.data.openid);
- wx.request({
- url: `${homeApi_empower}/wxstatus`,
- data: {
- //nickName: that.data.userInfo.nickName,
- unionid: wx.getStorageSync('unionid'),
- openid: wx.getStorageSync('openid'),
- //phoneNumber: that.data.phoneNumber,
- token: token_empower,
- },
- method: 'POST',
- success: function(intervalRes) {
- console.log('intervalRes='+JSON.stringify(intervalRes));
- console.log('intervalRes.data='+JSON.stringify(intervalRes.data));
- //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
- if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1' ) {
- wx.setStorageSync('hasAuth', 'hasAuth');
- //这里后边改成接口返回值
- wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
- wx.setStorageSync('roomEmpower', intervalRes.data.room);
- // 可以在这里提示用户进行手机号授权
- that.setData({
- canGetPhoneNumber: true,
- isLoading: false,
- disableButtom: false,
- });
- wx.showToast({
- title: '后台授权成功',
- icon: 'none'
- });
-
- that.clearInterval();
- setTimeout(function() {
- // if(!wx.getStorageSync('res') || !wx.getStorageSync('res').result){
- // 跳转到scan页面
- wx.reLaunch({
- url: '/pages/scan/scan',
- })
- // }else{
- // // 跳转到上一页
- // wx.navigateBack({
- // delta: 1 // 返回上级页面
- // });
- // }
- }, 2000) // 设置延时时间,单位为毫秒
- }else{
- that.clearInterval();
- wx.showToast({
- title: '授权失败,请先办理入住',
- icon: 'none'
- });
- }
- },
- fail: function (error) {
- that.clearInterval();
- wx.showToast({
- title: '后台授权失败',
- icon: 'none'
- });
- }
- });
- }, 3000);
- },
- clearInterval: function () {
- var that = this;
- if (this.data.intervalId) {
- clearInterval(that.data.intervalId);
- that.data.intervalId = null;
- }
- },
- onHide() {
- // this.clearInterval();
- },
- onUnload() {
- this.clearInterval();
- },
-
- })
|