OrderModel.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace app\api\model;
  3. use app\common\lib\WxPay;
  4. use think\Db;
  5. use think\Model;
  6. class OrderModel extends Model
  7. {
  8. protected $name = 'order';
  9. /*
  10. * 用户余额支付
  11. */
  12. public function userMoneyPay($params)
  13. {
  14. $userInfo = UsersModel::where('user_id', $params['user_id'])->find();
  15. if ($userInfo['user_user_paypwd'] != $params['user_paypwd']) {
  16. return json(['code' => 100, 'msg' => '支付密码不正确', 'data' => '']);
  17. }
  18. if (!$userInfo) {
  19. return json(['code' => 100, 'msg' => '网络错误', 'data' => '']);
  20. }
  21. //对照金额书否正确的
  22. $isMoney = Db::name('order_pre')->where('pre_id', $params['pre_id'])->where('money', $params['money'])->find();
  23. if (!$isMoney) {
  24. return json(['code' => 100, 'msg' => '金额不对', 'data' => '']);
  25. }
  26. if ($userInfo['user_money'] < $params['money']) {
  27. return json(['code' => 100, 'msg' => '您的余额不足', 'data' => '']);
  28. }
  29. if ($userInfo['user_paypwd'] == '') {
  30. return json(['code' => 60, 'msg' => '还未设置支付密码,请去设置', 'data' => '']);
  31. }
  32. Db::startTrans();
  33. try {
  34. //扣除用户余额
  35. $updUserMoney = UsersModel::where('user_id', $params['user_id'])->setDec('user_money', $params['money']);
  36. Db::commit();
  37. } catch (\Exception $e) {
  38. Db::rollback();
  39. }
  40. if (!$updUserMoney) {
  41. return json(['code' => 100, 'msg' => '扣款失败', 'data' => '']);
  42. }
  43. $list = $params['list'];
  44. //循环加入奖励的白豆
  45. $whitebean = 0; // 白豆初始化
  46. foreach ($list as &$v) {
  47. $commodity = CommodityModel::where('c_id', $v['c_id'])->find()->toArray(); // 查出商品详情
  48. $whitebean = $commodity['c_whitebean'] + $whitebean;
  49. }
  50. //创建订单信息
  51. $orderdata = array(
  52. 'user_id' => $params['user_id'],
  53. 'money' => $params['money'],
  54. 'freight' => $params['freight'],
  55. 'number' => $params['number'],
  56. 'create_time' => $params['create_time'],
  57. 'whitebeon' => $whitebean,
  58. 'pay_time' => date("Y-m-d H:i:s", time()),
  59. 'state' => 2,
  60. 'type' => $params['type'],
  61. );
  62. //生成已支付订单
  63. $addOrderId = Db::name('order')->insertGetId($orderdata);
  64. if (!$addOrderId) {
  65. return json(['code' => 100, 'msg' => '订单生成失败', 'data' => '']);
  66. }
  67. $list = $params['list'];
  68. //循环加入奖励的白豆
  69. $whitebean = 0; // 白豆初始化
  70. foreach ($list as &$v) {
  71. $commodity = CommodityModel::where('c_id', $v['c_id'])->find()->toArray(); // 查出商品详情
  72. $whitebean = $commodity['c_whitebean'] + $whitebean;
  73. }
  74. //创建订单商品信息
  75. foreach ($list as &$v) {
  76. $commoditycolor = Commoditycolor::where('colorid', $v['colorid'])->find()->toArray(); //查 出商品颜色表参数
  77. $parameter = Parameter::where('p_id', $v['p_id'])->find()->toArray(); // 查出商品规格表参数
  78. $commodity = CommodityModel::where('c_id', $v['c_id'])->field('c_vipprice,c_freight')->find()->toArray(); // 查出商品价格和运费
  79. $v = array_merge($commoditycolor, $parameter, $commodity, $v);
  80. $order_commodity_data = array(
  81. 'c_id' => $v['c_id'],
  82. 'p_id' => $v['p_id'],
  83. 'colorid' => $v['colorid'],
  84. 'name' => $v['name'],
  85. 'p_name' => $v['p_name'],
  86. 'color' => $v['color'],
  87. 'buy_number' => $v['buy_number'],
  88. 'c_vipprice' => $v['c_vipprice'],
  89. 'c_freight' => $v['c_freight'],
  90. 'o_id' => $addOrderId,
  91. );
  92. //循环插入订单商品信息
  93. $add_order_commodity = Db::name('order_commodity')->insert($order_commodity_data);
  94. }
  95. if (!$add_order_commodity) {
  96. return json(['code' => 100, 'msg' => '网络错误', 'data' => '']);
  97. }
  98. //创建地址信息
  99. $address = Address::where('a_id', $params['a_id'])->find();
  100. $order_address = array(
  101. 'a_id' => $params['a_id'],
  102. 'a_name' => $address['a_name'],
  103. 'a_tel' => $address['a_tel'],
  104. 'a_city' => $address['city'],
  105. 'a_area' => $address['area'],
  106. 'o_id' => $addOrderId
  107. );
  108. //订单地址添加
  109. $add_order_address = Db::name('order_address')->insert($order_address);
  110. if ($add_order_address) {
  111. return json(['code' => 200, 'msg' => '下单成功', 'data' => $addOrderId]);
  112. } else {
  113. return json(['code' => 200, 'msg' => '下单失败', 'data' => $addOrderId]);
  114. }
  115. }
  116. /*
  117. * 订单微信支付
  118. */
  119. public function wechatOrder($params)
  120. {
  121. $userInfo = UsersModel::where('user_id', $params['user_id'])->find();
  122. if (!$userInfo) {
  123. return json(['code' => 100, 'msg' => '网络错误', 'data' => '']);
  124. }
  125. //对照金额书否正确的
  126. $isMoney = Db::name('order_pre')->where('pre_id', $params['pre_id'])->where('money', $params['money'])->find();
  127. if (!$isMoney) {
  128. return json(['code' => 100, 'msg' => '金额不对', 'data' => '']);
  129. }
  130. //创建订单信息
  131. $orderdata = array(
  132. 'user_id' => $params['user_id'],
  133. 'money' => $params['money'],
  134. 'freight' => $params['freight'],
  135. 'number' => $params['number'],
  136. 'create_time' => $params['create_time'],
  137. 'whitebeon' => $params['whitebean'],
  138. 'pay_time' => date("Y-m-d H:i:s", time()),
  139. 'state' => 1,
  140. 'type' => $params['type'],
  141. );
  142. //生成已支付订单
  143. $addOrderId = Db::name('order')->insertGetId($orderdata);
  144. if (!$addOrderId) {
  145. return json(['code' => 100, 'msg' => '订单生成失败', 'data' => '']);
  146. }
  147. $list = $params['list'];
  148. //创建订单商品信息
  149. foreach ($list as &$v) {
  150. $commoditycolor = Commoditycolor::where('colorid', $v['colorid'])->find()->toArray(); //查 出商品颜色表参数
  151. $parameter = Parameter::where('p_id', $v['p_id'])->find()->toArray(); // 查出商品规格表参数
  152. $commodity = CommodityModel::where('c_id', $v['c_id'])->field('c_vipprice,c_freight')->find()->toArray(); // 查出商品价格和运费
  153. $v = array_merge($commoditycolor, $parameter, $commodity, $v);
  154. $order_commodity_data = array(
  155. 'c_id' => $v['c_id'],
  156. 'p_id' => $v['p_id'],
  157. 'colorid' => $v['colorid'],
  158. 'name' => $v['name'],
  159. 'p_name' => $v['p_name'],
  160. 'color' => $v['color'],
  161. 'buy_number' => $v['buy_number'],
  162. 'c_vipprice' => $v['c_vipprice'],
  163. 'c_freight' => $v['c_freight'],
  164. 'o_id' => $addOrderId,
  165. );
  166. //循环插入订单商品信息
  167. $add_order_commodity = Db::name('order_commodity')->insert($order_commodity_data);
  168. }
  169. if (!$add_order_commodity) {
  170. return json(['code' => 100, 'msg' => '网络错误', 'data' => '']);
  171. }
  172. //创建地址信息
  173. $address = Address::where('a_id', $params['a_id'])->find();
  174. $order_address = array(
  175. 'a_id' => $params['a_id'],
  176. 'a_name' => $address['a_name'],
  177. 'a_tel' => $address['a_tel'],
  178. 'a_city' => $address['city'],
  179. 'a_area' => $address['area'],
  180. 'o_id' => $addOrderId
  181. );
  182. //订单地址添加
  183. $add_order_address = Db::name('order_address')->insert($order_address);
  184. if (!$add_order_address) {
  185. return json(['code' => 200, 'msg' => '下单失败', 'data' => $addOrderId]);
  186. }
  187. //获取微信pre_id
  188. $wxpay = new WxPay();
  189. $body = "商品支付";
  190. $total_fee = $params['money'];
  191. $out_trade_no = $params['number'];
  192. $notify_url = config('site.httpurl') . 'api/order/order_notify';
  193. $res = $wxpay->getPrePayOrder($body, $out_trade_no, $total_fee, $notify_url);
  194. if ($res['return_code'] == "SUCCESS") {
  195. if ($res['return_msg'] == 'OK') {
  196. $result['prepay_id'] = $res['prepay_id'];
  197. $result['o_id'] = $addOrderId;
  198. return json(['data' => $result, 'msg' => '', 'code' => 200]);
  199. } else {
  200. return json(['data' => [], 'msg' => '拉取失败', 'code' => 100]);
  201. }
  202. } else {
  203. return json(['data' => [], 'msg' => '拉取失败', 'code' => 100]);
  204. }
  205. }
  206. /**
  207. * 全部订单
  208. */
  209. public function allOrder($user_id)
  210. {
  211. //全部订单
  212. $data = $this->with(['OrderCommodityModel', 'OrderAddress'])
  213. ->where('user_id', $user_id)
  214. ->select();
  215. if (!$data) {
  216. $data = [];
  217. }
  218. //待支付订单
  219. $data1 = $this->with(['OrderCommodityModel', 'OrderAddress'])
  220. ->where('state', 1)
  221. ->where('user_id', $user_id)
  222. ->select();
  223. if (!$data1) {
  224. $data1 = [];
  225. }
  226. //待发货订单
  227. $data2 = $this->with(['OrderCommodityModel', 'OrderAddress'])
  228. ->where('state', 2)
  229. ->where('user_id', $user_id)
  230. ->select();
  231. if (!$data2) {
  232. $data2 = [];
  233. }
  234. //待收货订单
  235. $data3 = $this->with(['OrderCommodityModel', 'OrderAddress'])
  236. ->where('state', 3)
  237. ->where('user_id', $user_id)
  238. ->select();
  239. if (!$data3) {
  240. $data3 = [];
  241. }
  242. //订单取消
  243. $data5 = $this->with(['OrderCommodityModel', 'OrderAddress'])
  244. ->where('state', 5)
  245. ->where('user_id', $user_id)
  246. ->select();
  247. if (!$data5) {
  248. $data5 = [];
  249. }
  250. //已完成订单
  251. $data6 = $this->with(['OrderCommodityModel', 'OrderAddress'])
  252. ->where('state', 6)
  253. ->where('user_id', $user_id)
  254. ->select();
  255. if (!$data6) {
  256. $data6 = [];
  257. }
  258. //退款售后
  259. $data4 = $this->with(['OrderCommodityModel', 'OrderAddress'])
  260. ->where('state', 4)
  261. ->where('user_id', $user_id)
  262. ->select();
  263. if (!$data4) {
  264. $data4 = [];
  265. }
  266. return json(['code' => 200, 'msg' => '', 'allorder' => $data, 'daizhifu' => $data1, 'daifahuo' => $data2, 'daishouhuo' => $data3, 'quxiao' => $data5, 'yiwancheng' => $data6, 'tuikuan' => $data4]);
  267. }
  268. public function OrderCommodityModel()
  269. {
  270. return $this->hasMany('OrderCommodityModel', 'o_id', 'o_id');
  271. }
  272. public function OrderAddress()
  273. {
  274. return $this->hasOne('OrderAddress', 'o_id', 'o_id');
  275. }
  276. public function Torder()
  277. {
  278. return $this->hasOne('Torder', 'o_id', 'o_id');
  279. }
  280. }