feat(api): 全链路后端 + 数据库设计 + E2E 测试(65 项断言全通过)
数据库: - db/schema_phase1.sql 16 张业务表(类目/服务/地址/师傅/订单/流水/取证/报价/评价/券/钱包/提现/结算/消息) - db/seed_phase1.sql 15 类目 + 32 服务项目 + 新人券(幂等) 后端 API (application/api/controller): - Base/Auth/Home/Address/Order/Master/Review - app/common/library/OrderStatus 11 状态机 + 流转矩阵 - application/api/common.php PHP 8.5 兼容垫片(E_DEPRECATED 不再升级为异常) 测试: - tests/e2e_flow.js 用户下单→支付→师傅抢单→履约→验收→结算→评价 全链路 65 项断言 - 含异常用例:越权/重复操作/非法状态流转/2h 未联系自动回流
这个提交包含在:
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 服务地址
|
||||
*/
|
||||
class Address extends Base
|
||||
{
|
||||
/**
|
||||
* 地址列表(默认地址置顶)
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$list = Db::name('address')
|
||||
->where('user_id', $this->uid())
|
||||
->order('is_default desc,id desc')
|
||||
->select();
|
||||
$this->success('', ['list' => $list]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 地址详情
|
||||
*/
|
||||
public function detail()
|
||||
{
|
||||
$id = (int)$this->request->param('id');
|
||||
$row = Db::name('address')->where('id', $id)->where('user_id', $this->uid())->find();
|
||||
if (!$row) {
|
||||
$this->error('地址不存在');
|
||||
}
|
||||
$this->success('', ['detail' => $row]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增 / 编辑地址
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
$id = (int)$this->request->post('id', 0);
|
||||
$data = [
|
||||
'contact' => trim((string)$this->request->post('contact')),
|
||||
'mobile' => trim((string)$this->request->post('mobile')),
|
||||
'province' => trim((string)$this->request->post('province')),
|
||||
'city' => trim((string)$this->request->post('city')),
|
||||
'district' => trim((string)$this->request->post('district')),
|
||||
'address' => trim((string)$this->request->post('address')),
|
||||
'lng' => trim((string)$this->request->post('lng')),
|
||||
'lat' => trim((string)$this->request->post('lat')),
|
||||
'floor' => max(1, (int)$this->request->post('floor', 1)),
|
||||
'has_elevator' => (int)$this->request->post('has_elevator', 1),
|
||||
'is_default' => (int)$this->request->post('is_default', 0),
|
||||
'updatetime' => time(),
|
||||
];
|
||||
|
||||
if ($data['contact'] === '' || $data['mobile'] === '' || $data['address'] === '') {
|
||||
$this->error('请填写联系人、手机号和详细地址');
|
||||
}
|
||||
if (!preg_match('/^1\d{10}$/', $data['mobile'])) {
|
||||
$this->error('手机号格式不正确');
|
||||
}
|
||||
|
||||
if ($id > 0) {
|
||||
$exist = Db::name('address')->where('id', $id)->where('user_id', $this->uid())->find();
|
||||
if (!$exist) {
|
||||
$this->error('地址不存在');
|
||||
}
|
||||
Db::name('address')->where('id', $id)->update($data);
|
||||
} else {
|
||||
$count = Db::name('address')->where('user_id', $this->uid())->count();
|
||||
if ($count >= 20) {
|
||||
$this->error('最多保存 20 个地址');
|
||||
}
|
||||
$data['user_id'] = $this->uid();
|
||||
$data['createtime'] = time();
|
||||
if ($count === 0) {
|
||||
$data['is_default'] = 1;
|
||||
}
|
||||
$id = Db::name('address')->insertGetId($data);
|
||||
}
|
||||
|
||||
if ($data['is_default'] === 1) {
|
||||
Db::name('address')->where('user_id', $this->uid())->where('id', '<>', $id)->update(['is_default' => 0]);
|
||||
}
|
||||
|
||||
$this->success('保存成功', ['id' => (int)$id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设为默认
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function setDefault()
|
||||
{
|
||||
$id = (int)$this->request->post('id');
|
||||
$exist = Db::name('address')->where('id', $id)->where('user_id', $this->uid())->find();
|
||||
if (!$exist) {
|
||||
$this->error('地址不存在');
|
||||
}
|
||||
Db::name('address')->where('user_id', $this->uid())->update(['is_default' => 0]);
|
||||
Db::name('address')->where('id', $id)->update(['is_default' => 1]);
|
||||
$this->success('已设为默认');
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除地址
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
$id = (int)$this->request->post('id');
|
||||
$exist = Db::name('address')->where('id', $id)->where('user_id', $this->uid())->find();
|
||||
if (!$exist) {
|
||||
$this->error('地址不存在');
|
||||
}
|
||||
Db::name('address')->where('id', $id)->delete();
|
||||
$this->success('已删除');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use fast\Random;
|
||||
use think\Db;
|
||||
use think\Validate;
|
||||
|
||||
/**
|
||||
* 登录 / 角色 / 个人信息
|
||||
*/
|
||||
class Auth extends Base
|
||||
{
|
||||
protected $noNeedLogin = ['login'];
|
||||
|
||||
/**
|
||||
* 手机号 + 验证码登录(验证码固定 123456,联调用;接入短信后走 Sms)
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
$mobile = $this->request->post('mobile');
|
||||
$code = $this->request->post('code');
|
||||
if (!$mobile || !$code) {
|
||||
$this->error('请填写手机号和验证码');
|
||||
}
|
||||
if (!Validate::regex($mobile, "^1\d{10}$")) {
|
||||
$this->error('手机号格式不正确');
|
||||
}
|
||||
if ($code !== '123456') {
|
||||
$this->error('验证码不正确');
|
||||
}
|
||||
|
||||
$user = \app\common\model\User::getByMobile($mobile);
|
||||
if ($user) {
|
||||
if ($user['status'] !== 'normal') {
|
||||
$this->error('账号已被禁用');
|
||||
}
|
||||
$this->auth->direct($user['id']);
|
||||
} else {
|
||||
$ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
|
||||
if (!$ret) {
|
||||
$this->error($this->auth->getError() ?: '注册失败');
|
||||
}
|
||||
}
|
||||
|
||||
$this->success('登录成功', [
|
||||
'token' => $this->auth->getToken(),
|
||||
'expires' => 2592000,
|
||||
'userinfo' => $this->profileData(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 我的信息(含角色与师傅状态)
|
||||
*/
|
||||
public function profile()
|
||||
{
|
||||
$this->success('', $this->profileData());
|
||||
}
|
||||
|
||||
/**
|
||||
* 组装个人信息
|
||||
*/
|
||||
protected function profileData()
|
||||
{
|
||||
$uid = $this->uid();
|
||||
$user = Db::name('user')->field('id,nickname,mobile,avatar,money,score,createtime')->where('id', $uid)->find();
|
||||
$master = Db::name('master')->where('user_id', $uid)->find();
|
||||
|
||||
$couponCount = Db::name('user_coupon')->where('user_id', $uid)->where('status', 'unused')->count();
|
||||
$unread = Db::name('message')->where('receive_type', 'user')->where('receive_id', $uid)->where('is_read', 0)->count();
|
||||
|
||||
$masterInfo = null;
|
||||
if ($master) {
|
||||
$masterInfo = [
|
||||
'id' => (int)$master['id'],
|
||||
'realname' => $master['realname'],
|
||||
'verify_status' => $master['verify_status'],
|
||||
'audit_msg' => $master['audit_msg'],
|
||||
'work_status' => $master['work_status'],
|
||||
'level' => (int)$master['level'],
|
||||
'score' => (float)$master['score'],
|
||||
'order_count' => (int)$master['order_count'],
|
||||
'money' => (float)$master['money'],
|
||||
'can_take' => $master['verify_status'] === 'pass',
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => (int)$user['id'],
|
||||
'nickname' => $user['nickname'] ?: ('用户' . substr((string)$user['mobile'], -4)),
|
||||
'mobile' => $user['mobile'],
|
||||
'avatar' => $user['avatar'] ?: '',
|
||||
'balance' => (float)$user['money'],
|
||||
'coupon_count' => (int)$couponCount,
|
||||
'unread_count' => (int)$unread,
|
||||
'is_master' => $master ? true : false,
|
||||
'master' => $masterInfo,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\controller\Api;
|
||||
use app\common\library\OrderStatus;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 业务接口基类
|
||||
* 统一处理:登录用户注入 / 师傅档案 / 订单归属校验 / 状态流转 / 通知
|
||||
*/
|
||||
class Base extends Api
|
||||
{
|
||||
protected $noNeedLogin = [];
|
||||
protected $noNeedRight = '*';
|
||||
|
||||
/** @var mixed 当前登录用户 */
|
||||
protected $user = null;
|
||||
|
||||
/** @var array|false|null 师傅档案:null未查询 / false未入驻 / array已入驻 */
|
||||
protected $master = null;
|
||||
|
||||
protected function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->user = $this->auth->getUser();
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前用户ID
|
||||
*/
|
||||
protected function uid()
|
||||
{
|
||||
return (int)$this->auth->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 师傅档案
|
||||
*/
|
||||
protected function masterInfo($reload = false)
|
||||
{
|
||||
if ($this->master === null || $reload) {
|
||||
$row = Db::name('master')->where('user_id', $this->uid())->find();
|
||||
$this->master = $row ? $row : false;
|
||||
}
|
||||
return $this->master;
|
||||
}
|
||||
|
||||
/**
|
||||
* 要求是已通过审核的师傅
|
||||
*/
|
||||
protected function requireMaster()
|
||||
{
|
||||
$master = $this->masterInfo();
|
||||
if (!$master) {
|
||||
$this->error('请先完成师傅入驻', ['need_apply' => 1], 2001);
|
||||
}
|
||||
if ($master['verify_status'] === 'pending') {
|
||||
$this->error('师傅资质审核中,请耐心等待', ['verify_status' => 'pending'], 2002);
|
||||
}
|
||||
if ($master['verify_status'] === 'reject') {
|
||||
$this->error('资质未通过:' . $master['audit_msg'], ['verify_status' => 'reject'], 2003);
|
||||
}
|
||||
return $master;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取本人订单(越权拦截)
|
||||
* @param int $orderId
|
||||
* @param string $side user|master
|
||||
*/
|
||||
protected function ownOrder($orderId, $side = 'user')
|
||||
{
|
||||
$orderId = (int)$orderId;
|
||||
if ($orderId <= 0) {
|
||||
$this->error('订单参数错误');
|
||||
}
|
||||
$order = Db::name('order')->where('id', $orderId)->find();
|
||||
if (!$order) {
|
||||
$this->error('订单不存在');
|
||||
}
|
||||
if ($side === 'user' && (int)$order['user_id'] !== $this->uid()) {
|
||||
$this->error('无权操作该订单', null, 403);
|
||||
}
|
||||
if ($side === 'master') {
|
||||
$master = $this->requireMaster();
|
||||
if ((int)$order['master_id'] !== (int)$master['id']) {
|
||||
$this->error('无权操作该订单', null, 403);
|
||||
}
|
||||
}
|
||||
return $order;
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单状态流转:校验 → 更新 → 写流水
|
||||
*/
|
||||
protected function transit(array $order, $to, $remark = '', $extra = [], $operatorType = 'system', $operatorId = 0)
|
||||
{
|
||||
$from = (int)$order['status'];
|
||||
if (!OrderStatus::can($from, $to)) {
|
||||
$this->error('订单状态「' . OrderStatus::label($from) . '」不允许变更为「' . OrderStatus::label($to) . '」');
|
||||
}
|
||||
$data = array_merge($extra, [
|
||||
'status' => (int)$to,
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
Db::name('order')->where('id', (int)$order['id'])->update($data);
|
||||
$this->addLog((int)$order['id'], (int)$to, $remark, $operatorType, $operatorId);
|
||||
return array_merge($order, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写订单流水
|
||||
*/
|
||||
protected function addLog($orderId, $status, $remark = '', $operatorType = 'system', $operatorId = 0)
|
||||
{
|
||||
Db::name('order_log')->insert([
|
||||
'order_id' => (int)$orderId,
|
||||
'status' => (int)$status,
|
||||
'remark' => mb_substr((string)$remark, 0, 255),
|
||||
'operator_type' => $operatorType,
|
||||
'operator_id' => (int)$operatorId,
|
||||
'createtime' => time(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息通知
|
||||
*/
|
||||
protected function notify($receiveType, $receiveId, $title, $content, $type = 'order', $orderId = 0)
|
||||
{
|
||||
if (!$receiveId) {
|
||||
return;
|
||||
}
|
||||
Db::name('message')->insert([
|
||||
'receive_type' => $receiveType,
|
||||
'receive_id' => (int)$receiveId,
|
||||
'type' => $type,
|
||||
'title' => mb_substr((string)$title, 0, 100),
|
||||
'content' => mb_substr((string)$content, 0, 500),
|
||||
'order_id' => (int)$orderId,
|
||||
'is_read' => 0,
|
||||
'createtime' => time(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成订单号
|
||||
*/
|
||||
protected function makeOrderNo()
|
||||
{
|
||||
return date('YmdHis') . str_pad((string)mt_rand(0, 999999), 6, '0', STR_PAD_LEFT);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\controller\Api;
|
||||
use app\common\exception\UploadException;
|
||||
use app\common\library\Upload;
|
||||
use app\common\model\Area;
|
||||
use app\common\model\Version;
|
||||
use fast\Random;
|
||||
use think\captcha\Captcha;
|
||||
use think\Config;
|
||||
use think\Hook;
|
||||
|
||||
/**
|
||||
* 公共接口
|
||||
*/
|
||||
class Common extends Api
|
||||
{
|
||||
protected $noNeedLogin = ['init', 'captcha'];
|
||||
protected $noNeedRight = '*';
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
|
||||
if (isset($_SERVER['HTTP_ORIGIN'])) {
|
||||
header('Access-Control-Expose-Headers: __token__');//跨域让客户端获取到
|
||||
}
|
||||
//跨域检测
|
||||
check_cors_request();
|
||||
|
||||
if (!isset($_COOKIE['PHPSESSID'])) {
|
||||
Config::set('session.id', $this->request->server("HTTP_SID"));
|
||||
}
|
||||
parent::_initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载初始化
|
||||
*
|
||||
* @ApiParams (name="version", type="string", required=true, description="版本号")
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
if ($version = $this->request->request('version')) {
|
||||
//配置信息
|
||||
$upload = Config::get('upload');
|
||||
|
||||
$uploaddata = [];
|
||||
$uploaddata['cdnurl'] = $upload['cdnurl'] ?: cdnurl('', true);
|
||||
$uploaddata['uploadurl'] = url('/api/common/upload', '', false, true);
|
||||
|
||||
$content = [
|
||||
'versiondata' => Version::check($version),
|
||||
'uploaddata' => $uploaddata,
|
||||
];
|
||||
$this->success('', $content);
|
||||
} else {
|
||||
$this->error(__('Invalid parameters'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="file", type="file", required=true, description="文件流")
|
||||
*/
|
||||
public function upload()
|
||||
{
|
||||
Config::set('default_return_type', 'json');
|
||||
//必须设定cdnurl为空,否则cdnurl函数计算错误
|
||||
Config::set('upload.cdnurl', '');
|
||||
$chunkid = $this->request->post("chunkid");
|
||||
if ($chunkid) {
|
||||
if (!Config::get('upload.chunking')) {
|
||||
$this->error(__('Chunk file disabled'));
|
||||
}
|
||||
$action = $this->request->post("action");
|
||||
$chunkindex = $this->request->post("chunkindex/d");
|
||||
$chunkcount = $this->request->post("chunkcount/d");
|
||||
$filename = $this->request->post("filename");
|
||||
$method = $this->request->method(true);
|
||||
if ($action == 'merge') {
|
||||
$attachment = null;
|
||||
//合并分片文件
|
||||
try {
|
||||
$upload = new Upload();
|
||||
$attachment = $upload->merge($chunkid, $chunkcount, $filename);
|
||||
} catch (UploadException $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
$this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
|
||||
} elseif ($method == 'clean') {
|
||||
//删除冗余的分片文件
|
||||
try {
|
||||
$upload = new Upload();
|
||||
$upload->clean($chunkid);
|
||||
} catch (UploadException $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
$this->success();
|
||||
} else {
|
||||
//上传分片文件
|
||||
//默认普通上传文件
|
||||
$file = $this->request->file('file');
|
||||
try {
|
||||
$upload = new Upload($file);
|
||||
$upload->chunk($chunkid, $chunkindex, $chunkcount);
|
||||
} catch (UploadException $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
} else {
|
||||
$attachment = null;
|
||||
//默认普通上传文件
|
||||
$file = $this->request->file('file');
|
||||
try {
|
||||
$upload = new Upload($file);
|
||||
$attachment = $upload->upload();
|
||||
} catch (UploadException $e) {
|
||||
$this->error($e->getMessage());
|
||||
} catch (\Exception $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
|
||||
$this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证码
|
||||
* @ApiParams (name="id", type="string", required=true, description="要生成验证码的标识")
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function captcha($id = "")
|
||||
{
|
||||
\think\Config::set([
|
||||
'captcha' => array_merge(config('captcha'), [
|
||||
'fontSize' => 44,
|
||||
'imageH' => 150,
|
||||
'imageW' => 350,
|
||||
])
|
||||
]);
|
||||
$captcha = new Captcha((array)Config::get('captcha'));
|
||||
return $captcha->entry($id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\controller\Api;
|
||||
|
||||
/**
|
||||
* 示例接口
|
||||
*/
|
||||
class Demo extends Api
|
||||
{
|
||||
|
||||
//如果$noNeedLogin为空表示所有接口都需要登录才能请求
|
||||
//如果$noNeedRight为空表示所有接口都需要验证权限才能请求
|
||||
//如果接口已经设置无需登录,那也就无需鉴权了
|
||||
//
|
||||
// 无需登录的接口,*表示全部
|
||||
protected $noNeedLogin = ['test', 'test1'];
|
||||
// 无需鉴权的接口,*表示全部
|
||||
protected $noNeedRight = ['test2'];
|
||||
|
||||
/**
|
||||
* 测试方法
|
||||
*
|
||||
* @ApiTitle (测试名称)
|
||||
* @ApiSummary (测试描述信息)
|
||||
* @ApiMethod (POST)
|
||||
* @ApiRoute (/api/demo/test/id/{id}/name/{name})
|
||||
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
|
||||
* @ApiParams (name="id", type="integer", required=true, description="会员ID")
|
||||
* @ApiParams (name="name", type="string", required=true, description="用户名")
|
||||
* @ApiParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
|
||||
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
|
||||
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
|
||||
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
|
||||
* @ApiReturn ({
|
||||
'code':'1',
|
||||
'msg':'返回成功'
|
||||
})
|
||||
*/
|
||||
public function test()
|
||||
{
|
||||
$this->success('返回成功', $this->request->param());
|
||||
}
|
||||
|
||||
/**
|
||||
* 无需登录的接口
|
||||
*
|
||||
*/
|
||||
public function test1()
|
||||
{
|
||||
$this->success('返回成功', ['action' => 'test1']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 需要登录的接口
|
||||
*
|
||||
*/
|
||||
public function test2()
|
||||
{
|
||||
$this->success('返回成功', ['action' => 'test2']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 需要登录且需要验证有相应组的权限
|
||||
*
|
||||
*/
|
||||
public function test3()
|
||||
{
|
||||
$this->success('返回成功', ['action' => 'test3']);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\controller\Api;
|
||||
use app\common\library\Ems as Emslib;
|
||||
use app\common\model\User;
|
||||
use think\Hook;
|
||||
|
||||
/**
|
||||
* 邮箱验证码接口
|
||||
*/
|
||||
class Ems extends Api
|
||||
{
|
||||
protected $noNeedLogin = '*';
|
||||
protected $noNeedRight = '*';
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送验证码
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="email", type="string", required=true, description="邮箱")
|
||||
* @ApiParams (name="event", type="string", required=true, description="事件名称")
|
||||
*/
|
||||
public function send()
|
||||
{
|
||||
$email = $this->request->post("email");
|
||||
$event = $this->request->post("event");
|
||||
$event = $event ? $event : 'register';
|
||||
|
||||
if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
$this->error(__('邮箱格式错误'));
|
||||
}
|
||||
if (!preg_match("/^[a-z0-9_\-]{3,30}\$/i", $event)) {
|
||||
$this->error(__('事件名称错误'));
|
||||
}
|
||||
|
||||
$last = Emslib::get($email, $event);
|
||||
if ($last && time() - $last['createtime'] < 60) {
|
||||
$this->error(__('发送频繁'));
|
||||
}
|
||||
|
||||
$ipSendTotal = \app\common\model\Ems::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
|
||||
if ($ipSendTotal >= 5) {
|
||||
$this->error(__('发送频繁'));
|
||||
}
|
||||
|
||||
if ($event) {
|
||||
$userinfo = User::getByEmail($email);
|
||||
if ($event == 'register' && $userinfo) {
|
||||
//已被注册
|
||||
$this->error(__('已被注册'));
|
||||
} elseif (in_array($event, ['changeemail']) && $userinfo) {
|
||||
//被占用
|
||||
$this->error(__('已被占用'));
|
||||
} elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
|
||||
//未注册
|
||||
$this->error(__('未注册'));
|
||||
}
|
||||
}
|
||||
$ret = Emslib::send($email, null, $event);
|
||||
if ($ret) {
|
||||
$this->success(__('发送成功'));
|
||||
} else {
|
||||
$this->error(__('发送失败'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测验证码
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="email", type="string", required=true, description="邮箱")
|
||||
* @ApiParams (name="event", type="string", required=true, description="事件名称")
|
||||
* @ApiParams (name="captcha", type="string", required=true, description="验证码")
|
||||
*/
|
||||
public function check()
|
||||
{
|
||||
$email = $this->request->post("email");
|
||||
$event = $this->request->post("event");
|
||||
$event = $event ? $event : 'register';
|
||||
$captcha = $this->request->post("captcha");
|
||||
|
||||
if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
$this->error(__('邮箱格式错误'));
|
||||
}
|
||||
if (!preg_match("/^[a-z0-9_\-]{3,30}\$/i", $event)) {
|
||||
$this->error(__('事件名称错误'));
|
||||
}
|
||||
|
||||
if (!preg_match("/^[a-z0-9]{4,6}\$/i", $captcha)) {
|
||||
$this->error(__('验证码格式错误'));
|
||||
}
|
||||
|
||||
if ($event) {
|
||||
$userinfo = User::getByEmail($email);
|
||||
if ($event == 'register' && $userinfo) {
|
||||
//已被注册
|
||||
$this->error(__('已被注册'));
|
||||
} elseif (in_array($event, ['changeemail']) && $userinfo) {
|
||||
//被占用
|
||||
$this->error(__('已被占用'));
|
||||
} elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
|
||||
//未注册
|
||||
$this->error(__('未注册'));
|
||||
}
|
||||
}
|
||||
$ret = Emslib::check($email, $captcha, $event);
|
||||
if ($ret) {
|
||||
$this->success(__('成功'));
|
||||
} else {
|
||||
$this->error(__('验证码不正确'));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 首页 / 服务类目 / 服务项目 / 费用试算
|
||||
*/
|
||||
class Home extends Base
|
||||
{
|
||||
protected $noNeedLogin = ['index', 'cats', 'services', 'detail'];
|
||||
|
||||
/**
|
||||
* 首页聚合:类目 + 热门服务 + 我的角标
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$cats = Db::name('service_cat')
|
||||
->field('id,name,icon')
|
||||
->where('status', 'normal')
|
||||
->order('weigh desc,id asc')
|
||||
->select();
|
||||
|
||||
$hot = Db::name('service')
|
||||
->field('id,cat_id,name,price_type,base_price,max_price,unit,image,sales')
|
||||
->where('status', 'normal')
|
||||
->order('sales desc,id asc')
|
||||
->limit(6)
|
||||
->select();
|
||||
|
||||
$data = [
|
||||
'cats' => $cats,
|
||||
'hot' => $this->formatServices($hot),
|
||||
'banners' => [
|
||||
['id' => 1, 'title' => '家装季 · 双11狂欢大促', 'desc' => '下单立减 · 满额再返券', 'date' => '10.23 - 11.15'],
|
||||
],
|
||||
'badge' => ['coupon' => 0, 'unread' => 0],
|
||||
];
|
||||
|
||||
if ($this->auth->isLogin()) {
|
||||
$uid = $this->uid();
|
||||
$data['badge']['coupon'] = (int)Db::name('user_coupon')->where('user_id', $uid)->where('status', 'unused')->count();
|
||||
$data['badge']['unread'] = (int)Db::name('message')->where('receive_type', 'user')->where('receive_id', $uid)->where('is_read', 0)->count();
|
||||
}
|
||||
|
||||
$this->success('', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 全部类目
|
||||
*/
|
||||
public function cats()
|
||||
{
|
||||
$cats = Db::name('service_cat')
|
||||
->field('id,name,icon')
|
||||
->where('status', 'normal')
|
||||
->order('weigh desc,id asc')
|
||||
->select();
|
||||
$this->success('', ['list' => $cats]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 类目下的服务列表
|
||||
*/
|
||||
public function services()
|
||||
{
|
||||
$catId = (int)$this->request->param('cat_id');
|
||||
$keyword = trim((string)$this->request->param('keyword'));
|
||||
if ($catId <= 0 && $keyword === '') {
|
||||
$this->error('请选择服务类目');
|
||||
}
|
||||
$query = Db::name('service')->where('status', 'normal');
|
||||
if ($catId > 0) {
|
||||
$query->where('cat_id', $catId);
|
||||
}
|
||||
if ($keyword !== '') {
|
||||
$query->where('name', 'like', '%' . $keyword . '%');
|
||||
}
|
||||
$list = $query->order('weigh desc,sales desc,id asc')->select();
|
||||
$this->success('', ['list' => $this->formatServices($list)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务详情
|
||||
*/
|
||||
public function detail()
|
||||
{
|
||||
$id = (int)$this->request->param('id');
|
||||
$service = Db::name('service')->where('id', $id)->where('status', 'normal')->find();
|
||||
if (!$service) {
|
||||
$this->error('服务不存在');
|
||||
}
|
||||
$cat = Db::name('service_cat')->where('id', $service['cat_id'])->find();
|
||||
$service['cat_name'] = $cat ? $cat['name'] : '';
|
||||
$this->success('', ['detail' => $this->formatService($service)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 费用试算(基础价 + 楼层费 + 加急费 - 优惠)
|
||||
*/
|
||||
public function calc()
|
||||
{
|
||||
$serviceId = (int)$this->request->param('service_id');
|
||||
$service = Db::name('service')->where('id', $serviceId)->where('status', 'normal')->find();
|
||||
if (!$service) {
|
||||
$this->error('服务不存在');
|
||||
}
|
||||
$result = $this->calcAmount($service, [
|
||||
'floor' => (int)$this->request->param('floor', 1),
|
||||
'has_elevator' => (int)$this->request->param('has_elevator', 1),
|
||||
'urgent' => (int)$this->request->param('urgent', 0),
|
||||
], (int)$this->request->param('coupon_id', 0), $this->uid());
|
||||
|
||||
$this->success('', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 金额计算(下单与试算共用,保证前后端口径一致)
|
||||
*/
|
||||
public static function calcAmount($service, $params, $couponId = 0, $uid = 0)
|
||||
{
|
||||
$base = (float)$service['base_price'];
|
||||
$floor = max(0, (int)$params['floor']);
|
||||
$freeFloor = (int)$service['free_floor'];
|
||||
$floorFee = (float)$service['floor_fee'];
|
||||
|
||||
$floorAmount = 0;
|
||||
if ((int)$params['has_elevator'] === 0 && $floor > $freeFloor) {
|
||||
$floorAmount = ($floor - $freeFloor) * $floorFee;
|
||||
}
|
||||
|
||||
$urgentAmount = (int)$params['urgent'] === 1 ? round($base * 0.2, 2) : 0;
|
||||
$total = round($base + $floorAmount + $urgentAmount, 2);
|
||||
|
||||
$discount = 0;
|
||||
$couponInfo = null;
|
||||
if ($couponId > 0 && $uid > 0) {
|
||||
$uc = Db::name('user_coupon')->where('id', $couponId)->where('user_id', $uid)->where('status', 'unused')->find();
|
||||
if ($uc) {
|
||||
$coupon = Db::name('coupon')->where('id', $uc['coupon_id'])->where('status', 'normal')->find();
|
||||
$notExpired = $coupon && (int)$coupon['end_time'] === 0 ? true : ($coupon && $coupon['end_time'] > time());
|
||||
if ($notExpired && $total >= (float)$coupon['min_amount']) {
|
||||
$discount = $coupon['type'] === 'cash'
|
||||
? (float)$coupon['amount']
|
||||
: round($total * (1 - (float)$coupon['discount']), 2);
|
||||
$discount = min($discount, $total);
|
||||
$couponInfo = ['id' => (int)$uc['id'], 'name' => $coupon['name']];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'price_type' => $service['price_type'],
|
||||
'base_amount' => round($base, 2),
|
||||
'floor_amount' => round($floorAmount, 2),
|
||||
'urgent_amount' => round($urgentAmount, 2),
|
||||
'total_amount' => $total,
|
||||
'discount_amount' => round($discount, 2),
|
||||
'pay_amount' => max(0, round($total - $discount, 2)),
|
||||
'coupon' => $couponInfo,
|
||||
'free_floor' => $freeFloor,
|
||||
'floor_fee' => round($floorFee, 2),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表格式化
|
||||
*/
|
||||
public function formatServices($list)
|
||||
{
|
||||
$ret = [];
|
||||
foreach ($list as $row) {
|
||||
$ret[] = $this->formatService($row);
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 单项格式化(价格文案统一由此输出)
|
||||
*/
|
||||
public function formatService($row)
|
||||
{
|
||||
$type = $row['price_type'];
|
||||
$priceText = '¥' . (float)$row['base_price'] . '起';
|
||||
if ($type === 'range') {
|
||||
$priceText = '¥' . (float)$row['base_price'] . '-' . (float)$row['max_price'] . '/' . $row['unit'];
|
||||
} elseif ($type === 'quote') {
|
||||
$priceText = '¥' . (float)$row['base_price'] . '起 · 师傅报价';
|
||||
}
|
||||
return [
|
||||
'id' => (int)$row['id'],
|
||||
'cat_id' => (int)$row['cat_id'],
|
||||
'name' => $row['name'],
|
||||
'cat_name' => isset($row['cat_name']) ? $row['cat_name'] : '',
|
||||
'image' => $row['image'],
|
||||
'price_type' => $type,
|
||||
'price_type_text' => $type === 'fixed' ? '一口价' : ($type === 'quote' ? '师傅报价' : '按面积计价'),
|
||||
'base_price' => (float)$row['base_price'],
|
||||
'max_price' => (float)$row['max_price'],
|
||||
'unit' => $row['unit'],
|
||||
'price_text' => $priceText,
|
||||
'sales' => (int)$row['sales'],
|
||||
'floor_fee' => isset($row['floor_fee']) ? (float)$row['floor_fee'] : 0,
|
||||
'free_floor' => isset($row['free_floor']) ? (int)$row['free_floor'] : 0,
|
||||
'need_model' => isset($row['need_model']) ? (int)$row['need_model'] : 1,
|
||||
'description' => isset($row['description']) ? $row['description'] : '',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\controller\Api;
|
||||
|
||||
/**
|
||||
* 首页接口
|
||||
*/
|
||||
class Index extends Api
|
||||
{
|
||||
protected $noNeedLogin = ['*'];
|
||||
protected $noNeedRight = ['*'];
|
||||
|
||||
/**
|
||||
* 首页
|
||||
*
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->success('请求成功');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,501 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\library\OrderStatus;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 师傅端:入驻 / 订单大厅 / 抢单 / 履约流转 / 收入
|
||||
*/
|
||||
class Master extends Base
|
||||
{
|
||||
/**
|
||||
* 师傅入驻申请
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function apply()
|
||||
{
|
||||
$data = [
|
||||
'realname' => trim((string)$this->request->post('realname')),
|
||||
'mobile' => trim((string)$this->request->post('mobile')),
|
||||
'idcard_no' => trim((string)$this->request->post('idcard_no')),
|
||||
'idcard_front' => (string)$this->request->post('idcard_front'),
|
||||
'idcard_back' => (string)$this->request->post('idcard_back'),
|
||||
'cert_images' => (string)$this->request->post('cert_images'),
|
||||
'city' => trim((string)$this->request->post('city')),
|
||||
'skills' => (array)$this->request->post('skills/a', []),
|
||||
];
|
||||
|
||||
if ($data['realname'] === '' || $data['mobile'] === '' || $data['idcard_no'] === '') {
|
||||
$this->error('请填写真实姓名、手机号和身份证号');
|
||||
}
|
||||
if (!preg_match('/^1\d{10}$/', $data['mobile'])) {
|
||||
$this->error('手机号格式不正确');
|
||||
}
|
||||
if (empty($data['skills'])) {
|
||||
$this->error('请至少选择一个服务类目');
|
||||
}
|
||||
|
||||
$exist = Db::name('master')->where('user_id', $this->uid())->find();
|
||||
if ($exist && $exist['verify_status'] === 'pass') {
|
||||
$this->error('您已是认证师傅,无需重复入驻');
|
||||
}
|
||||
|
||||
$save = [
|
||||
'realname' => $data['realname'],
|
||||
'mobile' => $data['mobile'],
|
||||
'idcard_no' => substr($data['idcard_no'], 0, 6) . '********' . substr($data['idcard_no'], -4),
|
||||
'idcard_front' => $data['idcard_front'],
|
||||
'idcard_back' => $data['idcard_back'],
|
||||
'cert_images' => $data['cert_images'],
|
||||
'city' => $data['city'],
|
||||
'verify_status' => 'pending',
|
||||
'audit_msg' => '',
|
||||
'updatetime' => time(),
|
||||
];
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
if ($exist) {
|
||||
$masterId = (int)$exist['id'];
|
||||
Db::name('master')->where('id', $masterId)->update($save);
|
||||
Db::name('master_skill')->where('master_id', $masterId)->delete();
|
||||
} else {
|
||||
$save['user_id'] = $this->uid();
|
||||
$save['createtime'] = time();
|
||||
$masterId = Db::name('master')->insertGetId($save);
|
||||
}
|
||||
foreach ($data['skills'] as $catId) {
|
||||
Db::name('master_skill')->insert([
|
||||
'master_id' => $masterId,
|
||||
'cat_id' => (int)$catId,
|
||||
'status' => 'normal',
|
||||
'createtime' => time(),
|
||||
]);
|
||||
}
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error('提交失败:' . $e->getMessage());
|
||||
}
|
||||
|
||||
$this->success('提交成功,请等待审核', ['master_id' => (int)$masterId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 我的师傅档案
|
||||
*/
|
||||
public function info()
|
||||
{
|
||||
$master = $this->masterInfo();
|
||||
if (!$master) {
|
||||
$this->success('未入驻', ['applied' => false]);
|
||||
}
|
||||
$skills = Db::name('master_skill')->alias('s')
|
||||
->join('service_cat c', 'c.id = s.cat_id', 'LEFT')
|
||||
->field('s.cat_id,c.name,c.icon')
|
||||
->where('s.master_id', $master['id'])
|
||||
->select();
|
||||
|
||||
$todo = Db::name('order')->where('master_id', $master['id'])
|
||||
->where('status', 'in', [OrderStatus::ACCEPTED, OrderStatus::SCHEDULED, OrderStatus::ARRIVED, OrderStatus::WORKING])
|
||||
->count();
|
||||
$pendingVerify = Db::name('order')->where('master_id', $master['id'])
|
||||
->where('status', OrderStatus::PENDING_VERIFY)->count();
|
||||
|
||||
$this->success('', [
|
||||
'applied' => true,
|
||||
'master' => [
|
||||
'id' => (int)$master['id'],
|
||||
'realname' => $master['realname'],
|
||||
'mobile' => $master['mobile'],
|
||||
'avatar' => $master['avatar'],
|
||||
'city' => $master['city'],
|
||||
'level' => (int)$master['level'],
|
||||
'score' => (float)$master['score'],
|
||||
'order_count' => (int)$master['order_count'],
|
||||
'accept_rate' => (float)$master['accept_rate'],
|
||||
'ontime_rate' => (float)$master['ontime_rate'],
|
||||
'work_status' => $master['work_status'],
|
||||
'verify_status' => $master['verify_status'],
|
||||
'audit_msg' => $master['audit_msg'],
|
||||
'money' => (float)$master['money'],
|
||||
'todo_count' => (int)$todo,
|
||||
'verify_count' => (int)$pendingVerify,
|
||||
],
|
||||
'skills' => $skills,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 接单开关
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function toggleWork()
|
||||
{
|
||||
$master = $this->requireMaster();
|
||||
$status = $this->request->post('work_status', '');
|
||||
if (!in_array($status, ['on', 'off'], true)) {
|
||||
$this->error('参数错误');
|
||||
}
|
||||
Db::name('master')->where('id', $master['id'])->update(['work_status' => $status, 'updatetime' => time()]);
|
||||
$this->success($status === 'on' ? '已开启接单' : '已暂停接单', ['work_status' => $status]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单大厅:待接单订单(按技能过滤,急单优先)
|
||||
*/
|
||||
public function hall()
|
||||
{
|
||||
$master = $this->requireMaster();
|
||||
$this->recycleExpired();
|
||||
|
||||
$catIds = Db::name('master_skill')->where('master_id', $master['id'])->column('cat_id');
|
||||
$page = max(1, (int)$this->request->param('page', 1));
|
||||
$limit = min(30, max(1, (int)$this->request->param('limit', 10)));
|
||||
|
||||
$query = Db::name('order')->where('status', OrderStatus::PENDING_ACCEPT)->where('master_id', 0);
|
||||
if (!empty($catIds)) {
|
||||
$query->where('cat_id', 'in', $catIds);
|
||||
}
|
||||
|
||||
$total = $query->count();
|
||||
$list = $query->order('urgent desc, id desc')->page($page, $limit)->select();
|
||||
|
||||
foreach ($list as &$row) {
|
||||
$row['appoint_text'] = $row['appoint_time'] ? date('m-d H:i', (int)$row['appoint_time']) : '尽快上门';
|
||||
$row['pay_amount'] = (float)$row['pay_amount'];
|
||||
$row['urgent'] = (int)$row['urgent'];
|
||||
$row['mobile'] = substr_replace($row['mobile'], '****', 3, 4);
|
||||
$row['address'] = $row['city'] . $row['district'] . $row['address'];
|
||||
$row['status_text'] = OrderStatus::label($row['status']);
|
||||
}
|
||||
unset($row);
|
||||
|
||||
$this->success('', ['list' => $list, 'total' => (int)$total, 'page' => $page]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 抢单(条件更新保证并发安全)
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function grab()
|
||||
{
|
||||
$master = $this->requireMaster();
|
||||
if ($master['work_status'] !== 'on') {
|
||||
$this->error('您已暂停接单,请先开启');
|
||||
}
|
||||
$orderId = (int)$this->request->post('order_id');
|
||||
$order = Db::name('order')->where('id', $orderId)->find();
|
||||
if (!$order) {
|
||||
$this->error('订单不存在');
|
||||
}
|
||||
if ((int)$order['status'] !== OrderStatus::PENDING_ACCEPT || (int)$order['master_id'] !== 0) {
|
||||
$this->error('手慢了,该订单已被接走');
|
||||
}
|
||||
|
||||
$affected = Db::name('order')
|
||||
->where('id', $orderId)
|
||||
->where('status', OrderStatus::PENDING_ACCEPT)
|
||||
->where('master_id', 0)
|
||||
->update([
|
||||
'master_id' => (int)$master['id'],
|
||||
'status' => OrderStatus::ACCEPTED,
|
||||
'accept_time' => time(),
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
if (!$affected) {
|
||||
$this->error('手慢了,该订单已被接走');
|
||||
}
|
||||
|
||||
$this->addLog($orderId, OrderStatus::ACCEPTED, '师傅接单:' . $master['realname'], 'master', (int)$master['id']);
|
||||
$this->notify('user', (int)$order['user_id'], '师傅已接单', '师傅 ' . $master['realname'] . ' 已接单,将尽快与您联系', 'order', $orderId);
|
||||
|
||||
$this->success('抢单成功,请在 2 小时内联系客户', [
|
||||
'order_id' => $orderId,
|
||||
'deadline' => time() + 7200,
|
||||
'user_mobile' => $order['mobile'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 标记已联系客户(2 小时考核)
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function contact()
|
||||
{
|
||||
$order = $this->ownOrder((int)$this->request->post('order_id'), 'master');
|
||||
if ((int)$order['contact_time'] > 0) {
|
||||
$this->success('已记录');
|
||||
}
|
||||
Db::name('order')->where('id', $order['id'])->update(['contact_time' => time(), 'updatetime' => time()]);
|
||||
$this->addLog((int)$order['id'], (int)$order['status'], '师傅已联系客户', 'master', (int)$order['master_id']);
|
||||
$this->success('已记录联系时间');
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置预约上门时间
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function schedule()
|
||||
{
|
||||
$order = $this->ownOrder((int)$this->request->post('order_id'), 'master');
|
||||
$appointTime = (int)$this->request->post('appoint_time');
|
||||
if ($appointTime <= 0) {
|
||||
$this->error('请选择上门时间');
|
||||
}
|
||||
if ((int)$order['status'] === OrderStatus::ACCEPTED) {
|
||||
$this->transit($order, OrderStatus::SCHEDULED, '已与客户约定上门时间', [
|
||||
'appoint_time' => $appointTime,
|
||||
'contact_time' => (int)$order['contact_time'] ?: time(),
|
||||
], 'master', (int)$order['master_id']);
|
||||
} else {
|
||||
Db::name('order')->where('id', $order['id'])->update(['appoint_time' => $appointTime, 'updatetime' => time()]);
|
||||
}
|
||||
$this->notify('user', (int)$order['user_id'], '师傅已预约上门', '约定时间:' . date('m-d H:i', $appointTime), 'order', (int)$order['id']);
|
||||
$this->success('预约成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 到达现场(定位打卡)
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function arrive()
|
||||
{
|
||||
$order = $this->ownOrder((int)$this->request->post('order_id'), 'master');
|
||||
$lng = (string)$this->request->post('lng', '');
|
||||
$lat = (string)$this->request->post('lat', '');
|
||||
$extra = ['arrive_time' => time()];
|
||||
if ($lng !== '' && $lat !== '') {
|
||||
$extra['lng'] = $lng;
|
||||
$extra['lat'] = $lat;
|
||||
}
|
||||
$this->transit($order, OrderStatus::ARRIVED, '师傅已到达现场', $extra, 'master', (int)$order['master_id']);
|
||||
$this->notify('user', (int)$order['user_id'], '师傅已到达', '师傅已到达服务地址,请做好准备', 'order', (int)$order['id']);
|
||||
$this->success('已打卡到达');
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始服务
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function startJob()
|
||||
{
|
||||
$order = $this->ownOrder((int)$this->request->post('order_id'), 'master');
|
||||
$this->transit($order, OrderStatus::WORKING, '师傅开始施工', ['start_time' => time()], 'master', (int)$order['master_id']);
|
||||
$this->notify('user', (int)$order['user_id'], '师傅已开工', '师傅已开始服务,请留意进度', 'order', (int)$order['id']);
|
||||
$this->success('已开工');
|
||||
}
|
||||
|
||||
/**
|
||||
* 完工并提交现场取证 → 进入待验收
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function finish()
|
||||
{
|
||||
$order = $this->ownOrder((int)$this->request->post('order_id'), 'master');
|
||||
$images = (array)$this->request->post('images/a', []);
|
||||
$damage = trim((string)$this->request->post('damage_remark', ''));
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$this->transit($order, OrderStatus::PENDING_VERIFY, '师傅完工,等待客户验收', [
|
||||
'finish_time' => time(),
|
||||
], 'master', (int)$order['master_id']);
|
||||
|
||||
foreach ($images as $url) {
|
||||
if (!$url) {
|
||||
continue;
|
||||
}
|
||||
Db::name('order_media')->insert([
|
||||
'order_id' => (int)$order['id'],
|
||||
'master_id' => (int)$order['master_id'],
|
||||
'stage' => $damage !== '' ? 'damage' : 'finish',
|
||||
'type' => 'image',
|
||||
'url' => (string)$url,
|
||||
'watermark' => date('Y-m-d H:i') . ' ' . $order['address'],
|
||||
'remark' => $damage !== '' ? mb_substr($damage, 0, 255) : '',
|
||||
'createtime' => time(),
|
||||
]);
|
||||
}
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error('提交失败:' . $e->getMessage());
|
||||
}
|
||||
|
||||
$code = (string)$order['verify_code'];
|
||||
$this->notify('user', (int)$order['user_id'], '师傅已完工,待您验收', '验收码:' . $code . ',请核对后确认完成', 'order', (int)$order['id']);
|
||||
$this->success('已提交完工,等待客户验收', ['verify_code' => $code]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 我的订单(师傅视角)
|
||||
*/
|
||||
public function orders()
|
||||
{
|
||||
$master = $this->requireMaster();
|
||||
$status = $this->request->param('status', 'active');
|
||||
$page = max(1, (int)$this->request->param('page', 1));
|
||||
$limit = min(30, max(1, (int)$this->request->param('limit', 10)));
|
||||
|
||||
$query = Db::name('order')->where('master_id', $master['id']);
|
||||
if ($status === 'active') {
|
||||
$query->where('status', 'in', [OrderStatus::ACCEPTED, OrderStatus::SCHEDULED, OrderStatus::ARRIVED, OrderStatus::WORKING]);
|
||||
} elseif ($status === 'verify') {
|
||||
$query->where('status', OrderStatus::PENDING_VERIFY);
|
||||
} elseif ($status === 'completed') {
|
||||
$query->where('status', OrderStatus::COMPLETED);
|
||||
}
|
||||
|
||||
$total = $query->count();
|
||||
$list = $query->order('id desc')->page($page, $limit)->select();
|
||||
foreach ($list as &$row) {
|
||||
$row['status_text'] = OrderStatus::label($row['status']);
|
||||
$row['pay_amount'] = (float)$row['pay_amount'];
|
||||
$row['appoint_text'] = $row['appoint_time'] ? date('m-d H:i', (int)$row['appoint_time']) : '尽快上门';
|
||||
$row['urgent'] = (int)$row['urgent'];
|
||||
$row['masked_mobile'] = substr_replace($row['mobile'], '****', 3, 4);
|
||||
}
|
||||
unset($row);
|
||||
|
||||
$this->success('', ['list' => $list, 'total' => (int)$total, 'page' => $page]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收入概览
|
||||
*/
|
||||
public function income()
|
||||
{
|
||||
$master = $this->requireMaster();
|
||||
$todayStart = strtotime(date('Y-m-d'));
|
||||
$todayIncome = Db::name('wallet_log')->where('user_type', 'master')->where('user_id', $master['id'])
|
||||
->where('type', 'settle')->where('createtime', '>=', $todayStart)->sum('amount');
|
||||
$totalIncome = Db::name('wallet_log')->where('user_type', 'master')->where('user_id', $master['id'])
|
||||
->where('type', 'settle')->sum('amount');
|
||||
$logs = Db::name('wallet_log')->where('user_type', 'master')->where('user_id', $master['id'])
|
||||
->order('id desc')->limit(20)->select();
|
||||
|
||||
$this->success('', [
|
||||
'money' => (float)$master['money'],
|
||||
'frozen_money' => (float)$master['frozen_money'],
|
||||
'today_income' => round((float)$todayIncome, 2),
|
||||
'total_income' => round((float)$totalIncome, 2),
|
||||
'order_count' => (int)$master['order_count'],
|
||||
'logs' => $logs,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 报价(报价竞价单)
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function quote()
|
||||
{
|
||||
$master = $this->requireMaster();
|
||||
$orderId = (int)$this->request->post('order_id');
|
||||
$amount = (float)$this->request->post('amount');
|
||||
$desc = trim((string)$this->request->post('description', ''));
|
||||
$hours = min(72, max(1, (int)$this->request->post('valid_hours', 24)));
|
||||
|
||||
$order = Db::name('order')->where('id', $orderId)->find();
|
||||
if (!$order) {
|
||||
$this->error('订单不存在');
|
||||
}
|
||||
if ((int)$order['status'] !== OrderStatus::PENDING_ACCEPT) {
|
||||
$this->error('该订单已不在报价阶段');
|
||||
}
|
||||
if ($amount <= 0) {
|
||||
$this->error('请填写报价金额');
|
||||
}
|
||||
if (Db::name('order_quote')->where('order_id', $orderId)->where('master_id', $master['id'])->find()) {
|
||||
$this->error('您已报价,请勿重复提交');
|
||||
}
|
||||
|
||||
Db::name('order_quote')->insert([
|
||||
'order_id' => $orderId,
|
||||
'master_id' => (int)$master['id'],
|
||||
'amount' => round($amount, 2),
|
||||
'description' => mb_substr($desc, 0, 500),
|
||||
'valid_hours' => $hours,
|
||||
'expire_time' => time() + $hours * 3600,
|
||||
'status' => 'wait',
|
||||
'createtime' => time(),
|
||||
]);
|
||||
$this->success('报价已提交');
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请提现
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function withdraw()
|
||||
{
|
||||
$master = $this->requireMaster();
|
||||
$amount = round((float)$this->request->post('amount'), 2);
|
||||
if ($amount < 10) {
|
||||
$this->error('单次提现金额不少于 10 元');
|
||||
}
|
||||
if ($amount > (float)$master['money']) {
|
||||
$this->error('可提现余额不足');
|
||||
}
|
||||
$fee = round($amount * 0.006, 2);
|
||||
$arrive = round($amount - $fee, 2);
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
Db::name('withdraw')->insert([
|
||||
'master_id' => (int)$master['id'],
|
||||
'amount' => $amount,
|
||||
'fee' => $fee,
|
||||
'arrive_amount' => $arrive,
|
||||
'channel' => $this->request->post('channel', 'wechat'),
|
||||
'account' => (string)$this->request->post('account', ''),
|
||||
'status' => 'wait',
|
||||
'createtime' => time(),
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
$newMoney = round((float)$master['money'] - $amount, 2);
|
||||
Db::name('master')->where('id', $master['id'])->update([
|
||||
'money' => $newMoney,
|
||||
'frozen_money' => round((float)$master['frozen_money'] + $amount, 2),
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
Db::name('wallet_log')->insert([
|
||||
'user_type' => 'master', 'user_id' => (int)$master['id'], 'type' => 'withdraw',
|
||||
'amount' => -$amount, 'balance' => $newMoney, 'order_id' => 0,
|
||||
'remark' => '提现申请(手续费 ¥' . $fee . ')', 'createtime' => time(),
|
||||
]);
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error('提现申请失败:' . $e->getMessage());
|
||||
}
|
||||
|
||||
$this->success('提现申请已提交,等待审核', ['arrive_amount' => $arrive]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 超 2 小时未联系客户 → 订单自动回流大厅
|
||||
*/
|
||||
protected function recycleExpired()
|
||||
{
|
||||
$rows = Db::name('order')
|
||||
->where('status', OrderStatus::ACCEPTED)
|
||||
->where('contact_time', 0)
|
||||
->where('accept_time', '<', time() - 7200)
|
||||
->limit(50)
|
||||
->select();
|
||||
foreach ($rows as $row) {
|
||||
Db::name('order')->where('id', $row['id'])->update([
|
||||
'master_id' => 0,
|
||||
'status' => OrderStatus::PENDING_ACCEPT,
|
||||
'accept_time' => null,
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
$this->addLog($row['id'], OrderStatus::PENDING_ACCEPT, '超 2 小时未联系客户,订单自动回流大厅', 'system', 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,356 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\library\OrderStatus;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 用户端订单
|
||||
*/
|
||||
class Order extends Base
|
||||
{
|
||||
/**
|
||||
* 状态字典(供前端渲染筛选 Tab)
|
||||
*/
|
||||
public function statusList()
|
||||
{
|
||||
$this->success('', ['list' => OrderStatus::all()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下单(创建待支付订单)
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$serviceId = (int)$this->request->post('service_id');
|
||||
$addressId = (int)$this->request->post('address_id');
|
||||
$appointTime = (int)$this->request->post('appoint_time', 0);
|
||||
$urgent = (int)$this->request->post('urgent', 0);
|
||||
$couponId = (int)$this->request->post('coupon_id', 0);
|
||||
$goodsModel = trim((string)$this->request->post('goods_model'));
|
||||
$goodsNum = max(1, (int)$this->request->post('goods_num', 1));
|
||||
$remark = trim((string)$this->request->post('remark'));
|
||||
$goodsImages = $this->request->post('goods_images', '');
|
||||
|
||||
$service = Db::name('service')->where('id', $serviceId)->where('status', 'normal')->find();
|
||||
if (!$service) {
|
||||
$this->error('服务不存在或已下架');
|
||||
}
|
||||
|
||||
$address = Db::name('address')->where('id', $addressId)->where('user_id', $this->uid())->find();
|
||||
if (!$address) {
|
||||
$this->error('请选择服务地址');
|
||||
}
|
||||
if ($appointTime > 0 && $appointTime < time()) {
|
||||
$this->error('预约时间不能早于当前时间');
|
||||
}
|
||||
|
||||
// 金额统一由 Home::calcAmount 计算,保证与试算口径一致
|
||||
$amount = Home::calcAmount($service, [
|
||||
'floor' => (int)$address['floor'],
|
||||
'has_elevator' => (int)$address['has_elevator'],
|
||||
'urgent' => $urgent,
|
||||
], $couponId, $this->uid());
|
||||
|
||||
$orderNo = $this->makeOrderNo();
|
||||
$verifyCode = (string)mt_rand(1000, 9999);
|
||||
|
||||
$id = Db::name('order')->insertGetId([
|
||||
'order_no' => $orderNo,
|
||||
'user_id' => $this->uid(),
|
||||
'master_id' => 0,
|
||||
'cat_id' => (int)$service['cat_id'],
|
||||
'service_id' => (int)$service['id'],
|
||||
'service_name' => $service['name'],
|
||||
'service_image' => $service['image'],
|
||||
'price_type' => $service['price_type'],
|
||||
'goods_images' => is_array($goodsImages) ? json_encode($goodsImages, JSON_UNESCAPED_UNICODE) : (string)$goodsImages,
|
||||
'goods_model' => mb_substr($goodsModel, 0, 100),
|
||||
'goods_num' => $goodsNum,
|
||||
'floor' => (int)$address['floor'],
|
||||
'has_elevator' => (int)$address['has_elevator'],
|
||||
'urgent' => $urgent ? 1 : 0,
|
||||
'remark' => mb_substr($remark, 0, 500),
|
||||
'contact' => $address['contact'],
|
||||
'mobile' => $address['mobile'],
|
||||
'province' => $address['province'],
|
||||
'city' => $address['city'],
|
||||
'district' => $address['district'],
|
||||
'address' => $address['address'],
|
||||
'lng' => $address['lng'],
|
||||
'lat' => $address['lat'],
|
||||
'appoint_time' => $appointTime ?: null,
|
||||
'base_amount' => $amount['base_amount'],
|
||||
'floor_amount' => $amount['floor_amount'],
|
||||
'urgent_amount' => $amount['urgent_amount'],
|
||||
'discount_amount' => $amount['discount_amount'],
|
||||
'total_amount' => $amount['total_amount'],
|
||||
'pay_amount' => $amount['pay_amount'],
|
||||
'status' => OrderStatus::PENDING_PAY,
|
||||
'pay_status' => 0,
|
||||
'coupon_id' => $amount['coupon'] ? (int)$amount['coupon']['id'] : 0,
|
||||
'verify_code' => $verifyCode,
|
||||
'createtime' => time(),
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
|
||||
$this->addLog($id, OrderStatus::PENDING_PAY, '用户下单', 'user', $this->uid());
|
||||
|
||||
$this->success('下单成功', [
|
||||
'order_id' => (int)$id,
|
||||
'order_no' => $orderNo,
|
||||
'amount' => $amount,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付(联调模拟支付,pay_type=mock;上线换微信支付回调)
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function pay()
|
||||
{
|
||||
$orderId = (int)$this->request->post('order_id');
|
||||
$payType = $this->request->post('pay_type', 'mock');
|
||||
$order = $this->ownOrder($orderId, 'user');
|
||||
|
||||
if ((int)$order['pay_status'] === 1) {
|
||||
$this->error('订单已支付,请勿重复支付');
|
||||
}
|
||||
if ((int)$order['status'] !== OrderStatus::PENDING_PAY) {
|
||||
$this->error('订单状态不允许支付');
|
||||
}
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
Db::name('order')->where('id', $orderId)->update([
|
||||
'pay_status' => 1,
|
||||
'pay_type' => $payType,
|
||||
'pay_time' => time(),
|
||||
'transaction_id' => 'MOCK' . date('YmdHis') . mt_rand(1000, 9999),
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
$this->transit($order, OrderStatus::PENDING_ACCEPT, '支付成功,订单已进入接单大厅', [], 'user', $this->uid());
|
||||
$this->addLog($orderId, OrderStatus::PENDING_ACCEPT, '订单进入师傅接单大厅', 'system', 0);
|
||||
|
||||
// 核销优惠券
|
||||
if ((int)$order['coupon_id'] > 0) {
|
||||
Db::name('user_coupon')->where('id', (int)$order['coupon_id'])->update([
|
||||
'status' => 'used',
|
||||
'order_id' => $orderId,
|
||||
'use_time' => time(),
|
||||
]);
|
||||
}
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error('支付失败:' . $e->getMessage());
|
||||
}
|
||||
|
||||
$this->success('支付成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 我的订单列表
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$status = $this->request->param('status', 'all');
|
||||
$page = max(1, (int)$this->request->param('page', 1));
|
||||
$limit = min(50, max(1, (int)$this->request->param('limit', 10)));
|
||||
|
||||
$query = Db::name('order')->where('user_id', $this->uid());
|
||||
if ($status === 'active') {
|
||||
$query->where('status', 'in', OrderStatus::activeValues());
|
||||
} elseif ($status === 'completed') {
|
||||
$query->where('status', OrderStatus::COMPLETED);
|
||||
} elseif ($status === 'cancelled') {
|
||||
$query->where('status', OrderStatus::CANCELLED);
|
||||
} elseif ($status !== 'all' && $status !== '') {
|
||||
$query->where('status', (int)$status);
|
||||
}
|
||||
|
||||
$total = $query->count();
|
||||
$list = $query->order('id desc')->page($page, $limit)->select();
|
||||
foreach ($list as &$row) {
|
||||
$row = $this->formatOrder($row);
|
||||
}
|
||||
unset($row);
|
||||
|
||||
$this->success('', ['list' => $list, 'total' => (int)$total, 'page' => $page]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单详情(含状态轨迹与师傅信息)
|
||||
*/
|
||||
public function detail()
|
||||
{
|
||||
$orderId = (int)$this->request->param('id');
|
||||
$order = $this->ownOrder($orderId, 'user');
|
||||
|
||||
$logs = Db::name('order_log')->where('order_id', $orderId)->order('id asc')->select();
|
||||
foreach ($logs as &$log) {
|
||||
$log['status_text'] = OrderStatus::label($log['status']);
|
||||
$log['time_text'] = date('m-d H:i', (int)$log['createtime']);
|
||||
}
|
||||
unset($log);
|
||||
|
||||
$master = null;
|
||||
if ((int)$order['master_id'] > 0) {
|
||||
$m = Db::name('master')->where('id', $order['master_id'])->find();
|
||||
if ($m) {
|
||||
$master = [
|
||||
'id' => (int)$m['id'],
|
||||
'realname' => $m['realname'],
|
||||
'mobile' => substr_replace($m['mobile'], '****', 3, 4),
|
||||
'score' => (float)$m['score'],
|
||||
'order_count' => (int)$m['order_count'],
|
||||
'level' => (int)$m['level'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$media = Db::name('order_media')->where('order_id', $orderId)->order('id asc')->select();
|
||||
|
||||
$this->success('', [
|
||||
'order' => $this->formatOrder($order),
|
||||
'master' => $master,
|
||||
'logs' => $logs,
|
||||
'media' => $media,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消订单(已支付则模拟退款)
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function cancel()
|
||||
{
|
||||
$orderId = (int)$this->request->post('order_id');
|
||||
$reason = trim((string)$this->request->post('reason', '用户取消'));
|
||||
$order = $this->ownOrder($orderId, 'user');
|
||||
|
||||
if (!OrderStatus::canCancelByUser($order['status'])) {
|
||||
$this->error('当前状态(' . OrderStatus::label($order['status']) . ')不可取消,请联系客服');
|
||||
}
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$extra = ['cancel_reason' => mb_substr($reason, 0, 255)];
|
||||
if ((int)$order['pay_status'] === 1) {
|
||||
$extra['pay_status'] = 2;
|
||||
$extra['refund_amount'] = $order['pay_amount'];
|
||||
}
|
||||
$this->transit($order, OrderStatus::CANCELLED, '用户取消:' . $reason, $extra, 'user', $this->uid());
|
||||
|
||||
if ((int)$order['coupon_id'] > 0) {
|
||||
Db::name('user_coupon')->where('id', (int)$order['coupon_id'])->update([
|
||||
'status' => 'unused', 'order_id' => 0, 'use_time' => null,
|
||||
]);
|
||||
}
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error('取消失败:' . $e->getMessage());
|
||||
}
|
||||
|
||||
$this->success('订单已取消');
|
||||
}
|
||||
|
||||
/**
|
||||
* 客户验收(验收码可选校验)→ 生成结算并给师傅入账
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function verify()
|
||||
{
|
||||
$orderId = (int)$this->request->post('order_id');
|
||||
$code = trim((string)$this->request->post('verify_code', ''));
|
||||
$order = $this->ownOrder($orderId, 'user');
|
||||
|
||||
if ((int)$order['status'] !== OrderStatus::PENDING_VERIFY) {
|
||||
$this->error('当前状态(' . OrderStatus::label($order['status']) . ')不可验收');
|
||||
}
|
||||
if ($code !== '' && $code !== (string)$order['verify_code']) {
|
||||
$this->error('验收码不正确');
|
||||
}
|
||||
|
||||
$masterId = (int)$order['master_id'];
|
||||
$payAmount = (float)$order['pay_amount'];
|
||||
$rate = 10.00;
|
||||
$commission = round($payAmount * $rate / 100, 2);
|
||||
$settleAmount = round($payAmount - $commission, 2);
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$this->transit($order, OrderStatus::COMPLETED, '客户验收通过,订单完成', [
|
||||
'verify_time' => time(),
|
||||
'settle_status' => 1,
|
||||
'settle_amount' => $settleAmount,
|
||||
], 'user', $this->uid());
|
||||
|
||||
Db::name('settle')->insert([
|
||||
'order_id' => $orderId,
|
||||
'master_id' => $masterId,
|
||||
'order_amount' => $payAmount,
|
||||
'commission_rate' => $rate,
|
||||
'commission' => $commission,
|
||||
'settle_amount' => $settleAmount,
|
||||
'status' => 'settled',
|
||||
'settle_time' => time(),
|
||||
'createtime' => time(),
|
||||
]);
|
||||
|
||||
if ($masterId > 0) {
|
||||
$master = Db::name('master')->where('id', $masterId)->lock(true)->find();
|
||||
$newMoney = round((float)$master['money'] + $settleAmount, 2);
|
||||
Db::name('master')->where('id', $masterId)->update([
|
||||
'money' => $newMoney,
|
||||
'order_count' => (int)$master['order_count'] + 1,
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
Db::name('wallet_log')->insert([
|
||||
'user_type' => 'master',
|
||||
'user_id' => $masterId,
|
||||
'type' => 'settle',
|
||||
'amount' => $settleAmount,
|
||||
'balance' => $newMoney,
|
||||
'order_id' => $orderId,
|
||||
'remark' => '订单结算入账',
|
||||
'createtime' => time(),
|
||||
]);
|
||||
$this->notify('master', (int)$master['user_id'], '订单结算入账', '订单 ' . $order['order_no'] . ' 已验收,收入 ¥' . $settleAmount, 'order', $orderId);
|
||||
}
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error('验收失败:' . $e->getMessage());
|
||||
}
|
||||
|
||||
$this->success('验收成功,订单已完成');
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单输出格式化
|
||||
*/
|
||||
protected function formatOrder($row)
|
||||
{
|
||||
$status = (int)$row['status'];
|
||||
$appoint = (int)$row['appoint_time'];
|
||||
return array_merge($row, [
|
||||
'id' => (int)$row['id'],
|
||||
'status' => $status,
|
||||
'status_text' => OrderStatus::label($status),
|
||||
'can_cancel' => OrderStatus::canCancelByUser($status),
|
||||
'can_verify' => $status === OrderStatus::PENDING_VERIFY,
|
||||
'can_review' => $status === OrderStatus::COMPLETED && (int)$row['is_review'] === 0,
|
||||
'can_pay' => $status === OrderStatus::PENDING_PAY,
|
||||
'pay_amount' => (float)$row['pay_amount'],
|
||||
'total_amount' => (float)$row['total_amount'],
|
||||
'appoint_text' => $appoint ? date('m-d H:i', $appoint) : '尽快上门',
|
||||
'create_text' => date('Y-m-d H:i', (int)$row['createtime']),
|
||||
'urgent' => (int)$row['urgent'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\library\OrderStatus;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 评价:用户提交 / 师傅收到的评价
|
||||
*/
|
||||
class Review extends Base
|
||||
{
|
||||
protected $noNeedLogin = ['masterList'];
|
||||
|
||||
/**
|
||||
* 提交评价
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$orderId = (int)$this->request->post('order_id');
|
||||
$score = (int)$this->request->post('score', 5);
|
||||
$content = trim((string)$this->request->post('content'));
|
||||
$tags = $this->request->post('tags', '');
|
||||
$images = $this->request->post('images', '');
|
||||
$anonymous = (int)$this->request->post('is_anonymous', 0);
|
||||
|
||||
$order = $this->ownOrder($orderId, 'user');
|
||||
if ((int)$order['status'] !== OrderStatus::COMPLETED) {
|
||||
$this->error('订单未完成,暂不能评价');
|
||||
}
|
||||
if ((int)$order['is_review'] === 1) {
|
||||
$this->error('该订单已评价');
|
||||
}
|
||||
if ($score < 1 || $score > 5) {
|
||||
$this->error('请选择 1-5 星');
|
||||
}
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
Db::name('review')->insert([
|
||||
'order_id' => $orderId,
|
||||
'user_id' => $this->uid(),
|
||||
'master_id' => (int)$order['master_id'],
|
||||
'score' => $score,
|
||||
'tags' => is_array($tags) ? json_encode($tags, JSON_UNESCAPED_UNICODE) : (string)$tags,
|
||||
'content' => mb_substr($content, 0, 500),
|
||||
'images' => is_array($images) ? json_encode($images, JSON_UNESCAPED_UNICODE) : (string)$images,
|
||||
'is_anonymous' => $anonymous ? 1 : 0,
|
||||
'status' => 'normal',
|
||||
'createtime' => time(),
|
||||
]);
|
||||
Db::name('order')->where('id', $orderId)->update(['is_review' => 1, 'updatetime' => time()]);
|
||||
|
||||
// 刷新师傅综合评分
|
||||
$masterId = (int)$order['master_id'];
|
||||
if ($masterId > 0) {
|
||||
$avg = Db::name('review')->where('master_id', $masterId)->where('status', 'normal')->avg('score');
|
||||
Db::name('master')->where('id', $masterId)->update([
|
||||
'score' => round((float)$avg, 2),
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
}
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error('评价失败:' . $e->getMessage());
|
||||
}
|
||||
|
||||
$this->success('评价成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 师傅收到的评价
|
||||
*/
|
||||
public function masterList()
|
||||
{
|
||||
$masterId = (int)$this->request->param('master_id');
|
||||
if ($masterId <= 0) {
|
||||
$master = $this->masterInfo();
|
||||
if (!$master) {
|
||||
$this->error('师傅不存在');
|
||||
}
|
||||
$masterId = (int)$master['id'];
|
||||
}
|
||||
$list = Db::name('review')
|
||||
->alias('r')
|
||||
->join('user u', 'u.id = r.user_id', 'LEFT')
|
||||
->field('r.id,r.score,r.tags,r.content,r.images,r.is_anonymous,r.reply,r.createtime,u.nickname,u.avatar')
|
||||
->where('r.master_id', $masterId)
|
||||
->where('r.status', 'normal')
|
||||
->order('r.id desc')
|
||||
->limit(50)
|
||||
->select();
|
||||
|
||||
foreach ($list as &$row) {
|
||||
if ((int)$row['is_anonymous'] === 1) {
|
||||
$row['nickname'] = '匿名用户';
|
||||
$row['avatar'] = '';
|
||||
}
|
||||
$row['time_text'] = date('Y-m-d H:i', (int)$row['createtime']);
|
||||
}
|
||||
unset($row);
|
||||
|
||||
$stat = Db::name('review')->where('master_id', $masterId)->where('status', 'normal')
|
||||
->field('count(*) as total, avg(score) as avg_score')->find();
|
||||
|
||||
$this->success('', [
|
||||
'list' => $list,
|
||||
'total' => (int)$stat['total'],
|
||||
'avg_score' => round((float)$stat['avg_score'], 2),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\controller\Api;
|
||||
use app\common\library\Sms as Smslib;
|
||||
use app\common\model\User;
|
||||
use think\Hook;
|
||||
|
||||
/**
|
||||
* 手机短信接口
|
||||
*/
|
||||
class Sms extends Api
|
||||
{
|
||||
protected $noNeedLogin = '*';
|
||||
protected $noNeedRight = '*';
|
||||
|
||||
/**
|
||||
* 发送验证码
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="mobile", type="string", required=true, description="手机号")
|
||||
* @ApiParams (name="event", type="string", required=true, description="事件名称")
|
||||
*/
|
||||
public function send()
|
||||
{
|
||||
$mobile = $this->request->post("mobile");
|
||||
$event = $this->request->post("event");
|
||||
$event = $event ? $event : 'register';
|
||||
|
||||
if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
|
||||
$this->error(__('手机号不正确'));
|
||||
}
|
||||
$last = Smslib::get($mobile, $event);
|
||||
if ($last && time() - $last['createtime'] < 60) {
|
||||
$this->error(__('发送频繁'));
|
||||
}
|
||||
$ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
|
||||
if ($ipSendTotal >= 5) {
|
||||
$this->error(__('发送频繁'));
|
||||
}
|
||||
if ($event) {
|
||||
$userinfo = User::getByMobile($mobile);
|
||||
if ($event == 'register' && $userinfo) {
|
||||
//已被注册
|
||||
$this->error(__('已被注册'));
|
||||
} elseif (in_array($event, ['changemobile']) && $userinfo) {
|
||||
//被占用
|
||||
$this->error(__('已被占用'));
|
||||
} elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
|
||||
//未注册
|
||||
$this->error(__('未注册'));
|
||||
}
|
||||
}
|
||||
if (!Hook::get('sms_send')) {
|
||||
$this->error(__('请在后台插件管理安装短信验证插件'));
|
||||
}
|
||||
$ret = Smslib::send($mobile, null, $event);
|
||||
if ($ret) {
|
||||
$this->success(__('发送成功'));
|
||||
} else {
|
||||
$this->error(__('发送失败,请检查短信配置是否正确'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测验证码
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="mobile", type="string", required=true, description="手机号")
|
||||
* @ApiParams (name="event", type="string", required=true, description="事件名称")
|
||||
* @ApiParams (name="captcha", type="string", required=true, description="验证码")
|
||||
*/
|
||||
public function check()
|
||||
{
|
||||
$mobile = $this->request->post("mobile");
|
||||
$event = $this->request->post("event");
|
||||
$event = $event ? $event : 'register';
|
||||
$captcha = $this->request->post("captcha");
|
||||
|
||||
if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
|
||||
$this->error(__('手机号不正确'));
|
||||
}
|
||||
if ($event) {
|
||||
$userinfo = User::getByMobile($mobile);
|
||||
if ($event == 'register' && $userinfo) {
|
||||
//已被注册
|
||||
$this->error(__('已被注册'));
|
||||
} elseif (in_array($event, ['changemobile']) && $userinfo) {
|
||||
//被占用
|
||||
$this->error(__('已被占用'));
|
||||
} elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
|
||||
//未注册
|
||||
$this->error(__('未注册'));
|
||||
}
|
||||
}
|
||||
$ret = Smslib::check($mobile, $captcha, $event);
|
||||
if ($ret) {
|
||||
$this->success(__('成功'));
|
||||
} else {
|
||||
$this->error(__('验证码不正确'));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\controller\Api;
|
||||
use fast\Random;
|
||||
|
||||
/**
|
||||
* Token接口
|
||||
*/
|
||||
class Token extends Api
|
||||
{
|
||||
protected $noNeedLogin = [];
|
||||
protected $noNeedRight = '*';
|
||||
|
||||
/**
|
||||
* 检测Token是否过期
|
||||
*
|
||||
*/
|
||||
public function check()
|
||||
{
|
||||
$token = $this->auth->getToken();
|
||||
$tokenInfo = \app\common\library\Token::get($token);
|
||||
$this->success('', ['token' => $tokenInfo['token'], 'expires_in' => $tokenInfo['expires_in']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新Token
|
||||
*
|
||||
*/
|
||||
public function refresh()
|
||||
{
|
||||
//删除源Token
|
||||
$token = $this->auth->getToken();
|
||||
\app\common\library\Token::delete($token);
|
||||
//创建新Token
|
||||
$attempts = 0;
|
||||
do {
|
||||
$token = Random::uuid();
|
||||
$result = \app\common\library\Token::set($token, $this->auth->id, 2592000);
|
||||
} while (!$result && ++$attempts < 5);
|
||||
if (!$result) {
|
||||
$this->error('Failed to generate token');
|
||||
}
|
||||
$tokenInfo = \app\common\library\Token::get($token);
|
||||
$this->success('', ['token' => $tokenInfo['token'], 'expires_in' => $tokenInfo['expires_in']]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,356 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\controller\Api;
|
||||
use app\common\library\Ems;
|
||||
use app\common\library\Sms;
|
||||
use fast\Random;
|
||||
use think\Config;
|
||||
use think\Validate;
|
||||
|
||||
/**
|
||||
* 会员接口
|
||||
*/
|
||||
class User extends Api
|
||||
{
|
||||
protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
|
||||
protected $noNeedRight = '*';
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
|
||||
if (!Config::get('fastadmin.usercenter')) {
|
||||
$this->error(__('User center already closed'));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员中心
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->success('', ['welcome' => $this->auth->nickname]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员登录
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="account", type="string", required=true, description="账号")
|
||||
* @ApiParams (name="password", type="string", required=true, description="密码")
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
$account = $this->request->post('account');
|
||||
$password = $this->request->post('password');
|
||||
if (!$account || !$password) {
|
||||
$this->error(__('Invalid parameters'));
|
||||
}
|
||||
$ret = $this->auth->login($account, $password);
|
||||
if ($ret) {
|
||||
$data = ['userinfo' => $this->auth->getUserinfo()];
|
||||
$this->success(__('Logged in successful'), $data);
|
||||
} else {
|
||||
$this->error($this->auth->getError());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 手机验证码登录
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="mobile", type="string", required=true, description="手机号")
|
||||
* @ApiParams (name="captcha", type="string", required=true, description="验证码")
|
||||
*/
|
||||
public function mobilelogin()
|
||||
{
|
||||
$mobile = $this->request->post('mobile');
|
||||
$captcha = $this->request->post('captcha');
|
||||
if (!$mobile || !$captcha) {
|
||||
$this->error(__('Invalid parameters'));
|
||||
}
|
||||
if (!Validate::regex($mobile, "^1\d{10}$")) {
|
||||
$this->error(__('Mobile is incorrect'));
|
||||
}
|
||||
if (!Sms::check($mobile, $captcha, 'mobilelogin', true)) {
|
||||
$this->error(__('Captcha is incorrect'));
|
||||
}
|
||||
$user = \app\common\model\User::getByMobile($mobile);
|
||||
if ($user) {
|
||||
if ($user->status != 'normal') {
|
||||
$this->error(__('Account is locked'));
|
||||
}
|
||||
//如果已经有账号则直接登录
|
||||
$ret = $this->auth->direct($user->id);
|
||||
} else {
|
||||
$ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
|
||||
}
|
||||
if ($ret) {
|
||||
Sms::flush($mobile, 'mobilelogin');
|
||||
$data = ['userinfo' => $this->auth->getUserinfo()];
|
||||
$this->success(__('Logged in successful'), $data);
|
||||
} else {
|
||||
$this->error($this->auth->getError());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册会员
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="username", type="string", required=true, description="用户名")
|
||||
* @ApiParams (name="password", type="string", required=true, description="密码")
|
||||
* @ApiParams (name="email", type="string", required=true, description="邮箱")
|
||||
* @ApiParams (name="mobile", type="string", required=true, description="手机号")
|
||||
* @ApiParams (name="code", type="string", required=true, description="验证码")
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$username = $this->request->post('username');
|
||||
$password = $this->request->post('password');
|
||||
$email = $this->request->post('email');
|
||||
$mobile = $this->request->post('mobile');
|
||||
$code = $this->request->post('code');
|
||||
if (!$username || !$password) {
|
||||
$this->error(__('Invalid parameters'));
|
||||
}
|
||||
if ($email && !Validate::is($email, "email")) {
|
||||
$this->error(__('Email is incorrect'));
|
||||
}
|
||||
if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
|
||||
$this->error(__('Mobile is incorrect'));
|
||||
}
|
||||
$ret = Sms::check($mobile, $code, 'register', true);
|
||||
if (!$ret) {
|
||||
$this->error(__('Captcha is incorrect'));
|
||||
}
|
||||
$ret = $this->auth->register($username, $password, $email, $mobile, []);
|
||||
if ($ret) {
|
||||
Sms::flush($mobile, 'register');
|
||||
$data = ['userinfo' => $this->auth->getUserinfo()];
|
||||
$this->success(__('Sign up successful'), $data);
|
||||
} else {
|
||||
$this->error($this->auth->getError());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function logout()
|
||||
{
|
||||
if (!$this->request->isPost()) {
|
||||
$this->error(__('Invalid parameters'));
|
||||
}
|
||||
$this->auth->logout();
|
||||
$this->success(__('Logout successful'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改会员个人信息
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="avatar", type="string", required=true, description="头像地址")
|
||||
* @ApiParams (name="username", type="string", required=true, description="用户名")
|
||||
* @ApiParams (name="nickname", type="string", required=true, description="昵称")
|
||||
* @ApiParams (name="bio", type="string", required=true, description="个人简介")
|
||||
*/
|
||||
public function profile()
|
||||
{
|
||||
$user = $this->auth->getUser();
|
||||
$username = $this->request->post('username');
|
||||
$nickname = $this->request->post('nickname');
|
||||
$bio = $this->request->post('bio');
|
||||
$avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
|
||||
if ($username) {
|
||||
$exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
|
||||
if ($exists) {
|
||||
$this->error(__('Username already exists'));
|
||||
}
|
||||
$user->username = $username;
|
||||
}
|
||||
if ($nickname) {
|
||||
$exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
|
||||
if ($exists) {
|
||||
$this->error(__('Nickname already exists'));
|
||||
}
|
||||
$user->nickname = $nickname;
|
||||
}
|
||||
if ($avatar) {
|
||||
//判断是否匹配config('upload.cdnurl')开头以及当前$SERVER['HTTP_HOST']开头。
|
||||
if (preg_match('/^' . preg_quote(config('upload.cdnurl') . '/', '/') . '/i', $avatar)
|
||||
|| preg_match('/^' . preg_quote(substr(config('upload.savekey'), 0, strpos(config('upload.savekey'), '{')), '/') . '/i', $avatar)
|
||||
|| preg_match('/^' . preg_quote($_SERVER['HTTP_HOST'] . '/', '/') . '/i', $avatar)) {
|
||||
$user->avatar = $avatar;
|
||||
}
|
||||
}
|
||||
$user->bio = $bio;
|
||||
$user->save();
|
||||
$this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改邮箱
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="email", type="string", required=true, description="邮箱")
|
||||
* @ApiParams (name="captcha", type="string", required=true, description="验证码")
|
||||
*/
|
||||
public function changeemail()
|
||||
{
|
||||
$user = $this->auth->getUser();
|
||||
$email = $this->request->post('email');
|
||||
$captcha = $this->request->post('captcha');
|
||||
if (!$email || !$captcha) {
|
||||
$this->error(__('Invalid parameters'));
|
||||
}
|
||||
if (!Validate::is($email, "email")) {
|
||||
$this->error(__('Email is incorrect'));
|
||||
}
|
||||
if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
|
||||
$this->error(__('Email already exists'));
|
||||
}
|
||||
$result = Ems::check($email, $captcha, 'changeemail', true);
|
||||
if (!$result) {
|
||||
$this->error(__('Captcha is incorrect'));
|
||||
}
|
||||
$verification = $user->verification;
|
||||
$verification->email = 1;
|
||||
$user->verification = $verification;
|
||||
$user->email = $email;
|
||||
$user->save();
|
||||
|
||||
Ems::flush($email, 'changeemail');
|
||||
$this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改手机号
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="mobile", type="string", required=true, description="手机号")
|
||||
* @ApiParams (name="captcha", type="string", required=true, description="验证码")
|
||||
*/
|
||||
public function changemobile()
|
||||
{
|
||||
$user = $this->auth->getUser();
|
||||
$mobile = $this->request->post('mobile');
|
||||
$captcha = $this->request->post('captcha');
|
||||
if (!$mobile || !$captcha) {
|
||||
$this->error(__('Invalid parameters'));
|
||||
}
|
||||
if (!Validate::regex($mobile, "^1\d{10}$")) {
|
||||
$this->error(__('Mobile is incorrect'));
|
||||
}
|
||||
if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
|
||||
$this->error(__('Mobile already exists'));
|
||||
}
|
||||
$result = Sms::check($mobile, $captcha, 'changemobile', true);
|
||||
if (!$result) {
|
||||
$this->error(__('Captcha is incorrect'));
|
||||
}
|
||||
$verification = $user->verification;
|
||||
$verification->mobile = 1;
|
||||
$user->verification = $verification;
|
||||
$user->mobile = $mobile;
|
||||
$user->save();
|
||||
|
||||
Sms::flush($mobile, 'changemobile');
|
||||
$this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 第三方登录
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="platform", type="string", required=true, description="平台名称")
|
||||
* @ApiParams (name="code", type="string", required=true, description="Code码")
|
||||
*/
|
||||
public function third()
|
||||
{
|
||||
$url = url('user/index');
|
||||
$platform = $this->request->post("platform");
|
||||
$code = $this->request->post("code");
|
||||
$config = get_addon_config('third');
|
||||
if (!$config || !isset($config[$platform])) {
|
||||
$this->error(__('Invalid parameters'));
|
||||
}
|
||||
$app = new \addons\third\library\Application($config);
|
||||
//通过code换access_token和绑定会员
|
||||
$result = $app->{$platform}->getUserInfo(['code' => $code]);
|
||||
if ($result) {
|
||||
$loginret = \addons\third\library\Service::connect($platform, $result);
|
||||
if ($loginret) {
|
||||
$data = [
|
||||
'userinfo' => $this->auth->getUserinfo(),
|
||||
'thirdinfo' => $result
|
||||
];
|
||||
$this->success(__('Logged in successful'), $data);
|
||||
}
|
||||
}
|
||||
$this->error(__('Operation failed'), $url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置密码
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="mobile", type="string", required=true, description="手机号")
|
||||
* @ApiParams (name="newpassword", type="string", required=true, description="新密码")
|
||||
* @ApiParams (name="captcha", type="string", required=true, description="验证码")
|
||||
*/
|
||||
public function resetpwd()
|
||||
{
|
||||
$type = $this->request->post("type", "mobile");
|
||||
$mobile = $this->request->post("mobile");
|
||||
$email = $this->request->post("email");
|
||||
$newpassword = $this->request->post("newpassword");
|
||||
$captcha = $this->request->post("captcha");
|
||||
if (!$newpassword || !$captcha) {
|
||||
$this->error(__('Invalid parameters'));
|
||||
}
|
||||
//验证Token
|
||||
if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
|
||||
$this->error(__('Password must be 6 to 30 characters'));
|
||||
}
|
||||
if ($type == 'mobile') {
|
||||
if (!Validate::regex($mobile, "^1\d{10}$")) {
|
||||
$this->error(__('Mobile is incorrect'));
|
||||
}
|
||||
$user = \app\common\model\User::getByMobile($mobile);
|
||||
if (!$user) {
|
||||
$this->error(__('User not found'));
|
||||
}
|
||||
$ret = Sms::check($mobile, $captcha, 'resetpwd', true);
|
||||
if (!$ret) {
|
||||
$this->error(__('Captcha is incorrect'));
|
||||
}
|
||||
Sms::flush($mobile, 'resetpwd');
|
||||
} else {
|
||||
if (!Validate::is($email, "email")) {
|
||||
$this->error(__('Email is incorrect'));
|
||||
}
|
||||
$user = \app\common\model\User::getByEmail($email);
|
||||
if (!$user) {
|
||||
$this->error(__('User not found'));
|
||||
}
|
||||
$ret = Ems::check($email, $captcha, 'resetpwd', true);
|
||||
if (!$ret) {
|
||||
$this->error(__('Captcha is incorrect'));
|
||||
}
|
||||
Ems::flush($email, 'resetpwd');
|
||||
}
|
||||
//模拟一次登录
|
||||
$this->auth->direct($user->id);
|
||||
$ret = $this->auth->changepwd($newpassword, '', true);
|
||||
if ($ret) {
|
||||
$this->success(__('Reset password successful'));
|
||||
} else {
|
||||
$this->error($this->auth->getError());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\controller\Api;
|
||||
use app\common\model\User;
|
||||
|
||||
/**
|
||||
* 验证接口
|
||||
*/
|
||||
class Validate extends Api
|
||||
{
|
||||
protected $noNeedLogin = '*';
|
||||
protected $layout = '';
|
||||
protected $error = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测邮箱
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="email", type="string", required=true, description="邮箱")
|
||||
* @ApiParams (name="id", type="string", required=true, description="排除会员ID")
|
||||
*/
|
||||
public function check_email_available()
|
||||
{
|
||||
$email = $this->request->post('email');
|
||||
$id = (int)$this->request->post('id');
|
||||
$count = User::where('email', '=', $email)->where('id', '<>', $id)->count();
|
||||
if ($count > 0) {
|
||||
$this->error(__('邮箱已经被占用'));
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测用户名
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="username", type="string", required=true, description="用户名")
|
||||
* @ApiParams (name="id", type="string", required=true, description="排除会员ID")
|
||||
*/
|
||||
public function check_username_available()
|
||||
{
|
||||
$username = $this->request->post('username');
|
||||
$id = (int)$this->request->post('id');
|
||||
$count = User::where('username', '=', $username)->where('id', '<>', $id)->count();
|
||||
if ($count > 0) {
|
||||
$this->error(__('用户名已经被占用'));
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测昵称
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="nickname", type="string", required=true, description="昵称")
|
||||
* @ApiParams (name="id", type="string", required=true, description="排除会员ID")
|
||||
*/
|
||||
public function check_nickname_available()
|
||||
{
|
||||
$nickname = $this->request->post('nickname');
|
||||
$id = (int)$this->request->post('id');
|
||||
$count = User::where('nickname', '=', $nickname)->where('id', '<>', $id)->count();
|
||||
if ($count > 0) {
|
||||
$this->error(__('昵称已经被占用'));
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测手机
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="mobile", type="string", required=true, description="手机号")
|
||||
* @ApiParams (name="id", type="string", required=true, description="排除会员ID")
|
||||
*/
|
||||
public function check_mobile_available()
|
||||
{
|
||||
$mobile = $this->request->post('mobile');
|
||||
$id = (int)$this->request->post('id');
|
||||
$count = User::where('mobile', '=', $mobile)->where('id', '<>', $id)->count();
|
||||
if ($count > 0) {
|
||||
$this->error(__('该手机号已经占用'));
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测手机
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="mobile", type="string", required=true, description="手机号")
|
||||
*/
|
||||
public function check_mobile_exist()
|
||||
{
|
||||
$mobile = $this->request->post('mobile');
|
||||
$count = User::where('mobile', '=', $mobile)->count();
|
||||
if (!$count) {
|
||||
$this->error(__('手机号不存在'));
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测邮箱
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="email", type="string", required=true, description="邮箱")
|
||||
*/
|
||||
public function check_email_exist()
|
||||
{
|
||||
$email = $this->request->post('email');
|
||||
$count = User::where('email', '=', $email)->count();
|
||||
if (!$count) {
|
||||
$this->error(__('邮箱不存在'));
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测手机验证码
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="mobile", type="string", required=true, description="手机号")
|
||||
* @ApiParams (name="captcha", type="string", required=true, description="验证码")
|
||||
* @ApiParams (name="event", type="string", required=true, description="事件")
|
||||
*/
|
||||
public function check_sms_correct()
|
||||
{
|
||||
$mobile = $this->request->post('mobile');
|
||||
$captcha = $this->request->post('captcha');
|
||||
$event = $this->request->post('event');
|
||||
if (!\app\common\library\Sms::check($mobile, $captcha, $event)) {
|
||||
$this->error(__('验证码不正确'));
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测邮箱验证码
|
||||
*
|
||||
* @ApiMethod (POST)
|
||||
* @ApiParams (name="email", type="string", required=true, description="邮箱")
|
||||
* @ApiParams (name="captcha", type="string", required=true, description="验证码")
|
||||
* @ApiParams (name="event", type="string", required=true, description="事件")
|
||||
*/
|
||||
public function check_ems_correct()
|
||||
{
|
||||
$email = $this->request->post('email');
|
||||
$captcha = $this->request->post('captcha');
|
||||
$event = $this->request->post('event');
|
||||
if (!\app\common\library\Ems::check($email, $captcha, $event)) {
|
||||
$this->error(__('验证码不正确'));
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户