count(); $list = $model->page($page, $limit)->select()->toArray(); return compact('count', 'list'); } public static function buyMachine($id, $uid, $num) { $info = self::valid()->where('id', $id)->find(); if (!$info) { return self::setErrorInfo('矿机已下架或不存在'); } if ($info['stock'] < $num) { return self::setErrorInfo('矿机已售罄'); } $money_type = init_money_type(); $user_money = UserMoney::initialUserMoney($uid, $info['cost_money_type']); $user_money_stand = UserMoney::initialUserMoney($uid, $info['stand_money_type']); if ($info['cost_money_type'] == $info['stand_money_type']) { $info['cost_money'] = bcadd($info['cost_money'], $info['stand_money'], 8); $info['stand_money'] = 0; } $info['cost_money'] = bcmul($info['cost_money'], $num); $info['stand_money'] = bcmul($info['stand_money'], $num); if ($user_money['money'] <= $info['cost_money']) { return self::setErrorInfo('购买矿机所需的' . $money_type[$info['cost_money_type']] . '不足'); } if ($user_money_stand['money'] <= $info['stand_money']) { return self::setErrorInfo('部署矿机需质押的' . $money_type[$info['stand_money_type']] . '(包含GAS)不足'); } BaseModel::beginTrans(); try { $res1 = UserMoney::expendMoney($uid, $info['cost_money_type'], $info['cost_money'], 'buy_machine', '购买矿机', '购买矿机' . $info['name'] . '*' . $num); $res2 = true; if ($info['stand_money'] > 0) $res2 = UserMoney::expendMoney($uid, $info['stand_money_type'], $info['stand_money'], 'buy_machine_stand', '部署矿机质押', '部署矿机' . $info['name'] . '*' . $num . ',' . '质押(包含GAS费)'); if ($res1 && $res2) { $res = UserMiningMachine::create([ 'uid' => $uid, 'mid' => $id, 'day_get' => $info['day_get'], 'get_money_type' => $info['get_money_type'], 'cost_money' => $info['cost_money'], 'cost_money_type' => $info['cost_money_type'], 'stand_money' => $info['stand_money'], 'stand_money_type' => $info['stand_money_type'], 'add_time' => time(), 'pay_time' => time(), 'paid' => 1, 'num' => $num, 'mining_end_time' => bcadd(time(), bcmul($info['mining_time'], 3600 * 24)), 'mining_start_time' => bcadd(time(), bcmul($info['stand_time'], 3600 * 24)), ]) && self::bcDec($id, 'stock', $num); if (!$res) { return BaseModel::setErrorInfo('购买失败', true); } } else { return BaseModel::setErrorInfo('支付失败', true); } BaseModel::commitTrans(); return true; } catch (Exception $e) { return BaseModel::setErrorInfo($e->getMessage(), true); } } }