123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- namespace app\api\model;
- use app\common\lib\WxPay;
- use think\Db;
- use think\Model;
- class OrderModel extends Model
- {
- protected $name = 'order';
- /*
- * 用户余额支付
- */
- public function userMoneyPay($params)
- {
- $userInfo = UsersModel::where('user_id', $params['user_id'])->find();
- if ($userInfo['user_user_paypwd'] != $params['user_paypwd']) {
- return json(['code' => 100, 'msg' => '支付密码不正确', 'data' => '']);
- }
- if (!$userInfo) {
- return json(['code' => 100, 'msg' => '网络错误', 'data' => '']);
- }
- //对照金额书否正确的
- $isMoney = Db::name('order_pre')->where('pre_id', $params['pre_id'])->where('money', $params['money'])->find();
- if (!$isMoney) {
- return json(['code' => 100, 'msg' => '金额不对', 'data' => '']);
- }
- if ($userInfo['user_money'] < $params['money']) {
- return json(['code' => 100, 'msg' => '您的余额不足', 'data' => '']);
- }
- if ($userInfo['user_paypwd'] == '') {
- return json(['code' => 60, 'msg' => '还未设置支付密码,请去设置', 'data' => '']);
- }
- Db::startTrans();
- try {
- //扣除用户余额
- $updUserMoney = UsersModel::where('user_id', $params['user_id'])->setDec('user_money', $params['money']);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- }
- if (!$updUserMoney) {
- return json(['code' => 100, 'msg' => '扣款失败', 'data' => '']);
- }
- $list = $params['list'];
- //循环加入奖励的白豆
- $whitebean = 0; // 白豆初始化
- foreach ($list as &$v) {
- $commodity = CommodityModel::where('c_id', $v['c_id'])->find()->toArray(); // 查出商品详情
- $whitebean = $commodity['c_whitebean'] + $whitebean;
- }
- //创建订单信息
- $orderdata = array(
- 'user_id' => $params['user_id'],
- 'money' => $params['money'],
- 'freight' => $params['freight'],
- 'number' => $params['number'],
- 'create_time' => $params['create_time'],
- 'whitebeon' => $whitebean,
- 'pay_time' => date("Y-m-d H:i:s", time()),
- 'state' => 2,
- 'type' => $params['type'],
- );
- //生成已支付订单
- $addOrderId = Db::name('order')->insertGetId($orderdata);
- if (!$addOrderId) {
- return json(['code' => 100, 'msg' => '订单生成失败', 'data' => '']);
- }
- $list = $params['list'];
- //循环加入奖励的白豆
- $whitebean = 0; // 白豆初始化
- foreach ($list as &$v) {
- $commodity = CommodityModel::where('c_id', $v['c_id'])->find()->toArray(); // 查出商品详情
- $whitebean = $commodity['c_whitebean'] + $whitebean;
- }
- //创建订单商品信息
- foreach ($list as &$v) {
- $commoditycolor = Commoditycolor::where('colorid', $v['colorid'])->find()->toArray(); //查 出商品颜色表参数
- $parameter = Parameter::where('p_id', $v['p_id'])->find()->toArray(); // 查出商品规格表参数
- $commodity = CommodityModel::where('c_id', $v['c_id'])->field('c_vipprice,c_freight')->find()->toArray(); // 查出商品价格和运费
- $v = array_merge($commoditycolor, $parameter, $commodity, $v);
- $order_commodity_data = array(
- 'c_id' => $v['c_id'],
- 'p_id' => $v['p_id'],
- 'colorid' => $v['colorid'],
- 'name' => $v['name'],
- 'p_name' => $v['p_name'],
- 'color' => $v['color'],
- 'buy_number' => $v['buy_number'],
- 'c_vipprice' => $v['c_vipprice'],
- 'c_freight' => $v['c_freight'],
- 'o_id' => $addOrderId,
- );
- //循环插入订单商品信息
- $add_order_commodity = Db::name('order_commodity')->insert($order_commodity_data);
- }
- if (!$add_order_commodity) {
- return json(['code' => 100, 'msg' => '网络错误', 'data' => '']);
- }
- //创建地址信息
- $address = Address::where('a_id', $params['a_id'])->find();
- $order_address = array(
- 'a_id' => $params['a_id'],
- 'a_name' => $address['a_name'],
- 'a_tel' => $address['a_tel'],
- 'a_city' => $address['city'],
- 'a_area' => $address['area'],
- 'o_id' => $addOrderId
- );
- //订单地址添加
- $add_order_address = Db::name('order_address')->insert($order_address);
- if ($add_order_address) {
- return json(['code' => 200, 'msg' => '下单成功', 'data' => $addOrderId]);
- } else {
- return json(['code' => 200, 'msg' => '下单失败', 'data' => $addOrderId]);
- }
- }
- /*
- * 订单微信支付
- */
- public function wechatOrder($params)
- {
- $userInfo = UsersModel::where('user_id', $params['user_id'])->find();
- if (!$userInfo) {
- return json(['code' => 100, 'msg' => '网络错误', 'data' => '']);
- }
- //对照金额书否正确的
- $isMoney = Db::name('order_pre')->where('pre_id', $params['pre_id'])->where('money', $params['money'])->find();
- if (!$isMoney) {
- return json(['code' => 100, 'msg' => '金额不对', 'data' => '']);
- }
- //创建订单信息
- $orderdata = array(
- 'user_id' => $params['user_id'],
- 'money' => $params['money'],
- 'freight' => $params['freight'],
- 'number' => $params['number'],
- 'create_time' => $params['create_time'],
- 'whitebeon' => $params['whitebean'],
- 'pay_time' => date("Y-m-d H:i:s", time()),
- 'state' => 1,
- 'type' => $params['type'],
- );
- //生成已支付订单
- $addOrderId = Db::name('order')->insertGetId($orderdata);
- if (!$addOrderId) {
- return json(['code' => 100, 'msg' => '订单生成失败', 'data' => '']);
- }
- $list = $params['list'];
- //创建订单商品信息
- foreach ($list as &$v) {
- $commoditycolor = Commoditycolor::where('colorid', $v['colorid'])->find()->toArray(); //查 出商品颜色表参数
- $parameter = Parameter::where('p_id', $v['p_id'])->find()->toArray(); // 查出商品规格表参数
- $commodity = CommodityModel::where('c_id', $v['c_id'])->field('c_vipprice,c_freight')->find()->toArray(); // 查出商品价格和运费
- $v = array_merge($commoditycolor, $parameter, $commodity, $v);
- $order_commodity_data = array(
- 'c_id' => $v['c_id'],
- 'p_id' => $v['p_id'],
- 'colorid' => $v['colorid'],
- 'name' => $v['name'],
- 'p_name' => $v['p_name'],
- 'color' => $v['color'],
- 'buy_number' => $v['buy_number'],
- 'c_vipprice' => $v['c_vipprice'],
- 'c_freight' => $v['c_freight'],
- 'o_id' => $addOrderId,
- );
- //循环插入订单商品信息
- $add_order_commodity = Db::name('order_commodity')->insert($order_commodity_data);
- }
- if (!$add_order_commodity) {
- return json(['code' => 100, 'msg' => '网络错误', 'data' => '']);
- }
- //创建地址信息
- $address = Address::where('a_id', $params['a_id'])->find();
- $order_address = array(
- 'a_id' => $params['a_id'],
- 'a_name' => $address['a_name'],
- 'a_tel' => $address['a_tel'],
- 'a_city' => $address['city'],
- 'a_area' => $address['area'],
- 'o_id' => $addOrderId
- );
- //订单地址添加
- $add_order_address = Db::name('order_address')->insert($order_address);
- if (!$add_order_address) {
- return json(['code' => 200, 'msg' => '下单失败', 'data' => $addOrderId]);
- }
- //获取微信pre_id
- $wxpay = new WxPay();
- $body = "商品支付";
- $total_fee = $params['money'];
- $out_trade_no = $params['number'];
- $notify_url = config('site.httpurl') . 'api/order/order_notify';
- $res = $wxpay->getPrePayOrder($body, $out_trade_no, $total_fee, $notify_url);
- if ($res['return_code'] == "SUCCESS") {
- if ($res['return_msg'] == 'OK') {
- $result['prepay_id'] = $res['prepay_id'];
- $result['o_id'] = $addOrderId;
- return json(['data' => $result, 'msg' => '', 'code' => 200]);
- } else {
- return json(['data' => [], 'msg' => '拉取失败', 'code' => 100]);
- }
- } else {
- return json(['data' => [], 'msg' => '拉取失败', 'code' => 100]);
- }
- }
- /**
- * 全部订单
- */
- public function allOrder($user_id)
- {
- //全部订单
- $data = $this->with(['OrderCommodityModel', 'OrderAddress'])
- ->where('user_id', $user_id)
- ->select();
- if (!$data) {
- $data = [];
- }
- //待支付订单
- $data1 = $this->with(['OrderCommodityModel', 'OrderAddress'])
- ->where('state', 1)
- ->where('user_id', $user_id)
- ->select();
- if (!$data1) {
- $data1 = [];
- }
- //待发货订单
- $data2 = $this->with(['OrderCommodityModel', 'OrderAddress'])
- ->where('state', 2)
- ->where('user_id', $user_id)
- ->select();
- if (!$data2) {
- $data2 = [];
- }
- //待收货订单
- $data3 = $this->with(['OrderCommodityModel', 'OrderAddress'])
- ->where('state', 3)
- ->where('user_id', $user_id)
- ->select();
- if (!$data3) {
- $data3 = [];
- }
- //订单取消
- $data5 = $this->with(['OrderCommodityModel', 'OrderAddress'])
- ->where('state', 5)
- ->where('user_id', $user_id)
- ->select();
- if (!$data5) {
- $data5 = [];
- }
- //已完成订单
- $data6 = $this->with(['OrderCommodityModel', 'OrderAddress'])
- ->where('state', 6)
- ->where('user_id', $user_id)
- ->select();
- if (!$data6) {
- $data6 = [];
- }
- //退款售后
- $data4 = $this->with(['OrderCommodityModel', 'OrderAddress'])
- ->where('state', 4)
- ->where('user_id', $user_id)
- ->select();
- if (!$data4) {
- $data4 = [];
- }
- return json(['code' => 200, 'msg' => '', 'allorder' => $data, 'daizhifu' => $data1, 'daifahuo' => $data2, 'daishouhuo' => $data3, 'quxiao' => $data5, 'yiwancheng' => $data6, 'tuikuan' => $data4]);
- }
- public function OrderCommodityModel()
- {
- return $this->hasMany('OrderCommodityModel', 'o_id', 'o_id');
- }
- public function OrderAddress()
- {
- return $this->hasOne('OrderAddress', 'o_id', 'o_id');
- }
- public function Torder()
- {
- return $this->hasOne('Torder', 'o_id', 'o_id');
- }
- }
|