微信小程序获取手机号

根据session_key解析加密数据获取手机号

根据code获取openId

详细代码如下

<template>
    <view>
        <view class="content u-flex">
            <image class="logo" src="/static/images/logo.jpg"></image>
            <view class="text-area">
                <text class="title">
                    扫码开门,关门自动结算,智能AI售货柜
                </text>
            </view>
            <view style="margin-top: 50px;">
                <button type="primary" size="default" :ripple="true" shape="square" @tap="scanQrcode" v-if="showWxTiXing">扫码开门</button>
                <button v-if="!showWxTiXing" type="primary" size="default" :ripple="true" shape="square" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">手机号快捷登录</button>
            </view>
        </view>
        <yu-toast :message="message" :verticalAlign="verticalAlign" ref="toast"></yu-toast>
    </view>
</template>

<script>
    var that;
    export default {
        data() {
            return {
                // baseUrl: 'http://139.155.73.60:9001',// 生产环境的API地址前缀 
                baseUrl: 'http://192.168.2.201:8081', // 测试环境的API地址前缀 
                message: '提示信息',        //文本信息
                xieYiChecked:false,
                showWxTiXing:true,
                verticalAlign:'bottom',    //提示框在垂直方向的位置
                guiJiCode: '', //柜机编号 
                currentTab: 1,
            }
        },
        onLoad(options) {
            that = this
            //#ifdef MP-WEIXIN
            var _this = this;
            if(options && options.scene){
                let scene = decodeURIComponent(options.scene);
                _this.guiJiCode = scene.split(',')[0];
            }else {//TODO测试时的默认柜机
                _this.guiJiCode = 'G340101002'
            }
            
            uni.login({
                success(res) {
                    _this.getOpenId(res)
                    console.log('res1',res);
                }
            });
            //#endif
        },
        methods: {
            async getPhoneNumber(e) {
                that = this
                console.log('e',e);
                if (e.detail.errMsg == "getPhoneNumber:ok") {
                    var encryptedData = e.detail.encryptedData;
                    var iv = e.detail.iv;
                    var sessionId = getApp().globalData.sessionId;
                    const wxPhoneJson = await this.$myRequest({
                        //根据session_key解析加密数据获取手机号接口
                        url: 'weixinapi/getDecryptJson',   
                        data: {
                            iv: e.detail.iv,
                            encryptedData: encryptedData,
                            sessionId: sessionId
                        },
                        showToast: false
                    })
                    console.log(wxPhoneJson);
                    if (wxPhoneJson.purePhoneNumber) {
                        getApp().globalData.wxPhone = wxPhoneJson.purePhoneNumber;
                        console.log(wxPhoneJson.purePhoneNumber);
                        this.wxQuickReg();
                    } else {
                        this.message = '获取绑定手机号码失败'
                        this.$refs.toast.show()
                    }
                }
            },
            wxQuickReg() {
                var phone = getApp().globalData.wxPhone;
                var openId = getApp().globalData.openid;
                this.$myRequest({
                    //根据phone和openId进行快捷登陆和注册并判断用户是否合法接口
                    url: 'user/quickRegister',
                    data: {
                        phone: phone,
                        openId: openId
                    },
                    showToast: false
                }).then((res) => {
                    if (res.status == "ERROR" && res.data == "isExitAndDel") {
                        this.message = '手机号已经注册,请联系管理员!'
                        this.$refs.toast.show()
                    } else if (res.status == "OK") {
                        const {
                            data
                        } = res
                        getApp().globalData.userid = data.id;
                        getApp().globalData.userGrade = data.userGrade;
                        console.info("页面跳转前")
                        uni.reLaunch({
                            url: '/pages/guiji/home?scene='+that.guiJiCode
                        })
                    } else {
                        this.message = '系统出现异常,请联系管理员或重试!'
                        this.$refs.toast.show()
                    }
                })
            }, 
            
            async getOpenId(res) {
                const openIdJson = await this.$myRequest({
                    //根据code获取openid和session_key接口
                    url: "weixinapi/getOpenId",
                    data: {
                        code: res.code
                    }
                })
                
                console.log('1',openIdJson);
                
                getApp().globalData.openid = openIdJson.data.openid;
                getApp().globalData.sessionId = openIdJson.data.session_key;
                this.doChooseLogin();
            },
            async doChooseLogin() {
                let that = this;
                console.log("1234");
                const user = await this.$myRequest({
                    url: 'user/getOneByWxId',
                    data: {
                        openid: getApp().globalData.openid
                    },
                    showToast: false
                })
                console.log('getOneByWxId',user);
                if ("OK" === user.status) {
                    getApp().globalData.userid = user.data.id;
                    getApp().globalData.userGrade = user.data.userGrade;
                    getApp().globalData.phone = user.data.phone;

                        if (user.data.id && user.data.wxAutoLoginFlag == 1 &&user.data.phone) {
                            //执行快捷登录
                            // 未支付订单判断
                            uni.request({
                                method:"GET",
                                url: that.baseUrl + '/f/api/guiji/getUnpaidOrders',
                                data:{
                                    userid: getApp().globalData.userid, 
                                    // userid: '44',
                                },
                                success(res){
                                    if(res.data.success && res.data.data.length==0){
                                        // return res.data
                                        //柜子是否为空
                                        uni.request({
                                            method:"GET",
                                            url: that.baseUrl + '/f/api/guiji/getGuiji',
                                            data:{
                                                guiJiCode: that.guiJiCode,
                                            },
                                            success(data) {
                                                if(data.data.success){
                                                    // 调用开门方法发送开门指令并生成视频订单
                                                     that.openDoor()
                                                }else{
                                                    //页面跳转到错误页面
                                                    uni.navigateTo({
                                                        url:'/pages/guiji/networcheck-404/networcheck-404'
                                                    })
                                                }
                                                console.log('data',data);
                                            }
                                        })
                                    }else{
                                        //跳到未支付订单页面,获取未支付的列表
                                        uni.navigateTo({
                                            url: '/pages/guiji/order/order?currentTab='+that.currentTab
                                        });
                                    }
                                    
                                }
                            })
                            
                        }else if(user.data.id && user.data.wxAutoLoginFlag == 1){
                            that.showWxTiXing =false
                        }else{
                            //黑名单
                        }
                    // }
                }else {
                    //消费者首次使用本小程序,需先注册登录
                    // uni.navigateTo({
                    //     url: '/pages/login/beginLogin/begin'
                    // })
                    that.showWxTiXing =false
                }
            },
            
            //开门并生成视频订单
            openDoor: function(){
                // 不使用微信支付分,测试环节使用
                that.openDoorAndCreateVideoOrder()
                return false;
                
                uni.request({
                    method:"GET",
                    url: that.baseUrl + '/f/api/guiji/createPayscoreServiceorder',
                    data:{
                        guiJiCode: that.guiJiCode,
                        // userid: getApp().globalData.userid,
                        userid: 59, //TODO 测试时的默认用户
                    },
                    success(res) {
                        if(res.data.success){
                            let currentOrderId = res.data.data.orderId; //扫码开门操作时创建的视频订单ID
                            if (wx.openBusinessView) {
                                wx.openBusinessView({
                                businessType: 'wxpayScoreUse',
                                extraData: {
                                  mch_id: res.data.data.mch_id,
                                  package: res.data.data.package,
                                  timestamp: res.data.data.timestamp,
                                  nonce_str: res.data.data.nonce_str,
                                  sign_type: res.data.data.sign_type,
                                  sign: res.data.data.sign
                                },
                                success(e) {
                                  //dosomething
                                  //{"errCode":0,"extraData":{},"errMsg":"openBusinessView:ok"}
                                  //{"errCode":0,"extraData":{"appid":"wxd8f3793ea3b935b8","query_id":"AAQTnZoAAAABAAAAAADUFmr8OZUkVE-WiDRFYyAAAABcwQVtru-5k9MmEOZJ_Pv_Nq7Cw56dNKKN5EjZKnt5jdNhCZvHOT8sSPWgG914_rxlIcK7bK3pytvlzJbGqFR0HAuFFNRhzvV3lDHrc0vE6p2UgobWrXjTtWBFx5YNAvsA5Nt_6WxNJeXBdr1e1Y1y82_3xPy9JBpH5lV4ZqD-Bdt0WNYKyiLMWJyngphVmIkdiAge56VGq8pIcINpqr-DXOR2gdvvCGES"},"errMsg":"openBusinessView:ok"}
                                  console.log(JSON.stringify(e))
                                  if(e.errCode == 0 && e.extraData && e.extraData.appid && e.extraData.query_id){
                                      //TODO 用户点击了“确认”按钮,调用查询支付分订单接口判断微信侧是否确认成功
                                      uni.request({
                                        method: "GET",
                                          url: that.baseUrl +'/f/api/guiji/queryPayscoreServiceorder',
                                        data:{
                                            // userid: getApp().globalData.userid,
                                            userid: 59, //TODO 测试时的默认用户
                                            orderId: currentOrderId
                                        },
                                        success(queryRes) {
                                            if(queryRes.data.success){
                                                if(queryRes.data.data == 'CREATED'){//商户已创建服务订单
                                                    uni.showToast({
                                                        icon:"none",
                                                        title: "您未进行确认操作,开门失败,请重新扫描开门"
                                                    })
                                                    // 调接口取消订单
                                                    that.cancelPayscoreServiceorder(currentOrderId)
                                                }else if(queryRes.data.data == 'DOING'){//服务订单进行中
                                                
                                                    // 调确认订单接口
                                                    uni.request({
                                                        method:"GET",
                                                        url: that.baseUrl +'/f/api/guiji/openDoorAndModifyVideoOrder',
                                                        data:{
                                                            guiJiCode: that.guiJiCode,
                                                            // userid: getApp().globalData.userid,
                                                            userid: 59, //TODO 测试时的默认用户
                                                            orderId: currentOrderId
                                                        },
                                                        success(openRes) {
                                                            if(openRes.data.success){//开门成功
                                                                uni.showToast({
                                                                    icon:"none",
                                                                    title: "已开门,请选购商品"
                                                                })                            
                                                            }else {//开门失败
                                                                uni.showToast({
                                                                    icon:"none",
                                                                    title: openRes.data.msg
                                                                })
                                                                // 调接口取消订单
                                                                that.cancelPayscoreServiceorder(currentOrderId)
                                                            }
                                                        }    
                                                    })
                                                    
                                                }
                                            }else {
                                                uni.showToast({
                                                    icon:"none",
                                                    title: "开门失败,请重新扫描开门"
                                                })
                                                // 调接口取消订单
                                                that.cancelPayscoreServiceorder(currentOrderId)
                                            }
                                        }
                                        
                                      })
                                  }else {
                                      //TODO 用户未点击了“确认”按钮,返回了当前小程序
                                      uni.showToast({
                                        icon:"none",
                                        title: "您未进行确认操作,开门失败,请重新扫描开门"
                                      })
                                      // 调接口取消订单
                                      that.cancelPayscoreServiceorder(currentOrderId)
                                  }
                                },
                                fail(e) {
                                  //dosomething
                                  console.log('确认订单失败:'+JSON.stringify(e))
                                  //dosomething
                                  uni.showToast({
                                    icon:"none",
                                    title: "确认订单失败,请重试"
                                  })
                                  // 调接口取消订单
                                  that.cancelPayscoreServiceorder(currentOrderId)
                                },
                                complete() {
                                  //dosomething
                                }
                              });
                            } else {
                              //引导用户升级微信版本
                              uni.showToast({
                                icon:"none",
                                title: "微信版本过低,请升级微信后重试"
                              })
                              // 调接口取消订单
                              that.cancelPayscoreServiceorder(currentOrderId)
                            }
                            
                        }else {
                            uni.showToast({
                                icon:"none",
                                title: res.data.msg
                            })
                        }
                    }
                })
            },
            
            
            // 取消微信支付分订单
            cancelPayscoreServiceorder: function(orderId){
                // 调接口取消订单
                uni.request({
                    method:"GET",
                    url: that.baseUrl +'/f/api/guiji/cancelPayscoreServiceorder',
                    data:{
                        // userid: getApp().globalData.userid,
                        userid: 59, //TODO 测试时的默认用户
                        orderId: orderId
                    },
                    success(cancelRes) {
                        if(cancelRes.data.success){//取消订单成功
                            // uni.showToast({
                            //     icon:"none",
                            //     title: "已开门,请选购商品"
                            // })
                        }else {//取消订单失败
                            // uni.showToast({
                            //     icon:"none",
                            //     title: res.data.msg
                            // })
                        }
                    }
                })
            },
            //调启摄像头扫描柜机二维码
            scanQrcode: function(){
                let that = this
                
                uni.scanCode({
                    onlyFromCamera: true, // 只允许通过相机扫码
                    success: function (res) {
                        console.log('条码类型:' + res.scanType);
                        console.log('条码内容:' + res.result);
                        uni.reLaunch({
                            url: res.result
                        })
                    }
                })
            },
            // 不使用微信支付分,测试环节使用
            openDoorAndCreateVideoOrder: function(){
                uni.request({
                    method:"GET",
                    url: that.baseUrl + '/f/api/guiji/openDoorAndCreateVideoOrder',
                    data:{
                        guiJiCode: that.guiJiCode,
                        // userid: getApp().globalData.userid,
                        userid: 59, //TODO 测试时的默认用户
                    },
                    success(res) {
                        if(res.data.success){//开门成功
                            uni.showToast({
                                icon:"none",
                                title: "已开门,请选购商品"
                            })
                        }else {//开门失败
                            uni.showToast({
                                icon:"none",
                                title: res.data.msg
                            })
                        }
                    }
                })
                
            }
            
        }
    }
</script>

<style>
.content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40rpx;
}

.logo {
    height: 400rpx;
    width: 400rpx;
    margin-top: 100rpx;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 50rpx;
}
.text-area {
    display: flex;
    justify-content: center;
}

.title {
    font-size: 28rpx;
    color: $u-content-color;
}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值