This commit is contained in:
尖叫 2023-12-08 10:47:13 +08:00
parent 3cd85454ad
commit 81708abe83
2 changed files with 36 additions and 13 deletions

View File

@ -1,8 +1,11 @@
<?php
namespace app\api\controller\food;
use app\BaseController;
use app\api\model\food\User as UserModel;
use think\response\Json;
/**
* API控制器基类
*/
@ -15,6 +18,7 @@ class Controller extends BaseController
protected $user_id; //用户ID
protected $location; //用户位置
protected $mp; //运行平台
/**
* 基类初始化
*/
@ -22,24 +26,26 @@ class Controller extends BaseController
{
$this->getApplet(); //初始化应用数据
}
/**
* 初始化应用数据
*/
private function getApplet()
{
if (!$applet_id = $this->request->param('applet_id')) {
die(hema_json(['code' => 0, 'msg' => '缺少必要的参数applet_id']));
}
if($mp = $this->request->param('mp')){
// if (!$applet_id = $this->request->param('applet_id')) {
// die(hema_json(['code' => 0, 'msg' => '缺少必要的参数applet_id']));
// }
if ($mp = $this->request->param('mp')) {
$this->mp = $mp;
}else{
} else {
$this->mp = 'weixin';
}
$this->applet_id = (int)$applet_id;
$this->applet_id = (int)10001;
$this->user_id = (int)$this->request->param('user_id');
$this->shop_id = (int)$this->request->param('shop_id');
$this->location = (string)$this->request->param('location');
}
/**
* 获取当前用户信息
*/
@ -53,6 +59,7 @@ class Controller extends BaseController
}
return $user;
}
/**
* 返回封装后的 API 数据到客户端
*/
@ -60,6 +67,7 @@ class Controller extends BaseController
{
return json(compact('code', 'msg', 'data'));
}
/**
* 返回操作成功json - 有返回值
*/
@ -67,6 +75,7 @@ class Controller extends BaseController
{
return $this->renderJson(self::JSON_SUCCESS_STATUS, $msg, $data);
}
/**
* 返回操作成功json - 无返回值
*/
@ -74,6 +83,7 @@ class Controller extends BaseController
{
return $this->renderJson(self::JSON_SUCCESS_STATUS, $msg);
}
/**
* 返回操作失败json
*/
@ -81,6 +91,7 @@ class Controller extends BaseController
{
return $this->renderJson(self::JSON_ERROR_STATUS, $msg, $data);
}
/**
* 获取post数据 (数组)
*/
@ -88,6 +99,7 @@ class Controller extends BaseController
{
return $this->request->post(is_null($key) ? '' : $key . '/a');
}
/**
* 获取post数据 (数组)
* @param $key

View File

@ -171,16 +171,27 @@ class User extends Controller
return $this->renderSuccess($data, '成功');
}
public function wxCode()
{
$appid = 'wx89c12dd426a55a2e';
$app_secret = '33e66bcf944f9810abbb5ddd7825403d';
$redirect_uri=$this->request->host().'api/food.user/getWxUserCoupon';
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
}
public function wxLogin()
{
$post = $this->request->post();
if (empty($post['code'])) {
$get = $this->request->get();
if (empty($get['code'])) {
return $this->renderError('授权失败');
}
$appid = 'wx89c12dd426a55a2e';
$app_secret = '33e66bcf944f9810abbb5ddd7825403d';
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$app_secret&code={$post['code']}&grant_type=authorization_code";
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$app_secret&code={$get['code']}&grant_type=authorization_code";
$result = json_decode(Http::get($url), true);
if (isset($result['access_token'])) {
$access_token = $result['access_token'];