User.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace app\api\model\user;
  3. use app\common\library\helper;
  4. use app\common\library\wechat\WxBizDataCrypt;
  5. use think\facade\Cache;
  6. use app\common\exception\BaseException;
  7. use app\common\model\user\User as UserModel;
  8. use app\api\model\plus\agent\Referee as RefereeModel;
  9. use app\common\library\easywechat\AppWx;
  10. use app\common\model\user\Grade as GradeModel;
  11. use app\api\model\plus\invitationgift\Partake;
  12. use app\common\model\page\CenterMenu as CenterMenuModel;
  13. /**
  14. * 用户模型类
  15. */
  16. class User extends UserModel
  17. {
  18. private $token;
  19. /**
  20. * 隐藏字段
  21. */
  22. protected $hidden = [
  23. 'open_id',
  24. 'is_delete',
  25. 'app_id',
  26. 'create_time',
  27. 'update_time'
  28. ];
  29. /**
  30. * 获取用户信息
  31. */
  32. public static function getUser($token)
  33. {
  34. $userId = Cache::get($token);
  35. return (new static())->where(['user_id' => $userId])->with(['address', 'addressDefault', 'grade'])->order('user_id desc')->find();
  36. }
  37. /**
  38. * 获取openID
  39. */
  40. public function login($post)
  41. {
  42. // 微信登录 获取session_key
  43. $app = AppWx::getApp();
  44. $session = $app->auth->session($post['code']);
  45. if (empty($session['openid'])) {
  46. throw new BaseException(['msg' => 'code码失效']);
  47. }
  48. return $session;
  49. // // 自动注册用户
  50. // $refereeId = isset($post['referee_id']) ? $post['referee_id'] : null;
  51. // $userInfo = json_decode(htmlspecialchars_decode($post['user_info']), true);
  52. // //邀请好友
  53. // $invitation_id = isset($post['invitation_id']) ? $post['invitation_id'] : 0;
  54. // $user_id = $this->register($session['openid'], $userInfo, $refereeId, $session, $invitation_id);
  55. // // 生成token (session3rd)
  56. // $this->token = $this->token($session['openid']);
  57. // // 记录缓存, 7天
  58. // Cache::tag('cache')->set($this->token, $user_id, 86400 * 7);
  59. // return $user_id;
  60. }
  61. /**
  62. * 账号密码登陆
  63. */
  64. public function accountlogin($post)
  65. {
  66. if (empty($post['openid'])) {
  67. $this->error = '微信openid为空';
  68. return false;
  69. }
  70. if (empty($post['user_no']) || empty($post['password'])) {
  71. $this->error = '登陆账号密码为空';
  72. return false;
  73. }
  74. $where = [
  75. ['user_no','=',$post['user_no']],
  76. ['is_delete','=',0],
  77. ];
  78. $user = $this->where($where)->order('user_id desc')->find();
  79. if (empty($user)) {
  80. $this->error = '登录账号错误';
  81. return false;
  82. }
  83. if ($user['password'] != md5($post['password'])) {
  84. $this->error = '登陆密码错误';
  85. return false;
  86. }
  87. $info = [
  88. 'open_id' => $post['openid'],
  89. 'reg_source' => 'wx',
  90. ];
  91. $this->where('user_id',$user['user_id'])->update($info);
  92. $this->token = $this->token($user['user_no']);
  93. // 记录缓存, 30天
  94. Cache::tag('cache')->set($this->token, $user['user_id'], 86400 * 30);
  95. return $user['user_id'];
  96. }
  97. /**
  98. * 用户登录
  99. */
  100. public function bindMobile($post)
  101. {
  102. try {
  103. // 微信登录 获取session_key
  104. $app = AppWx::getApp();
  105. $iv = $post['iv'];
  106. $encrypted_data = $post['encrypted_data'];
  107. $pc = new WxBizDataCrypt($app['config']['app_id'], $post['session_key']);
  108. $errCode = $pc->decryptData($encrypted_data, $iv, $data);
  109. if ($errCode == 0) {
  110. $data = json_decode($data, true);
  111. $this->save([
  112. 'mobile' => $data['phoneNumber']
  113. ]);
  114. return $data['phoneNumber'];
  115. } else {
  116. log_write('$errCode' . $errCode);
  117. return false;
  118. }
  119. } catch (\Exception $e) {
  120. $this->error = '获取手机号失败,请重试';
  121. return false;
  122. }
  123. }
  124. /**
  125. * 获取token
  126. */
  127. public function getToken()
  128. {
  129. return $this->token;
  130. }
  131. /**
  132. * 生成用户认证的token
  133. */
  134. private function token($openid)
  135. {
  136. $app_id = self::$app_id;
  137. // 生成一个不会重复的随机字符串
  138. $guid = \getGuidV4();
  139. // 当前时间戳 (精确到毫秒)
  140. $timeStamp = microtime(true);
  141. // 自定义一个盐
  142. $salt = 'token_salt';
  143. return md5("{$app_id}_{$timeStamp}_{$openid}_{$guid}_{$salt}");
  144. }
  145. /**
  146. * 自动注册用户
  147. */
  148. private function register($open_id, $data, $refereeId, $decryptedData, $invitation_id)
  149. {
  150. //通过unionid查询用户是否存在
  151. $user = null;
  152. if (isset($decryptedData['unionid']) && !empty($decryptedData['unionid'])) {
  153. $data['union_id'] = $decryptedData['unionid'];
  154. $user = self::detailByUnionid($decryptedData['unionid']);
  155. }
  156. if (!$user) {
  157. // 通过open_id查询用户是否已存在
  158. $user = self::detail(['open_id' => $open_id]);
  159. }
  160. if ($user) {
  161. $model = $user;
  162. // 只修改union_id
  163. if (isset($data['union_id'])) {
  164. $data = [
  165. 'union_id' => $data['union_id'],
  166. ];
  167. }else{
  168. return $user['user_id'];
  169. }
  170. } else {
  171. $model = $this;
  172. $data['referee_id'] = $refereeId;
  173. $data['reg_source'] = 'wx';
  174. //默认等级
  175. $data['grade_id'] = GradeModel::getDefaultGradeId();
  176. }
  177. $this->startTrans();
  178. try {
  179. // 保存/更新用户记录
  180. if (!$model->save(array_merge($data, [
  181. 'open_id' => $open_id,
  182. 'app_id' => self::$app_id
  183. ]))
  184. ) {
  185. throw new BaseException(['msg' => '用户注册失败']);
  186. }
  187. if (!$user && $refereeId > 0) {
  188. // 记录推荐人关系
  189. RefereeModel::createRelation($model['user_id'], $refereeId);
  190. //更新用户邀请数量
  191. (new UserModel())->setIncInvite($refereeId);
  192. //邀请好友送好礼
  193. $invitation_id > 0 && (new Partake())->addPartake($invitation_id, $refereeId, $model['user_id']);
  194. }
  195. $this->commit();
  196. } catch (\Exception $e) {
  197. $this->rollback();
  198. throw new BaseException(['msg' => $e->getMessage()]);
  199. }
  200. return $model['user_id'];
  201. }
  202. /**
  203. *统计被邀请人数
  204. */
  205. public function getCountInv($user_id)
  206. {
  207. return $this->where('referee_id', '=', $user_id)->count('user_id');
  208. }
  209. /**
  210. * 签到更新用户积分
  211. */
  212. public function setPoints($user_id, $days, $sign_conf, $sign_date)
  213. {
  214. $rank = $sign_conf['ever_sign'];
  215. if ($sign_conf['is_increase'] == 'true') {
  216. if ($days >= $sign_conf['no_increase']) {
  217. $days = $sign_conf['no_increase'] - 1;
  218. }
  219. $rank = ($days - 1) * $sign_conf['increase_reward'] + $rank;
  220. }
  221. //是否奖励
  222. if (isset($sign_conf['reward_data'])) {
  223. $arr = array_column($sign_conf['reward_data'], 'day');
  224. if (in_array($days, $arr)) {
  225. $key = array_search($days, $arr);
  226. if ($sign_conf['reward_data'][$key]['is_point'] == 'true') {
  227. $rank = $sign_conf['reward_data'][$key]['point'] + $rank;
  228. }
  229. }
  230. }
  231. // 新增积分变动明细
  232. $this->setIncPoints($rank, '用户签到:签到日期' . $sign_date);
  233. return $rank;
  234. }
  235. /**
  236. * 个人中心菜单列表
  237. */
  238. public static function getMenus($source, $user_id)
  239. {
  240. // 系统菜单
  241. $sys_menus = CenterMenuModel::getSysMenu();
  242. // 查询用户菜单
  243. $model = new CenterMenuModel();
  244. $user_menus = $model->getAll();
  245. $user_menu_tags = [];
  246. foreach ($user_menus as $menu) {
  247. $menu['sys_tag'] != '' && array_push($user_menu_tags, $menu['sys_tag']);
  248. }
  249. $save_data = [];
  250. foreach ($sys_menus as $menu) {
  251. if ($menu['sys_tag'] != '' && !in_array($menu['sys_tag'], $user_menu_tags)) {
  252. $save_data[] = array_merge($sys_menus[$menu['sys_tag']], [
  253. 'sort' => 100,
  254. 'app_id' => self::$app_id
  255. ]);
  256. }
  257. }
  258. if (count($save_data) > 0) {
  259. $model->saveAll($save_data);
  260. Cache::delete('center_menu_' . self::$app_id);
  261. $user_menus = $model->getAll();
  262. }
  263. $show_menus = [];
  264. foreach ($user_menus as $menus) {
  265. if (strpos($menus['icon'], 'http') !== 0) {
  266. $menus['icon'] = self::$base_url . $menus['icon'];
  267. }
  268. if ($menus['is_show'] == 1) {
  269. array_push($show_menus, $menus);
  270. }
  271. }
  272. return $show_menus;
  273. }
  274. /**
  275. * 修改会员信息
  276. */
  277. public function edit($data)
  278. {
  279. return $this->save($data);
  280. }
  281. }