123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- <?php
- namespace app\mobile\logic;
- use app\model\AccountModel;
- use app\model\AddrModel;
- use app\model\CommonModel;
- use app\model\GoodModel;
- use app\model\GroupModel;
- use app\model\OrderModel;
- use app\model\PayInfoModel;
- use app\model\ServiceModel;
- use app\model\ThemeModel;
- use app\model\VideoModel;
- use think\Exception;
- use think\facade\Db;
- use think\response\Json;
- //公共
- class CommonLogic extends BaseLogic
- {
- //视频列表
- public static function getVideoList(array $data = []): Json
- {
- $rs = AccountModel::field('id,video_ids')
- ->where(['id' => self::$aid, 'is_del' => CommonModel::$del_normal])
- ->findOrEmpty()
- ->toArray();
- if (empty($rs)) return json_show(CommonModel::$error_param, '该账户不存在');
- $db = VideoModel::where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
- ->whereIn('id', $rs['video_ids']);
- $count = $db->count('id');
- $list = $db
- ->field('id,video_sn,video_name,video_url,video_img')
- ->page($data['page'], $data['size'])
- ->order(['weight' => 'desc', 'id' => 'desc'])
- ->select()
- ->toArray();
- return json_show(CommonModel::$success, '获取视频列表成功', ['count' => $count, 'list' => $list]);
- }
- //手机主题
- public static function theme(): Json
- {
- $rs = ThemeModel::field('id,code')
- ->where(['is_del' => CommonModel::$del_normal, 'group_id' => self::$group_id, 'status' => CommonModel::$status_normal])
- ->append(['modular'])
- ->withAttr('modular', function ($val, $data) {
- return Db::name('theme_modular')
- ->field('id,title,type')
- ->where(['is_del' => CommonModel::$del_normal, 'theme_id' => $data['id'], 'status' => CommonModel::$status_normal])
- ->order(['addtime' => 'desc', 'id' => 'desc'])
- ->append(['data'])
- ->withAttr('data', function ($v, $d) {
- return Db::name('theme_modular_data')
- ->field('id,img,jump_type,jump_param,good_name,good_id,style_type')
- ->where(['is_del' => CommonModel::$del_normal, 'theme_modular_id' => $d['id']])
- ->order(['addtime' => 'desc', 'id' => 'desc'])
- ->select()
- ->toArray();
- })
- ->select()
- ->toArray();
- })
- ->findOrEmpty()
- ->toArray();
- return empty($rs) ? json_show(CommonModel::$error_param, '该手机主题不存在') : json_show(CommonModel::$success, '获取手机主题成功', $rs);
- }
- //微信支付预下单
- public static function getPrepayId(array $data = []): json
- {
- $openId = AccountModel::where(['id' => self::$aid, 'is_del' => CommonModel::$del_normal])
- ->value('wx_openId', '');
- if ($openId == '') return json_show(CommonModel::$error_token, '获取账户openId失败,请重新登录');
- Db::startTrans();
- try {
- $rs = AddrModel::field('id')
- ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['addr_id'], 'uid' => self::$aid])
- ->findOrEmpty()
- ->isEmpty();
- if ($rs) throw new Exception('该地址不存在');
- $amount = '0';//支付金额
- $num = array_column($data['list'], 'num', 'id');//
- $date = date('Y-m-d H:i:s');
- $code = make_no('ZF');
- //支付信息表
- $pay_info_id = Db::name('pay_info')->insertGetId([
- 'code' => $code,
- 'wx_openId' => $openId,
- 'type' => $data['type'],
- 'ids' => json_encode($data['list']),
- 'status' => CommonModel::$order_status_wait_pay,
- 'createrid' => self::$aid,
- 'creater' => self::$aname,
- 'addtime' => $date,
- 'updaterid' => self::$aid,
- 'updater' => self::$aname,
- 'updatetime' => $date,
- ]);
- if ($data['type'] == CommonModel::$pay_type_service) {
- //购买服务
- $rs = ServiceModel::field('id,original_price,activity_price,title,activity_status')
- ->whereIn('id', array_column($data['list'], 'id'))
- ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
- ->select()
- ->toArray();
- if (empty($rs)) throw new Exception('购买的服务不存在或已下架');
- $insert_service = [];
- foreach ($rs as $item) {
- if ($item['activity_status'] == ServiceModel::$activity_status_ing) $total = bcmul($rs['activity_price'], $num[$item['id']] ?? 0, 2);
- else $total = bcmul($rs['original_price'], $num[$item['id']] ?? 0, 2);
- $amount = bcadd($amount, $total, 2);//总金额
- $insert_service[] = [
- 'orderCode' => make_no('FW'),
- 'pay_info_id' => $pay_info_id,
- 'uid' => self::$aid,
- 'service_id' => $item['id'],
- 'num' => $num[$item['id']] ?? 0,
- 'addr_id' => $data['addr_id'],
- 'amount' => $total,
- 'status' => CommonModel::$order_status_wait_pay,
- 'is_del' => CommonModel::$del_normal,
- 'createrid' => self::$aid,
- 'creater' => self::$aname,
- 'addtime' => $date,
- 'updaterid' => self::$aid,
- 'updater' => self::$aname,
- 'updatetime' => $date,
- ];
- }
- //向服务订单表写数据
- Db::name('order_service')->insertAll($insert_service);
- } else {
- //购买商城商品
- $order_insert = [];
- $rs = GoodModel::field('id,price,good_name')
- ->whereIn('id', array_column($data['list'], 'id'))
- ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
- ->select()
- ->toArray();
- if (empty($rs)) throw new Exception('购买的商品不存在或已禁用');
- $i = 0;
- $orderCode = make_no('QR');
- $orderCode = substr($orderCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
- foreach ($rs as $item) {
- $total_price = bcmul($item['price'], $num[$item['id']] ?? 0, 2);//总价
- $order_insert[] = [
- 'orderCode' => $orderCode,
- 'pay_info_id' => $pay_info_id,
- 'company_id' => self::$company_id,
- 'card_id' => self::$card_id,
- 'uid' => self::$aid,
- 'good_id' => $item['id'],
- 'num' => $num[$item['id']],
- 'price' => $item['price'],
- 'total_price' => $total_price,
- 'addr_id' => $data['addr_id'],
- 'type' => CommonModel::$pay_type_shopping_good,
- 'status' => CommonModel::$order_status_wait_pay,
- 'is_del' => CommonModel::$del_normal,
- 'createrid' => self::$aid,
- 'creater' => self::$aname,
- 'addtime' => $date,
- 'updaterid' => self::$aid,
- 'updater' => self::$aname,
- 'updatetime' => $date,
- ];
- $amount = bcadd($amount, $total_price, 2);
- $i++;
- }
- }
- //获取预支付信息,
- //@todo body要写什么????????
- $result = WechatLogic::getPrepayId($openId, '', $code, $amount);
- //更新pay_info表
- Db::name('pay_info')
- ->where('id', $pay_info_id)
- ->update([
- 'prepay_info' => $result,
- 'amount' => $amount,
- 'expiretime' => date('Y-m-d H:i:s', time() + 5 * 60),
- ]);
- //@todo 要给前端返回什么信息?
- $res = '';
- Db::commit();
- return json_show(CommonModel::$success, '微信支付预下单成功', $res);
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(CommonModel::$success, '微信支付预下单失败' . $exception->getMessage());
- }
- }
- }
|