OrderModel.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. namespace app\api\model;
  3. use app\common\lib\WxPay;
  4. use think\Cache;
  5. use think\Db;
  6. use think\Model;
  7. class OrderModel extends Model
  8. {
  9. protected $name = 'order';
  10. protected $resultSetType = 'collection';
  11. /*
  12. * 用户余额支付
  13. */
  14. public function userMoneyPay($params)
  15. {
  16. $userInfo = UsersModel::where('user_id', $params['user_id'])->find();
  17. if ($userInfo['is_setpaypwd'] == 0) {
  18. return json(['code' => 90, 'msg' => '还未设置支付密码', 'data' => []]);
  19. }
  20. if ($userInfo['user_paypwd'] != $params['user_paypwd']) {
  21. return json(['code' => 100, 'msg' => '支付密码不正确', 'data' => '']);
  22. }
  23. if (!$userInfo) {
  24. return json(['code' => 100, 'msg' => '网络错误', 'data' => '']);
  25. }
  26. //对照金额书否正确的
  27. // $isMoney = Db::name('order_pre')->where('pre_id', $params['pre_id'])->where('money', $params['money'])->find();
  28. // if (!$isMoney) {
  29. // return json(['code' => 100, 'msg' => '金额不对', 'data' => '']);
  30. // }
  31. if ($userInfo['user_money'] < $params['money']) {
  32. return json(['code' => 100, 'msg' => '您的余额不足', 'data' => '']);
  33. }
  34. if ($userInfo['user_paypwd'] == '') {
  35. return json(['code' => 60, 'msg' => '还未设置支付密码,请去设置', 'data' => '']);
  36. }
  37. Db::startTrans();
  38. try {
  39. //扣除用户余额
  40. $updUserMoney = UsersModel::where('user_id', $params['user_id'])->setDec('user_money', $params['money']);
  41. Db::commit();
  42. } catch (\Exception $e) {
  43. Db::rollback();
  44. }
  45. if (!$updUserMoney) {
  46. return json(['code' => 100, 'msg' => '扣款失败', 'data' => '']);
  47. }
  48. $list = $params['list'];
  49. //循环加入奖励的白豆
  50. $whitebean = 0; // 白豆初始化
  51. foreach ($list as &$v) {
  52. $commodity = CommodityModel::where('c_id', $v['c_id'])->find()->toArray(); // 查出商品详情
  53. $whitebean = $commodity['c_whitebean'] + $whitebean;
  54. }
  55. //创建订单信息
  56. $orderdata = array(
  57. 'user_id' => $params['user_id'],
  58. 'money' => $params['money'],
  59. 'freight' => $params['freight'],
  60. 'number' => $params['number'],
  61. 'create_time' => $params['create_time'],
  62. 'whitebeon' => $whitebean,
  63. 'pay_time' => date("Y-m-d H:i:s", time()),
  64. 'state' => 2,
  65. 'type' => $params['type'],
  66. );
  67. //生成已支付订单
  68. $addOrderId = Db::name('order')->insertGetId($orderdata);
  69. if (!$addOrderId) {
  70. return json(['code' => 100, 'msg' => '订单生成失败', 'data' => '']);
  71. }
  72. $list = $params['list'];
  73. //循环加入奖励的白豆
  74. $whitebean = 0; // 白豆初始化
  75. foreach ($list as &$v) {
  76. $commodity = CommodityModel::where('c_id', $v['c_id'])->find()->toArray(); // 查出商品详情
  77. $whitebean = $commodity['c_whitebean'] + $whitebean;
  78. }
  79. //创建订单商品信息
  80. foreach ($list as &$v) {
  81. $commoditycolor = Commoditycolor::where('colorid', $v['colorid'])->find()->toArray(); //查 出商品颜色表参数
  82. $parameter = Parameter::where('p_id', $v['p_id'])->find()->toArray(); // 查出商品规格表参数
  83. $commodity = CommodityModel::where('c_id', $v['c_id'])->field('c_vipprice,c_freight')->find()->toArray(); // 查出商品价格和运费
  84. $v = array_merge($commoditycolor, $parameter, $commodity, $v);
  85. $order_commodity_data = array(
  86. 'c_id' => $v['c_id'],
  87. 'p_id' => $v['p_id'],
  88. 'colorid' => $v['colorid'],
  89. 'name' => $v['name'],
  90. 'p_name' => $v['p_name'],
  91. 'color' => $v['color'],
  92. 'buy_number' => $v['buy_number'],
  93. 'c_vipprice' => $v['money'],
  94. 'c_freight' => $v['c_freight'],
  95. 'o_id' => $addOrderId,
  96. );
  97. //循环插入订单商品信息
  98. $add_order_commodity = Db::name('order_commodity')->insert($order_commodity_data);
  99. }
  100. if (!$add_order_commodity) {
  101. return json(['code' => 100, 'msg' => '网络错误', 'data' => '']);
  102. }
  103. //创建地址信息
  104. $address = Address::where('a_id', $params['a_id'])->find();
  105. $order_address = array(
  106. 'a_id' => $params['a_id'],
  107. 'a_name' => $address['a_name'],
  108. 'a_tel' => $address['a_tel'],
  109. 'a_city' => $address['city'],
  110. 'a_area' => $address['area'],
  111. 'o_id' => $addOrderId
  112. );
  113. //订单地址添加
  114. $add_order_address = Db::name('order_address')->insert($order_address);
  115. if ($add_order_address) {
  116. //用户白豆增加
  117. Db::name('users')->where('user_id', $params['user_id'])->setInc('user_whitebean', $whitebean);
  118. return json(['code' => 200, 'msg' => '下单成功', 'data' => $addOrderId]);
  119. } else {
  120. Db::name('order')->where('o_id', $addOrderId)->delete();
  121. return json(['code' => 200, 'msg' => '下单失败', 'data' => $addOrderId]);
  122. }
  123. }
  124. /*
  125. * 订单微信支付
  126. */
  127. public function wechatOrder($params)
  128. {
  129. $userInfo = UsersModel::where('user_id', $params['user_id'])->find();
  130. if (!$userInfo) {
  131. return json(['code' => 100, 'msg' => '网络错误11', 'data' => '']);
  132. }
  133. //对照金额书否正确的
  134. // $isMoney = Db::name('order_pre')->where('pre_id', $params['pre_id'])->where('money', $params['money'])->find();
  135. // if (!$isMoney) {
  136. // return json(['code' => 100, 'msg' => '金额不对', 'data' => '']);
  137. // }
  138. //创建订单信息
  139. $orderdata = array(
  140. 'user_id' => $params['user_id'],
  141. 'money' => $params['money'],
  142. 'freight' => $params['freight'],
  143. 'number' => $params['number'],
  144. 'create_time' => $params['create_time'],
  145. 'whitebeon' => $params['whitebean'],
  146. 'pay_time' => date("Y-m-d H:i:s", time()),
  147. 'state' => 1,
  148. 'type' => $params['type'],
  149. );
  150. //生成已支付订单
  151. $addOrderId = Db::name('order')->insertGetId($orderdata);
  152. if (!$addOrderId) {
  153. return json(['code' => 100, 'msg' => '订单生成失败', 'data' => '']);
  154. }
  155. $list = $params['list'];
  156. //创建订单商品信息
  157. foreach ($list as &$v) {
  158. $commoditycolor = Commoditycolor::where('colorid', $v['colorid'])->find()->toArray(); //查 出商品颜色表参数
  159. $parameter = Parameter::where('p_id', $v['p_id'])->find()->toArray(); // 查出商品规格表参数
  160. $commodity = CommodityModel::where('c_id', $v['c_id'])->field('c_vipprice,c_freight')->find()->toArray(); // 查出商品价格和运费
  161. $v = array_merge($commoditycolor, $parameter, $commodity, $v);
  162. $order_commodity_data = array(
  163. 'c_id' => $v['c_id'],
  164. 'p_id' => $v['p_id'],
  165. 'colorid' => $v['colorid'],
  166. 'name' => $v['name'],
  167. 'p_name' => $v['p_name'],
  168. 'color' => $v['color'],
  169. 'buy_number' => $v['buy_number'],
  170. 'c_vipprice' => $v['money'], // 颜色商品的价格
  171. 'c_freight' => $v['c_freight'],
  172. 'o_id' => $addOrderId,
  173. );
  174. //循环插入订单商品信息
  175. $add_order_commodity = Db::name('order_commodity')->insert($order_commodity_data);
  176. }
  177. // if (!$add_order_commodity) {
  178. // return json(['code' => 100, 'msg' => '网络错误22', 'data' => '']);
  179. // }
  180. //创建地址信息
  181. $address = Address::where('a_id', $params['a_id'])->find();
  182. $order_address = array(
  183. 'a_id' => $params['a_id'],
  184. 'a_name' => $address['a_name'],
  185. 'a_tel' => $address['a_tel'],
  186. 'a_city' => $address['city'],
  187. 'a_area' => $address['area'],
  188. 'o_id' => $addOrderId
  189. );
  190. //订单地址添加
  191. $add_order_address = Db::name('order_address')->insert($order_address);
  192. if (!$add_order_address) {
  193. return json(['code' => 200, 'msg' => '下单失败', 'data' => $addOrderId]);
  194. }
  195. //获取微信pre_id
  196. $wxpay = new WxPay();
  197. $body = "商品支付";
  198. //$total_fee = $params['money'] * 100;
  199. $total_fee = 1;
  200. $out_trade_no = $params['number'];
  201. $notify_url = config('site.httpurl') . '/api/order/order_notify';
  202. $res = $wxpay->getPrePayOrder($body, $out_trade_no, $total_fee, $notify_url);
  203. if ($res['return_code'] == "SUCCESS") {
  204. if ($res['return_msg'] == 'OK') {
  205. $result['params'] = $wxpay->getOrder($res['prepay_id']);
  206. $result['o_id'] = $addOrderId;
  207. $wechatinfo = array(
  208. "appid" => $result['params']['appid'],
  209. "noncestr" => $result['params']['noncestr'],
  210. "package" => $result['params']['package'],
  211. "partnerid" => $result['params']['partnerid'],
  212. "prepayid" => $result['params']['prepayid'],
  213. "timestamp" => $result['params']['timestamp'],
  214. "sign" => $result['params']['sign'],
  215. "o_id" => $result['o_id'],
  216. );
  217. Cache::set('wechat', $result);
  218. // 插入拉取微信的支付的信息表
  219. $add = Db::name('order_wechatpay')->insert($wechatinfo);
  220. return json(['data' => $result, 'msg' => '', 'code' => 200]);
  221. } else {
  222. return json(['data' => [], 'msg' => '拉取失败', 'code' => 100]);
  223. }
  224. } else {
  225. return json(['data' => [], 'msg' => '拉取失败', 'code' => 100]);
  226. }
  227. }
  228. /**
  229. * 全部订单
  230. */
  231. public function allOrder($user_id)
  232. {
  233. //全部订单
  234. $data = $this->with(['OrderCommodityModel', 'OrderAddress'])
  235. ->where('user_id', $user_id)
  236. ->order('o_id desc')
  237. ->select()->toArray();
  238. if (!$data) {
  239. $data = [];
  240. } else {
  241. foreach ($data as &$v) {
  242. foreach ($v['order_commodity_model'] as &$n) {
  243. $commodity = CommodityModel::where('c_id', $n['c_id'])->field('c_name')->find();
  244. if (!$commodity) {
  245. $n['c_name'] = '已下架商品';
  246. } else {
  247. $n['c_name'] = $commodity['c_name'];
  248. }
  249. $n['c_money'] = $n['c_vipprice'] * $n['buy_number']; // c_vipprice存的是颜色商品的价格,而不是商品的初始价格
  250. $n['money'] = $n['c_vipprice'] * $n['buy_number'] + $n['c_freight'];
  251. $n['freight'] = $n['c_freight'] * $n['buy_number'];
  252. }
  253. }
  254. }
  255. //待支付订单
  256. $data1 = $this->with(['OrderCommodityModel' => function ($query) {
  257. $query->with('Commodity');
  258. }, 'OrderAddress'])
  259. ->where('state', 1)
  260. ->where('user_id', $user_id)
  261. ->order('o_id desc')
  262. ->select()->toArray();
  263. if (!$data1) {
  264. $data1 = [];
  265. } else {
  266. foreach ($data1 as &$v) {
  267. foreach ($v['order_commodity_model'] as &$n) {
  268. $commodity = CommodityModel::where('c_id', $n['c_id'])->field('c_name')->find();
  269. if (!$commodity) {
  270. $n['c_name'] = '已下架商品';
  271. } else {
  272. $n['c_name'] = $commodity['c_name'];
  273. }
  274. $n['c_money'] = $n['c_vipprice'] * $n['buy_number'];
  275. $n['money'] = $n['c_vipprice'] * $n['buy_number'] + $n['c_freight'];
  276. $n['freight'] = $n['c_freight'] * $n['buy_number'];
  277. }
  278. }
  279. }
  280. //待发货订单
  281. $data2 = $this->with(['OrderCommodityModel', 'OrderAddress'])
  282. ->where('state', 2)
  283. ->where('user_id', $user_id)
  284. ->order('o_id desc')
  285. ->select()->toArray();
  286. if (!$data2) {
  287. $data2 = [];
  288. } else {
  289. foreach ($data2 as &$v) {
  290. foreach ($v['order_commodity_model'] as &$n) {
  291. $commodity = CommodityModel::where('c_id', $n['c_id'])->field('c_name')->find();
  292. if (!$commodity) {
  293. $n['c_name'] = '已下架商品';
  294. } else {
  295. $n['c_name'] = $commodity['c_name'];
  296. }
  297. $n['c_money'] = $n['c_vipprice'] * $n['buy_number'];
  298. $n['money'] = $n['c_vipprice'] * $n['buy_number'] + $n['c_freight'];
  299. $n['freight'] = $n['c_freight'] * $n['buy_number'];
  300. }
  301. }
  302. }
  303. //待收货订单
  304. $data3 = $this->with(['OrderCommodityModel', 'OrderAddress'])
  305. ->where('state', 3)
  306. ->where('user_id', $user_id)
  307. ->order('o_id desc')
  308. ->select()->toArray();
  309. if (!$data3) {
  310. $data3 = [];
  311. } else {
  312. foreach ($data3 as &$v) {
  313. foreach ($v['order_commodity_model'] as &$n) {
  314. $commodity = CommodityModel::where('c_id', $n['c_id'])->field('c_name')->find();
  315. if (!$commodity) {
  316. $n['c_name'] = '已下架商品';
  317. } else {
  318. $n['c_name'] = $commodity['c_name'];
  319. }
  320. $n['c_money'] = $n['c_vipprice'] * $n['buy_number'];
  321. $n['money'] = $n['c_vipprice'] * $n['buy_number'] + $n['c_freight'];
  322. $n['freight'] = $n['c_freight'] * $n['buy_number'];
  323. }
  324. }
  325. }
  326. //订单取消
  327. $data5 = $this->with(['OrderCommodityModel', 'OrderAddress'])
  328. ->where('state', 5)
  329. ->where('user_id', $user_id)
  330. ->order('o_id desc')
  331. ->select()->toArray();
  332. if (!$data5) {
  333. $data5 = [];
  334. } else {
  335. foreach ($data5 as &$v) {
  336. foreach ($v['order_commodity_model'] as &$n) {
  337. $commodity = CommodityModel::where('c_id', $n['c_id'])->field('c_name')->find();
  338. if (!$commodity) {
  339. $n['c_name'] = '已下架商品';
  340. } else {
  341. $n['c_name'] = $commodity['c_name'];
  342. }
  343. $n['c_money'] = $n['c_vipprice'] * $n['buy_number'];
  344. $n['money'] = $n['c_vipprice'] * $n['buy_number'] + $n['c_freight'];
  345. $n['freight'] = $n['c_freight'] * $n['buy_number'];
  346. }
  347. }
  348. }
  349. //已完成订单
  350. $data6 = $this->with(['OrderCommodityModel', 'OrderAddress'])
  351. ->where('state', 6)
  352. ->where('user_id', $user_id)
  353. ->order('o_id desc')
  354. ->select()->toArray();
  355. if (!$data6) {
  356. $data6 = [];
  357. } else {
  358. foreach ($data6 as &$v) {
  359. foreach ($v['order_commodity_model'] as &$n) {
  360. $commodity = CommodityModel::where('c_id', $n['c_id'])->field('c_name')->find();
  361. if (!$commodity) {
  362. $n['c_name'] = '已下架商品';
  363. } else {
  364. $n['c_name'] = $commodity['c_name'];
  365. }
  366. $n['c_money'] = $n['c_vipprice'] * $n['buy_number'];
  367. $n['money'] = $n['c_vipprice'] * $n['buy_number'] + $n['c_freight'];
  368. $n['freight'] = $n['c_freight'] * $n['buy_number'];
  369. }
  370. }
  371. }
  372. //退款售后
  373. $data4 = $this->with(['OrderCommodityModel', 'OrderAddress'])
  374. ->where('state', 4)
  375. ->where('user_id', $user_id)
  376. ->order('o_id desc')
  377. ->select()->toArray();
  378. if (!$data4) {
  379. $data4 = [];
  380. } else {
  381. foreach ($data4 as &$v) {
  382. foreach ($v['order_commodity_model'] as &$n) {
  383. $commodity = CommodityModel::where('c_id', $n['c_id'])->field('c_name')->find();
  384. if (!$commodity) {
  385. $n['c_name'] = '已下架商品';
  386. } else {
  387. $n['c_name'] = $commodity['c_name'];
  388. }
  389. $n['c_money'] = $n['c_vipprice'] * $n['buy_number'];
  390. $n['money'] = $n['c_vipprice'] * $n['buy_number'] + $n['c_freight'];
  391. $n['freight'] = $n['c_freight'] * $n['buy_number'];
  392. }
  393. }
  394. }
  395. return json(['code' => 200, 'msg' => '', 'allorder' => $data, 'daizhifu' => $data1, 'daifahuo' => $data2, 'daishouhuo' => $data3, 'quxiao' => $data5, 'yiwancheng' => $data6, 'tuikuan' => $data4]);
  396. }
  397. public function OrderCommodityModel()
  398. {
  399. return $this->hasMany('OrderCommodityModel', 'o_id', 'o_id');
  400. }
  401. public function OrderAddress()
  402. {
  403. return $this->hasOne('OrderAddress', 'o_id', 'o_id');
  404. }
  405. public function Torder()
  406. {
  407. return $this->hasOne('Torder', 'o_id', 'o_id');
  408. }
  409. }