接口样例.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace app\index\controller;
  3. use EasyWeChat\Factory;
  4. use think\facade\Config;
  5. use think\facade\Log;
  6. use think\facade\Cache;
  7. use app\common\util\wechat\WxAccountConfig;
  8. class Test extends Base
  9. {
  10. private $wechat_config;
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. $this->wechat_config = WxAccountConfig::getMiniProgramConfig();
  15. $this->wechat_config['app_id'] = Config::get('wechat.request.app_id'); //微信appid,推荐写在配置文件或环境变量
  16. $this->wechat_config['secret'] = Config::get('wechat.request.secret'); //微信appid对应secret,推荐写在配置文件或环境变量
  17. }
  18. //二维码被扫判断是否已登录
  19. public function miniProgram_qrcode_login(){
  20. $my_token = trim(input('my_token', ''));//二维码token
  21. //获取缓存中的对应token
  22. $check = Cache::get('mini_logged_'.$my_token);
  23. $id = intval($check);
  24. if($id==0){
  25. $this->error('未登录');
  26. }
  27. /*
  28. * 获取账号信息result 业务部分
  29. */
  30. $this->success($result, "登录成功");
  31. }
  32. //小程序已扫码标记
  33. public function miniProgram_scan(){
  34. $my_token = trim(input('my_token', ''));//二维码token
  35. if($my_token != ''){//通过扫二维码登录
  36. $check = Cache::get('mini_login_'.$my_token);
  37. if(!isset($check)){
  38. //113可以自定义
  39. $this->error('二维码失效', 113);
  40. }
  41. Cache::set('mini_login_'.$my_token, 1, 60);//把二维码标记为已扫
  42. $this->success([], "扫描成功");
  43. }
  44. $this->error("二维码token不能为空");
  45. }
  46. //小程序扫码后请求登录接口
  47. public function miniProgram_login(){
  48. $my_token = trim(input('my_token', ''));//二维码token
  49. $code = trim(input('code', ''));//微信码
  50. if($code == ''){
  51. $this->error("微信码不能为空");
  52. }
  53. $app = Factory::miniProgram($this->wechat_config);
  54. //$result = $app->auth->session($code);
  55. $params = ['code'=>$code];
  56. $result = $app->plugin->httpPostJson('wxa/business/getuserphonenumber', $params);
  57. if (!isset($result['phone_info']['phoneNumber'])) {
  58. $this->error('获取手机号失败');
  59. }
  60. $phone = $result['phone_info']['phoneNumber'];
  61. /*
  62. * 获取登录账号id 业务部分
  63. */
  64. if($my_token != ''){//通过扫二维码登录
  65. $check = Cache::get('mini_logged_'.$my_token);
  66. Cache::set('mini_login_'.$my_token, 1, 60);//把二维码标记为已扫
  67. if(isset($check)){//已登录
  68. $id = intval($check);
  69. /*
  70. * 获取账号信息result 业务部分
  71. */
  72. $this->success($result, "登录成功");
  73. }
  74. else{
  75. $check = Cache::get('mini_login_'.$my_token);
  76. if(!isset($check)){
  77. //113可以自定义
  78. $this->error('二维码失效', 113);
  79. }
  80. else {
  81. /*
  82. * 获取账号信息result 业务部分
  83. */
  84. Cache::set('mini_logged_'.$my_token, $id, 3600*24);//把二维码绑定扫码id
  85. $this->success($result, "登录成功");
  86. }
  87. }
  88. }
  89. //直接授权登录
  90. /*
  91. * 获取账号信息result 业务部分
  92. */
  93. $this->success($result, "登录成功");
  94. }
  95. //页面判断二维码是否已被扫
  96. public function miniProgram_qrcode_check(){
  97. $my_token = trim(input('my_token', ''));//二维码token
  98. //获取缓存中的对应token
  99. $check = Cache::get('mini_login_'.$my_token);
  100. //其中113与114可以自定义
  101. if(!isset($check)){
  102. $this->error('二维码失效', 113);
  103. }
  104. else if($check == 1){
  105. $this->error('二维码已被扫', 114);
  106. }
  107. $this->success([], "二维码未被扫");
  108. }
  109. //获取小程序登录二维码图片文件
  110. public function miniProgram_qrcode(){
  111. $page = trim(input('page', ''));//前端定义的页面
  112. $width = intval(input('width', 600));//前端定义的二维码的宽度,单位 px,最小 280,最大 1280
  113. $env_version = trim(input('env_version', 'release'));//二维码版本,正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
  114. //判断wechat.log文件是否存在,不存在则创建
  115. // $fileHelp = new FileHelp();
  116. // if(!is_file($fileHelp->qrcode_path.'wechat.log')){
  117. // file_put_contents($fileHelp->qrcode_path.'wechat.log', "");
  118. // }
  119. $config = [
  120. 'app_id' => Config::get('wechat.request.app_id'), //微信appid,推荐写在配置文件或环境变量
  121. 'secret' => Config::get('wechat.request.secret'), //微信appid对应secret,推荐写在配置文件或环境变量
  122. // 下面为可选项
  123. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  124. 'response_type' => 'array',
  125. // 'log' => [
  126. // 'level' => 'debug',
  127. // 'file' => $fileHelp->qrcode_path.'wechat.log',
  128. // ],
  129. ];
  130. $filename = time().'_qrcode.jpg';//自定义二维码图片文件名
  131. $app = Factory::miniProgram($config);
  132. $opt = [
  133. 'page' => $page,
  134. 'width' => $width,
  135. 'env_version' => $env_version,
  136. ];
  137. //生成二维码token
  138. $my_token = substr(md5(time()), 0, 16);
  139. //token写入缓存
  140. Cache::set('mini_login_'.$my_token, 0, 60);
  141. $scene = $my_token;
  142. $response = $app->app_code->getUnlimit($scene, $opt);
  143. Log::info(' scene:'.json_encode($scene).' opt:'.json_encode($opt).' response:'.json_encode($response));
  144. // 保存小程序码到文件
  145. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  146. //目录不存在则创建
  147. // if(!is_dir($fileHelp->qrcode_path)){
  148. // mkdir($fileHelp->qrcode_path, 0777);
  149. // }
  150. //保存二维码图片文件
  151. $new_filename = $response->saveAs($fileHelp->qrcode_path, $filename);
  152. //转换图片地址
  153. $result['qrcode_url'] = $fileHelp->getQrcodeUrlByPath($fileHelp->qrcode_path.$new_filename);
  154. $result['my_token'] = $my_token;
  155. //生成二维码成功,返回图片与token
  156. $this->success($result, "生成成功");
  157. }
  158. $this->error("二维码生成失败");
  159. }
  160. }