User.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 $userId
  61. * @param $post
  62. * @return UserModel
  63. * @throws Exception
  64. */
  65. public static function setProfile($userId, $post)
  66. {
  67. $nickname = isset($post['nickname'])? trim($post['nickname']) : '';
  68. if(empty($nickname)){
  69. throw new Exception('请填写用户昵称');
  70. }
  71. $mobile = isset($post['mobile'])? trim($post['mobile']) : '';
  72. if(empty($mobile) || !isMobile($mobile)){
  73. throw new Exception('请填写正确的手机号码');
  74. }
  75. if(UserModel::where(['mobile'=> $mobile])->whereNotIn('user_id', $userId)->value('user_id')){
  76. throw new Exception('手机号码已被使用');
  77. }
  78. return UserModel::where('user_id', $userId)->update(['nickName'=>$nickname,'mobile'=>$mobile,'update_time'=>time()]);
  79. }
  80. /**
  81. * 用户升级
  82. * @param $user_id
  83. * @param int $wxapp_id
  84. * @return bool
  85. * @throws Exception
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. * @throws \think\exception\DbException
  89. */
  90. public static function upgrade($user_id, $wxapp_id=0)
  91. {
  92. $info = UserModel::alias('a')
  93. ->join('user_grade ug', 'ug.grade_id=a.grade_id','left')
  94. ->where('user_id', $user_id)
  95. ->field('a.user_id,a.grade_id,ug.name,a.expend_upgrade_money')
  96. ->find();
  97. if(empty($info)){
  98. return false;
  99. }
  100. $gradeId = isset($info['grade_id'])? intval($info['grade_id']) : 0;
  101. $upgradeMoney = isset($info['expend_upgrade_money'])? floatval($info['expend_upgrade_money']) : 0;
  102. if($upgradeMoney>0){
  103. $gradeList = Grade::where(['status'=> 1, 'is_delete'=> 0,'wxapp_id'=>$wxapp_id])
  104. ->where('grade_id','>', $gradeId)
  105. ->field('grade_id,name,upgrade')
  106. ->order('weight asc')
  107. ->select();
  108. $gradeList = $gradeList? $gradeList->toArray() : [];
  109. if($gradeList){
  110. $log = [];
  111. $newGradeId = 0;
  112. foreach ($gradeList as $v){
  113. $id = isset($v['grade_id'])? $v['grade_id'] : 0;
  114. $upgrade = isset($v['upgrade']) && $v['upgrade']? json_decode($v['upgrade'], true) : [];
  115. $upgradeGradeMoney = isset($upgrade['expend_money'])? floatval($upgrade['expend_money']) : 0;
  116. if($id>$gradeId && $upgradeGradeMoney<=$upgradeMoney){
  117. $newGradeId = $id;
  118. }
  119. }
  120. // 处理
  121. if($newGradeId>0){
  122. Db::startTrans();
  123. if(!UserModel::where(['user_id'=>$user_id])->update(['grade_id'=> $newGradeId,'update_time'=> time()])){
  124. Db::rollback();
  125. throw new Exception('升级处理失败');
  126. }
  127. $log = [
  128. 'user_id'=> $user_id,
  129. 'old_grade_id'=> $gradeId,
  130. 'new_grade_id'=> $newGradeId,
  131. 'change_type'=> 20,
  132. 'wxapp_id'=> $wxapp_id,
  133. 'remark'=> "升级成功",
  134. 'create_time'=> time()
  135. ];
  136. if(!GradeLog::insertgetId($log)){
  137. Db::rollback();
  138. throw new Exception('升级处理失败');
  139. }
  140. // 推荐奖金
  141. self::settleAward($user_id, $wxapp_id);
  142. Db::commit();
  143. return true;
  144. }
  145. }
  146. }
  147. return false;
  148. }
  149. /**
  150. * 获取用户信息
  151. * @param $userId
  152. * @param $wxapp_id
  153. * @return mixed
  154. */
  155. public static function getUserInfo($userId, $wxapp_id)
  156. {
  157. if(empty($userId)){
  158. return false;
  159. }
  160. $model = new \app\common\model\dealer\User();
  161. return $dealerUserInfo = $model::alias('a')
  162. ->join('user u','u.user_id=a.user_id','left')
  163. ->join('user_grade ug','ug.grade_id=u.grade_id','left')
  164. ->where(['a.user_id'=>$userId,'a.wxapp_id'=>$wxapp_id,'a.is_delete'=>0])
  165. ->field('a.user_id,a.money,a.referee_id,u.grade_id,ug.weight as level,ug.name as grade_name')
  166. ->find();
  167. }
  168. /**
  169. * 结算推荐佣金
  170. */
  171. public static function settleAward($userId, $wxapp_id)
  172. {
  173. // 奖金配置参数
  174. $config = Setting::getItem('commission', $wxapp_id);
  175. if(empty($config)){
  176. return false;
  177. }
  178. $dealerUserInfo = self::getUserInfo($userId, $wxapp_id);
  179. $userLevel = isset($dealerUserInfo['level'])? $dealerUserInfo['level'] : 0;
  180. $firstId = isset($dealerUserInfo['referee_id'])? $dealerUserInfo['referee_id'] : 0;
  181. if(empty($dealerUserInfo) || $firstId<=0){
  182. return false;
  183. }
  184. $firstInfo = self::getUserInfo($firstId, $wxapp_id);
  185. $firstLevel = isset($firstInfo['level'])? $firstInfo['level'] : 0;
  186. $firstLevelName = isset($firstInfo['grade_name'])? $firstInfo['grade_name'] : '';
  187. $secondId = isset($firstInfo['referee_id'])? $firstInfo['referee_id'] : 0;
  188. $secondInfo = self::getUserInfo($secondId, $wxapp_id);
  189. $secondLevel = isset($secondInfo['level'])? $secondInfo['level'] : 0;
  190. $secondLevelName = isset($secondInfo['grade_name'])? $secondInfo['grade_name'] : '';
  191. // 平级奖 (vip以及以上等级)
  192. $model = new \app\common\model\dealer\User();
  193. if($firstInfo && $userLevel>=1){
  194. // 直推平级奖
  195. $equalScore1 = isset($config['equal_score_1'])? floatval($config['equal_score_1']) : 0;
  196. if($userLevel==$firstLevel && $equalScore1>0){
  197. $model->grantMoney($firstId, $equalScore1, 2, "来自用户[{$userId}]直推[{$firstLevelName}]平级奖");
  198. }
  199. // 间推平级奖
  200. $equalScore2 = isset($config['equal_score_2'])? floatval($config['equal_score_2']) : 0;
  201. if($secondInfo && $userLevel==$secondLevel && $equalScore2>0){
  202. $model->grantMoney($secondId, $equalScore2, 2, "来自用户[{$userId}]间推[{$secondLevelName}]平级奖");
  203. }
  204. }
  205. // 反推奖(上级的级别大于当前用户)
  206. $reverseScore = isset($config['reverse_score_'.$firstLevel])? floatval($config['reverse_score_'.$firstLevel]) : 0;
  207. if($firstLevel>$userLevel && $reverseScore>0){
  208. $model->grantMoney($firstId, $reverseScore, 2, "来自用户[{$userId}]反推[{$firstLevelName}]奖");
  209. }
  210. return true;
  211. }
  212. }