251 lines
8.0 KiB
PHP
251 lines
8.0 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller\food;
|
|
|
|
use app\api\controller\food\Controller;
|
|
use app\api\model\food\User as UserModel;
|
|
use app\api\model\Setting;
|
|
use hema\Http;
|
|
use hema\sms\Driver as Sms;
|
|
use think\facade\Cache;
|
|
use think\facade\Db;
|
|
|
|
/**
|
|
* 用户管理
|
|
*/
|
|
class User extends Controller
|
|
{
|
|
/**
|
|
* 获取当前用户信息
|
|
*/
|
|
public function h5()
|
|
{
|
|
$detail = (new UserModel)->where('user_id', 10042)->find();
|
|
Cache::set('food_token_10042', $detail, 3600 * 24 * 7);//登录状态存储7天
|
|
$detail['is_login'] = true;
|
|
return $this->renderSuccess(compact('detail'));
|
|
}
|
|
|
|
/**
|
|
* 获取当前用户信息
|
|
*/
|
|
public function detail()
|
|
{
|
|
// 当前用户信息
|
|
$detail = $this->getUserDetail();
|
|
if (!$this->request->isPost()) {
|
|
return $this->renderSuccess(compact('detail'));
|
|
}
|
|
// 更新记录
|
|
if ($detail->edit($this->request->post())) {
|
|
return $this->renderMsg('更新成功');
|
|
}
|
|
$error = $detail->getError() ?: '更新失败';
|
|
return $this->renderError($error);
|
|
}
|
|
|
|
/**
|
|
* 自动登录/注册
|
|
*/
|
|
public function autoLogin()
|
|
{
|
|
$model = new UserModel;
|
|
if ($detail = $model->autoLogin($this->request->post())) {
|
|
return $this->renderSuccess(compact('detail'), '登录成功');
|
|
}
|
|
$error = $model->getError() ?: '登录失败';
|
|
return $this->renderError($error);
|
|
}
|
|
|
|
/**
|
|
* 获取用户手机号
|
|
*/
|
|
public function getPhoneNumber()
|
|
{
|
|
$userInfo = $this->getUserDetail();
|
|
$model = new UserModel;
|
|
if ($detail = $model->getPhoneNumber($this->request->post(), $userInfo)) {
|
|
return $this->renderSuccess(compact('detail'), '登录成功');
|
|
}
|
|
$error = $model->getError() ?: '手机号获取失败';
|
|
return $this->renderError($error);
|
|
}
|
|
|
|
/**
|
|
* 安全退出
|
|
*/
|
|
public function logout()
|
|
{
|
|
$model = new UserModel;
|
|
if ($model->logout($this->user_id)) {
|
|
return $this->renderMsg('退出成功');
|
|
}
|
|
$error = $model->getError() ?: '退出失败';
|
|
return $this->renderError($error);
|
|
}
|
|
|
|
/**
|
|
* 发动短信验证码
|
|
*/
|
|
public function sendsms()
|
|
{
|
|
$data = $this->request->post();
|
|
$values = Setting::getItem('sms');
|
|
if ($values['gateway'] == '') {
|
|
return $this->renderError('未配置短信平台');
|
|
}
|
|
if ($values['scene']['captcha'] == 0) {
|
|
return $this->renderError('未开启短信验证码');
|
|
}
|
|
if ($error = get_addons_status('sms' . $values['gateway'])) {
|
|
return $this->renderError($error);
|
|
}
|
|
$sms = new Sms($values['gateway']);
|
|
$code = get_captcha();
|
|
if ($sms->sendSms($data['phone'], ['code' => $code])) {
|
|
Cache::set($data['phone'] . '_' . $code, 'sms_captcha', 300);
|
|
return $this->renderSuccess('发送成功');
|
|
}
|
|
$error = $sms->getError() ?: '短信验证码发送失败';
|
|
return $this->renderError($error);
|
|
|
|
}
|
|
|
|
/**
|
|
* 手机验证码登录
|
|
*/
|
|
public function h5CodeLogin()
|
|
{
|
|
$model = new UserModel;
|
|
if ($detail = $model->h5CodeLogin($this->request->post())) {
|
|
return $this->renderSuccess(compact('detail'), '登录成功');
|
|
}
|
|
$error = $model->getError() ?: '登录失败';
|
|
return $this->renderError($error);
|
|
}
|
|
|
|
|
|
/**
|
|
* 公众号 优惠券列表
|
|
* @return \think\response\Json
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function getWxUserCoupon()
|
|
{
|
|
$post = $_GET;
|
|
|
|
if (!isset($post['openid']) && !isset($post['coupon_id'])) {
|
|
return $this->renderSuccess([], '成功');
|
|
}
|
|
$where = [];
|
|
|
|
if (!empty($post['openid'])) {
|
|
$where['openid'] = $post['openid'];
|
|
$openid = $post['openid'];
|
|
|
|
}
|
|
if (!empty($post['coupon_id'])) {
|
|
$where['coupon_user_id'] = $post['coupon_id'];
|
|
if (!empty($where['openid'])) {
|
|
unset($where['openid']);
|
|
}
|
|
}
|
|
|
|
$data = Db::name('food_coupon_user_wx')->where($where)->select();
|
|
$data['wx_openid'] = $openid ?? '';
|
|
return $this->renderSuccess($data, '成功');
|
|
}
|
|
|
|
|
|
/**
|
|
* 公众号 优惠券核销
|
|
* @return \think\response\Json
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function write()
|
|
{
|
|
$post = $this->request->post();
|
|
|
|
$user = Db::name('user_wx')->where(['wx_openid' => $post['openid']])->value('is_role');
|
|
if (empty($user) || $user == 0) {
|
|
return $this->renderError('你没核销权限');
|
|
}
|
|
|
|
$where = [
|
|
// 'openid' => $post['openid'],
|
|
'coupon_user_id' => $post['coupon_id'],
|
|
];
|
|
|
|
$data = Db::name('food_coupon_user_wx')->where($where)->update(['status' => 2]);
|
|
return $this->renderSuccess($data, '成功');
|
|
}
|
|
|
|
|
|
public function is_write()
|
|
{
|
|
$get = $this->request->get();
|
|
$user = Db::name('user_wx')->where(['wx_openid' => $get['openid']])->value('is_role');
|
|
$write_status = empty($user) || $user == 0 ? 0 : 1;
|
|
return $this->renderSuccess($write_status, '成功');
|
|
}
|
|
|
|
|
|
//扫码重定向
|
|
public function wxCode()
|
|
{
|
|
$coupon_id = $this->request->get('coupon_id') ?? '';
|
|
$appid = 'wx89c12dd426a55a2e';
|
|
$wx_redirect_uri = 'https://app.cxhxy.dev.1nww.com/api/food.user/wxLogin?coupon_id=' . $coupon_id;
|
|
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri={$wx_redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
|
|
return $this->renderSuccess($url, '成功');
|
|
}
|
|
|
|
|
|
public function wxLogin()
|
|
{
|
|
|
|
$get = $this->request->get();
|
|
if (empty($get['code'])) {
|
|
return $this->renderError('授权失败');
|
|
}
|
|
$coupon_id = $_GET['coupon_id'] ?? '';
|
|
$appid = 'wx89c12dd426a55a2e';
|
|
$app_secret = '33e66bcf944f9810abbb5ddd7825403d';
|
|
$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'];
|
|
$openid = $result['openid'];
|
|
|
|
$wx_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
|
|
$data = json_decode(Http::get($wx_url), true);
|
|
if (isset($data['headimgurl'])) {
|
|
Db::name('user_wx')->where(['wx_openid' => $openid])->update(['headimgurl' => $data['headimgurl'], 'nickname' => $data['nickname']]);
|
|
$arr = [
|
|
'openid' => $openid,
|
|
'redirect_uri' => 'https://app.cxhxy.dev.1nww.com/#/pages/details/details'
|
|
];
|
|
|
|
|
|
if (!empty($coupon_id)) {
|
|
//优惠券详情
|
|
$url = 'https://app.cxhxy.h5.dev.1nww.com/#/pages/details/details?coupon_id=' . $coupon_id;
|
|
} else {
|
|
//优惠券列表
|
|
$url = 'https://app.cxhxy.h5.dev.1nww.com/#/?openid=' . $openid;
|
|
}
|
|
write_log($url, __DIR__);
|
|
|
|
return redirect($url);
|
|
}
|
|
}
|
|
return $this->renderError('授权失败');
|
|
}
|
|
|
|
|
|
}
|