CommonLogic.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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\PayInfoModel;
  8. use app\model\ServiceModel;
  9. use app\model\ThemeModel;
  10. use app\model\VideoModel;
  11. use think\Exception;
  12. use think\facade\Db;
  13. use think\response\Json;
  14. //公共
  15. class CommonLogic extends BaseLogic
  16. {
  17. //视频列表
  18. public static function getVideoList(array $data = []): Json
  19. {
  20. $rs = AccountModel::field('id,video_ids')
  21. ->where(['id' => self::$aid, 'is_del' => CommonModel::$del_normal])
  22. ->findOrEmpty()
  23. ->toArray();
  24. if (empty($rs)) return json_show(CommonModel::$error_param, '该账户不存在');
  25. $db = VideoModel::where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
  26. ->whereIn('id', $rs['video_ids']);
  27. $count = $db->count('id');
  28. $list = $db
  29. ->field('id,video_sn,video_name,video_url,video_img')
  30. ->page($data['page'], $data['size'])
  31. ->order(['weight' => 'desc', 'id' => 'desc'])
  32. ->select()
  33. ->toArray();
  34. return json_show(CommonModel::$success, '获取视频列表成功', ['count' => $count, 'list' => $list]);
  35. }
  36. //手机主题
  37. public static function theme(): Json
  38. {
  39. $rs = ThemeModel::field('id,code')
  40. ->where(['is_del' => CommonModel::$del_normal, 'group_id' => self::$group_id, 'status' => CommonModel::$status_normal])
  41. ->append(['modular'])
  42. ->withAttr('modular', function ($val, $data) {
  43. return Db::name('theme_modular')
  44. ->field('id,title,type')
  45. ->where(['is_del' => CommonModel::$del_normal, 'theme_id' => $data['id'], 'status' => CommonModel::$status_normal])
  46. ->order(['addtime' => 'desc', 'id' => 'desc'])
  47. ->append(['data'])
  48. ->withAttr('data', function ($v, $d) {
  49. return Db::name('theme_modular_data')
  50. ->field('id,img,jump_type,jump_param,good_name,good_id,style_type')
  51. ->where(['is_del' => CommonModel::$del_normal, 'theme_modular_id' => $d['id']])
  52. ->order(['addtime' => 'desc', 'id' => 'desc'])
  53. ->select()
  54. ->toArray();
  55. })
  56. ->select()
  57. ->toArray();
  58. })
  59. ->findOrEmpty()
  60. ->toArray();
  61. return empty($rs) ? json_show(CommonModel::$error_param, '该手机主题不存在') : json_show(CommonModel::$success, '获取手机主题成功', $rs);
  62. }
  63. //微信支付预下单
  64. public static function getPrepayId(array $data = []): json
  65. {
  66. $openId = AccountModel::where(['id' => self::$aid, 'is_del' => CommonModel::$del_normal])
  67. ->value('wx_openId', '');
  68. if ($openId == '') return json_show(CommonModel::$error_token, '获取账户openId失败,请重新登录');
  69. Db::startTrans();
  70. try {
  71. $amount = '0';//支付金额
  72. $num = array_column($data['list'], 'num', 'id');//
  73. $date = date('Y-m-d H:i:s');
  74. if ($data['type'] == CommonModel::$pay_type_service) {
  75. //购买服务
  76. $rs = ServiceModel::field('id,original_price,activity_price,title,activity_status')
  77. ->whereIn('id', array_column($data['list'], 'id'))
  78. ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
  79. ->select()
  80. ->toArray();
  81. if (empty($rs)) throw new Exception('购买的服务不存在或已下架');
  82. foreach ($rs as $item) {
  83. if ($item['activity_status'] == ServiceModel::$activity_status_ing) $amount = bcadd($amount, bcmul($rs['activity_price'], $num[$item['id']] ?? 0), 2);
  84. else $amount = bcadd($amount, bcmul($rs['original_price'], $num[$item['id']] ?? 0), 2);
  85. }
  86. } else {
  87. //购买商城商品
  88. $rs = GoodModel::field('id,price,good_name')
  89. ->whereIn('id', array_column($data['list'], 'id'))
  90. ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
  91. ->select()
  92. ->toArray();
  93. if (empty($rs)) throw new Exception('购买的商品不存在或已禁用');
  94. foreach ($rs as $item) {
  95. $amount = bcadd($amount, bcmul($rs['price'], $num[$item['id']] ?? 0), 2);
  96. }
  97. }
  98. $code = make_no('ZF');
  99. //获取预支付信息
  100. $result = WechatLogic::getPrepayId($openId, '', $code, $amount);
  101. PayInfoModel::create([
  102. 'code' => $code,
  103. 'wx_openId' => $openId,
  104. 'type' => $data['type'],
  105. 'ids' => json_encode($data['list']),
  106. 'prepay_info' => $result,
  107. 'amount' => $amount,
  108. '' => '',//流水号
  109. 'expiretime' => date('Y-m-d H:i:s', time() + 5 * 60),
  110. 'status' => PayInfoModel::$pay_wait,
  111. 'createrid' => self::$aid,
  112. 'creater' => self::$aname,
  113. 'addtime' => $date,
  114. 'updaterid' => self::$aid,
  115. 'updater' => self::$aname,
  116. 'updatetime' => $date,
  117. ])->save();
  118. Db::commit();
  119. return json_show(CommonModel::$success, '微信支付预下单成功', $res);
  120. } catch (Exception $exception) {
  121. Db::rollback();
  122. return json_show(CommonModel::$success, '微信支付预下单失败' . $exception->getMessage());
  123. }
  124. }
  125. }