MemberCheck.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. *
  4. * @author: wuhaotian<442384644@qq.com>
  5. * @day: 2019/12/07
  6. */
  7. namespace app\models\member;
  8. use app\admin\model\user\User;
  9. use app\models\store\StoreOrderStatus;
  10. use app\models\store\StorePink;
  11. use app\models\user\UserBill;
  12. use app\models\user\UserSpread;
  13. use crmeb\basic\BaseModel;
  14. use crmeb\repositories\PaymentRepositories;
  15. use crmeb\traits\ModelTrait;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. use think\db\Where;
  20. /**
  21. * Class UserGroup
  22. * @package app\admin\model\user
  23. */
  24. class MemberCheck extends BaseModel
  25. {
  26. /**
  27. * 数据表主键
  28. * @var string
  29. */
  30. protected $pk = 'id';
  31. /**
  32. * 模型名称
  33. * @var string
  34. */
  35. protected $name = 'member_check';
  36. protected static $payType = ['weixin' => '微信支付', 'yue' => '余额支付'];
  37. use ModelTrait;
  38. public static function valid()
  39. {
  40. return self::where('paid', 1);
  41. }
  42. public static function getValidList($where)
  43. {
  44. $data = self::valid()
  45. ->page((int)$where['page'], (int)$where['limit'])
  46. ->order('add_time', 'desc')->select()->each(function ($item) {
  47. $user = \app\admin\model\user\User::get($item['uid']);
  48. $item['user_name'] = $user['nickname'];
  49. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  50. $item['_pay_time'] = date('Y-m-d H:i:s', $item['pay_time']);
  51. });
  52. $count = self::valid()->count();
  53. return compact('count', 'data');
  54. }
  55. /**
  56. * @param $where
  57. * @return array
  58. */
  59. public static function getList($where)
  60. {
  61. $model = self::where('paid', $where['paid']);
  62. if (isset($where['search']) && $where['search'] != '') $model = $model->where('name|iacard', "%{$where['search']}%");
  63. $data = $model->page((int)$where['page'], (int)$where['limit'])->order('status asc,add_time desc')->select()->each(function ($item) {
  64. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  65. $item['_valid_time'] = date('Y-m-d H:i:s', $item['valid_time']);
  66. $item['_pay_time'] = date('Y-m-d H:i:s', $item['pay_time']);
  67. $item['_pay_time'] = date('Y-m-d H:i:s', $item['pay_time']);
  68. $item['_type'] = $item['type'] == 1 ? "个人会员" : "企业会员";
  69. $item['grade_info'] = MemberGrade::where('grade=' . $item['grade'])->where('type=' . $item['type'])->find();
  70. $item['grade_name'] = $item['grade_info']['name'];
  71. $item['status_txt'] = ($item['status'] == 0 ? "待审核" : ($item['status'] == 1 ? "通过" : "驳回"));
  72. if ($item['status'] == 1) $item['status_txt'] .= '<br/>有效期至:<br/>' . date('Y-m-d', $item['valid_time']);
  73. });
  74. $count = $model->count();
  75. return compact('count', 'data');
  76. }
  77. public static function createOrder($param, $uid, $pay_type, $from)
  78. {
  79. $type = $param['type'] ?? '';
  80. $name = $param['name'] ?? '';
  81. $idcard = $param['id'] ?? '';
  82. $phone = $param['phone'] ?? '';
  83. $location = $param['location'] ?? '';
  84. $detail_address = $param['address'] ?? '';
  85. $job = $param['occupation'] ?? '';
  86. $job_year = $param['careerYears'] ?? '';
  87. $saver = $param['sponsor'] ?? '';
  88. $logo = $param['img'] ?? '';
  89. $grade = $param['grade'] ?? '';
  90. $location = explode('/', $location);
  91. $province = $location[0] ?? '';
  92. $city = $location[1] ?? '';
  93. $area = $location[2] ?? '';
  94. self::beginTrans();
  95. try {
  96. if (!array_key_exists($pay_type, self::$payType)) return self::setErrorInfo('选择支付方式有误!', true);
  97. $userInfo = User::getUserInfo($uid);
  98. if (!$userInfo) return self::setErrorInfo('用户不存在!', true);
  99. if (!$grade || !$uid || !$type || !$name || !$idcard || !$phone || !$province || !$city || !$area || !$detail_address || !$job || !$job_year || !$saver || !$logo) {
  100. return self::setErrorInfo('参数不完整', true);
  101. }
  102. $member = MemberGrade::where(['type' => $type, 'grade' => $grade])->find();
  103. if (!$member) self::setErrorInfo('会员类型不存在', true);
  104. if (self::where('idcard', $idcard)->where('paid', 1)->where('status', 'in', '0,1')->find())
  105. return self::setErrorInfo('已有同身份证/企业代码的记录', true);
  106. $add_time = time();
  107. $pay_money = $member['price'];
  108. $order_id = self::getOrderId();
  109. $order = self::create(compact('pay_type', 'from', 'order_id', 'grade', 'add_time', 'pay_money', 'saver', 'logo', 'uid', 'type', 'name', 'idcard', 'phone', 'province', 'city', 'area', 'detail_address', 'job', 'job_year'));
  110. if (!$order) return self::setErrorInfo('订单生成失败!', true);
  111. self::commitTrans();
  112. return $order;
  113. } catch (\PDOException $e) {
  114. self::rollbackTrans();
  115. return self::setErrorInfo('生成订单时SQL执行错误错误原因:' . $e->getMessage());
  116. } catch (\Exception $e) {
  117. self::rollbackTrans();
  118. return self::setErrorInfo('生成订单时系统错误错误原因:' . $e->getMessage());
  119. }
  120. }
  121. public static function jsPayPrice($order_id, $uid)
  122. {
  123. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->find();
  124. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  125. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  126. $userInfo = \app\models\user\User::getUserInfo($uid);
  127. self::beginTrans();
  128. $res1 = UserBill::expend('申请会员', $uid, 'now_money', 'pay_member', $orderInfo['pay_money'], $orderInfo['id'], $userInfo['now_money'], '微信支付' . floatval($orderInfo['pay_money']) . '元申请会员');
  129. $res2 = self::paySuccess($order_id, 'weixin');//微信支付为0时
  130. $res = $res1 && $res2;
  131. self::checkTrans($res);
  132. return $res;
  133. }
  134. public static function yuePay($order_id, $uid)
  135. {
  136. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->find();
  137. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  138. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  139. $userInfo = \app\models\user\User::getUserInfo($uid);
  140. if ($userInfo['now_money'] < $orderInfo['pay_money'])
  141. return self::setErrorInfo(['status' => 'pay_deficiency', 'msg' => '余额不足' . floatval($orderInfo['pay_money'])]);
  142. self::beginTrans();
  143. $res1 = false !== User::bcDec($uid, 'now_money', $orderInfo['pay_money'], 'uid');
  144. $res2 = UserBill::expend('申请会员', $uid, 'now_money', 'pay_member', $orderInfo['pay_money'], $orderInfo['id'], $userInfo['now_money'], '余额支付' . floatval($orderInfo['pay_money']) . '元申请会员');
  145. $res3 = self::paySuccess($order_id, 'yue');//余额支付成功
  146. $res = $res1 && $res2 && $res3;
  147. self::checkTrans($res);
  148. return $res;
  149. }
  150. /**
  151. * //TODO 支付成功后
  152. * @param $orderId
  153. * @param string $paytype
  154. * @param string $formId
  155. * @return bool
  156. * @throws DataNotFoundException
  157. * @throws DbException
  158. * @throws ModelNotFoundException
  159. */
  160. public static function paySuccess($orderId, $paytype = 'weixin')
  161. {
  162. $order = self::where('order_id', $orderId)->find();
  163. $res1 = self::where('order_id', $orderId)->update(['paid' => 1, 'pay_type' => $paytype, 'pay_time' => time()]);//订单改为支付
  164. $now_money = \app\models\user\User::where('uid', $order['uid'])->value('now_money');
  165. UserBill::expend('申请会员', $order['uid'], 'now_money', 'pay_money', $order['pay_money'], $order['id'], $now_money, '支付' . floatval($order['pay_money']) . '元申请会员');
  166. return false !== $res1;
  167. }
  168. public static function getOrderId()
  169. {
  170. do {
  171. $str = 'mem' . time() . rand(1000, 9999);
  172. } while (self::be(['order_id' => $str]));
  173. return $str;
  174. }
  175. }