User.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace app\api\model;
  3. use think\Cache;
  4. use app\common\library\wechat\WxUser;
  5. use app\common\exception\BaseException;
  6. use app\common\model\User as UserModel;
  7. use app\api\model\dealer\Referee as RefereeModel;
  8. use app\api\model\dealer\Setting as DealerSettingModel;
  9. /**
  10. * 用户模型类
  11. * Class User
  12. * @package app\api\model
  13. */
  14. class User extends UserModel
  15. {
  16. private $token;
  17. /**
  18. * 隐藏字段
  19. * @var array
  20. */
  21. protected $hidden = [
  22. 'open_id',
  23. 'is_delete',
  24. 'wxapp_id',
  25. 'create_time',
  26. 'update_time'
  27. ];
  28. /**
  29. * 获取用户信息
  30. * @param $token
  31. * @return null|static
  32. * @throws \think\exception\DbException
  33. */
  34. public static function getUser($token)
  35. {
  36. $openId = Cache::get($token)['openid'];
  37. return self::detail(['open_id' => $openId], ['address', 'addressDefault', 'grade']);
  38. }
  39. /**
  40. * 用户登录
  41. * @param array $post
  42. * @return string
  43. * @throws BaseException
  44. * @throws \think\Exception
  45. * @throws \think\exception\DbException
  46. */
  47. public function login($post)
  48. {
  49. // 微信登录 获取session_key
  50. $session = $this->wxlogin($post['code']);
  51. // 自动注册用户
  52. $refereeId = isset($post['referee_id']) ? $post['referee_id'] : null;
  53. $userInfo = json_decode(htmlspecialchars_decode($post['user_info']), true);
  54. $user_id = $this->register($session['openid'], $userInfo, $refereeId);
  55. // 生成token (session3rd)
  56. $this->token = $this->token($session['openid']);
  57. // 记录缓存, 7天
  58. Cache::set($this->token, $session, 86400 * 7);
  59. return $user_id;
  60. }
  61. /**
  62. * 获取token
  63. * @return mixed
  64. */
  65. public function getToken()
  66. {
  67. return $this->token;
  68. }
  69. /**
  70. * 微信登录
  71. * @param $code
  72. * @return array|mixed
  73. * @throws BaseException
  74. * @throws \think\exception\DbException
  75. */
  76. private function wxlogin($code)
  77. {
  78. // 获取当前小程序信息
  79. $wxConfig = Wxapp::getWxappCache();
  80. // 验证appid和appsecret是否填写
  81. if (empty($wxConfig['app_id']) || empty($wxConfig['app_secret'])) {
  82. throw new BaseException(['msg' => '请到 [后台-小程序设置] 填写appid 和 appsecret']);
  83. }
  84. // 微信登录 (获取session_key)
  85. $WxUser = new WxUser($wxConfig['app_id'], $wxConfig['app_secret']);
  86. if (!$session = $WxUser->sessionKey($code)) {
  87. throw new BaseException(['msg' => $WxUser->getError()]);
  88. }
  89. return $session;
  90. }
  91. /**
  92. * 生成用户认证的token
  93. * @param $openid
  94. * @return string
  95. */
  96. private function token($openid)
  97. {
  98. $wxapp_id = self::$wxapp_id;
  99. // 生成一个不会重复的随机字符串
  100. $guid = \getGuidV4();
  101. // 当前时间戳 (精确到毫秒)
  102. $timeStamp = microtime(true);
  103. // 自定义一个盐
  104. $salt = 'token_salt';
  105. return md5("{$wxapp_id}_{$timeStamp}_{$openid}_{$guid}_{$salt}");
  106. }
  107. /**
  108. * 自动注册用户
  109. * @param $open_id
  110. * @param $data
  111. * @param int $refereeId
  112. * @return mixed
  113. * @throws \Exception
  114. * @throws \think\exception\DbException
  115. */
  116. private function register($open_id, $data, $refereeId = null)
  117. {
  118. // 查询用户是否已存在
  119. $user = self::detail(['open_id' => $open_id]);
  120. $model = $user ?: $this;
  121. $this->startTrans();
  122. try {
  123. // 保存/更新用户记录
  124. if (!$model->allowField(true)->save(array_merge($data, [
  125. 'open_id' => $open_id,
  126. 'status' => $user?$user['status'] : 0,
  127. 'wxapp_id' => self::$wxapp_id
  128. ]))) {
  129. throw new BaseException(['msg' => '用户注册失败']);
  130. }
  131. // 记录推荐人关系
  132. if (!$user && $refereeId > 0) {
  133. RefereeModel::createRelation($model['user_id'], $refereeId);
  134. }
  135. $this->commit();
  136. } catch (\Exception $e) {
  137. $this->rollback();
  138. throw new BaseException(['msg' => $e->getMessage()]);
  139. }
  140. return $model['user_id'];
  141. }
  142. /**
  143. * 个人中心菜单列表
  144. * @return array
  145. */
  146. public function getMenus()
  147. {
  148. $menus = [
  149. 'address' => [
  150. 'name' => '收货地址',
  151. 'url' => 'pages/address/index',
  152. 'icon' => 'map'
  153. ],
  154. /*
  155. 'coupon' => [
  156. 'name' => '领券中心',
  157. 'url' => 'pages/coupon/coupon',
  158. 'icon' => 'lingquan'
  159. ],
  160. 'my_coupon' => [
  161. 'name' => '我的优惠券',
  162. 'url' => 'pages/user/coupon/coupon',
  163. 'icon' => 'youhuiquan'
  164. ],
  165. */
  166. // 'sharing_order' => [
  167. // 'name' => '拼团订单',
  168. // 'url' => 'pages/sharing/order/index',
  169. // 'icon' => 'pintuan'
  170. // ],
  171. // 'my_bargain' => [
  172. // 'name' => '我的砍价',
  173. // 'url' => 'pages/bargain/index/index?tab=1',
  174. // 'icon' => 'kanjia'
  175. // ],
  176. 'dealer' => [
  177. 'name' => '分销中心',
  178. 'url' => 'pages/dealer/index/index',
  179. 'icon' => 'fenxiaozhongxin'
  180. ],
  181. 'help' => [
  182. 'name' => '我的帮助',
  183. 'url' => 'pages/user/help/index',
  184. 'icon' => 'help'
  185. ],
  186. 'setting' => [
  187. 'name' => '我的设置',
  188. 'url' => 'pages/user/setting/index',
  189. 'icon' => 'shezhi1'
  190. ],
  191. 'logout' => [
  192. 'name' => '退出登录',
  193. 'url' => 'pages/login/login',
  194. 'icon' => 'logout'
  195. ],
  196. ];
  197. // 判断分销功能是否开启
  198. if (DealerSettingModel::isOpen()) {
  199. $menus['dealer']['name'] = DealerSettingModel::getDealerTitle();
  200. } else {
  201. unset($menus['dealer']);
  202. }
  203. return $menus;
  204. }
  205. }