|
- // pages/index/index.js
- const APP = getApp();
- import * as UTIL from '../../../utils/util.js';
- import * as API from '../../../utils/API.js';
- import * as STORAGE from '../../../utils/storage'
- Page({
- data: {
- isIPhoneX:false,
- isIPX: APP.globalData.isIPX,
- privacyCheck:false, //用户协议
- showDialog:false,
- nowDress:'',
- showPhoneBtn:false
- },
- onShow: function (options) {
- // this.setData({
- // isIPhoneX:UTIL.isIPhoneX()
- // })
- var that = this;
- let url = wx.getStorageSync('dressCode')
- that.setData({
- nowDress: wx.getStorageSync('dressName')
- })
- console.log(getApp().globalData.APIURL);
- if (url == '' || url == undefined || url == null) {
- wx.redirectTo({
- url: '../region/region',
- })
- }else{
- //获取code
- UTIL.getCOdeFromWX({
- complate: (code) => {
- //获取openId
- console.log('aaaa');
- that.getOpenIdFromFW(code);
- }
- });
- }
- // this.automaticLogin()
- //this.getUserInfo()
- //this.getPhoneNumber()
- },
- goRegion(){
- wx.navigateTo({
- url: '/pages/user/region/region',
- })
- },
- /**
- * 从服务端获取openId
- */
- getOpenIdFromFW(code) {
- let sendData = {
- code: code
- }
- console.log(wx.getStorageSync('dressCode')+API.URL_GET_OPENID);
- UTIL.httpRequestNoneDetal(API.URL_GET_OPENID, sendData, "POST", {
- success: (res) => {
- console.log(res);
- if (res.code == API.SUCCESS_CODE) {
- // UTIL.showToastNoneIcon("openId:" + res._data.openid);
- if(res.token){
- wx.setStorageSync('token', res.token);
- wx.setStorageSync('openId', res.data.openId);
- wx.setStorageSync('sessionKey', res.data.sessionKey);
- getApp().globalData.userInfo.token = res.token;
- }else{
- this.setData({showPhoneBtn:true})
- }
- } else {
- //未获取到openId
- // wx.setStorageSync('openId', res.data.openId);
- // wx.setStorageSync('sessionKey', res.data.sessionKey);
- this.setData({showPhoneBtn:true})
- }
- }
- })
- },
- goIndex(){
- if(this.data.privacyCheck == false){
- APP.showToast("请阅读并同意【服务须知】!");
- return;
- }
- wx.reLaunch({
- url: '/pages/index/index',
- })
- },
- //自动登录
- automaticLogin(){
- let automatic = STORAGE.getToken();
- let getOpenId = STORAGE.getOpenId();
- if(automatic == '' && getOpenId ==''){
- console.log(1);
- UTIL.getCOdeFromWX({
- complate: (code) => {
- let sendData = {
- code: code
- }
- UTIL.httpRequestNoneDetal(API.URL_GET_OPENID, sendData, "POST", {
- success: (res) => {
- console.log(res);
- if (res.code == API.SUCCESS_CODE) {
- wx.reLaunch({
- url: '/pages/index/index',
- })
- } else{
- this.getPhoneNumber()
- }
- }
- })
- }
- });
- }else{
- // wx.navigateTo({
- // url: '/pages/index/index',
- // })
- }
- },
- //用户隐私协议选项
- checkboxChange: function(res) {
- let checkStatus = false;
- if(res.detail.value.length!=0){
- checkStatus = true;
- }else{
- checkStatus = false;
- }
- this.setData({
- privacyCheck:checkStatus
- })
- },
- /* 获取用户信息*/
- getUserInfo(){
- UTIL.httpRequest(API.URL_GET_GETINFO, {method:'GET'}, {
- success: (res) => {
- if (res.code == API.SUCCESS_CODE) {
- this.setData({userInfoObj:res.user})
- }
- }
- })
- },
- //微信一键登录授权
- getPhoneNumber: function(res) {
- let that = this;
- let {
- detail
- } = res;
- if (!detail.encryptedData) {
- //允许授权
- APP.showToast("未获取到手机号码,登录失败!");
- return;
- }else if(this.data.privacyCheck == false){
- APP.showToast("请阅读并同意【服务须知】!");
- return;
- }
- console.log(detail)
- let sendData = {
- sessionKey:STORAGE.getSessionKey(),
- iv:detail.iv,
- encryptedData:detail.encryptedData
- }
- UTIL.httpRequestNoneDetal(API.URL_POST_DECRYPTEDWXDATA, sendData, "POST", {
- success: (res) => {
- console.log(res)
- if (res.code == API.SUCCESS_CODE) {
- let phoneNumber = res.data.phoneNumber;
- //微信手机号码绑定
- that.wxUserBand(phoneNumber)
- // UTIL.showToastNoneIcon("数据共:" + res._data.length + "条");
- } else {
- //待删
- UTIL.showToastNoneIcon(res.msg)
- }
- },
- fail: (res) => {
- console.log("fail");
- UTIL.showToastNoneIcon(API.MSG_FAIL_HTTP)
- }
- })
- // UTIL.httpRequest(API.URL_POST_DECRYPTEDWXDATA, sendData,{
- // success: (res) => {
- // console.log(res);
- // // if (res.code == API.SUCCESS_CODE) {
- // // let phoneNumber = res.data.phoneNumber;
- // // //微信手机号码绑定
- // // that.wxUserBand(phoneNumber)
- // // // UTIL.showToastNoneIcon("数据共:" + res._data.length + "条");
- // // } else {
- // // //待删
-
- // // UTIL.showToastNoneIcon(res.msg)
- // // }
- // },
- // fail: (res) => {
- // console.log("fail");
- // UTIL.showToastNoneIcon(API.MSG_FAIL_HTTP)
- // }
- // });
- },
- wxUserBand(phone){
- let sendData = {
- openId:STORAGE.getOpenId(),
- phonenumber:phone
- }
- console.log(STORAGE.getOpenId());
- UTIL.httpRequestNoneDetal(API.URL_POST_USERBAND, sendData,"POST",{
- success: (res) => {
- if (res.code == API.SUCCESS_CODE) {
- wx.setStorageSync('token', res.token);
- wx.reLaunch({
- // url: '/pages/user/region/region',
- url: '/pages/index/index',
- })
- }else{
- UTIL.showToastNoneIcon(res.msg)
- }
- },
- fail: (res) => {
- UTIL.showToastNoneIcon(API.MSG_FAIL_HTTP)
- }
- });
- //
- },
- //打开协议窗口
- openDialog(){
- this.setData({showDialog:true})
- },
- agree(){
- this.setData({
- privacyCheck:true
- })
- },
- goPhoneLogin(){
- wx.navigateTo({
- url: '/pages/phoneLogin',
- })
- }
-
- })
|