<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\web\controller;
use app\wap\model\user\SmsCode;
use app\web\model\user\User;
use app\web\model\user\UserBill;
use app\web\model\user\UserRecharge;
use app\web\model\activity\EventSignUp;
use app\web\model\special\Special as SpecialModel;
use app\web\model\special\StoreOrder;
use app\web\model\special\Lecturer;
use app\web\model\article\Article;
use app\web\model\user\MemberShip;
use app\web\model\user\MemberCard;//会员卡
use service\sms\storage\Sms;
use app\web\model\material\DataDownload;
use app\web\model\material\DataDownloadBuy;
use app\web\model\material\DataDownloadOrder;
use service\AliMessageService;
use service\CacheService;
use service\HookService;
use service\JsonService;
use service\SystemConfigService;
use service\GroupDataService;
use service\UtilService;
use think\Cache;
use think\Db;
use think\Request;
use think\Session;
use think\Url;
/**接口
* Class AuthApi
* @package app\web\controller
*/
class AuthApi extends AuthController
{
public static function WhiteList()
{
return [
'code',
'merber_data',
'suspensionButton',
'getVersion',
'get_course_ranking',
'get_new_course_first',
'get_article_unifiend_list',
'get_good_class_recommend',
'lecturer_special_list',
'getLecturer',
'lecturer_details'
];
}
public function upload()
{
$aliyunOss = \Api\AliyunOss::instance([
'AccessKey' => SystemConfigService::get('accessKeyId'),
'AccessKeySecret' => SystemConfigService::get('accessKeySecret'),
'OssEndpoint' => SystemConfigService::get('end_point'),
'OssBucket' => SystemConfigService::get('OssBucket'),
'uploadUrl' => SystemConfigService::get('uploadUrl'),
]);
$res = $aliyunOss->upload('file');
if ($res && isset($res['url'])) {
return JsonService::successful('上传成功', ['url' => $res['url']]);
} else {
return JsonService::fail('上传失败');
}
}
/**
* 发送短信验证码
* @param string $phone
*/
public function code($phone = '')
{
$name = "is_phone_code" . $phone;
if ($phone == '') return JsonService::fail('请输入手机号码!');
$time = Session::get($name, 'web');
if ($time < time() + 60) Session::delete($name, 'web');
if (Session::has($name, 'web') && $time < time()) return JsonService::fail('您发送验证码的频率过高,请稍后再试!');
$code = AliMessageService::getVerificationCode();
SmsCode::set(['tel' => $phone, 'code' => md5('is_phone_code' . $code), 'last_time' => time() + 300, 'uid' => $this->uid]);
Session::set($name, time() + 60, 'web');
$smsHandle = new Sms();
$sms_platform_selection = SystemConfigService::get('sms_platform_selection');
$smsSignName = SystemConfigService::get('smsSignName');//短信签名
$smsTemplateCode = SystemConfigService::get('smsTemplateCode');//短信模板ID
if ($sms_platform_selection == 1) {
if (!$smsSignName || !$smsTemplateCode) return JsonService::fail('系统后台短信没有配置,请稍后在试!');
$res = AliMessageService::sendmsg($phone, $smsTemplateCode, ['code' => $code]);
} else {
if (!(int)$smsTemplateCode) return JsonService::fail('请正确的填写系统后台短信配置!');
$res = $smsHandle->send($phone, $smsTemplateCode, ['code' => $code]);
}
if ($res['Code'] == 'OK') {
return JsonService::successful('发送成功', $res);
} else {
return JsonService::fail($res['Message']);
}
}
/**
* 用户信息
*/
public function user_info()
{
$user = $this->userInfo;
if ($user['level'] > 0) {
$user['overdue_time'] = date('Y-m-d', $user['overdue_time']);
}
return JsonService::successful($user);
}
/**支付接口
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function create_order()
{
list($special_id, $pay_type_num, $payType, $price_id, $event) = UtilService::PostMore([
['special_id', 0],
['pay_type_num', -1],
['payType', 'weixin'],
['price_id', 0],
['event', []]
], $this->request, true);
switch ($pay_type_num) {
case 10: //会员支付
$this->create_member_order($special_id, $payType);
break;
case 20://报名支付
$this->create_activity_order($special_id, $payType, $price_id, $event);
break;
case 30: //虚拟币充值
$this->user_wechat_recharge($special_id, $payType);
break;
case 70: //资料购买
$this->create_data_download_order($special_id, $payType);
break;
default: //专题支付
$this->create_special_order($special_id, $pay_type_num, $payType);
}
}
/**会员订单创建
* @param $id
* @param $payType
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function create_member_order($id, $payType)
{
if (!$id) return JsonService::fail('参数错误!');
$order = StoreOrder::cacheMemberCreateOrder($this->uid, $id, $payType);
$orderId = $order['order_id'];
$info = compact('orderId');
if ($orderId) {
$orderInfo = StoreOrder::where('order_id', $orderId)->find();
if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
if ($orderInfo['paid']) exception('支付已支付!');
if (bcsub((float)$orderInfo['pay_price'], 0, 2) <= 0) {
if (StoreOrder::jsPayMePrice($orderId, $this->uid))
return JsonService::status('success', '领取成功', $info);
else
return JsonService::status('pay_error', StoreOrder::getErrorInfo());
} else {
switch ($payType) {
case 'weixin':
try {
$jsConfig = StoreOrder::nativePayMember($orderId);
} catch (\Exception $e) {
return JsonService::status('pay_error', $e->getMessage(), $info);
}
$info['jsConfig'] = $jsConfig;
return JsonService::status('wechat_pay', '订单创建成功', $info);
break;
case 'zhifubao':
try {
$jsConfig = StoreOrder::alipayAativePayMember($orderId);
} catch (\Exception $e) {
return JsonService::status('pay_error', $e->getMessage(), $info);
}
$info['jsConfig'] = $jsConfig;
return JsonSe