502 lines
17 KiB
PHP
Executable File
502 lines
17 KiB
PHP
Executable File
<?php
|
||
|
||
namespace app\api\controller;
|
||
|
||
use app\api\logic\LocalStoreLogic;
|
||
use app\api\logic\MallNewsLogic;
|
||
use app\api\logic\UserLogic;
|
||
use app\common\basics\Api;
|
||
use app\common\enum\NoticeEnum;
|
||
use app\common\model\user\User;
|
||
use app\common\server\ConfigServer;
|
||
use app\common\server\FileServer;
|
||
use app\common\server\JsonServer;
|
||
use app\common\server\UrlServer;
|
||
use app\common\server\WeChatServer;
|
||
use app\common\model\LocalStorePostStation;
|
||
use EasyWeChat\Factory;
|
||
use think\facade\Config as FileConfig;
|
||
use think\facade\Db;
|
||
|
||
class LocalStore extends Api
|
||
{
|
||
public $like_not_need_login = ['makeMyPayImage', 'getLocalShopById', 'getLocalList', 'getLocation', 'getStreetlList', 'lists', 'countData'];
|
||
|
||
public function getLocalStore()
|
||
{
|
||
$lstore_id = $this->request->post('lstore_id');
|
||
$user_id = $this->user_id;
|
||
if ($lstore_id) {
|
||
$result = Db::name("local_store")->where(['lstore_id' => $lstore_id, "user_id" => $user_id])->find();
|
||
} else {
|
||
$result = Db::name("local_store")->where("user_id", $user_id)->select()->toArray();
|
||
}
|
||
$data['local'] = $result;
|
||
$data['set_state'] = 1;
|
||
return JsonServer::success('查询成功', $data);
|
||
}
|
||
|
||
// 查询本地商家
|
||
public function getLocalList()
|
||
{
|
||
$model = new LocalStoreLogic;
|
||
$data = $this->request->param();
|
||
$result = $model->getLocalLists($data);
|
||
return JsonServer::success('查询成功', $result);
|
||
}
|
||
|
||
// 查询店铺街
|
||
public function getStreetlList() {
|
||
$data = $this->request->param();
|
||
$result = (new LocalStoreLogic)->getStreetlList($data);
|
||
return JsonServer::success('查询成功', $result);
|
||
}
|
||
|
||
// 查询本地商户订单
|
||
public function getLocalOrderList()
|
||
{
|
||
$model = new LocalStoreLogic;
|
||
$data = $this->request->param();
|
||
$result = $model->getLocalOrderList($data);
|
||
return JsonServer::success('查询成功', $result);
|
||
}
|
||
|
||
// 查询本地商户用户订单
|
||
public function getLocalUserOrderList()
|
||
{
|
||
$model = new LocalStoreLogic;
|
||
$data = $this->request->param();
|
||
$user_id = $this->user_id;
|
||
$data['user_id'] = $user_id;
|
||
$result = $model->getLocalUserOrderList($data);
|
||
return JsonServer::success('查询成功', $result);
|
||
}
|
||
|
||
public function addLocalStore()
|
||
{
|
||
$user_id = $this->user_id;
|
||
$data = $this->request->param();
|
||
// 调用腾讯地图将本地生活地理位置转换为经纬度
|
||
try {
|
||
$url = 'https://apis.map.qq.com/ws/geocoder/v1?key=' . ConfigServer::get('map', 'tx_map_key') . '&address=' . str_replace(' ', '', $data['address']);
|
||
$content = file_get_contents($url);
|
||
$location = json_decode($content, true);
|
||
if ($location['message'] === 'Success') {
|
||
$data['latitude'] = $location['result']['location']['lat'];
|
||
$data['longitude'] = $location['result']['location']['lng'];
|
||
}
|
||
} catch (\Exception $e) {}
|
||
|
||
// 如果商家没有上传Logo的话,就设置个默认的头像
|
||
$data['shopLogo'] = !empty($data['shopLogo']) ? $data['shopLogo'] : UrlServer::getFileUrl('local_life_shop_logo/20240910182643579523534.png');
|
||
|
||
if (isset($data['lstore_id']) && $data['lstore_id'] != 0) {
|
||
$data['store_state'] = 0;
|
||
$data['update_time'] = date("Y-m-d H:i:s");
|
||
$result = Db::name("local_store")->where("lstore_id", $data['lstore_id'])->save($data);
|
||
} else {
|
||
$data['user_id'] = $user_id;
|
||
$data['create_time'] = date("Y-m-d H:i:s");
|
||
$result = Db::name("local_store")->save($data);
|
||
}
|
||
|
||
if ($result) {
|
||
event('Notice', [
|
||
'scene' => NoticeEnum::LOCAL_SHOP_SETTLED_NOTICE,
|
||
'mobile' => 18436015360, // 张慧娟的手机号码
|
||
'params' => [
|
||
'user_id' => $this->user_id,
|
||
'shopName' => $data['shopName'],
|
||
]
|
||
]);
|
||
return JsonServer::success('上传成功');
|
||
} else {
|
||
return JsonServer::error('上传失败');
|
||
}
|
||
}
|
||
|
||
// 查询协议
|
||
public function checkTreaty()
|
||
{
|
||
$result = Db::name("procurement_treaty")->where("id", 1)->find();
|
||
return JsonServer::success('查询成功', $result);
|
||
|
||
}
|
||
|
||
// 获取本地生活商家列表
|
||
public function getLocalStoreList()
|
||
{
|
||
$page = $this->request->get('page_no', $this->page_no);
|
||
$size = $this->request->get('page_size', $this->page_size);
|
||
$lists = Db::name('local_store')
|
||
->where(['user_id' => $this->user_id, 'store_state' => 1, 'is_delete' => 0])
|
||
->order('create_time', 'desc')
|
||
->page($page, $size)
|
||
->select()
|
||
->toArray();
|
||
|
||
$count = Db::name('local_store')
|
||
->where(['user_id' => $this->user_id, 'store_state' => 1, 'is_delete' => 0])
|
||
->count();
|
||
|
||
$more = is_more($count, $page, $size); //是否有下一页
|
||
$data = [
|
||
'list' => $lists,
|
||
'page_no' => $page,
|
||
'page_size' => $size,
|
||
'count' => $count,
|
||
'more' => $more,
|
||
];
|
||
|
||
return JsonServer::success('查询成功', $data);
|
||
}
|
||
|
||
// 获取本地生活商家列表
|
||
public function getLocalAllStoreList()
|
||
{
|
||
$page = $this->request->get('page_no', $this->page_no);
|
||
$size = $this->request->get('page_size', $this->page_size);
|
||
$lists = Db::name('local_store')
|
||
->where(['store_state' => 1])
|
||
->order('create_time', 'desc')
|
||
->page($page, $size)
|
||
->select()
|
||
->toArray();
|
||
|
||
foreach ($lists as &$v) {
|
||
if (!empty($v['header_image'])) {
|
||
$v['header_image'] = FileConfig::get('aliyunoss.domain') . 'local_life_shop/' . $v['header_image'];
|
||
}
|
||
}
|
||
|
||
$count = Db::name('local_store')
|
||
->where(['user_id' => $this->user_id, 'store_state' => 1])
|
||
->count();
|
||
|
||
$more = is_more($count, $page, $size); //是否有下一页
|
||
$data = [
|
||
'list' => $lists,
|
||
'page_no' => $page,
|
||
'page_size' => $size,
|
||
'count' => $count,
|
||
'more' => $more,
|
||
];
|
||
|
||
return JsonServer::success('查询成功', $data);
|
||
}
|
||
|
||
/**
|
||
*生成本地商家支付二维码
|
||
*/
|
||
public function makeMyPayImage()
|
||
{
|
||
// $user_id = 2;
|
||
$user_id = $this->user_id;
|
||
$lstore_id = $this->request->post('lstore_id');
|
||
$user_msg = User::where("id", $user_id)->where("del", 0)->where("disable", 0)->find();
|
||
if ($user_msg == null) {
|
||
return JsonServer::error('暂无用户');
|
||
}
|
||
$user_local_msg = Db::name("local_store")
|
||
->where("user_id", $user_id)
|
||
->where("lstore_id",$lstore_id)
|
||
->where("store_state", 1)
|
||
->find();
|
||
if ($user_local_msg != null) {
|
||
if ($user_local_msg['pay_image'] != null) {
|
||
$data['pay_image'] = $user_local_msg['pay_image'];
|
||
return JsonServer::success('查询成功', $data);
|
||
} else {
|
||
$url = "bundle_b/pages/local_shop/local_pay";
|
||
$save_dir = ROOT_PATH . '/uploads/localpay/';
|
||
$scene = 'lid=' . $lstore_id;
|
||
$a = time() . $scene;
|
||
$qr_src = $a . '.jpg';
|
||
}
|
||
} else {
|
||
$url = "bundle_b/pages/local_shop/local_pay";
|
||
$save_dir = ROOT_PATH . '/uploads/localpay/';
|
||
$scene = 'lid=' . $lstore_id;
|
||
$a = time() . $scene;
|
||
$qr_src = $a . '.jpg';
|
||
}
|
||
try {
|
||
$config = WeChatServer::getMnpConfig();
|
||
$app = Factory::miniProgram($config);
|
||
|
||
$response = $app->app_code->getUnlimit($scene, [
|
||
'page' => $url
|
||
// 'env_version' => 'trial',
|
||
// 'env_version' => 'develop'
|
||
]);
|
||
// print_r("<pre>");
|
||
// var_dump($response);die;
|
||
if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
|
||
$response->saveAs($save_dir, $a);
|
||
$us = \think\facade\Request::instance()->domain();
|
||
$data['pay_image'] = $us . '/uploads/localpay/' . $qr_src;
|
||
$user_msg = Db::name("local_store")->where("lstore_id", $lstore_id)->update($data);
|
||
if ($user_msg) {
|
||
return JsonServer::success('查询成功', $data);
|
||
} else {
|
||
return JsonServer::error('查询失败');
|
||
}
|
||
}
|
||
if (isset($response['errcode']) && 41030 === $response['errcode']) {
|
||
return '商城小程序未上线或商城小程序页面发布';
|
||
}
|
||
return $response['errmsg'];
|
||
} catch (\EasyWeChat\Kernel\Exceptions\Exception $e) {
|
||
return $e->getMessage();
|
||
}
|
||
}
|
||
|
||
public function getLocalShopById()
|
||
{
|
||
$id = $this->request->post('id');
|
||
$latitude = $this->request->post('latitude', 0);
|
||
$longitude = $this->request->post('longitude', 0);
|
||
$type = $this->request->post('type', 0);
|
||
$user_id = $this->user_id;
|
||
$u = "";
|
||
if ($type == 0) {
|
||
$u = "user_id = $user_id";
|
||
}
|
||
$result = Db::name('local_store')
|
||
->where('lstore_id', $id)
|
||
->where('store_state', 1)
|
||
->where($u)
|
||
->order('create_time desc')
|
||
->find();
|
||
if ($result) {
|
||
$model = new LocalStoreLogic;
|
||
$result['distance'] = round($model->distance($result['latitude'], $result['longitude'], $latitude, $longitude) / 1000, 2) . "公里";
|
||
$result['header_image'] = FileConfig::get('aliyunoss.domain') . 'local_life_shop/' . $result['header_image'];
|
||
$result['post_station'] = LocalStorePostStation::where(['lstore_id' => $id, 'is_delete' => 0])->field('id, province, city, area, address, lat, lng, phone')->select()->toArray();
|
||
|
||
|
||
$result['show_bill'] = true;
|
||
if ($id == 138) {
|
||
// 东龙汇超市不显示去买单按钮
|
||
$result['show_bill'] = false;
|
||
} else {
|
||
// TODO 除了东龙汇超市其它店铺都要暂时关闭
|
||
$result['close'] = array(
|
||
'status' => ConfigServer::get('website', 'show_local_notice'),
|
||
'text' => ConfigServer::get('website', 'local_notice')
|
||
);
|
||
}
|
||
|
||
return JsonServer::success('成功获取', $result);
|
||
}
|
||
|
||
return JsonServer::error('获取失败');
|
||
}
|
||
|
||
public function getPostStationList()
|
||
{
|
||
$id = $this->request->post('id');
|
||
$post_station = LocalStorePostStation::where(['lstore_id' => $id, 'is_delete' => 0])->field('id, province, city, area, address, lat,lng, phone')->select();
|
||
if ($post_station) {
|
||
return JsonServer::success('成功获取', $post_station->toArray());
|
||
}
|
||
|
||
return JsonServer::error('获取失败');
|
||
}
|
||
|
||
public function editLocaShopInfo()
|
||
{
|
||
$id = $this->request->post('id');
|
||
$header_image = $this->request->post('header_image');
|
||
$store_name = $this->request->post('store_name');
|
||
$result = false;
|
||
|
||
if ($id && $header_image) {
|
||
$result = Db::name('local_store')
|
||
->where(['lstore_id' => $id, 'user_id' => $this->user_id])
|
||
->update(['shopLogo' => $header_image, 'update_time' => date('Y-m-d H:i:s')]);
|
||
}
|
||
|
||
if ($id && $store_name) {
|
||
$result = Db::name('local_store')
|
||
->where(['lstore_id' => $id, 'user_id' => $this->user_id])
|
||
->update(['shopName' => $store_name, 'update_time' => date('Y-m-d H:i:s')]);
|
||
}
|
||
|
||
if ($result) {
|
||
return JsonServer::success('修改成功');
|
||
}
|
||
|
||
return JsonServer::error('修改失败');
|
||
}
|
||
|
||
// 本地商家提交记录
|
||
public function record()
|
||
{
|
||
$page = $this->request->get('page_no', $this->page_no);
|
||
$size = $this->request->get('page_size', $this->page_size);
|
||
$data = LocalStoreLogic::record($page, $size, $this->user_id);
|
||
return JsonServer::success('查询成功', $data);
|
||
}
|
||
|
||
//下单购买商品
|
||
public function submitLocalOrder()
|
||
{
|
||
$post = $this->request->post();
|
||
$user_id = $this->user_id;
|
||
$data = LocalStoreLogic::submitOrder($post, $user_id);
|
||
if ($data === -1) {
|
||
return JsonServer::error('库存不足');
|
||
}
|
||
return JsonServer::success('下单成功', $data);
|
||
}
|
||
|
||
// 订单详情
|
||
public function localOrderDetails()
|
||
{
|
||
$post = $this->request->post();
|
||
$data = LocalStoreLogic::orderDetails($post);
|
||
return JsonServer::success('查询成功', $data);
|
||
}
|
||
|
||
// 取消订单
|
||
public function cancelLocalOrder()
|
||
{
|
||
$post = $this->request->post();
|
||
$data = LocalStoreLogic::cancelOrder($post);
|
||
if ($data) {
|
||
return JsonServer::success('取消成功');
|
||
} else {
|
||
return JsonServer::error('取消失败');
|
||
}
|
||
}
|
||
|
||
// 我的订单
|
||
public function localMyOrderList()
|
||
{
|
||
$post = $this->request->post();
|
||
$user_id = $this->user_id;
|
||
$post['user_id'] = $user_id;
|
||
$data = LocalStoreLogic::myOrderList($post);
|
||
return JsonServer::success('查询成功', $data);
|
||
}
|
||
|
||
// 店铺订单列表
|
||
public function localOrderList()
|
||
{
|
||
$post = $this->request->post();
|
||
$data = LocalStoreLogic::orderList($post);
|
||
return JsonServer::success('查询成功', $data);
|
||
}
|
||
|
||
// 腾讯地图逆编码
|
||
public function getLocation()
|
||
{
|
||
try {
|
||
$post = $this->request->post();
|
||
$url = 'https://apis.map.qq.com/ws/geocoder/v1?key=' . ConfigServer::get('map', 'tx_map_key') . '&address=' . $post['address'];
|
||
$result = file_get_contents($url);
|
||
$data = json_decode($result, true);
|
||
if ($data['message'] == 'Success') {
|
||
return JsonServer::success('', $data['result']['location']);
|
||
}
|
||
} catch (\Exception $e) {
|
||
return JsonServer::error($e->getMessage());
|
||
}
|
||
}
|
||
|
||
// 设置自提站点
|
||
public function addPostStation()
|
||
{
|
||
try {
|
||
$post = $this->request->post();
|
||
LocalStoreLogic::addPostStation($post, $this->user_id);
|
||
return JsonServer::success('设置成功');
|
||
} catch (\Exception $e) {
|
||
return JsonServer::error($e->getMessage());
|
||
}
|
||
}
|
||
|
||
// 修改自提站点联系电话
|
||
public function editPostStationPhone()
|
||
{
|
||
try {
|
||
$post = $this->request->post();
|
||
LocalStoreLogic::editPostStationPhone($post);
|
||
return JsonServer::success('设置成功');
|
||
} catch (\Exception $e) {
|
||
return JsonServer::error($e->getMessage());
|
||
}
|
||
}
|
||
|
||
// 删除自提点
|
||
public function delPostStation()
|
||
{
|
||
try {
|
||
$post = $this->request->post();
|
||
LocalStoreLogic::delPostStation($post);
|
||
return JsonServer::success('设置成功');
|
||
} catch (\Exception $e) {
|
||
return JsonServer::error($e->getMessage());
|
||
}
|
||
}
|
||
|
||
// 通过sn搜索用户
|
||
public function searchSn()
|
||
{
|
||
$post = $this->request->post();
|
||
$result = LocalStoreLogic::searchSn($post);
|
||
if ($result) {
|
||
return JsonServer::success('', $result);
|
||
}
|
||
|
||
return JsonServer::error('未找到该用户');
|
||
}
|
||
|
||
// 绑定用户列表
|
||
public function bindList()
|
||
{
|
||
$post = $this->request->post();
|
||
$data = LocalStoreLogic::bindList($post);
|
||
return JsonServer::success('获取成功', $data);
|
||
}
|
||
|
||
// 绑定用户
|
||
public function bindUser()
|
||
{
|
||
try {
|
||
$post = $this->request->post();
|
||
LocalStoreLogic::bindUser($post);
|
||
return JsonServer::success('添加成功');
|
||
} catch (\Exception $e) {
|
||
return JsonServer::error($e->getMessage());
|
||
}
|
||
}
|
||
|
||
// 解绑用户
|
||
public function unBindUser()
|
||
{
|
||
try {
|
||
$post = $this->request->post();
|
||
LocalStoreLogic::unbindUser($post);
|
||
return JsonServer::success('解绑成功');
|
||
} catch (\Exception $e) {
|
||
return JsonServer::error($e->getMessage());
|
||
}
|
||
}
|
||
|
||
// 是否在绑定列表中
|
||
public function inBindList()
|
||
{
|
||
$page = $this->request->get('page_no', $this->page_no);
|
||
$size = $this->request->get('page_size', $this->page_size);
|
||
return JsonServer::success('', LocalStoreLogic::inBindList($this->user_id, $page, $size));
|
||
}
|
||
|
||
// 获取统计数据
|
||
public function countData()
|
||
{
|
||
$form = $this->request->post();
|
||
return JsonServer::success('', LocalStoreLogic::countData($form, $this->user_id));
|
||
}
|
||
} |