User.php 8.4 KB

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