123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace weixin;
- /**
- * @title : 微信小程序基础类
- * @desc :
- * @Author : Mr.Fu
- * @Date : 2023-04-06 19:06:59
- */
- class Wxmin
- {
- public $AccessToken="";
- public function __construct(){
- $this->APPID=sysconfig('wchat.wxmini_user_appid');
- $this->APPSECRET=sysconfig('wchat.wxmini_user_secret');
- $this->getAccesstoken();
- }
- /**
- * @title: 获取ACCESSTOKEN
- * @desc: 描述
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 19:57:45
- */
- public function getAccesstoken()
- {
- $this->AccessToken=Cache('wx_minapp_access_token');
- if(empty($this->AccessToken)){
- $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->APPID."&secret=".$this->APPSECRET;
- $result=file_get_contents($url);
- $result=json_decode($result,true);
- Cache('wx_minapp_access_token',$result['access_token'],$result['expires_in']);
- $this->AccessToken = $result['access_token'];
- }
- return $this->AccessToken;
- }
- /**
- * @title: 获取OPENID
- * @desc: 描述
- * @param {string} {code} {} {用户授权码}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 19:59:21
- */
- public function getopenid(string $code='')
- {
- $API_URL='https://api.weixin.qq.com/sns/jscode2session';
- $url=$API_URL.'?appid='.$this->APPID.'&secret='.$this->APPSECRET.'&js_code='.$code.'&grant_type=authorization_code';
- $d=file_get_contents($url);
- $result=json_decode($d,true);
- if(!isset($result['openid'])){
- return Result(0,'获取OPENID失败');
- }else{
- return Result(1,'获取OPENID成功',$result);
- }
- }
- /**
- * @title: 生成小程序二维码
- * @desc: 描述
- * @param {string} {scene} {} {二维码额外参数}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 20:00:23
- */
- public function CreateCode(string $scene='')
- {
- $url='https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$this->AccessToken;
- $data['scene']=$scene;
- $d=json_encode($data);
- $result=Post_Json($d,$url);
- $DIR1="/uploads/qrcode/".date('Ymd')."/";
- $FILENAME="QR_".$scene.".png";
- $path=root_path().$DIR1;
- if(!file_exists($path)){
- mkdir($path,0777,true);
- }
- $savefile=$path.$FILENAME;
- $return_path=$DIR1.$FILENAME;
- file_put_contents($savefile,$result);
- return $return_path;
- }
- /**
- * @title:解密微信手机信息
- * @desc: 描述
- * @param {string} {sessionKey} {} {用户的sessionKey}
- * @param {array} {ciphertext} {} {待解密文本信息}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 20:02:28
- */
- public function DecodeWxMobile($sessionKey,$ciphertext){
- if(!isset($ciphertext['encryptedData'])){
- return false;
- }
- $encryptedData=$ciphertext['encryptedData'];
- $iv=$ciphertext['iv'];
- $aesKey=base64_decode($sessionKey);
- $aesIV=base64_decode($iv);
- $aesCipher=base64_decode($encryptedData);
- $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
- return $result?json_decode($result,true):false;
- }
- }
|