微信小程序--登录+获取用户信息+获取手机号+获取位置信息

微信小程序--登录+获取用户信息+获取手机号+获取位置信息

主题部分

1、登录基础代码
wxml:

  <view class="userinfo" style="margin-bottom:50rpx">
    <view class="userinfo" style="margin-top:45%">
      <button wx:if="{{hasUserInfo==1}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo">
      登录
      </button> 
    </view>
    <block wx:if="{{hasUserInfo==2}}">
      <image class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
      <text class="userinfo-nickname">{{userInfo.nickName}}</text>
    </block>
  </view>

js:

  getUserInfo: function (e) {
    console.log("-=-=-=-=-=-=-=-=-=")
    console.log(e.detail.userInfo)
    if (e.detail.userInfo){
      app.globalData.userInfo = e.detail.userInfo
      userInfo = e.detail.userInfo
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: 2
      })
      this.flush();
    }else{
      this.setData({
        hasUserInfo: 1
      })
    }
  }

2、根据wx.login登录获取code,根据code获取openId和sessionKey,每个人的openId都是唯一的。
js代码:

 wx.login({
      success: function (res) {
        if (res.code) {
          console.log("code------"+res.code)
          //根据code获取openId, sessionKey, unionId
          wx.request({
            url: urlIndex+'/minicx/mini/getOpenid?code=' + res.code, 
            method: 'GET',
            success: function (res) {
              openId = res.data.openid;
              sessionKey = res.data.session_key;
              console.log("openId------"+openId);
              console.log("sessionKey------" +sessionKey);
            }
          })
       }
    }
})

后台代码:

	/**
	 * 小程序登录接口
	 * @param code 用户登录凭证
	 * @return
	 */
	@RequestMapping("/getOpenid")
	@ResponseBody
	public String login(String code){
		logger.debug("======小程序登录接口getOpenid=====");
		String appid = PayConfigUtil.MINI_APP_ID;
		String secret = PayConfigUtil.MINI_APP_SECRET;
		String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code";
		JSONObject json  = new JSONObject();
		try {
			json =  HttpClientUtilsmini.httpsRequest(url, "get", null);
		} catch (Exception e) {
			logger.error(ExceptionUtils.getStackTrace(e));
		}
		
		return json.toJSONString();
	}
/** 
     * 发起https请求并获取结果 
     *  
     * @param requestUrl 请求地址 
     * @param requestMethod 请求方式(GET、POST) 
     * @param submitStr 提交的json格式数据 
     * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值) 
     */  
    public static JSONObject httpsRequest(String requestUrl, String requestMethod, String submitStr) throws Exception{  
        JSONObject jsonObject = null;  
        StringBuffer buffer = new StringBuffer();  
        URL url = new URL(requestUrl);  
        HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();  
        httpUrlConn.setDoOutput(true);  
        httpUrlConn.setDoInput(true);  
        httpUrlConn.setUseCaches(false);  
        httpUrlConn.setConnectTimeout(10000);
        httpUrlConn.setReadTimeout(10000);
        // 设置请求方式(GET/POST)  
        httpUrlConn.setRequestMethod(requestMethod.toUpperCase());  
        if ("GET".equalsIgnoreCase(requestMethod))  
            httpUrlConn.connect();  
        // 当有数据需要提交时  
	    if (null != submitStr) {  
        OutputStream outputStream = httpUrlConn.getOutputStream();  
        // 注意编码格式,防止中文乱码  
        outputStream.write(submitStr.getBytes("UTF-8"));  
            outputStream.close();  
        }  
  
        // 将返回的输入流转换成字符串  
        InputStream inputStream = httpUrlConn.getInputStream();  
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");  
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
        String str = null;  
        while ((str = bufferedReader.readLine()) != null) {  
            buffer.append(str);  
        }  
        bufferedReader.close();  
        inputStreamReader.close();  
        // 释放资源  
        inputStream.close();  
        inputStream = null;  
        httpUrlConn.disconnect();  
        jsonObject = JSONObject.parseObject(buffer.toString());  
        return jsonObject;  
    }

3、用户授权后,获取用户加密手机号信息,根据openId和sessionKey将加密手机号解析。
wxml:

<button wx:if="{{!phoneNum}}" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">扫一扫</button>
<button wx:else bindtap="scanCode">扫一扫</button>

