UserSign.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace app\models\user;
  3. use crmeb\basic\BaseModel;
  4. use crmeb\services\SystemConfigService;
  5. use crmeb\traits\ModelTrait;
  6. /**
  7. * TODO 用户签到模型 Model
  8. * Class UserSign
  9. * @package app\models\user
  10. */
  11. class UserSign extends BaseModel
  12. {
  13. /**
  14. * 数据表主键
  15. * @var string
  16. */
  17. protected $pk = 'id';
  18. /**
  19. * 模型名称
  20. * @var string
  21. */
  22. protected $name = 'user_sign';
  23. use ModelTrait;
  24. /**
  25. * 设置签到数据
  26. * @param $uid 用户uid
  27. * @param string $title 签到说明
  28. * @param int $number 签到获得积分
  29. * @param int $balance 签到前剩余积分
  30. * @return bool
  31. */
  32. public static function setSignData($uid, $title = '', $number = 0, $balance = 0, $money_type = 'integral')
  33. {
  34. $add_time = time();
  35. $res = false;
  36. if ($money_type == 'integral') {
  37. $res1 = self::create(compact('uid', 'title', 'number', 'balance', 'add_time', 'money_type'));
  38. $res2 = UserBill::income($title, $uid, 'integral', 'sign', $number, 0, $balance, $title);
  39. $res = $res1 && $res2 && User::bcInc($uid, 'integral', $number, 'uid');
  40. } else if ($money_type == 'now_money') {
  41. $user_money = User::where('uid', $uid)->value('now_money');
  42. $balance = $user_money + $number;
  43. $res1 = self::create(compact('uid', 'title', 'number', 'balance', 'add_time', 'money_type'));
  44. $res2 = UserBill::income($title, $uid, 'now_money', 'sign', $number, 0, $balance, $title);
  45. $res = $res1 && $res2 && User::bcInc($uid, 'now_money', $number, 'uid');
  46. } else if ($money_type == 'brokerage') {
  47. $user_money = User::where('uid', $uid)->value('brokerage_price');
  48. $balance = $user_money + $number;
  49. $res1 = self::create(compact('uid', 'title', 'number', 'balance', 'add_time', 'money_type'));
  50. $res2 = UserBill::income($title, $uid, 'now_money', 'brokerage', $number, 0, $balance, $title);
  51. $res = $res1 && $res2;
  52. } else {
  53. if (!in_array($money_type, array_keys(init_money_type()))) {
  54. return false;
  55. }
  56. $user_money = UserMoney::initialUserMoney($uid, $money_type)['money'];
  57. $balance = $user_money + $number;
  58. $res1 = self::create(compact('uid', 'title', 'number', 'balance', 'add_time', 'money_type'));
  59. $res = $res1 && UserMoney::incomeMoney($uid, $money_type, $number, 'sign', $title, $title);
  60. }
  61. return $res;
  62. }
  63. /**
  64. * 分页获取用户签到数据
  65. * @param $uid 用户uid
  66. * @param $page 页码
  67. * @param $limit 展示条数
  68. * @return array|\think\Collection
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. * @throws \think\exception\DbException
  72. */
  73. public static function getSignList($uid, $page, $limit)
  74. {
  75. if (!$limit) return [];
  76. $billModel = UserBill::where('a.type', 'sign')
  77. ->where('a.status', 1)->where('a.uid', $uid)->alias('a')
  78. ->join("user u", 'u.uid=a.uid')
  79. ->order('a.add_time desc')->field('FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as add_time,a.title,a.number,a.category');
  80. if ($page) $billModel = $billModel->page((int)$page, (int)$limit);
  81. return $billModel->select()->each(function ($item) {
  82. $item['money_type_name'] = get_money_name($item['category']);
  83. });
  84. }
  85. /**
  86. * 获取用户累计签到次数
  87. * @param $uid
  88. * @return int
  89. */
  90. public static function getSignSumDay($uid)
  91. {
  92. return self::where('uid', $uid)->count();
  93. }
  94. /**
  95. * 获取用户是否签到
  96. * @param $uid
  97. * @return bool
  98. */
  99. public static function getIsSign($uid, string $type = 'today')
  100. {
  101. return self::where('uid', $uid)->whereTime('add_time', $type)->count() ? true : false;
  102. }
  103. /**
  104. * 获取签到配置
  105. * @param string $key
  106. * @return array
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. * @throws \think\exception\DbException
  110. */
  111. public static function getSignSystemList($key = 'sign_day_num')
  112. {
  113. return sys_data($key) ?: [];
  114. }
  115. /**
  116. * 用户签到
  117. * @param $uid
  118. * @return bool|int
  119. * @throws \think\db\exception\DataNotFoundException
  120. * @throws \think\db\exception\ModelNotFoundException
  121. * @throws \think\exception\DbException
  122. */
  123. public static function sign($uid)
  124. {
  125. $sign_list = self::getSignSystemList();
  126. if (!count($sign_list)) return self::setErrorInfo('请先配置签到天数');
  127. $user = User::where('uid', $uid)->find();
  128. $sign_num = 0;
  129. $money_tpe = 'integral';
  130. //检测昨天是否签到
  131. if (self::getIsSign($uid, 'yesterday')) {
  132. if ($user->sign_num > (count($sign_list) - 1)) $user->sign_num = 0;
  133. } else {
  134. //如果昨天没签到,回退到第一天
  135. $user->sign_num = 0;
  136. }
  137. foreach ($sign_list as $key => $item) {
  138. if ($key == $user->sign_num) {
  139. $sign_num = $item['sign_num'];
  140. $money_tpe = $item['money_type'];
  141. break;
  142. }
  143. }
  144. $user->sign_num += 1;
  145. if ($user->sign_num == count($sign_list))
  146. $res1 = self::setSignData($uid, '连续签到奖励', $sign_num, bcadd($user->integral, $sign_num, 0), $money_tpe);
  147. else
  148. $res1 = self::setSignData($uid, '签到奖励', $sign_num, bcadd($user->integral, $sign_num, 0), $money_tpe);
  149. // $res2 = User::bcInc($uid, 'integral', $sign_num, 'uid');
  150. $res3 = $user->save();
  151. $res = $res1 && $res3 !== false;
  152. BaseModel::checkTrans($res);
  153. event('UserLevelAfter', [$user]);
  154. if ($res)
  155. return $sign_num . get_money_name($money_tpe);
  156. else
  157. return false;
  158. }
  159. /*
  160. * 获取签到列表按月加载
  161. * @param int $uid 用户uid
  162. * @param int $page 页码
  163. * @param int $limit 显示多少条
  164. * @return array
  165. * */
  166. public static function getSignMonthList($uid, $page = 1, $limit = 8)
  167. {
  168. if (!$limit) return [];
  169. if ($page) {
  170. $list = UserBill::where('uid', $uid)
  171. ->where('category', 'integral')
  172. ->where('type', 'sign')
  173. ->field('FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids')
  174. ->group('time')
  175. ->order('time DESC')
  176. ->page($page, $limit)
  177. ->select();
  178. } else {
  179. $list = UserBill::where('uid', $uid)
  180. ->where('category', 'integral')
  181. ->where('type', 'sign')
  182. ->field('FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids')
  183. ->group('time')
  184. ->order('time DESC')
  185. ->select();
  186. }
  187. $data = [];
  188. foreach ($list as $key => &$item) {
  189. $value['month'] = $item['time'];
  190. $value['list'] = UserBill::where('id', 'in', $item['ids'])->field('FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time,title,number')->order('add_time DESC')->select();
  191. array_push($data, $value);
  192. }
  193. return $data;
  194. }
  195. public static function checkUserSigned($uid)
  196. {
  197. return UserBill::be(['uid' => $uid, 'add_time' => ['>', strtotime('today')], 'category' => 'integral', 'type' => 'sign']);
  198. }
  199. public static function userSignedCount($uid)
  200. {
  201. return self::userSignBillWhere($uid)->count();
  202. }
  203. /**
  204. * @param $uid
  205. * @return UserBill
  206. */
  207. public static function userSignBillWhere($uid)
  208. {
  209. return UserBill::where('uid', $uid)->where('category', 'integral')->where('type', 'sign');
  210. }
  211. public static function signEbApi($userInfo)
  212. {
  213. $uid = $userInfo['uid'];
  214. $min = sys_config('sx_sign_min_int') ?: 0;
  215. $max = sys_config('sx_sign_max_int') ?: 5;
  216. $integral = rand($min, $max);
  217. BaseModel::beginTrans();
  218. $res1 = UserBill::income('用户签到', $uid, 'integral', 'sign', $integral, 0, $userInfo['integral'], '签到获得' . floatval($integral) . '积分');
  219. $res2 = User::bcInc($uid, 'integral', $integral, 'uid');
  220. $res = $res1 && $res2;
  221. BaseModel::checkTrans($res);
  222. if ($res)
  223. return $integral;
  224. else
  225. return false;
  226. }
  227. }