User.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\facade\Log;
  5. class User extends Base
  6. {
  7. protected $pk = 'uid';
  8. public function userAuth()
  9. {
  10. return $this->hasMany('UserAuths', 'uid', 'uid');
  11. }
  12. public function invitation()
  13. {
  14. return $this->hasOne('InviteRelation', 'uid', 'uid');
  15. }
  16. /**
  17. * 用户登录模型
  18. * @param $param
  19. * @param int $type
  20. * @return array|int|mixed
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. * @throws \think\exception\DbException
  24. */
  25. public function login($param, $type = 1)
  26. {
  27. $user = array();
  28. $auth = array();
  29. switch ($type) {
  30. case 1:
  31. $auth['identity_type'] = 'username';
  32. $auth['identifier'] = $param['username'];
  33. break;
  34. case 2:
  35. $auth['identity_type'] = 'email';
  36. $auth['identifier'] = $param['username'];
  37. break;
  38. case 3:
  39. $auth['identity_type'] = 'mobile';
  40. $auth['identifier'] = $param['username'];
  41. break;
  42. case 4:
  43. Log::record($param['username']);
  44. $auth['identity_type'] = $param['username']['channel'];
  45. $auth['identifier'] = $param['username']['openid'];
  46. break;
  47. }
  48. $user = $this->userAuth()->where($auth)->find();
  49. if (isset($user['uid']) && $user['uid']) {
  50. $uinfo = $this->where('uid',$user['uid'])->field('inviter_code,social_type')->find();
  51. $data = array(
  52. 'uid' => $user['uid'],
  53. 'inviter_code' => $uinfo['inviter_code']
  54. );
  55. if (isset($uinfo['social_type']) && $uinfo['social_type']) {
  56. $data['social_type'] = $uinfo['social_type'];
  57. }
  58. if ($auth['identity_type'] == 'weixin') {
  59. $data['openid'] = $auth['identifier'];
  60. }
  61. } else {
  62. $inviter_code = null;
  63. $user['nickname'] = 'hm_'. rand_string(8);
  64. $user['gender'] = 1;
  65. $user['avatar'] = 'http://www.wercf.com/img/avatar_1.png';
  66. if ($type == 2) {
  67. $user['email'] = $param['username'];
  68. } elseif ($type == 3) {
  69. $user['mobile'] = $param['username'];
  70. } elseif ($type == 4) {
  71. $user['nickname'] = $param['username']['nick'];
  72. $user['gender'] = $param['username']['gender'];
  73. $user['avatar'] = $param['username']['avatar'];
  74. }
  75. if (isset($param['inviter_code']) && $param['inviter_code'] != '') {
  76. $inviter_code = $param['inviter_code'];
  77. }
  78. $auth['credential'] = $param['password'];
  79. $data = $this->register($auth, $user, $inviter_code);
  80. }
  81. return $data;
  82. }
  83. /**
  84. * 注册
  85. * @param $auth
  86. * @param array $user
  87. * @return int|mixed
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. * @throws \think\exception\DbException
  91. */
  92. public function register($auth, $user = [], $inviter_code = null)
  93. {
  94. $auth['salt'] = rand_string(6);
  95. $user['inviter_code'] = createInviteCode();
  96. if ($auth['identity_type'] === 'username') {
  97. $auth['credential'] = md5($auth['credential'] . $auth['salt']);
  98. }
  99. $this->startTrans();
  100. try {
  101. $this->save($user);
  102. $auth['uid'] = $this->uid;
  103. $user_discount = [
  104. 'uid' => $this->uid,
  105. 'discount_id' => 1,
  106. 'created_at' =>date("Y-m-d",time()),
  107. 'end_at' => date("Y-m-d",time()+3600*24*14)
  108. ];
  109. Db::name('user_discount')->insert($user_discount);
  110. $this->userAuth()->save($auth);
  111. if ($inviter_code) {
  112. $data = array(
  113. 'uid' => $auth['uid'],
  114. 'inviter' => $this->where('inviter_code', $inviter_code)->value('uid'),
  115. 'status' => 1,
  116. 'create_timestamp' => date('Y-m-d H:i:s')
  117. );
  118. $this->invitation()->save($data);
  119. $user_discount['discount_id'] = 2;
  120. Db::name('user_discount')->insert($user_discount);
  121. }
  122. $ret = array(
  123. 'uid' => $this->uid,
  124. 'inviter_code' => $user['inviter_code'],
  125. );
  126. if($auth['identity_type'] === 'weixin') {
  127. $ret['openid'] = $auth['identifier'];
  128. }
  129. $this->commit();
  130. } catch (\Exception $e) {
  131. $ret = -1;
  132. $this->rollback();
  133. }
  134. return $ret;
  135. }
  136. /**
  137. * @param $uid
  138. * @throws \think\db\exception\DataNotFoundException
  139. * @throws \think\db\exception\ModelNotFoundException
  140. * @throws \think\exception\DbException
  141. */
  142. public function getInvitationList($uid){
  143. return $this->table('hippo_invite_relation')
  144. ->alias('a')
  145. ->leftJoin('hippo_user b', 'a.uid = b.uid')
  146. ->field('a.uid,a.status,b.avatar,b.nickname,a.create_timestamp,b.realname')
  147. ->where('a.inviter',$uid)
  148. ->select();
  149. }
  150. //获取一级邀请列表 pc端
  151. public function getInvitationListpc($uid,$page,$number){
  152. return $this->table("hippo_invite_relation")
  153. ->alias('a')
  154. ->leftJoin('hippo_user b', 'a.uid = b.uid')
  155. ->field('a.uid,a.status,b.avatar,b.nickname,a.create_timestamp,b.realname')
  156. -> where("inviter=$uid")
  157. ->page($page.','.$number)
  158. ->order('create_timestamp asc')
  159. -> select();
  160. }
  161. //获取二级邀请列表 pc端
  162. public function getInvitationListpcchildren($uid,$page,$number){
  163. $firstlist = $this->table("hippo_invite_relation")
  164. ->field('uid')
  165. -> where("inviter=$uid")
  166. -> select();
  167. return $firstlist;
  168. }
  169. public function logout()
  170. {
  171. session('user_auth', null);
  172. session('user_auth_sign', null);
  173. }
  174. public function getInfo($uid)
  175. {
  176. $data = $this->where(array('uid' => $uid))->find();
  177. return $data;
  178. }
  179. /**
  180. * @param $uid
  181. * @throws \think\db\exception\DataNotFoundException
  182. * @throws \think\db\exception\ModelNotFoundException
  183. * @throws \think\exception\DbException
  184. */
  185. public function getDiscountList($uid){
  186. $list = Db::table('hippo_user_discount')
  187. ->alias('a')
  188. ->leftJoin('hippo_discount b', 'a.discount_id = b.id')
  189. ->field('a.id,a.end_at,b.money')
  190. ->where("a.uid=$uid AND a.status=0")
  191. ->select();
  192. if(!empty($list)){
  193. $array = array();
  194. foreach($list as $k => $v){
  195. $array[$k]['id'] = $list[$k]['id'];
  196. $array[$k]['money'] = '-¥'.$list[$k]['money'];
  197. $array[$k]['end_at'] = $list[$k]['end_at'];
  198. }
  199. return $array;
  200. }
  201. return $list;
  202. }
  203. //获取用户余额
  204. public function getUserBalance($uid)
  205. {
  206. $data = Db::table('hippo_user')->field('balance')->where(array('uid' => $uid))->find();
  207. return $data;
  208. }
  209. }