js代码:

  //用户点击扫一扫
  scanCode:function(){
    console.log("不需要手机授权的扫一扫")
    this.locationAndScanCode();
  },
  
  //用户点击扫一扫
  getPhoneNumber(e){
      console.log("需要手机授权的扫一扫")
      var that = this;
      console.log(e.detail)
      console.log(e.detail.iv)
      console.log(e.detail.encryptedData)
      //获取用户授权店的手机号,从后台转换
      wx.request({
        url: urlIndex + '/minicx/mini/getPhoneNum', 
        method:'POST',
        data:{
          encrypData: e.detail.encryptedData,
          ivData: e.detail.iv,
          sessionKey: sessionKey
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success:function(res){
          console.log(res.data.phoneNumber);
          that.setData({
            phoneNum: res.data.phoneNumber
          })
          if (res.data.phoneNumber!=null){
            //将刚获取的手机号加上openId一起传到后台保存
            wx.request({
              url: urlIndex + '/minicx/mini/insertPhoneNum',
              method: 'POST',
              data: {
                openId: openId,
                userPhone: res.data.phoneNumber
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                userId = res.data;
              }
            })
          }
          
        }
      })
      
      if (e.detail.encryptedData!=null){
        this.locationAndScanCode();
      }
      
  },

java:

	/**
	 * 获取微信手机号
	 * @param encrypData
	 * @param ivData
	 * @param sessionKey
	 * @return
	 */
	@RequestMapping(value="/getPhoneNum")
	@ResponseBody
	public String getPhoneNum(String encrypData,String ivData,String sessionKey){
		System.out.println("----"+encrypData);
		System.out.println("----"+ivData);
		System.out.println("----"+sessionKey);
		logger.debug("======小程序获取手机号接口getPhoneNum=====");
		JSONObject json = new JSONObject();
		try {
			if(encrypData!=null && ivData!=null && sessionKey!=null
					&& encrypData!="" && ivData!="" && sessionKey!="") {
				json = miniService.getPhoneNum(encrypData, ivData, sessionKey);
			}
		} catch (Exception e) {
			logger.error(ExceptionUtils.getStackTrace(e));
		}
		return json.toJSONString();
	}
	@Override
	public JSONObject getPhoneNum(String encrypData, String ivData,String sessionKey) throws Exception {
        
	    //Thread.sleep(850);
		JSONObject json = new JSONObject();
		byte[] encryp = Base64.decodeBase64(encrypData);
		byte[] iv= Base64.decodeBase64(ivData);
		byte[] key = Base64.decodeBase64(sessionKey);
		AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
        String result = new String(cipher.doFinal(encryp),"UTF-8");
		JSONObject resultJson = JSONObject.parseObject(result);
		String phoneNumber = resultJson.getString("phoneNumber");
		logger.info(phoneNumber);
		json.put("errcode", 0);
		json.put("errmsg", "ok");
		json.put("phoneNumber", phoneNumber);
		return json;
	}

4、位置授权,获取位置信息。
js:

  //位置授权+扫一扫
  locationAndScanCode:function(){
    //获取位置
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        console.log("纬度:" + latitude)
        console.log("经度:" + longitude)
        // 允许从相机和相册扫码
        wx.scanCode({
          success(res) {
            console.log(res.result);
            if (Object.keys(res.result).length == 17) {
              wx.redirectTo({
                url: '../vin/vin?vinCode=' + res.result +'&userId='+userId,
              })
            }else{
              wx.showModal({
                title: '提示',
                content: '扫描失败,请重新扫描!',
                showCancel: false,
                confirmText: '好的',
                success: function (res) {
                }
              })
            }
          },
          fail(res) {
            console.log("扫描失败")
          }
        })
      },
      fail: function () {
        wx.getSetting({
          success: function (res) {
            if (!res.authSetting['scope.userLocation']) {
              wx.showModal({
                title: '',
                content: '请允许系统获取您的定位!',
                confirmText: '授权',
                showCancel: false,
                success: function (res) {
                  if (res.confirm) {
                    wx.openSetting();
                  } else {
                    console.log('用户拒绝了位置授权。');
                  }
                }
              })
            } else {
              //用户已授权,但是获取地理位置失败,提示用户去系统设置中打开定位
              wx.showModal({
                title: 'GPS未开启',
                content: '请在系统设置中打开定位服务!',
                showCancel:false,
                confirmText: '好的',
                success: function (res) {
                }
              })
            }
          }
        })
      }
    })
  },

小坑部分

1、小程序跟后台交互时,post请求必须加上header属性,如下,否则参数传不到后台。

	wx.request({
       url: urlIndex + '/minicx/mini/insertPhoneNum',
        method: 'POST',
        data: {
          openId: openId,
          userPhone: res.data.phoneNumber
        },
        //post请求要加header,不然参数传不到后台
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          userId = res.data;
        }
      })

2、小程序只支持https请求,在本地测试时,可自行购买隧道,申请域名、证书。
链接: https://segmentfault.com/a/1190000011310292.

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序中,开发者可以通过微信提供的API接口实现用户登录获取用户的手机号。 1. 用户登录 用户登录可以使用微信提供的wx.login()接口,该接口会返回一个临时登录凭证code,开发者可以将该code发送到自己的服务器进行验证,以获取用户的openid等信息。 示例代码: ``` wx.login({ success: function(res) { if (res.code) { // 发送 res.code 到后台换取 openId, sessionKey, unionId } else { console.log('登录失败!' + res.errMsg) } } }) ``` 2. 获取用户手机号 获取用户手机号需要先获取用户的授权,可以使用微信提供的wx.getSetting()接口判断用户是否已经授权手机号。如果用户已经授权,则可以使用wx.getUserInfo()接口获取用户信息,其中包括手机号。 示例代码: ``` wx.getSetting({ success(res) { if (!res.authSetting['scope.phoneNumber']) { wx.authorize({ scope: 'scope.phoneNumber', success () { wx.getUserInfo({ success: function(res) { console.log(res) // 可以通过 res.userInfo.phoneNumber 获取用户手机号 } }) } }) } } }) ``` 需要注意的是,获取用户手机号需要用户授权,因此需要在小程序的app.json文件中配置相应的权限,例如: ``` { "mp-weixin": { "appid": "your appid", "permission": { "scope.userLocation": { "desc": "你的位置信息将用于小程序定位" }, "scope.userInfo": { "desc": "你的基本信息将用于小程序登录" }, "scope.userLocationBackground": { "desc": "你的位置信息将在后台运行时持续获取" }, "scope.phoneNumber": { "desc": "获取你的手机号码" } } } } ``` 以上是获取用户手机号的一般流程,开发者可以根据自己的需求进行相应的调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值