User.php 5.9 KB

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