No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UsersRechargePack.php 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海口快推科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 陈风任 <491085389@qq.com>
  11. * Date: 2019-1-7
  12. */
  13. namespace app\common\model;
  14. use think\Db;
  15. use think\Cache;
  16. use think\Config;
  17. /**
  18. * 会员充值套餐模型
  19. */
  20. load_trait('controller/Jump');
  21. class UsersRechargePack
  22. {
  23. use \traits\controller\Jump;
  24. private $times = 0;
  25. private $param = [];
  26. private $users = [];
  27. private $users_id = 0;
  28. // 构造函数
  29. public function __construct($param = [], $users = [])
  30. {
  31. // 统一接收参数处理
  32. $this->times = getTime();
  33. $this->param = !empty($param) ? $param : [];
  34. $this->users = !empty($users) ? $users : [];
  35. $this->users_id = !empty($this->users['users_id']) ? intval($this->users['users_id']) : 0;
  36. }
  37. // 会员余额中心
  38. public function usersMoneyCenter()
  39. {
  40. // 查询条件
  41. $condition = [];
  42. array_push($condition, "status IN (2, 3)");
  43. array_push($condition, "users_id = " . $this->users_id);
  44. // 余额类型查询(收入、支出)
  45. $decrease_type = [3, 5, 6];
  46. $increase_type = [1, 2, 4, 7];
  47. // 查询总收入和总支出
  48. $where = "";
  49. if (0 < count($condition)) $where = implode(" AND ", $condition);
  50. $allIncrease = $allDecrease = 0;
  51. $money = Db::name('users_money')->where($where)->select();
  52. foreach ($money as $key => $value) {
  53. // 收入
  54. if (in_array($value['cause_type'], $increase_type)) {
  55. $allIncrease = unifyPriceHandle(unifyPriceHandle($allIncrease) + unifyPriceHandle($value['money']));
  56. }
  57. // 支出
  58. else if (in_array($value['cause_type'], $decrease_type) || (0 === intval($value['cause_type']) && 'balance' == $value['pay_method'])) {
  59. $allDecrease = unifyPriceHandle(unifyPriceHandle($allDecrease) + unifyPriceHandle($value['money']));
  60. }
  61. }
  62. $result['allIncrease'] = $allIncrease;
  63. $result['allDecrease'] = $allDecrease;
  64. // 金额类型条件
  65. if (!empty($this->param['moneyType']) && in_array($this->param['moneyType'], [10, 20])) {
  66. // 收入
  67. if (10 === intval($this->param['moneyType'])) {
  68. $cause_type = implode(',', $increase_type);
  69. array_push($condition, "cause_type IN ({$cause_type})");
  70. }
  71. // 支出
  72. else {
  73. $cause_type = implode(',', $decrease_type);
  74. array_push($condition, "(cause_type IN ({$cause_type}) OR (cause_type = 0 AND pay_method ='balance'))");
  75. }
  76. }
  77. // 余额操作时间查询
  78. if (!empty($this->param['date'])) {
  79. $firstday = date('Y-m-01', strtotime($this->param['date']));
  80. $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
  81. $firstday = strtotime($firstday);
  82. $lastday = strtotime($lastday);
  83. // 时间检索
  84. if (!empty($firstday) && !empty($lastday)) array_push($condition, "update_time BETWEEN ".$firstday." AND " . $lastday);
  85. }
  86. // 查询数据
  87. $where = "";
  88. if (0 < count($condition)) $where = implode(" AND ", $condition);
  89. $list = Db::name('users_money')->where($where)->order('update_time desc, moneyid desc')->select();
  90. // 处理数据
  91. $payCauseTypeArr = Config::get('global.pay_cause_type_arr');
  92. foreach ($list as $key => $value) {
  93. $value['money'] = unifyPriceHandle($value['money']);
  94. $value['users_money'] = unifyPriceHandle($value['users_money']);
  95. $value['add_time'] = date('Y-m-d H:i:s', $value['add_time']);
  96. $value['update_time'] = date('Y-m-d H:i:s', $value['update_time']);
  97. if (!strstr($value['cause'], '(充值套餐)')) {
  98. $value['cause'] = !empty($payCauseTypeArr[$value['cause_type']]) ? $payCauseTypeArr[$value['cause_type']] : '';
  99. }
  100. $value['moneyType'] = 0;
  101. // 收入
  102. if (in_array($value['cause_type'], $increase_type)) {
  103. $value['moneyType'] = 10;
  104. }
  105. // 支出
  106. else if (in_array($value['cause_type'], $decrease_type) || (0 === intval($value['cause_type']) && 'balance' == $value['pay_method'])) {
  107. $value['moneyType'] = 20;
  108. }
  109. $list[$key] = $value;
  110. }
  111. // 返回数据
  112. $result['list'] = $list;
  113. $result['tabBar'] = [['id' => 0, 'name' => '全部'], ['id' => 10, 'name' => '收入'], ['id' => 20, 'name' => '支出']];
  114. $this->unifyParamSuccess('查询成功', null, $result);
  115. }
  116. // 会员充值套餐页面
  117. public function usersRechargePackPage()
  118. {
  119. // 查询数据
  120. $where = [
  121. 'status' => 1,
  122. ];
  123. $result['list'] = Db::name('users_recharge_pack')->where($where)->order('pack_face_value asc, pack_pay_prices asc')->select();
  124. foreach ($result['list'] as $key => $value) {
  125. $value['pack_face_value'] = unifyPriceHandle($value['pack_face_value']);
  126. $value['pack_pay_prices'] = unifyPriceHandle($value['pack_pay_prices']);
  127. $result['list'][$key] = $value;
  128. }
  129. // 返回数据
  130. $this->unifyParamSuccess('查询成功', null, $result);
  131. }
  132. // 会员余额充值记录
  133. public function usersMoneyRechargeLog()
  134. {
  135. // 查询数据
  136. $where = [
  137. 'status' => 3,
  138. 'cause_type' => 1,
  139. 'users_id' => $this->users_id,
  140. ];
  141. $result['list'] = Db::name('users_money')->where($where)->order('add_time desc')->select();
  142. foreach ($result['list'] as $key => $value) {
  143. $value['money'] = unifyPriceHandle($value['money']);
  144. $value['users_money'] = unifyPriceHandle($value['users_money']);
  145. $value['add_time'] = date('Y-m-d H:i:s', $value['add_time']);
  146. $value['update_time'] = date('Y-m-d H:i:s', $value['update_time']);
  147. $result['list'][$key] = $value;
  148. }
  149. // 返回数据
  150. $this->unifyParamSuccess('查询成功', null, $result);
  151. }
  152. // 会员充值套餐充值记录
  153. public function usersRechargePackOrder()
  154. {
  155. // 查询数据
  156. $where = [
  157. 'order_status' => 3,
  158. 'users_id' => $this->users_id,
  159. ];
  160. $result['list'] = Db::name('users_recharge_pack_order')->where($where)->order('add_time desc, order_id desc')->select();
  161. foreach ($result['list'] as $key => $value) {
  162. $value['add_time'] = date('Y-m-d H:i:s', $value['add_time']);
  163. $value['order_pay_time'] = date('Y-m-d H:i:s', $value['order_pay_time']);
  164. $value['order_face_value'] = unifyPriceHandle($value['order_face_value']);
  165. $value['order_pay_prices'] = unifyPriceHandle($value['order_pay_prices']);
  166. $result['list'][$key] = $value;
  167. }
  168. // 返回数据
  169. $this->unifyParamSuccess('查询成功', null, $result);
  170. }
  171. // 会员充值套餐订单创建
  172. public function usersRechargePackOrderCreate()
  173. {
  174. if (empty($this->param['pack_id'])) $this->error('充值套餐不存在,请重新选择');
  175. // 查询数据
  176. $where = [
  177. 'status' => 1,
  178. 'pack_id' => intval($this->param['pack_id'])
  179. ];
  180. $pack = Db::name('users_recharge_pack')->where($where)->find();
  181. if (empty($pack)) $this->error('充值套餐不存在,请重新选择');
  182. // 订单编号
  183. $orderCode = date('Ymd') . $this->times . rand(10, 99);
  184. // 订单支付类型
  185. $orderPayName = 'wechat';
  186. // 订单支付终端
  187. $orderPayTerminal = 3;
  188. if (9 === intval($this->param['pay_type'])) {
  189. $orderPayName = 'tikTokPay';
  190. $orderPayTerminal = 4;
  191. }
  192. // 查询会员是否有未支付的订单
  193. $orderData = $this->usersRechargePackOrderFind();
  194. // 当前订单完成后的会员储值余额
  195. $stored_money = unifyPriceHandle($this->users['users_money'] + $pack['pack_face_value']);
  196. // 存在未支付订单则执行订单更新
  197. if (!empty($orderData['order_id']) && !empty($orderData['order_code']) && !empty($orderData['order_pay_code'])) {
  198. $where = [
  199. 'users_id' => $this->users_id,
  200. 'order_id' => $orderData['order_id'],
  201. 'order_code' => $orderData['order_code'],
  202. 'order_pay_code' => $orderData['order_pay_code'],
  203. ];
  204. $update = [
  205. 'pack_id' => intval($pack['pack_id']),
  206. 'stored_money' => $stored_money,
  207. 'order_pack_names' => $pack['pack_names'],
  208. 'order_face_value' => unifyPriceHandle($pack['pack_face_value']),
  209. 'order_pay_prices' => unifyPriceHandle($pack['pack_pay_prices']),
  210. 'order_pay_code' => $orderCode,
  211. 'order_pay_name' => $orderPayName,
  212. 'order_pay_terminal' => $orderPayTerminal,
  213. 'update_time' => $this->times,
  214. ];
  215. $result = Db::name('users_recharge_pack_order')->where($where)->update($update);
  216. if (!empty($result)) {
  217. $orderID = intval($orderData['order_id']);
  218. $orderData = array_merge($orderData, $update);
  219. }
  220. }
  221. // 订单创建
  222. else {
  223. $orderData = [
  224. 'pack_id' => intval($pack['pack_id']),
  225. 'users_id' => intval($this->users_id),
  226. 'stored_money' => $stored_money,
  227. 'order_code' => 'CZ' . $orderCode,
  228. 'order_status' => 1,
  229. 'order_pack_names' => $pack['pack_names'],
  230. 'order_face_value' => unifyPriceHandle($pack['pack_face_value']),
  231. 'order_pay_prices' => unifyPriceHandle($pack['pack_pay_prices']),
  232. 'order_pay_code' => $orderCode,
  233. 'order_pay_time' => 0,
  234. 'order_pay_name' => $orderPayName,
  235. 'order_pay_terminal' => $orderPayTerminal,
  236. 'order_pay_details' => '',
  237. 'add_time' => $this->times,
  238. 'update_time' => $this->times,
  239. ];
  240. $orderID = Db::name('users_recharge_pack_order')->insertGetId($orderData);
  241. }
  242. // 订单支付信息
  243. if (!empty($orderID)) {
  244. // 订单ID
  245. $orderData['order_id'] = intval($orderID);
  246. // 微信小程序支付
  247. if (1 === intval($this->param['pay_type'])) {
  248. // 调用微信支付接口
  249. $weChatPay = model('ShopPublicHandle')->getWechatAppletsPay($this->users_id, $orderData['order_pay_code'], $orderData['order_pay_prices'], 20);
  250. $result = [
  251. 'weChatPay' => $weChatPay,
  252. 'orderData' => [
  253. 'order_id' => $orderID,
  254. 'order_code' => $orderData['order_code'],
  255. 'order_pay_code' => $orderData['order_pay_code']
  256. ]
  257. ];
  258. // 返回提示
  259. $this->success('正在支付', null, $result);
  260. }
  261. // 抖音小程序支付
  262. else if (9 === intval($this->param['pay_type'])) {
  263. $tikTokPay = model('TikTok')->getTikTokAppletsPay($orderID, $orderData['order_pay_code'], $orderData['order_pay_prices'], 30, 'users_recharge_pack_order');
  264. $result = [
  265. 'tikTokPay' => $tikTokPay,
  266. 'orderData' => [
  267. 'order_id' => $orderID,
  268. 'order_code' => $orderData['order_code'],
  269. 'order_pay_code' => $orderData['order_pay_code']
  270. ]
  271. ];
  272. // 返回提示
  273. $this->success('正在支付', null, $result);
  274. }
  275. }
  276. }
  277. // 会员充值余额下单
  278. public function usersRechargeMoneyOrderCreate()
  279. {
  280. if (empty($this->param['users_money'])) $this->error('请输入充值金额');
  281. // 订单类型(1:会员充值余额)
  282. $causeType = 1;
  283. // 订单编号
  284. $orderNumber = date('Ymd') . $this->times . rand(10, 99);
  285. // 订单支付类型
  286. $payMethod = 9 === intval($this->param['pay_type']) ? 'tikTokPay' : 'wechat';
  287. // 会员当前余额
  288. $usersMoney = Db::name('users')->where('users_id', $this->users_id)->getField('users_money');
  289. // 查询会员充值余额订单
  290. $moneyData = $this->usersRechargeMoneyOrderFind();
  291. // 数据添加到订单表
  292. $payCauseTypeArr = Config::get('global.pay_cause_type_arr');
  293. if (!empty($moneyData['moneyid']) && !empty($moneyData['order_number'])) {
  294. $where = [
  295. 'users_id' => $this->users_id,
  296. 'moneyid' => $moneyData['moneyid'],
  297. 'order_number' => $moneyData['order_number'],
  298. ];
  299. $update = [
  300. 'money' => unifyPriceHandle($this->param['users_money']),
  301. 'users_money' => unifyPriceHandle($usersMoney + $this->param['users_money']),
  302. 'order_number' => $orderNumber,
  303. 'update_time' => $this->times,
  304. ];
  305. $result = Db::name('users_money')->where($where)->update($update);
  306. if (!empty($result)) {
  307. $moneyID = intval($moneyData['moneyid']);
  308. $moneyData = array_merge($moneyData, $update);
  309. }
  310. } else {
  311. $moneyData = [
  312. 'users_id' => $this->users_id,
  313. 'cause_type' => $causeType,
  314. 'pay_method' => $payMethod,
  315. 'cause' => $payCauseTypeArr[$causeType],
  316. 'money' => unifyPriceHandle($this->param['users_money']),
  317. 'users_money' => unifyPriceHandle($usersMoney + $this->param['users_money']),
  318. 'pay_details' => '',
  319. 'order_number' => $orderNumber,
  320. 'status' => 1,
  321. 'lang' => get_home_lang(),
  322. 'add_time' => $this->times,
  323. 'update_time' => $this->times,
  324. ];
  325. $moneyID = Db::name('users_money')->insertGetId($moneyData);
  326. }
  327. // 订单支付信息
  328. if (!empty($moneyID)) {
  329. // 订单ID
  330. $moneyData['moneyid'] = intval($moneyID);
  331. // 微信小程序支付
  332. if (1 === intval($this->param['pay_type'])) {
  333. // 调用微信支付接口
  334. $weChatPay = model('ShopPublicHandle')->getWechatAppletsPay($this->users_id, $moneyData['order_number'], $moneyData['money'], 1);
  335. $result = [
  336. 'weChatPay' => $weChatPay,
  337. 'orderData' => [
  338. 'moneyid' => $moneyID,
  339. 'order_number' => $moneyData['order_number'],
  340. ]
  341. ];
  342. // 返回提示
  343. $this->success('正在支付', null, $result);
  344. }
  345. // 抖音小程序支付
  346. else if (9 === intval($this->param['pay_type'])) {
  347. $tikTokPay = model('TikTok')->getTikTokAppletsPay($moneyID, $moneyData['order_number'], $moneyData['money'], 30, 'users_money');
  348. $result = [
  349. 'tikTokPay' => $tikTokPay,
  350. 'orderData' => [
  351. 'moneyid' => $moneyID,
  352. 'order_number' => $moneyData['order_number'],
  353. ]
  354. ];
  355. // 返回提示
  356. $this->success('正在支付', null, $result);
  357. }
  358. }
  359. }
  360. // 会员充值套餐订单支付后续处理
  361. public function usersRechargePackOrderPayHandle()
  362. {
  363. $orderData = [];
  364. if (!empty($this->param['moneyid']) && !empty($this->param['order_number'])) {
  365. // 数据表
  366. $table = 'users_money';
  367. // 查询会员充值余额订单
  368. $orderData = $this->usersRechargeMoneyOrderFind();
  369. $orderData['unified_id'] = $orderData['moneyid'];
  370. $orderData['unified_number'] = $orderData['order_number'];
  371. $orderData['transaction_type'] = 1;
  372. } else if (!empty($this->param['order_id']) && !empty($this->param['order_code']) && !empty($this->param['order_pay_code'])) {
  373. // 数据表
  374. $table = 'users_recharge_pack_order';
  375. // 查询会员充值套餐订单
  376. $orderData = $this->usersRechargePackOrderFind();
  377. $orderData['unified_id'] = $orderData['order_id'];
  378. $orderData['unified_number'] = $orderData['order_pay_code'];
  379. $orderData['transaction_type'] = 20;
  380. }
  381. // 查询是否真实支付并完成支付后续操作
  382. if (!empty($orderData)) {
  383. // 微信支付查询
  384. if (1 === intval($this->param['pay_type'])) {
  385. model('ShopPublicHandle')->getWeChatPayResult($this->users_id, $orderData, $type);
  386. }
  387. // 抖音支付查询
  388. else if (9 === intval($this->param['pay_type'])) {
  389. model('TikTok')->tikTokAppletsPayDealWith($orderData, false, $table);
  390. }
  391. }
  392. }
  393. // 查询会员充值套餐订单
  394. private function usersRechargePackOrderFind()
  395. {
  396. $orderData = [];
  397. // 存在订单信息则查询订单号是否真实存在
  398. if (!empty($this->param['order_id']) && !empty($this->param['order_code']) && !empty($this->param['order_pay_code'])) {
  399. $where = [
  400. 'users_id' => $this->users_id,
  401. 'order_id' => $this->param['order_id'],
  402. 'order_code' => $this->param['order_code'],
  403. 'order_status' => 1,
  404. 'order_pay_code' => $this->param['order_pay_code'],
  405. ];
  406. $orderData = Db::name('users_recharge_pack_order')->where($where)->order('order_id desc')->find();
  407. }
  408. // 查询会员是否存在未支付的订单
  409. if (empty($orderData)) {
  410. $where = [
  411. 'users_id' => $this->users_id,
  412. 'order_status' => 1,
  413. ];
  414. $orderData = Db::name('users_recharge_pack_order')->where($where)->order('order_id desc')->find();
  415. }
  416. return $orderData;
  417. }
  418. // 查询会员充值余额订单
  419. private function usersRechargeMoneyOrderFind()
  420. {
  421. $moneyData = [];
  422. // 存在订单信息则查询订单号是否真实存在
  423. if (!empty($this->param['moneyid']) && !empty($this->param['order_number'])) {
  424. $where = [
  425. 'moneyid' => $this->param['moneyid'],
  426. 'users_id' => $this->users_id,
  427. 'cause_type' => 1,
  428. 'status' => 1,
  429. 'order_number' => $this->param['order_number'],
  430. ];
  431. $moneyData = Db::name('users_money')->where($where)->order('moneyid desc')->find();
  432. }
  433. // 查询会员是否存在未支付的订单
  434. if (empty($moneyData)) {
  435. $where = [
  436. 'users_id' => $this->users_id,
  437. 'cause_type' => 1,
  438. 'status' => 1,
  439. ];
  440. $moneyData = Db::name('users_money')->where($where)->order('moneyid desc')->find();
  441. }
  442. return $moneyData;
  443. }
  444. // 统一携带默认参数返回
  445. private function unifyParamSuccess($msg = '操作成功', $url = null, $result = [])
  446. {
  447. // 是否加载显示
  448. $result['loadShow'] = 1;
  449. // 会员信息
  450. $result['users'] = $this->users;
  451. // 返回结果
  452. if (model('ShopPublicHandle')->detectH5Terminal($this->param['terminal'])) {
  453. // $pointsShopController = new \app\plugins\controller\PointsShop;
  454. // $paramA = !empty($this->param['a']) ? $this->param['a'] : 'errorMsg';
  455. // $pointsShopController->$paramA($result);
  456. } else {
  457. $this->success($msg, $url, $result);
  458. }
  459. }
  460. }