WechatUser.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/21
  6. */
  7. namespace app\models\user;
  8. use app\models\store\StoreCouponIssue;
  9. use crmeb\basic\BaseModel;
  10. use crmeb\services\WechatService;
  11. use crmeb\traits\ModelTrait;
  12. use crmeb\services\SystemConfigService;
  13. use app\models\routine\RoutineQrcode;
  14. use app\models\store\StoreCouponUser;
  15. use think\facade\Db;
  16. use think\facade\Log;
  17. /**
  18. * TODO 微信用户Model
  19. * Class WechatUser
  20. * @package app\models\user
  21. */
  22. class WechatUser extends BaseModel
  23. {
  24. /**
  25. * 数据表主键
  26. * @var string
  27. */
  28. protected $pk = 'uid';
  29. /**
  30. * 模型名称
  31. * @var string
  32. */
  33. protected $name = 'wechat_user';
  34. use ModelTrait;
  35. /**
  36. * uid获取小程序Openid
  37. * @param string $uid
  38. * @return bool|mixed
  39. */
  40. public static function getOpenId($uid = '')
  41. {
  42. if ($uid == '') return false;
  43. return self::where('uid', $uid)->value('routine_openid');
  44. }
  45. /**
  46. * TODO 用uid获得openid
  47. * @param $uid
  48. * @param string $openidType
  49. * @return mixed
  50. * @throws \Exception
  51. */
  52. public static function uidToOpenid($uid, $openidType = 'routine_openid')
  53. {
  54. $openid = self::where('uid', $uid)->value($openidType);
  55. return $openid;
  56. }
  57. /**
  58. * TODO 用openid获得uid
  59. * @param $openid
  60. * @param string $openidType
  61. * @return mixed
  62. */
  63. public static function openidTouid($openid, $openidType = 'openid')
  64. {
  65. return self::where($openidType, $openid)->where('user_type', '<>', 'h5')->value('uid');
  66. }
  67. /**
  68. * 小程序创建用户后返回uid
  69. * @param $routineInfo
  70. * @return mixed
  71. */
  72. public static function routineOauth($routine)
  73. {
  74. $routineInfo['nickname'] = filter_emoji($routine['nickName']);//姓名
  75. $routineInfo['sex'] = $routine['gender'];//性别
  76. $routineInfo['language'] = $routine['language'];//语言
  77. $routineInfo['city'] = $routine['city'];//城市
  78. $routineInfo['province'] = $routine['province'];//省份
  79. $routineInfo['country'] = $routine['country'];//国家
  80. $routineInfo['headimgurl'] = $routine['avatarUrl'];//头像
  81. $routineInfo['routine_openid'] = $routine['openId'];//openid
  82. $routineInfo['session_key'] = $routine['session_key'];//会话密匙
  83. $routineInfo['unionid'] = $routine['unionId'];//用户在开放平台的唯一标识符
  84. $routineInfo['user_type'] = 'routine';//用户类型
  85. $spid = 0;//绑定关系uid
  86. $isCOde = false;
  87. //获取是否有扫码进小程序
  88. if ($routine['code']) {
  89. if ($info = RoutineQrcode::getRoutineQrcodeFindType($routine['code'])) {
  90. $spid = $info['third_id'];
  91. $isCOde = true;
  92. } else
  93. $spid = $routine['spid'];
  94. } else if ($routine['spid']) {
  95. $spid = $routine['spid'];
  96. }
  97. if (sys_config('spread_look') == 1) {
  98. $spid = 0;
  99. }
  100. // 判断unionid 存在根据unionid判断
  101. if ($routineInfo['unionid'] != '' && ($uid = self::where(['unionid' => $routineInfo['unionid']])->where('user_type', '<>', 'h5')->value('uid'))) {
  102. self::edit($routineInfo, $uid, 'uid');
  103. $routineInfo['code'] = $spid;
  104. $routineInfo['isPromoter'] = $isCOde;
  105. if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
  106. User::updateWechatUser($routineInfo, $uid);
  107. if (sys_config('spread_look') == 0) {
  108. if (!User::where('uid', $uid)->value('spread_uid') && $spid && $uid > $spid) { //绑定关系
  109. Db::name('user')->where('uid', $spid)->inc('spread_count', 1)->update();
  110. User::where('uid', $uid)->update([
  111. 'spread_uid' => $spid,
  112. 'spread_time' => time(),
  113. ]);
  114. }
  115. }
  116. } else if ($uid = self::where(['routine_openid' => $routineInfo['routine_openid']])->where('user_type', '<>', 'h5')->value('uid')) { //根据小程序openid判断
  117. self::edit($routineInfo, $uid, 'uid');
  118. $routineInfo['code'] = $spid;
  119. $routineInfo['isPromoter'] = $isCOde;
  120. if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
  121. User::updateWechatUser($routineInfo, $uid);
  122. if (sys_config('spread_look') == 0) {
  123. if (!User::where('uid', $uid)->value('spread_uid') && $spid && $uid > $spid) { //绑定关系
  124. Db::name('user')->where('uid', $spid)->inc('spread_count', 1)->update();
  125. User::where('uid', $uid)->update([
  126. 'spread_uid' => $spid,
  127. 'spread_time' => time(),
  128. ]);
  129. }
  130. }
  131. } else {
  132. $routineInfo['add_time'] = time();//用户添加时间
  133. $routineInfo = self::create($routineInfo);
  134. $res = User::setRoutineUser($routineInfo, $spid);
  135. $uid = $res->uid;
  136. }
  137. if (intval(User::where('uid', $uid)->value('level')) == 0) {
  138. UserLevel::setUserLevel($uid, 1);
  139. }
  140. return $uid;
  141. }
  142. /**
  143. * 判断是否是小程序用户
  144. * @param int $uid
  145. * @return bool|int|string
  146. */
  147. public static function isRoutineUser($uid = 0)
  148. {
  149. if (!$uid) return false;
  150. return self::where('uid', $uid)->where('user_type', 'routine')->count();
  151. }
  152. /**
  153. * @param int $uid
  154. * @return int
  155. */
  156. public static function isUserStatus($uid = 0)
  157. {
  158. if (!$uid) return 0;
  159. $user = User::getUserInfo($uid);
  160. return $user['status'];
  161. }
  162. /**
  163. * .添加新用户
  164. * @param $openid
  165. * @return object
  166. */
  167. public static function setNewUser($openid)
  168. {
  169. $userInfo = WechatService::getUserInfo($openid);
  170. if (!isset($userInfo['subscribe']) || !$userInfo['subscribe'] || !isset($userInfo['openid']))
  171. exception('请关注公众号!');
  172. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  173. //判断 unionid 是否存在
  174. if (isset($userInfo['unionid'])) {
  175. $wechatInfo = self::where('unionid', $userInfo['unionid'])->find();
  176. unset($userInfo['qr_scene'], $userInfo['qr_scene_str'], $userInfo['qr_scene_str'], $userInfo['subscribe_scene']);
  177. if ($wechatInfo) {
  178. return self::edit($userInfo->toArray(), $userInfo['unionid'], 'unionid');
  179. }
  180. }
  181. self::beginTrans();
  182. $wechatUser = self::create(is_object($userInfo) ? $userInfo->toArray() : $userInfo);
  183. if (!$wechatUser) {
  184. self::rollbackTrans();
  185. exception('用户储存失败!');
  186. }
  187. if (!User::setWechatUser($wechatUser)) {
  188. self::rollbackTrans();
  189. exception('用户信息储存失败!');
  190. }
  191. self::commitTrans();
  192. self::userFirstSubGiveCoupon($openid);
  193. if (intval(User::where('uid', $userInfo['uid'])->value('level')) == 0) {
  194. UserLevel::setUserLevel($userInfo['uid'], 1);
  195. }
  196. if (!UserBill::be(['uid' => $wechatUser['uid'], 'type' => 'focus_wechat', 'status' => 1]) && $wechatUser['subscribe'] == 1) {
  197. self::focusWechatGiveIntegral($wechatUser['uid']); //关注送积分
  198. }
  199. return $wechatUser;
  200. }
  201. /**
  202. * TODO 关注送优惠券
  203. * @param $openid
  204. */
  205. public static function userFirstSubGiveCoupon($openid)
  206. {
  207. $couponIds = StoreCouponIssue::where('status', 1)
  208. ->where('is_give_subscribe', 1)
  209. ->where('is_del', 0)
  210. ->column('cid');
  211. if ($couponIds)
  212. foreach ($couponIds as $couponId)
  213. if ($couponId)
  214. StoreCouponUser::addUserCoupon(self::openidToUid($openid), $couponId);
  215. }
  216. /**
  217. * 订单金额达到预设金额赠送优惠卷
  218. * @param $uid
  219. */
  220. public static function userTakeOrderGiveCoupon($uid, $total_price)
  221. {
  222. $couponIds = StoreCouponIssue::where('status', 1)
  223. ->where('is_full_give', 1)
  224. ->where('is_del', 0)
  225. ->column('cid,full_reduction');
  226. if ($couponIds)
  227. foreach ($couponIds as $couponId)
  228. if ($couponId)
  229. if ($total_price >= $couponId['full_reduction'])
  230. StoreCouponUser::addUserCoupon($uid, $couponId['cid']);
  231. }
  232. /**
  233. * 首次注册送积分
  234. * @param $uid
  235. */
  236. public static function firstRegGiveIntegral($uid)
  237. {
  238. $integral = sys_config('first_reg_user_give_integral');
  239. if ($integral > 0) {
  240. BaseModel::beginTrans();
  241. $balance = User::where('uid', $uid)->value('integral');
  242. $res1 = User::bcInc($uid, 'integral', $integral, 'uid');
  243. $res2 = UserBill::income('首次注册', $uid, 'integral', 'first_reg', $integral, 0, $balance, '首次注册送贡献值');
  244. $res = $res1 && $res2;
  245. BaseModel::checkTrans($res);
  246. }
  247. }
  248. /**
  249. * 拼团成功送积分
  250. * @param $uid
  251. */
  252. public static function buyProductGiveIntegral($uid, $integral = 0)
  253. {
  254. // $integral = sys_config('buy_product_give_integral');
  255. // if ($integral > 0) {
  256. // BaseModel::beginTrans();
  257. // $balance = User::where('uid', $uid)->value('integral');
  258. // $res1 = User::bcInc($uid, 'integral', $integral, 'uid');
  259. // $res2 = UserBill::income('购物成功', $uid, 'integral', 'buy_product', $integral, 0, $balance, '购物成功送积分');
  260. // $res = $res1 && $res2;
  261. // BaseModel::checkTrans($res);
  262. // }
  263. return true;
  264. }
  265. /**
  266. * 中奖失败奖励WDC
  267. * @param array $pinkRegimental 成功的团长编号
  268. * @return bool
  269. * @throws \Exception
  270. */
  271. public static function failGiveWdc($uid, $wdc = 0)
  272. {
  273. BaseModel::beginTrans();
  274. $userInfo = User::where('uid', $uid)->find();
  275. $sum_wdc = bcadd($userInfo['wdc'], $wdc, 2);
  276. $res1 = false != User::where('uid', $userInfo['uid'])->update(['wdc' => $sum_wdc]);
  277. $res3 = false != User::where('uid', $userInfo['uid'])->update(['wdc_all' => $sum_wdc]);
  278. $res2 = false != UserBill::income('未中奖赠送酒币', $uid, 'wdc', 'fail_give', $wdc, 0, bcadd($userInfo['wdc'], $wdc, 2), '拼团未中奖赠送' . floatval($wdc) . 'wdc');
  279. $res = $res1 && $res2 && $res3;
  280. BaseModel::checkTrans($res);
  281. return $res;
  282. }
  283. //拼中上级增加一次排队结束机会
  284. public static function OverLikeNum($uid)
  285. {
  286. BaseModel::beginTrans();
  287. $res = true;
  288. $userInfo = User::where('uid', $uid)->find();
  289. if ($userInfo['spread_uid ']) {
  290. $res1 = User::bcInc($userInfo['spread_uid '], 'over_like_num', 1, 'uid');
  291. $spreadInfo = User::where('uid', $userInfo['spread_uid '])->find();
  292. if ($spreadInfo['over_like_num'] == sys_config('invite_num_pd')) {
  293. $res2 = User::bcInc($userInfo['spread_uid '], ' over_num', 1, 'uid');
  294. $res3 = User::bcDec($userInfo['spread_uid '], ' over_like_num', sys_config('invite_num_pd'), 'uid');
  295. $res = $res1 && $res2 && $res3;
  296. } else {
  297. $res = $res1;
  298. }
  299. }
  300. BaseModel::checkTrans($res);
  301. return $res;
  302. }
  303. /**
  304. * 中奖奖励WDC
  305. * @param array $pinkRegimental 成功的团长编号
  306. * @return bool
  307. * @throws \Exception
  308. */
  309. public static function successGiveWdc($uid, $wdc = 0)
  310. {
  311. BaseModel::beginTrans();
  312. $userInfo = User::where('uid', $uid)->find();
  313. $sum_wdc = bcadd($userInfo['wdc'], $wdc, 2);
  314. $res1 = false != User::where('uid', $userInfo['uid'])->update(['wdc' => $sum_wdc]);
  315. $res3 = false != User::where('uid', $userInfo['uid'])->update(['wdc_all' => $sum_wdc]);
  316. $res2 = false != UserBill::income('中奖赠送酒币', $uid, 'wdc', 'success_give', $wdc, 0, bcadd($userInfo['wdc'], $wdc, 2), '拼团中奖赠送' . floatval($wdc) . 'wdc');
  317. $res = $res1 && $res2 && $res3;
  318. BaseModel::checkTrans($res);
  319. return $res;
  320. }
  321. /**
  322. * 成功发展下线送积分
  323. * @param $uid
  324. */
  325. public static function spreadSuccessGiveIntegral($uid)
  326. {
  327. $integral = sys_config('spread_success_give_integral');
  328. if ($integral > 0) {
  329. BaseModel::beginTrans();
  330. $balance = User::where('uid', $uid)->value('integral');
  331. $res1 = User::bcInc($uid, 'integral', $integral, 'uid');
  332. $res2 = UserBill::income('推广成功', $uid, 'integral', 'spread_success', $integral, 0, $balance, '推广成功送积分');
  333. $res = $res1 && $res2;
  334. BaseModel::checkTrans($res);
  335. }
  336. }
  337. /**
  338. * 关注公众号送积分
  339. * @param $uid
  340. */
  341. public static function focusWechatGiveIntegral($uid)
  342. {
  343. $integral = sys_config('focus_wechat_give_integral');
  344. if ($integral > 0) {
  345. BaseModel::beginTrans();
  346. $balance = User::where('uid', $uid)->value('integral');
  347. $res1 = User::bcInc($uid, 'integral', $integral, 'uid');
  348. $res2 = UserBill::income('关注公众号', $uid, 'integral', 'focus_wechat', $integral, 0, $balance, '关注公众号送积分');
  349. $res = $res1 && $res2;
  350. BaseModel::checkTrans($res);
  351. }
  352. }
  353. /**
  354. * 更新用户信息
  355. * @param $openid
  356. * @return bool
  357. */
  358. public static function updateUser($openid)
  359. {
  360. $userInfo = WechatService::getUserInfo($openid);
  361. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  362. self::edit($userInfo->toArray(), $openid, 'openid');
  363. $user = WechatUser::where("openid", $openid)->find();
  364. if (!UserBill::be(['uid' => $user['uid'], 'type' => 'focus_wechat', 'status' => 1]) && $user['subscribe'] == 1) {
  365. self::focusWechatGiveIntegral($user['uid']); //关注送积分
  366. }
  367. }
  368. /**
  369. * 用户存在就更新 不存在就添加
  370. * @param $openid
  371. */
  372. public static function saveUser($openid)
  373. {
  374. self::be(['openid' => $openid]) == true ? self::updateUser($openid) : self::setNewUser($openid);
  375. }
  376. /**
  377. * 用户取消关注
  378. * @param $openid
  379. * @return bool
  380. */
  381. public static function unSubscribe($openid)
  382. {
  383. return self::edit(['subscribe' => 0], $openid, 'openid');
  384. }
  385. /**
  386. * TODO 用uid获得Unionid
  387. * @param $uid
  388. * @return mixed
  389. */
  390. public static function uidToUnionid($uid)
  391. {
  392. return self::where('uid', $uid)->value('unionid');
  393. }
  394. /**
  395. * TODO 获取用户信息
  396. * @param $openid
  397. * @param $openidType
  398. * @return array
  399. * @throws \think\db\exception\DataNotFoundException
  400. * @throws \think\db\exception\ModelNotFoundException
  401. * @throws \think\exception\DbException
  402. */
  403. public static function getWechatInfo($openid, $openidType)
  404. {
  405. if (is_numeric($openid)) $openid = self::uidToOpenid($openid);
  406. $wechatInfo = self::where($openidType, $openid)->find();
  407. if (!$wechatInfo) {
  408. self::setNewUser($openid);
  409. $wechatInfo = self::where($openidType, $openid)->find();
  410. }
  411. if (!$wechatInfo) exception('获取用户信息失败!');
  412. return $wechatInfo->toArray();
  413. }
  414. }