EventSignUp.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\model\activity;
  12. use app\wap\model\activity\EventRegistration;
  13. use app\wap\model\activity\EventPrice;
  14. use app\wap\model\merchant\MerchantFlowingWater;
  15. use app\wap\model\user\User;
  16. use app\wap\model\user\UserBill;
  17. use service\HookService;
  18. use traits\ModelTrait;
  19. use basic\ModelBasic;
  20. use think\Db;
  21. use service\SystemConfigService;
  22. use service\WechatService;
  23. use service\WechatTemplateService;
  24. use app\wap\model\user\WechatUser;
  25. use behavior\wechat\PaymentBehavior;
  26. use service\AlipayTradeWapService;
  27. use think\Url;
  28. use app\wap\model\routine\RoutineTemplate;
  29. use app\wap\model\wap\SmsTemplate;
  30. /**报名订单 model
  31. * Class EventSignUp
  32. * @package app\wap\model\activity
  33. */
  34. class EventSignUp extends ModelBasic
  35. {
  36. use ModelTrait;
  37. protected $insert = ['add_time'];
  38. protected static $payType = ['weixin' => '微信支付', 'yue' => '余额支付', 'offline' => '线下支付', 'zhifubao' => '支付宝'];
  39. protected function setAddTimeAttr()
  40. {
  41. return time();
  42. }
  43. /**条件处理
  44. * @param string $alias
  45. * @param null $model
  46. * @return EventSignUp
  47. */
  48. public static function setWhere($alias = '', $model = null)
  49. {
  50. if (is_null($model)) $model = new self();
  51. if ($alias) {
  52. $model = $model->alias($alias);
  53. $alias .= '.';
  54. }
  55. return $model->where([$alias . 'paid' => 1, $alias . 'is_del' => 0, $alias . 'refund_status' => 0]);
  56. }
  57. public static function getNewOrderId()
  58. {
  59. $count = (int)self::where('add_time', ['>=', strtotime(date("Y-m-d"))], ['<', strtotime(date("Y-m-d", strtotime('+1 day')))])->count();
  60. return 'su' . date('YmdHis', time()) . (10000 + $count + 1);
  61. }
  62. /**用户提交报名
  63. * @param $id
  64. * @param $userName
  65. * @param $userPhone
  66. */
  67. public static function userEventSignUp($id, $price_id, $event, $payType, $uid)
  68. {
  69. if (!array_key_exists($payType, self::$payType)) return self::setErrorInfo('选择支付方式有误!');
  70. $userInfo = User::getUserData($uid);
  71. if (!$userInfo) return self::setErrorInfo('用户不存在!');
  72. $activity = EventRegistration::oneActivitys($id);
  73. if (!$activity) return self::setErrorInfo('活动不存在!');
  74. if (bcsub($activity['number'], $activity['count'], 0) <= 0) return self::setErrorInfo('活动报名结束!');
  75. $userCount = self::setWhere()->where(['uid' => $uid, 'activity_id' => $id])->count();//用户该活动报名次数
  76. if ($activity['restrictions'] && bcsub($activity['restrictions'], $userCount, 0) <= 0) return self::setErrorInfo('您的活动报名已超过限额!');
  77. $priceData = EventPrice::where(['id' => $price_id])->find();
  78. $payPrice = 0;
  79. if (isset($userInfo['level']) && $userInfo['level'] > 0 && $priceData['event_mer_price'] > 0) {
  80. $payPrice = $priceData['event_mer_price'];
  81. } elseif ($userInfo['level'] == 0 && $priceData['event_price'] > 0) {
  82. $payPrice = $priceData['event_price'];
  83. }
  84. if ($priceData['event_number'] > $activity['number']) return self::setErrorInfo('选择的活动人数过大!');
  85. if ($priceData['event_number'] > bcsub($activity['number'], $activity['count'], 0)) return self::setErrorInfo('剩余报名人数不足!');
  86. $order_id = self::getNewOrderId();
  87. $write_off_code = self::qrcodes_url($order_id);
  88. $data = [
  89. 'order_id' => $order_id,
  90. 'uid' => $uid,
  91. 'mer_id' => $activity['mer_id'],
  92. 'user_info' => $event,
  93. 'activity_id' => $id,
  94. 'pay_price' => $payPrice,
  95. 'pay_type' => $payType,
  96. 'number' => $priceData['event_number'],
  97. 'write_off_code' => $write_off_code,
  98. 'code' => self::codes()
  99. ];
  100. $order = self::set($data);
  101. if (!$order) return self::setErrorInfo('报名订单生成失败!');
  102. return $order;
  103. }
  104. /**
  105. * 微信支付 为 0元时
  106. * @param $order_id
  107. * @param $uid
  108. * @return bool
  109. */
  110. public static function jsPayPrice($order_id, $uid)
  111. {
  112. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->find();
  113. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  114. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  115. $userInfo = User::getUserData($uid);
  116. self::beginTrans();
  117. $res1 = UserBill::expend('活动报名成功', $uid, 'now_money', 'pay_sign_up', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '支付' . floatval($orderInfo['pay_price']) . '元活动报名');
  118. $res2 = self::paySuccess($order_id);
  119. $res = $res1 && $res2;
  120. self::checkTrans($res);
  121. return $res;
  122. }
  123. /**微信js支付
  124. * @param $orderId
  125. * @param string $field
  126. * @return array|string
  127. * @throws \think\db\exception\DataNotFoundException
  128. * @throws \think\db\exception\ModelNotFoundException
  129. * @throws \think\exception\DbException
  130. */
  131. public static function jsPay($orderId, $field = 'order_id')
  132. {
  133. if (is_string($orderId))
  134. $orderInfo = self::where($field, $orderId)->find();
  135. else
  136. $orderInfo = $orderId;
  137. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  138. if ($orderInfo['paid']) exception('支付已支付!');
  139. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  140. $openid = WechatUser::uidToOpenid($orderInfo['uid']);
  141. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'signup', SystemConfigService::get('site_name'));
  142. }
  143. /**
  144. * 微信h5支付
  145. * @param $orderId
  146. * @param string $field
  147. * @return array|string
  148. * @throws \think\db\exception\DataNotFoundException
  149. * @throws \think\db\exception\ModelNotFoundException
  150. * @throws \think\exception\DbException
  151. */
  152. public static function h5Pay($orderId, $field = 'order_id')
  153. {
  154. if (is_string($orderId))
  155. $orderInfo = self::where($field, $orderId)->find();
  156. else
  157. $orderInfo = $orderId;
  158. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  159. if ($orderInfo['paid']) exception('支付已支付!');
  160. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  161. $site_name = SystemConfigService::get('site_name');
  162. if (!$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  163. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'signup', self::getSubstrUTf8($site_name . '-活动报名', 30), '', 'MWEB');
  164. }
  165. /**余额支付
  166. * @param $order_id
  167. * @param $uid
  168. * @return bool
  169. * @throws \think\db\exception\DataNotFoundException
  170. * @throws \think\db\exception\ModelNotFoundException
  171. * @throws \think\exception\DbException
  172. */
  173. public static function yuePay($order_id, $uid)
  174. {
  175. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->find();
  176. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  177. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  178. if ($orderInfo['pay_type'] != 'yue') return self::setErrorInfo('该订单不能使用余额支付!');
  179. $userInfo = User::getUserData($uid);
  180. if ($userInfo['now_money'] < $orderInfo['pay_price']) return self::setErrorInfo('余额不足' . floatval($orderInfo['pay_price']));
  181. self::beginTrans();
  182. $res1 = false !== User::bcDec($uid, 'now_money', $orderInfo['pay_price'], 'uid');
  183. $res2 = UserBill::expend('活动报名', $uid, 'now_money', 'pay_sign_up', $orderInfo['pay_price'], $orderInfo['id'], bcsub($userInfo['now_money'], $orderInfo['pay_price'], 2), '余额支付' . floatval($orderInfo['pay_price']) . '元活动报名');
  184. $res3 = self::paySuccess($order_id);
  185. $res = $res1 && $res2 && $res3;
  186. self::checkTrans($res);
  187. return $res;
  188. }
  189. /**
  190. * //TODO 支付成功后
  191. * @param $orderId
  192. * @param $notify
  193. * @return bool
  194. */
  195. public static function paySuccess($orderId)
  196. {
  197. $order = self::where('order_id', $orderId)->find();
  198. $res1 = self::where('order_id', $orderId)->update(['paid' => 1, 'pay_time' => time()]);
  199. $site_url = SystemConfigService::get('site_url');
  200. $res2 = true;
  201. $res3 = true;
  202. try {
  203. if ($res1 && $order['pay_type'] != 'yue') {
  204. $res2 = UserBill::expend('活动报名', $order['uid'], $order['pay_type'], 'pay_sign_up', $order['pay_price'], $order['id'], 0, '支付' . floatval($order['pay_price']) . '元活动报名');
  205. }
  206. if ($res1) {
  207. $res3 = MerchantFlowingWater::setMerchantFlowingWater($order, 4);
  208. }
  209. $wechat_notification_message = SystemConfigService::get('wechat_notification_message');
  210. $event = EventRegistration::where('id', $order['activity_id'])->field('title,start_time,end_time,province,city,district,detail')->find();
  211. if ($wechat_notification_message == 1) {
  212. WechatTemplateService::sendTemplate(WechatUser::where('uid', $order['uid'])->value('openid'), WechatTemplateService::ORDER_USER_SIGN_UP_SUCCESS, [
  213. 'first' => '亲,你已成功报名了活动',
  214. 'keyword1' => $event['title'],
  215. 'keyword2' => date('Y/m/d/H/i', $event['start_time']) . '~' . date('Y/m/d/H/i', $event['end_time']),
  216. 'keyword3' => $event['province'] . $event['city'] . $event['district'] . $event['detail'],
  217. 'remark' => '点击查看报名详情'
  218. ], $site_url . Url::build('wap/my/sign_list'));
  219. WechatTemplateService::sendAdminNoticeTemplate([
  220. 'first' => "亲,您有一个新的活动报名订单",
  221. 'keyword1' => $orderId,
  222. 'keyword2' => $order['pay_price'],
  223. 'remark' => '请及时查看'
  224. ]);
  225. } else {
  226. $data['thing1']['value'] = $event['title'];
  227. $data['time2']['value'] = date('Y/m/d/H/i', $event['start_time']) . '~' . date('Y/m/d/H/i', $event['end_time']);
  228. $data['thing6']['value'] = $event['province'] . $event['city'] . $event['district'] . $event['detail'];
  229. RoutineTemplate::sendSignUpSuccess($data, $order['uid'], $site_url . Url::build('wap/my/sign_list'));
  230. $dataAdmin['character_string1']['value'] = $orderId;
  231. $dataAdmin['amount3']['value'] = $order['pay_price'];
  232. $dataAdmin['time2']['value'] = date('Y-m-d H:i:s', time());
  233. $dataAdmin['thing6']['value'] = '您有一个新的活动报名订单';
  234. RoutineTemplate::sendAdminNoticeTemplate($dataAdmin);
  235. }
  236. $dat['title'] = $event['title'];
  237. $dat['code'] = $order['code'];
  238. SmsTemplate::sendSms($order['uid'], $dat, 'ORDER_USER_SIGN_UP_SUCCESS');
  239. } catch (\Throwable $e) {
  240. }
  241. $res = $res1 && $res2 && $res3;
  242. return false !== $res;
  243. }
  244. /**报名二维码
  245. * @param string $order_id
  246. * @param int $size
  247. * @return string
  248. */
  249. public static function qrcodes_url($order_id = '', $size = 5)
  250. {
  251. vendor('phpqrcode.phpqrcode');
  252. $urls = SystemConfigService::get('site_url') . '/';
  253. $url = $urls . 'wap/my/sign_order/type/2/order_id/' . $order_id;
  254. $value = $url; //二维码内容
  255. $errorCorrectionLevel = 'H'; //容错级别
  256. $matrixPointSize = $size; //生成图片大小
  257. //生成二维码图片
  258. $filename = 'public/qrcode/' . 'su' . rand(10000000, 99999999) . '.png';
  259. \QRcode::png($value, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
  260. return $urls . $filename;
  261. }
  262. /**
  263. * 核销码
  264. */
  265. public static function codes()
  266. {
  267. return rand(100000, 999999);
  268. }
  269. /**活动报名次数
  270. * @param $activity_id
  271. */
  272. public static function signUpCount($activity_id)
  273. {
  274. $count = self::setWhere()->where(['activity_id' => $activity_id])->sum('number');
  275. $orderCount = self::setWhere()->where(['activity_id' => $activity_id, 'number' => 0])->count();
  276. $count = bcadd($count, $orderCount, 0);
  277. return $count;
  278. }
  279. }