发票管理apiadmin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

171 lines
5.0 KiB

<?php
namespace app\controller\wechat;
use app\model\WechatPucode;
use app\model\WechatUser;
use app\service\user\SmsService;
use app\service\user\UserService;
use app\service\webService\FeeService;
use app\util\AuthCodeUtil;
use app\validate\WechatUser as WechatUserValidate;
use app\service\wechat\WechatService;
use app\util\ReturnCode;
use think\cache\driver\Redis;
use think\exception\ValidateException;
use think\facade\Db;
use think\Request;
class Wechat extends Base
{
public function index(Request $request)
{
$param = $request->param();
$signature = $param['signature'] ?? '';// 签名
$timestamp = $param['timestamp'] ?? '';// 时间戳
$nonce = $param['nonce'] ?? ''; // 随机数
$echostr = $param['echostr'] ?? '0'; // 随机字符串
$checkRes = (new WechatService())->wechatChekToken($signature, $timestamp, $nonce);
$param['res_error'] = $checkRes;
if ($checkRes) {
echo $echostr;
die;
}
return $this->buildFailed(400, '微信认证失败!');
}
public function sendSms(Request $request)
{
$param = $request->param();
try {
validate(WechatUserValidate::class)->scene('smsCode')->check($param);
$AuthCodeUtil = new AuthCodeUtil(new Redis());
$AuthCodeUtil->setRate($param['mobile']);
$code = $AuthCodeUtil->setCode($param['mobile']);
$SmsService = new SmsService();
$SmsService->sendSms($param['mobile'], $SmsService->smsCodeTemplate($code));
return $this->buildSuccess([], '短信已发送');
} catch (\Exception $e) {
return $this->buildFailed(ReturnCode::INVALID, $e->getMessage());
}
}
/**
* 用户添加手机号用户编号
* @param Request $request
* @return \think\Response
* @throws \Exception
*/
public function savePuCode(Request $request)
{
$param = $request->param();
// 验证数据
$wechat_user_id = $request->wechat_user_id;
//$defaultSnsCode = '439764';
try {
validate(WechatUserValidate::class)->scene('savePuCode')->check($param);
$res = (new AuthCodeUtil(new Redis()))->verifyCode($param['mobile'],$param['snsCode']);
if (!$res) {
throw new ValidateException('短信验证码错误');
}
} catch (ValidateException $e) {
return $this->buildFailed(ReturnCode::INVALID, $e->getMessage());
}
Db::startTrans();
try {
$mobile = $param['mobile'];
$pucode = $param['pucode'];
$FeeService = new FeeService($pucode);
$feeUserInfo = $FeeService->getUsers();
if ($feeUserInfo['UserCode'] != $pucode) {
throw new \Exception('用户不存在');
}
if ($feeUserInfo['CellPhone'] != $mobile) {
throw new \Exception('手机号或用户编号错误');
}
$userData = ['phone' => $mobile];
$WechatUser = WechatUser::update($userData, ['id' => $wechat_user_id]);
if (!$WechatUser) {
throw new \Exception('添加失败');
}
$WechatPucode = WechatPucode::createPuCode($wechat_user_id, $pucode);
if (!$WechatPucode) {
throw new \Exception('添加失败');
}
Db::commit();
$data = $WechatUser->getUserInfo($wechat_user_id);
$token = $this->signToken($data);
return $this->buildSuccess(['token' => $token]);
} catch (\Exception $e) {
Db::rollback();
return $this->buildFailed(ReturnCode::UPDATE_FAILED, $e->getMessage());
}
}
/**
* 修改密码
* @param Request $request
* @return \think\Response|void
*/
public function updatePassword(Request $request)
{
try {
$wechat_user_id = $request->wechat_user_id;
$param = $request->post();
validate(WechatUserValidate::class)->scene('updatePassword')->check($param);
$UserService = new UserService();
$result = $UserService->savePassword($wechat_user_id, $param['password']);
if (!$result) {
throw new \Exception('修改失败');
}
return $this->buildSuccess([], '修改成功');
} catch (\Exception $e) {
return $this->buildFailed(ReturnCode::UPDATE_FAILED, $e->getMessage());
}
}
public function getJsSdkData(Request $request)
{
try {
$param = $request->param();
validate()->rule(['url' => 'require'])->check($param);
$url = urldecode($param['url']);
$WechatService = new WechatService();
return $this->buildSuccess($WechatService->getJsSdkSignature($url));
} catch (\Exception $e) {
return $this->buildFailed(ReturnCode::INVALID,$e->getMessage());
}
}
}