|
- const app = getApp()
- import request from '../../utils/request'
- import Dialog from '@vant/weapp/dialog/dialog';
-
- Page({
-
- /**
- * 页面的初始数据
- */
- data: {
- isAgree: false,
- tmpUserId: ""
- },
-
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
- console.log(options, 'options');
- wx.hideHomeButton()
-
- if (options.scene) {
- this.setData({
- tmpUserId: options.scene
- })
- }
- app.tokenCallBack = token => {
- if (token) {
- this.bindUser()
- } else {
-
- }
- }
-
- },
-
-
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
-
- },
-
- getPhoneNumber(e) {
- const that = this
-
- if (e.detail.iv) {
- const openId = wx.getStorageSync('openId')
- const appId = request.appId
- const iv = e.detail.iv
- const encryptedData = e.detail.encryptedData
-
- const data = {
- openId,
- appId,
- encryptedData,
- iv
- }
- that.loginWithPhone(data)
- } else {
- that.getPhoneNumberFail()
- wx.showToast({
- title: '授权失败',
- icon: "error"
- })
- }
-
- },
-
- checkUserPro() {
- if (!this.data.isAgree) {
- Dialog.confirm({
- title: '提示',
- message: '同意智像相机用户使用协议?',
- })
- .then(() => {
- this.setData({
- isAgree: true
- })
- })
- .catch(() => {
- // on cancel
- });
- }
- },
-
- // 用户协议状态变更
- checkboxChange(e) {
- if (e.detail.value[0]) {
- this.setData({
- isAgree: true
- })
- } else {
- this.setData({
- isAgree: false
- })
- }
- },
-
- // 查看用户协议
- goPro() {
- wx.navigateTo({
- url: '/pages/userProtocol/userProtocol',
- })
- },
-
- loginWithPhone(data) {
- const that = this
- request.post({
- url: '/api/miniApp/loginPhone',
- data
- }).then(res => {
- // 获取手机号之后,绑定用户与机器
- if (res.data.token) {
- wx.setStorageSync('token', res.data.token)
- request.setHead(res.data.token)
- }
-
- that.bindUser()
- }).catch(err => {
- console.log(err, 'err');
- })
- },
-
- // 绑定用户与机器
- bindUser() {
- const openId = wx.getStorageSync('openId')
- const tmpUserId = this.data.tmpUserId
- const data = {
- openId,
- tmpUserId
- }
- request.post({
- url: '/api/miniApp/bindUser',
- data
- }).then(res => {
- console.log(res, 'res');
- wx.showToast({
- title: '登录成功!',
- icon: "success",
- duration: 1500,
- complete: () => {
- setTimeout(() => {
- if (tmpUserId) {
- wx.setStorageSync('isPay', false)
- wx.redirectTo({
- url: `/pages/index/index?machineQrcodeId=${tmpUserId}`,
- })
- } else {
- wx.redirectTo({
- url: `/pages/index/index`,
- })
- }
-
- }, 1500);
- }
- })
- }).catch(err => {
- console.log(err, 'err');
- wx.showToast({
- title: '服务器异常,请稍后再试',
- icon: "none"
- })
- })
- },
-
- getPhoneNumberFail() {
-
- },
-
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
-
- },
-
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
-
- },
-
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
-
- }
- })
|