UserMp.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace app\api\model\user;
  3. use app\api\model\plus\agent\Referee as RefereeModel;
  4. use app\api\model\plus\invitationgift\Partake;
  5. use app\common\model\user\Grade as GradeModel;
  6. use think\facade\Cache;
  7. use app\common\exception\BaseException;
  8. use app\common\model\user\User as UserModel;
  9. /**
  10. * 公众号用户模型类
  11. */
  12. class UserMp extends UserModel
  13. {
  14. private $token;
  15. /**
  16. * 隐藏字段
  17. */
  18. protected $hidden = [
  19. 'open_id',
  20. 'is_delete',
  21. 'app_id',
  22. 'create_time',
  23. 'update_time'
  24. ];
  25. /**
  26. * 用户登录
  27. */
  28. public function login($userInfo, $referee_id = null)
  29. {
  30. $userInfo = $userInfo['original'];
  31. return [
  32. // 'nickName' => authcode($userInfo['nickname'], 'ENCODE'),
  33. // 'avatarUrl' => authcode($userInfo['headimgurl'], 'ENCODE'),
  34. // 'gender' => $userInfo['sex'],
  35. // 'province' => $userInfo['province'],
  36. // 'country' => $userInfo['country'],
  37. // 'city' => $userInfo['city'],
  38. 'openid' => authcode($userInfo['openid'], 'ENCODE'),
  39. // 'referee_id' => $referee_id,
  40. ];
  41. return $data;
  42. }
  43. // /**
  44. // * 用户登录
  45. // */
  46. // public function login($userInfo, $referee_id = null)
  47. // {
  48. // $userInfo = $userInfo['original'];
  49. // // 邀请好友
  50. // $invitation_id = isset($post['invitation_id']) ? $post['invitation_id'] : 0;
  51. // // 自动注册用户
  52. // $user_id = $this->register($userInfo, $referee_id, $invitation_id);
  53. // // 生成token (session3rd)
  54. // $this->token = $this->token($userInfo['openid']);
  55. // // 记录缓存, 7天
  56. // Cache::set($this->token, $user_id, 86400 * 7);
  57. // return $user_id;
  58. // }
  59. /**
  60. * 获取token
  61. */
  62. public function getToken()
  63. {
  64. return $this->token;
  65. }
  66. /**
  67. * 生成用户认证的token
  68. */
  69. private function token($openid)
  70. {
  71. return md5($openid . 'token_salt');
  72. }
  73. /**
  74. * 自动注册用户
  75. */
  76. private function register($userInfo, $referee_id, $invitation_id)
  77. {
  78. $data = [];
  79. //通过unionid查询用户是否存在
  80. $user = null;
  81. if(isset($userInfo['unionid']) && !empty($userInfo['unionid'])){
  82. $data['union_id'] = $userInfo['unionid'];
  83. $user = self::detailByUnionid($userInfo['unionid']);
  84. }
  85. // 查询用户是否已存在
  86. if(!$user){
  87. $user = self::detail(['mpopen_id' => $userInfo['openid']]);
  88. }
  89. if($user){
  90. $model = $user;
  91. if (isset($data['union_id'])) {
  92. // 只修改union_id
  93. $data = [
  94. 'union_id' => $data['union_id'],
  95. ];
  96. }else {
  97. return $user['user_id'];
  98. }
  99. }else{
  100. if($referee_id > 0){
  101. if(UserModel::detail($referee_id) == null){
  102. $referee_id = 0;
  103. }
  104. }
  105. $model = $this;
  106. $data['referee_id'] = $referee_id;
  107. $data['reg_source'] = 'mp';
  108. //默认等级
  109. $data['grade_id'] = GradeModel::getDefaultGradeId();
  110. }
  111. $data['mpopen_id'] = $userInfo['openid'];
  112. // 用户昵称
  113. if(!$user){
  114. $data['nickName'] = preg_replace('/[\xf0-\xf7].{3}/', '', $userInfo['nickname']);
  115. }
  116. $data['avatarUrl'] = $userInfo['headimgurl'];
  117. $data['gender'] = $userInfo['sex'];
  118. $data['province'] = $userInfo['province'];
  119. $data['country'] = $userInfo['country'];
  120. $data['city'] = $userInfo['city'];
  121. try {
  122. $this->startTrans();
  123. // 保存/更新用户记录
  124. if (!$model->save(array_merge($data, [
  125. 'app_id' => self::$app_id
  126. ]))
  127. ) {
  128. throw new BaseException(['msg' => '用户注册失败']);
  129. }
  130. if (!$user && $referee_id > 0) {
  131. // 记录推荐人关系,
  132. RefereeModel::createRelation($model['user_id'], $referee_id);
  133. //更新用户邀请数量
  134. (new UserModel())->setIncInvite($referee_id);
  135. //邀请好友送好礼
  136. $invitation_id > 0 && (new Partake())->addPartake($invitation_id, $referee_id, $model['user_id']);
  137. }
  138. $this->commit();
  139. } catch (\Exception $e) {
  140. $this->rollback();
  141. throw new BaseException(['msg' => $e->getMessage()]);
  142. }
  143. return $model['user_id'];
  144. }
  145. }