作者 jingchen

submit

不能预览此文件类型
... ... @@ -35,10 +35,12 @@ class Order extends Model
{
$orderSn = get_order_sn('G');
$money = Money::where('id',$moneyId)->find();
// 卡片订单
$order = new self();
// 实际支付金额
$payMoney = bcmul($money['money'],$money['discount'],2);
// 预计充值金额为 $money['money']
self::create([
$orderData = [
'order_sn'=>$orderSn,
'user_id'=>$userId,
'card_id'=>$cardId,
... ... @@ -46,8 +48,9 @@ class Order extends Model
'discount'=>bcsub($money['money'],$payMoney,2),
'pay_price'=>$payMoney,
'status'=>1,
]);
return $orderSn;
];
$order->save($orderData);
return $order->id;
}
... ...
... ... @@ -7,6 +7,7 @@ use app\admin\model\gift\Cover;
use app\admin\model\gift\Money;
use app\admin\model\gift\Order;
use app\admin\model\UserGift;
use app\api\library\Share;
use app\common\model\Config;
class Gift extends Api
... ... @@ -24,14 +25,17 @@ class Gift extends Api
public function getGiftDesc()
{
$this->success('获取成功',Config::where('id',25)->value('value'));
$this->success('获取成功', [
'desc' => Config::where('id', 26)->value('value'),
'poster' => cdnurl(Config::where('id', 27)->value('value'), true)
]);
}
public function createOrder()
{
$userId = $this->auth->id;
$cardId = $this->request->request('card_id');
$moneyId = $this->request->request('money_id');
$cardId = $this->request->post('card_id');
$moneyId = $this->request->post('money_id');
$order = new Order();
$res = $order->pay($userId, $cardId, $moneyId);
if ($res) {
... ... @@ -41,18 +45,62 @@ class Gift extends Api
}
/**
* 领取卡片
* @return void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function collectCard()
{
$userId = $this->auth->id;
$orderId = $this->request->request('order_id');
$userGift = UserGift::where(['order_id' => $orderId])->find();
if (empty($userGift)) {
$this->error('卡片不存在');
}
if ($userGift['get_status'] == 'expire') {
$this->error('该卡片已过期');
}
if ($userGift['get_status'] == 'already') {
$this->error('该卡片已领取');
}
$userGift->get_status = 'already';
$userGift->active_time = time();
$userGift->puser_id = $userId;
if ($userGift->save()) {
$this->success('领取成功');
}
$this->error('领取失败');
}
public function getUserCardCode()
{
$cardId = $this->request->post('card_id');
$userGift = UserGift::where(['id' => $cardId])->find();
$uid = $this->auth->id;
if (empty($userGift->code)) {
$promo_code = Share::share($cardId, $uid);
$userGift->code = $promo_code;
$userGift->save();
}
$this->success('获取成功', cdnurl($userGift->code));
}
public function myGiftCard()
{
$userId = $this->auth->id;
$type = $this->request->post('type');
$res = UserGift::with('card')->where(['user_id'=>$userId, 'type'=>$type])->select();
$res = array_map(function ($item){
$res = UserGift::with('card')->where(['user_id' => $userId, 'type' => $type])->select();
$res = array_map(function ($item) {
$item['get_status_format'] = status_and_chinese($item['get_status']);
$item['create_time_format'] = date('Y-m-d',$item['create_time']);
$item['active_time_format'] = date('Y-m-d',$item['active_time']);
$item['card']['image'] = cdnurl($item['card']['image'],true);
$item['create_time_format'] = date('Y-m-d', $item['create_time']);
$item['active_time_format'] = date('Y-m-d', $item['active_time']);
$item['card']['image'] = cdnurl($item['card']['image'], true);
return $item;
},(array)$res);
$this->success('获取成功',$res);
}, (array)$res);
$this->success('获取成功', $res);
}
}
\ No newline at end of file
... ...
<?php
namespace app\api\library;
use fast\Http;
use think\Cache;
class Share
{
protected $miniProgramConfig;
public function __construct()
{
$this->miniProgramConfig = [
'app_id' => 'wx2a6d9ce4bffbe304',
'secret' => 'c52864075c132c27c286e7c842cf07bf'
];
}
/**
* 获取用户个人分享码
* @param $uid
* @return string|array
*/
public static function share($cardId, &$uid)
{
$access_token = Cache::get('access_token');
if (!$access_token) {
$access_token = self::getAccessToken();
}
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;
$data = [
'scene' => "?cardId={$cardId}&userId={$uid}",
'page' => 'pages/index/index',
'env_version' => 'trial',
'check_path' => false
];
$result = Http::sendRequest($url, json_encode($data), 'POST');
$time = time();
// 分享二维码保存到用户数
$date = date('Ymd');
$filename = "share$time$uid.png";
$furl = ROOT_PATH . 'public' . DS . "uploads/userShareCode/$date/" . $filename;
$filepath = "uploads/userShareCode/$date/" . $filename;
file_put_contents($furl, $result);
return $filepath;
}
public static function getAccessToken()
{
$app_id = 'wx2a6d9ce4bffbe304';
$secret = 'c52864075c132c27c286e7c842cf07bf';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$app_id}&secret={$secret}";
$access_token = Http::get($url);
$access_token = json_decode($access_token, true);
//将access_token存入缓存
Cache::dec('access_token');
Cache::set('access_token', $access_token['access_token'], $access_token['expires_in']);
return $access_token['access_token'];
}
}
\ No newline at end of file
... ...