E企盈营销工具技术服务商 热线:4006-838-530

微信小程序获取获取用户微信运动数据

E企盈直播平台营销卖货系统

官方API地址https://developers.weixin.qq.com/miniprogram/dev/api/wx.getWeRunData.html推荐一篇博文:https://www.cnblogs.com/liqinghuan/p/9184222.html实现步骤1.调用小程序API:wx.login获取code和sessionKey;2.调用小程序API: wx.getWeRunData获取微信运动数据(加密的)3.解密步骤2的数据;这里我后台的代码块用的是PHP语言(PHP是世界上最好的语言)先奉上微信代码块const util = require(‘../../utils/util.js’)Page({  /**   * 页面的初始数据   */  data: {    run_day:[]  },  /**   * 生命周期函数–监听页面加载   */  onLoad: function (options) {    //1、调用小程序API:wx.login获取code和sessionKey;    var that = this;    wx.login({      success: function (resLogin) {        console.log(resLogin.code);        if (resLogin.code) {          wx.request({            url: ‘https://www.****.cn/weirun/onlogin’,            data: {              code: resLogin.code            },            success: function (resSession) {              console.log(resSession);//session_key              //2、调用小程序API: wx.getWeRunData获取微信运动数据(加密的);              wx.getWeRunData({                success(resRun) {                  const encryptedData = resRun                  console.info(resRun);                  //3、解密步骤2的数据;                  wx.request({                    url: ‘https://www.*****.cn/weirun/Decrypt’,                    data: {                      encryptedData: resRun.encryptedData,                      iv: resRun.iv,                      code: resLogin.code                    },                    method: ‘GET’,                    // header: {}, // 设置请求的 header                    success: function (resDecrypt) {                      that.setData({ run_day: resDecrypt.data });                      console.log(that.data.run_day);                    }                  });                }              })            }          })        } else {          console.log(‘获取用户登录态失败!’ + res.errMsg)        }      }    });  },  /**   * 生命周期函数–监听页面初次渲染完成   */  onReady: function () {      },  /**   * 生命周期函数–监听页面显示   */  onShow: function () {      },  /**   * 生命周期函数–监听页面隐藏   */  onHide: function () {  },  /**   * 生命周期函数–监听页面卸载   */  onUnload: function () {  },  /**   * 页面相关事件处理函数–监听用户下拉动作   */  onPullDownRefresh: function () {  },  /**   * 页面上拉触底事件的处理函数   */  onReachBottom: function () {  },  /**   * 用户点击右上角分享   */  onShareAppMessage: function () {  },})接下来是PHP代码块<?phpnamespace AppHttpControllersWeRun;use AppHttpControllersController as BaseController;use AppHttpControllersPHPWXBizDataCrypt;use IlluminateHttpRequest;use IlluminateSupportFacadesRedis;class RunController extends BaseController{    protected $crontab;    protected $user;    protected $appid=”wxa2d68e****32ec56″; //小程序Appid    protected $appsecret=”cc3e33e924677********fb168813426″; //小程序秘钥    public function __construct()    {    }    public function onlogin(Request $request){        $code=$request->all(‘code’);        if (!empty($code)){            $sessionKey=$this->GetSession(“wxa2d68e****32ec56”, “cc3e33e924677********fb168813426”,$code[‘code’]);            $arr=json_decode($sessionKey,true);            if (!empty($sessionKey))            {            //三种方法                //1.                //$_SESSION[$code[‘code’]]=$arr[‘session_key’];                //2.                //session()->put($code[‘code’],$arr[‘session_key’]);                //$sess=session()->get($code[‘code’]);                //3.                Redis::setex(‘code:’.$code[‘code’],3600,$arr[‘session_key’]);                $user=Redis::get(‘code:’.$code[‘code’]);                $data=[                    ‘msg’=>’登录成功’,                    ‘info’=>$user                ];                return json_encode($data);            }        }    }    public function GetSession($appid,$appsecret,$code){        $url= “https://api.weixin.qq.com/sns/jscode2session?appid=”.$appid.”&secret=”.$appsecret.”&js_code=”.$code.”&grant_type=authorization_code”;        $arr = $this->http_curl($url);        if (!empty($arr))        {            return json_encode($arr);        }        return null;    }    protected function http_curl($url, $arr = ”, $type = ‘get’, $res = ‘json’)    {        $ch = curl_init();        //设置curl的参数        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        if ($type == ‘post’) {            curl_setopt($ch, CURLOPT_POST, 1);            curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);        }        //采集        $output = curl_exec($ch);        if ($res == ‘json’) {            if ($err = curl_errno($ch)) {                //要在关闭之前获得curl_errno                curl_close($ch);                //请求失败,返回错误信息                return $err;            } else {                //请求成功                return json_decode($output, true);            }        }    }    /**     * @param Request $request     * @return string     * 微信获取加密的用户步数     */    public function Decrypt(Request $request){        $rqt = $request->all();        $appid=”wxa2d68ed24d32ec56″;        $sessionKey = Redis::get(‘code:’.$rqt[“code”]); //取出OnLogin的sessionKey        $rawData = $this->AES_decrypt($appid,$rqt[‘encryptedData’], $sessionKey, $rqt[‘iv’]);        if (!empty($rawData)){            return json_encode($rawData);        }        return json_encode(“解密失败”);    }    /**     * @param $appid     * @param $encryptedData     * @param $sessionKey     * @param $iv     * @return string     * 解密算法     */    public function AES_decrypt($appid,$encryptedData,$sessionKey,$iv){        $datacrypt=new WXBizDataCrypt($appid,$sessionKey);        $errCode = $datacrypt->decryptData($encryptedData, $iv, $data );

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 微信小程序获取获取用户微信运动数据
分享到: 更多 (0)
E企盈小程序直播营销卖货系统
E企盈直播平台营销卖货系统

评论 抢沙发

E企盈小程序开发

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