最近搜索

小程序 获取 头像 和 昵称 获取 用户微信 资料 获取 手机号

浏览:34
管理员 2025-05-08 09:35




获取头像

 <view class="item" style="align-items: center;">
          <view style="width: 100rpx;">
            头像
          </view>
          <view style="flex: 1; display: flex; justify-content: space-between;  align-items: center; padding-right: 45rpx; ">
            <text></text>
            <image class="img" src="{{baseURL+imgUrl}}" />
            <button class="touming_btn" open-type="chooseAvatar" bindchooseavatar="onChooseImage"></button>
          </view>
        </view>
        
        这个按钮实际上是透明的。
        如果上传成功图片。就替换imgUrl,
        
        
        
            onChooseImage: function (e) {
      var this_ = this;
      wx.showLoading({
        title: '上传中',
        mask: true
      })
      var header = {
        "Content-Type": "multipart/form-data"
      };
      var openid = this.data.openid;
      var formData = {
        'openid': openid
      };
      var tempFilePath = e.detail.avatarUrl;
      uploadFile2(baseURL + "/api/xcx/user/uploadimage", tempFilePath, "image", header, formData).then(res => {
        wx.hideLoading()
        var result = JSON.parse(res.data); //
        console.log(result.url);
        this_.setData({
          imgUrl: result.url
        });
      })
    },
    
    
    
    /**
 *  更新 用户头像。 接受的有openid
 * /api/xcx/user/uploadimage
 *   @RequestParam("file") MultipartFile file,
 *
 */
@ResponseBody
@RequestMapping("/uploadimage")
public JSONObject uploadimage( @RequestParam("openid") String openid,  HttpServletRequest request)throws Exception {
   JSONObject result = new  JSONObject();
   //获取文件需要上传到的路径
   String webPath=request.getServletContext().getRealPath("");
   System.out.println("openid"+openid);//这里输出是 openid[object Undefined]

   XcxUser xcxUser = xcxUserService.findByOpenid(openid);

   if(xcxUser==null){
   }else{
      //判断 路径 有没有包含 xcx_upload   如果有就删除头像吧。
      if(xcxUser.getHeadimgurl().contains("upload")){
         FileUtil.deleteFile(webPath+xcxUser.getHeadimgurl());
      }
   }
   String filePath= "/static/xcx_upload/xcx_user_head/"+ DateUtil.formatDate(new Date(), "yyyyMMdd")+"/";
   FileUtil.makeDirs(webPath+filePath);
   request.setCharacterEncoding("utf-8"); //设置编码
   try {
      StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request;
      Iterator<String> iterator = req.getFileNames();
      while (iterator.hasNext()) {
         HashMap<String, Object> res = new HashMap<String, Object>();
         MultipartFile file = req.getFile(iterator.next());
         // 获取文件名
         String fileNames = file.getOriginalFilename();
         int split = fileNames.lastIndexOf(".");
         //获取上传文件的后缀
         String extName = fileNames.substring(split + 1, fileNames.length());
         //申明UUID
         String uuid = UUID.randomUUID().toString().replace("-", "");
         //组成新的图片名称
         String newName = uuid + "." + extName;
         System.out.println(newName);
         String destPath = webPath+filePath + newName;
         //真正写到磁盘上
         File file1 = new File(destPath);
         OutputStream out = new FileOutputStream(file1);
         out.write(file.getBytes());
         res.put("url", destPath);
         result.put("url", filePath+ newName);

         //更新头像,
         xcxUser.setHeadimgurl(filePath+ newName);
         xcxUserService.updateById(xcxUser);

         out.close();
      }
   } catch (Exception e) {
   }
   return result;
}

image.png

image.png







获取 昵称  

        <view class="item">
          <view style="width: 100rpx;">
            昵称
          </view>
          <view style="flex: 1;  display: flex; justify-content: space-between;      ">
            <text></text>
            <input style="width: 200rpx;" bindinput="nameInput" placeholder="请输入昵称" type="nickname" />
          </view>
        </view>
        
        
            nameInput(e) {
      console.log(e);
      this.setData({
        name: e.detail.value,
      })
    },
    
    点击昵称后,可以触发nameinput 事件。 也可以手动输入一个新昵称。

image.png




小程序 获取 手机号。


  <view style="flex: 1;"> <button  open-type="getPhoneNumber" bindgetphonenumber="click_get_phone" class="btn2">确认授权</button></view>

  
    click_get_phone(e) {
      //获取手机号
      console.log(this.properties.state);
      console.log(e);
      console.log(e.detail.errMsg); //getPhoneNumber:ok     getPhoneNumber:fail user deny
      var msg = e.detail.errMsg;
      var this_ = this;
      if (msg.indexOf("ok") != -1) {
        console.log(e.detail.iv);
        console.log(e.detail.encryptedData);
        console.log(app.globalData.session_key);
        var obj = {};
        obj.encryptedData =e.detail.encryptedData;
        obj.session_key = app.globalData.session_key;
        obj.iv=e.detail.iv;
        get_phone(obj).then(res => {
          console.log(res.data.phoneNumber);
          //更新用户的手机号。
          var obj={};
          obj.id = this_.data.xcxUser.id
          obj.phone=res.data.phoneNumber
          xcx_user_update(obj).then(res=>{
          });
          // this.setData({
          //   click_phone: res.data.phoneNumber
          // })
        })
      } else {}
    },


联系站长

站长微信:xiaomao0055

站长QQ:14496453