Wxmin.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace weixin;
  3. /**
  4. * @title : 微信小程序基础类
  5. * @desc :
  6. * @Author : Mr.Fu
  7. * @Date : 2023-04-06 19:06:59
  8. */
  9. class Wxmin
  10. {
  11. public $AccessToken="";
  12. public function __construct(){
  13. $this->APPID=sysconfig('wchat.wxmini_user_appid');
  14. $this->APPSECRET=sysconfig('wchat.wxmini_user_secret');
  15. $this->getAccesstoken();
  16. }
  17. /**
  18. * @title: 获取ACCESSTOKEN
  19. * @desc: 描述
  20. * @return {*}
  21. * @author: Rock
  22. * @method: POST
  23. * @Date: 2023-04-06 19:57:45
  24. */
  25. public function getAccesstoken()
  26. {
  27. $this->AccessToken=Cache('wx_minapp_access_token');
  28. if(empty($this->AccessToken)){
  29. $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->APPID."&secret=".$this->APPSECRET;
  30. $result=file_get_contents($url);
  31. $result=json_decode($result,true);
  32. Cache('wx_minapp_access_token',$result['access_token'],$result['expires_in']);
  33. $this->AccessToken = $result['access_token'];
  34. }
  35. return $this->AccessToken;
  36. }
  37. /**
  38. * @title: 获取OPENID
  39. * @desc: 描述
  40. * @param {string} {code} {} {用户授权码}
  41. * @return {*}
  42. * @author: Rock
  43. * @method: POST
  44. * @Date: 2023-04-06 19:59:21
  45. */
  46. public function getopenid(string $code='')
  47. {
  48. $API_URL='https://api.weixin.qq.com/sns/jscode2session';
  49. $url=$API_URL.'?appid='.$this->APPID.'&secret='.$this->APPSECRET.'&js_code='.$code.'&grant_type=authorization_code';
  50. $d=file_get_contents($url);
  51. $result=json_decode($d,true);
  52. if(!isset($result['openid'])){
  53. return Result(0,'获取OPENID失败');
  54. }else{
  55. return Result(1,'获取OPENID成功',$result);
  56. }
  57. }
  58. /**
  59. * @title: 生成小程序二维码
  60. * @desc: 描述
  61. * @param {string} {scene} {} {二维码额外参数}
  62. * @return {*}
  63. * @author: Rock
  64. * @method: POST
  65. * @Date: 2023-04-06 20:00:23
  66. */
  67. public function CreateCode(string $scene='')
  68. {
  69. $url='https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$this->AccessToken;
  70. $data['scene']=$scene;
  71. $d=json_encode($data);
  72. $result=Post_Json($d,$url);
  73. $DIR1="/uploads/qrcode/".date('Ymd')."/";
  74. $FILENAME="QR_".$scene.".png";
  75. $path=root_path().$DIR1;
  76. if(!file_exists($path)){
  77. mkdir($path,0777,true);
  78. }
  79. $savefile=$path.$FILENAME;
  80. $return_path=$DIR1.$FILENAME;
  81. file_put_contents($savefile,$result);
  82. return $return_path;
  83. }
  84. /**
  85. * @title:解密微信手机信息
  86. * @desc: 描述
  87. * @param {string} {sessionKey} {} {用户的sessionKey}
  88. * @param {array} {ciphertext} {} {待解密文本信息}
  89. * @return {*}
  90. * @author: Rock
  91. * @method: POST
  92. * @Date: 2023-04-06 20:02:28
  93. */
  94. public function DecodeWxMobile($sessionKey,$ciphertext){
  95. if(!isset($ciphertext['encryptedData'])){
  96. return false;
  97. }
  98. $encryptedData=$ciphertext['encryptedData'];
  99. $iv=$ciphertext['iv'];
  100. $aesKey=base64_decode($sessionKey);
  101. $aesIV=base64_decode($iv);
  102. $aesCipher=base64_decode($encryptedData);
  103. $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
  104. return $result?json_decode($result,true):false;
  105. }
  106. }