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

View File

@ -171,16 +171,27 @@ class User extends Controller
return $this->renderSuccess($data, '成功'); 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() public function wxLogin()
{ {
$post = $this->request->post(); $get = $this->request->get();
if (empty($post['code'])) { if (empty($get['code'])) {
return $this->renderError('授权失败'); return $this->renderError('授权失败');
} }
$appid = 'wx89c12dd426a55a2e'; $appid = 'wx89c12dd426a55a2e';
$app_secret = '33e66bcf944f9810abbb5ddd7825403d'; $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); $result = json_decode(Http::get($url), true);
if (isset($result['access_token'])) { if (isset($result['access_token'])) {
$access_token = $result['access_token']; $access_token = $result['access_token'];