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