CommonLogic.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace app\mobile\logic;
  3. use app\model\AccountModel;
  4. use app\model\CommonModel;
  5. use app\model\GoodModel;
  6. use app\model\GroupModel;
  7. use app\model\OrderModel;
  8. use app\model\PayInfoModel;
  9. use app\model\ServiceModel;
  10. use app\model\ThemeModel;
  11. use app\model\VideoModel;
  12. use think\Exception;
  13. use think\facade\Db;
  14. use think\response\Json;
  15. //公共
  16. class CommonLogic extends BaseLogic
  17. {
  18. //视频列表
  19. public static function getVideoList(array $data = []): Json
  20. {
  21. $rs = AccountModel::field('id,video_ids')
  22. ->where(['id' => self::$aid, 'is_del' => CommonModel::$del_normal])
  23. ->findOrEmpty()
  24. ->toArray();
  25. if (empty($rs)) return json_show(CommonModel::$error_param, '该账户不存在');
  26. $db = VideoModel::where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
  27. ->whereIn('id', $rs['video_ids']);
  28. $count = $db->count('id');
  29. $list = $db
  30. ->field('id,video_sn,video_name,video_url,video_img')
  31. ->page($data['page'], $data['size'])
  32. ->order(['weight' => 'desc', 'id' => 'desc'])
  33. ->select()
  34. ->toArray();
  35. return json_show(CommonModel::$success, '获取视频列表成功', ['count' => $count, 'list' => $list]);
  36. }
  37. //手机主题
  38. public static function theme(): Json
  39. {
  40. $rs = ThemeModel::field('id,code')
  41. ->where(['is_del' => CommonModel::$del_normal, 'group_id' => self::$group_id, 'status' => CommonModel::$status_normal])
  42. ->append(['modular'])
  43. ->withAttr('modular', function ($val, $data) {
  44. return Db::name('theme_modular')
  45. ->field('id,title,type')
  46. ->where(['is_del' => CommonModel::$del_normal, 'theme_id' => $data['id'], 'status' => CommonModel::$status_normal])
  47. ->order(['addtime' => 'desc', 'id' => 'desc'])
  48. ->append(['data'])
  49. ->withAttr('data', function ($v, $d) {
  50. return Db::name('theme_modular_data')
  51. ->field('id,img,jump_type,jump_param,good_name,good_id,style_type')
  52. ->where(['is_del' => CommonModel::$del_normal, 'theme_modular_id' => $d['id']])
  53. ->order(['addtime' => 'desc', 'id' => 'desc'])
  54. ->select()
  55. ->toArray();
  56. })
  57. ->select()
  58. ->toArray();
  59. })
  60. ->findOrEmpty()
  61. ->toArray();
  62. return empty($rs) ? json_show(CommonModel::$error_param, '该手机主题不存在') : json_show(CommonModel::$success, '获取手机主题成功', $rs);
  63. }
  64. //微信支付预下单
  65. public static function getPrepayId(array $data = []): json
  66. {
  67. $openId = AccountModel::where(['id' => self::$aid, 'is_del' => CommonModel::$del_normal])
  68. ->value('wx_openId', '');
  69. if ($openId == '') return json_show(CommonModel::$error_token, '获取账户openId失败,请重新登录');
  70. Db::startTrans();
  71. try {
  72. $amount = '0';//支付金额
  73. $num = array_column($data['list'], 'num', 'id');//
  74. $date = date('Y-m-d H:i:s');
  75. $code = make_no('ZF');
  76. //支付信息表
  77. $pay_info_id = Db::name('pay_info')->insertGetId([
  78. 'code' => $code,
  79. 'wx_openId' => $openId,
  80. 'type' => $data['type'],
  81. 'ids' => json_encode($data['list']),
  82. 'status' => CommonModel::$order_status_wait_pay,
  83. 'createrid' => self::$aid,
  84. 'creater' => self::$aname,
  85. 'addtime' => $date,
  86. 'updaterid' => self::$aid,
  87. 'updater' => self::$aname,
  88. 'updatetime' => $date,
  89. ]);
  90. if ($data['type'] == CommonModel::$pay_type_service) {
  91. //购买服务
  92. $rs = ServiceModel::field('id,original_price,activity_price,title,activity_status')
  93. ->whereIn('id', array_column($data['list'], 'id'))
  94. ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
  95. ->select()
  96. ->toArray();
  97. if (empty($rs)) throw new Exception('购买的服务不存在或已下架');
  98. $insert_service = [];
  99. foreach ($rs as $item) {
  100. if ($item['activity_status'] == ServiceModel::$activity_status_ing) $total = bcmul($rs['activity_price'], $num[$item['id']] ?? 0, 2);
  101. else $total = bcmul($rs['original_price'], $num[$item['id']] ?? 0, 2);
  102. $amount = bcadd($amount, $total, 2);//总金额
  103. $insert_service[] = [
  104. 'orderCode' => make_no('FW'),
  105. 'pay_info_id' => $pay_info_id,
  106. 'uid' => self::$aid,
  107. 'service_id' => $item['id'],
  108. 'num' => $num[$item['id']] ?? 0,
  109. 'addr_id' => $data['addr_id'],
  110. 'amount' => $total,
  111. 'status' => CommonModel::$order_status_wait_pay,
  112. 'is_del' => CommonModel::$del_normal,
  113. 'createrid' => self::$aid,
  114. 'creater' => self::$aname,
  115. 'addtime' => $date,
  116. 'updaterid' => self::$aid,
  117. 'updater' => self::$aname,
  118. 'updatetime' => $date,
  119. ];
  120. }
  121. //向服务订单表写数据
  122. Db::name('order_service')->insertAll($insert_service);
  123. } else {
  124. //购买商城商品
  125. $order_insert = [];
  126. $rs = GoodModel::field('id,price,good_name')
  127. ->whereIn('id', array_column($data['list'], 'id'))
  128. ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
  129. ->select()
  130. ->toArray();
  131. if (empty($rs)) throw new Exception('购买的商品不存在或已禁用');
  132. $i = 0;
  133. $orderCode = make_no('QR');
  134. $orderCode = substr($orderCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  135. foreach ($rs as $item) {
  136. $total_price = bcmul($item['price'], $num[$item['id']] ?? 0, 2);//总价
  137. $order_insert[] = [
  138. 'orderCode' => $orderCode,
  139. 'pay_info_id' => $pay_info_id,
  140. 'company_id' => self::$company_id,
  141. 'card_id' => self::$card_id,
  142. 'uid' => self::$aid,
  143. 'good_id' => $item['id'],
  144. 'num' => $num[$item['id']],
  145. 'price' => $item['price'],
  146. 'total_price' => $total_price,
  147. 'addr_id' => $data['addr_id'],
  148. 'type' => CommonModel::$pay_type_shopping_good,
  149. 'status' => CommonModel::$order_status_wait_pay,
  150. 'is_del' => CommonModel::$del_normal,
  151. 'createrid' => self::$aid,
  152. 'creater' => self::$aname,
  153. 'addtime' => $date,
  154. 'updaterid' => self::$aid,
  155. 'updater' => self::$aname,
  156. 'updatetime' => $date,
  157. ];
  158. $amount = bcadd($amount, $total_price, 2);
  159. $i++;
  160. }
  161. }
  162. //获取预支付信息,
  163. //@todo body要写什么????????
  164. $result = WechatLogic::getPrepayId($openId, '', $code, $amount);
  165. //更新pay_info表
  166. Db::name('pay_info')
  167. ->where('id', $pay_info_id)
  168. ->update([
  169. 'prepay_info' => $result,
  170. 'amount' => $amount,
  171. 'expiretime' => date('Y-m-d H:i:s', time() + 5 * 60),
  172. ]);
  173. //@todo 要给前端返回什么信息?
  174. $res = '';
  175. Db::commit();
  176. return json_show(CommonModel::$success, '微信支付预下单成功', $res);
  177. } catch (Exception $exception) {
  178. Db::rollback();
  179. return json_show(CommonModel::$success, '微信支付预下单失败' . $exception->getMessage());
  180. }
  181. }
  182. }