User.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace app\api\service;
  3. use app\api\model\user\Grade;
  4. use app\api\model\user\GradeLog;
  5. use app\common\model\dealer\Setting;
  6. use think\Cache;
  7. use think\Db;
  8. use think\Exception;
  9. use app\api\model\User as UserModel;
  10. class User
  11. {
  12. /**
  13. * 记忆上门自提联系人
  14. * @param $userId
  15. * @param $linkman
  16. * @param $phone
  17. * @return bool
  18. */
  19. public static function setLastExtract($userId, $linkman, $phone)
  20. {
  21. // 缓存时间30天
  22. $expire = 86400 * 30;
  23. return Cache::set("{$userId}_LastExtract", compact('linkman', 'phone'), $expire);
  24. }
  25. /**
  26. * 记忆上门自提联系人
  27. * @param $userId
  28. * @return mixed
  29. */
  30. public static function getLastExtract($userId)
  31. {
  32. if ($lastExtract = Cache::get("{$userId}_LastExtract")) {
  33. return $lastExtract;
  34. }
  35. return ['linkman' => '', 'phone' => ''];
  36. }
  37. /**
  38. * 设置支付密码
  39. * @param $userId
  40. * @param $password
  41. * @return UserModel
  42. * @throws Exception
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. * @throws \think\exception\DbException
  46. */
  47. public static function setPayPassword($userId, $password)
  48. {
  49. if(empty($password)){
  50. throw new Exception('请输入6位数字密码');
  51. }
  52. if(!preg_match("/^[0-9]\d{5}$/", $password)){
  53. throw new Exception('密码格式不正确,请输入6位数字密码');
  54. }
  55. $payPassword = makePassword($password);
  56. return UserModel::where('user_id', $userId)->update(['pay_password'=>$payPassword,'update_time'=>time()]);
  57. }
  58. /**
  59. * 用户升级
  60. * @param $user_id
  61. * @param int $wxapp_id
  62. * @return bool
  63. * @throws Exception
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. * @throws \think\exception\DbException
  67. */
  68. public static function upgrade($user_id, $wxapp_id=0)
  69. {
  70. $info = UserModel::alias('a')
  71. ->leftJoin('user_grade ug', 'ug.grade_id=a.grade_id')
  72. ->where('user_id', $user_id)
  73. ->field('a.user_id,a.grade_id,ug.name,a.expend_upgrade_money')
  74. ->find();
  75. if(empty($info)){
  76. return false;
  77. }
  78. $gradeId = isset($info['grade_id'])? intval($info['grade_id']) : 0;
  79. $upgradeMoney = isset($info['expend_upgrade_money'])? floatval($info['expend_upgrade_money']) : 0;
  80. if($upgradeMoney>0){
  81. $gradeList = Grade::where(['status'=> 1, 'is_delete'=> 0,'wxapp_id'=>$wxapp_id])
  82. ->where('grade_id','>', $gradeId)
  83. ->field('grade_id,name,upgrade')
  84. ->order('weight asc')
  85. ->select();
  86. $gradeList = $gradeList? $gradeList->toArray() : [];
  87. if($gradeList){
  88. $log = [];
  89. $newGradeId = 0;
  90. foreach ($gradeList as $v){
  91. $id = isset($v['grade_id'])? $v['grade_id'] : 0;
  92. $upgrade = isset($v['upgrade']) && $v['upgrade']? json_decode($v['upgrade'], true) : [];
  93. $upgradeGradeMoney = isset($upgrade['expend_money'])? floatval($upgrade['expend_money']) : 0;
  94. if($id>$gradeId && $upgradeGradeMoney<=$upgradeMoney){
  95. $newGradeId = $id;
  96. }
  97. }
  98. // 处理
  99. if($newGradeId>0){
  100. Db::startTrans();
  101. if(!UserModel::where(['user_id'=>$user_id])->update(['grade_id'=> $newGradeId,'update_time'=> time()])){
  102. Db::rollback();
  103. throw new Exception('升级处理失败');
  104. }
  105. $log = [
  106. 'user_id'=> $user_id,
  107. 'old_grade_id'=> $gradeId,
  108. 'new_grade_id'=> $newGradeId,
  109. 'change_type'=> 20,
  110. 'wxapp_id'=> $wxapp_id,
  111. 'remark'=> "升级成功",
  112. 'create_time'=> time()
  113. ];
  114. if(!GradeLog::insertgetId($log)){
  115. Db::rollback();
  116. throw new Exception('升级处理失败');
  117. }
  118. // 推荐奖金
  119. self::settleAward($user_id, $wxapp_id);
  120. Db::commit();
  121. return true;
  122. }
  123. }
  124. }
  125. return false;
  126. }
  127. /**
  128. * 获取用户信息
  129. * @param $userId
  130. * @param $wxapp_id
  131. * @return mixed
  132. */
  133. public static function getUserInfo($userId, $wxapp_id)
  134. {
  135. if(empty($userId)){
  136. return false;
  137. }
  138. $model = new \app\common\model\dealer\User();
  139. return $dealerUserInfo = $model::alias('a')
  140. ->leftJoin('user u','u.user_id=a.user_id')
  141. ->leftJoin('user_grade ug','ug.grade_id=u.grade_id')
  142. ->where(['a.user_id'=>$userId,'a.wxapp_id'=>$wxapp_id,'a.is_delete'=>0])
  143. ->field('a.user_id,a.money,a.referee_id,u.grade_id,ug.weight as level,ug.name as grade_name')
  144. ->find();
  145. }
  146. /**
  147. * 结算推荐佣金
  148. */
  149. public static function settleAward($userId, $wxapp_id)
  150. {
  151. // 奖金配置参数
  152. $config = Setting::getItem('commission', $wxapp_id);
  153. if(empty($config)){
  154. return false;
  155. }
  156. $dealerUserInfo = self::getUserInfo($userId, $wxapp_id);
  157. $userLevel = isset($dealerUserInfo['level'])? $dealerUserInfo['level'] : 0;
  158. $firstId = isset($dealerUserInfo['referee_id'])? $dealerUserInfo['referee_id'] : 0;
  159. if(empty($dealerUserInfo) || $firstId<=0){
  160. return false;
  161. }
  162. $firstInfo = self::getUserInfo($firstId, $wxapp_id);
  163. $firstLevel = isset($firstInfo['level'])? $firstInfo['level'] : 0;
  164. $firstLevelName = isset($firstInfo['grade_name'])? $firstInfo['grade_name'] : '';
  165. $secondId = isset($firstInfo['referee_id'])? $firstInfo['referee_id'] : 0;
  166. $secondInfo = self::getUserInfo($secondId, $wxapp_id);
  167. $secondLevel = isset($secondInfo['level'])? $secondInfo['level'] : 0;
  168. $secondLevelName = isset($secondInfo['grade_name'])? $secondInfo['grade_name'] : '';
  169. // 平级奖 (vip以及以上等级)
  170. $model = new \app\common\model\dealer\User();
  171. if($firstInfo && $userLevel>=1){
  172. // 直推平级奖
  173. $equalScore1 = isset($config['equal_score_1'])? floatval($config['equal_score_1']) : 0;
  174. if($userLevel==$firstLevel && $equalScore1>0){
  175. $model->grantMoney($firstId, $equalScore1, 2, "来自用户[{$userId}]直推[{$firstLevelName}]平级奖");
  176. }
  177. // 间推平级奖
  178. $equalScore2 = isset($config['equal_score_2'])? floatval($config['equal_score_2']) : 0;
  179. if($secondInfo && $userLevel==$secondLevel && $equalScore2>0){
  180. $model->grantMoney($secondId, $equalScore2, 2, "来自用户[{$userId}]间推[{$secondLevelName}]平级奖");
  181. }
  182. }
  183. // 反推奖(上级的级别大于当前用户)
  184. $reverseScore = isset($config['reverse_score_'.$firstLevel])? floatval($config['reverse_score_'.$firstLevel]) : 0;
  185. if($firstLevel>$userLevel && $reverseScore>0){
  186. $model->grantMoney($firstId, $reverseScore, 2, "来自用户[{$userId}]反推[{$firstLevelName}]奖");
  187. }
  188. return true;
  189. }
  190. }