User.php 9.2 KB

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