WechatUser.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. if(sys_config('spread_look')==1) {$spid=0;}
  97. // 判断unionid 存在根据unionid判断
  98. if ($routineInfo['unionid'] != '' && ($uid = self::where(['unionid' => $routineInfo['unionid']])->where('user_type', '<>', 'h5')->value('uid'))) {
  99. self::edit($routineInfo, $uid, 'uid');
  100. $routineInfo['code'] = $spid;
  101. $routineInfo['isPromoter'] = $isCOde;
  102. if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
  103. User::updateWechatUser($routineInfo, $uid);
  104. if(sys_config('spread_look')==0) {
  105. if (!User::where('uid', $uid)->value('spread_uid') && $spid && $uid > $spid) { //绑定关系
  106. Db::name('user')->where('uid', $spid)->inc('spread_count', 1)->update();
  107. User::where('uid', $uid)->update([
  108. 'spread_uid' => $spid,
  109. 'spread_time' => time(),
  110. ]);
  111. }
  112. }
  113. } else if ($uid = self::where(['routine_openid' => $routineInfo['routine_openid']])->where('user_type', '<>', 'h5')->value('uid')) { //根据小程序openid判断
  114. self::edit($routineInfo, $uid, 'uid');
  115. $routineInfo['code'] = $spid;
  116. $routineInfo['isPromoter'] = $isCOde;
  117. if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
  118. User::updateWechatUser($routineInfo, $uid);
  119. if(sys_config('spread_look')==0) {
  120. if (!User::where('uid', $uid)->value('spread_uid') && $spid && $uid > $spid) { //绑定关系
  121. Db::name('user')->where('uid', $spid)->inc('spread_count', 1)->update();
  122. User::where('uid', $uid)->update([
  123. 'spread_uid' => $spid,
  124. 'spread_time' => time(),
  125. ]);
  126. }
  127. }
  128. } else {
  129. $routineInfo['add_time'] = time();//用户添加时间
  130. $routineInfo = self::create($routineInfo);
  131. $res = User::setRoutineUser($routineInfo, $spid);
  132. $uid = $res->uid;
  133. }
  134. if(intval(User::where('uid', $uid)->value('level'))==0)
  135. {
  136. UserLevel::setUserLevel($uid,1);
  137. }
  138. return $uid;
  139. }
  140. /**
  141. * 判断是否是小程序用户
  142. * @param int $uid
  143. * @return bool|int|string
  144. */
  145. public static function isRoutineUser($uid = 0)
  146. {
  147. if (!$uid) return false;
  148. return self::where('uid', $uid)->where('user_type', 'routine')->count();
  149. }
  150. /**
  151. * @param int $uid
  152. * @return int
  153. */
  154. public static function isUserStatus($uid = 0)
  155. {
  156. if (!$uid) return 0;
  157. $user = User::getUserInfo($uid);
  158. return $user['status'];
  159. }
  160. /**
  161. * .添加新用户
  162. * @param $openid
  163. * @return object
  164. */
  165. public static function setNewUser($openid)
  166. {
  167. $userInfo = WechatService::getUserInfo($openid);
  168. if (!isset($userInfo['subscribe']) || !$userInfo['subscribe'] || !isset($userInfo['openid']))
  169. exception('请关注公众号!');
  170. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  171. //判断 unionid 是否存在
  172. if (isset($userInfo['unionid'])) {
  173. $wechatInfo = self::where('unionid', $userInfo['unionid'])->find();
  174. unset($userInfo['qr_scene'], $userInfo['qr_scene_str'], $userInfo['qr_scene_str'], $userInfo['subscribe_scene']);
  175. if ($wechatInfo) {
  176. return self::edit($userInfo->toArray(), $userInfo['unionid'], 'unionid');
  177. }
  178. }
  179. self::beginTrans();
  180. $wechatUser = self::create(is_object($userInfo) ? $userInfo->toArray() : $userInfo);
  181. if (!$wechatUser) {
  182. self::rollbackTrans();
  183. exception('用户储存失败!');
  184. }
  185. if (!User::setWechatUser($wechatUser)) {
  186. self::rollbackTrans();
  187. exception('用户信息储存失败!');
  188. }
  189. self::commitTrans();
  190. self::userFirstSubGiveCoupon($openid);
  191. if(intval(User::where('uid', $userInfo['uid'])->value('level'))==0)
  192. {
  193. UserLevel::setUserLevel($userInfo['uid'],1);
  194. }
  195. if (!UserBill::be(['uid' => $wechatUser['uid'], 'type' => 'focus_wechat', 'status' => 1]) && $wechatUser['subscribe'] == 1) {
  196. self::focusWechatGiveIntegral($wechatUser['uid']); //关注送积分
  197. }
  198. return $wechatUser;
  199. }
  200. /**
  201. * TODO 关注送优惠券
  202. * @param $openid
  203. */
  204. public static function userFirstSubGiveCoupon($openid)
  205. {
  206. $couponIds = StoreCouponIssue::where('status', 1)
  207. ->where('is_give_subscribe', 1)
  208. ->where('is_del', 0)
  209. ->column('cid');
  210. if ($couponIds)
  211. foreach ($couponIds as $couponId)
  212. if ($couponId)
  213. StoreCouponUser::addUserCoupon(self::openidToUid($openid), $couponId);
  214. }
  215. /**
  216. * 订单金额达到预设金额赠送优惠卷
  217. * @param $uid
  218. */
  219. public static function userTakeOrderGiveCoupon($uid, $total_price)
  220. {
  221. $couponIds = StoreCouponIssue::where('status', 1)
  222. ->where('is_full_give', 1)
  223. ->where('is_del', 0)
  224. ->column('cid,full_reduction');
  225. if ($couponIds)
  226. foreach ($couponIds as $couponId)
  227. if ($couponId)
  228. if ($total_price >= $couponId['full_reduction'])
  229. StoreCouponUser::addUserCoupon($uid, $couponId['cid']);
  230. }
  231. /**
  232. * 首次注册送积分
  233. * @param $uid
  234. */
  235. public static function firstRegGiveIntegral($uid)
  236. {
  237. $integral = sys_config('first_reg_user_give_integral');
  238. if ($integral > 0) {
  239. BaseModel::beginTrans();
  240. $balance = User::where('uid', $uid)->value('integral');
  241. $res1 = User::bcInc($uid, 'integral', $integral, 'uid');
  242. $res2 = UserBill::income('首次注册', $uid, 'integral', 'first_reg', $integral, 0, $balance, '首次注册送贡献值');
  243. $res = $res1 && $res2;
  244. BaseModel::checkTrans($res);
  245. }
  246. }
  247. /**
  248. * 拼团成功送积分
  249. * @param $uid
  250. */
  251. public static function buyProductGiveIntegral($uid, $integral = 0)
  252. {
  253. // $integral = sys_config('buy_product_give_integral');
  254. // if ($integral > 0) {
  255. // BaseModel::beginTrans();
  256. // $balance = User::where('uid', $uid)->value('integral');
  257. // $res1 = User::bcInc($uid, 'integral', $integral, 'uid');
  258. // $res2 = UserBill::income('购物成功', $uid, 'integral', 'buy_product', $integral, 0, $balance, '购物成功送积分');
  259. // $res = $res1 && $res2;
  260. // BaseModel::checkTrans($res);
  261. // }
  262. return true;
  263. }
  264. /**
  265. * 中奖失败奖励WDC
  266. * @param array $pinkRegimental 成功的团长编号
  267. * @return bool
  268. * @throws \Exception
  269. */
  270. public static function failGiveWdc($uid, $wdc = 0)
  271. {
  272. BaseModel::beginTrans();
  273. $userInfo=User::where('uid',$uid)->find();
  274. $sum_wdc = bcadd($userInfo['wdc'], $wdc, 2);
  275. $res1 = false != User::where('uid', $userInfo['uid'])->update(['wdc' => $sum_wdc]);
  276. $res2 = false != UserBill::income('未中奖赠送酒币',$uid, 'wdc', 'fail_give',$wdc,0, bcadd($userInfo['wdc'],$wdc, 2), '拼团未中奖赠送' . floatval($wdc) . 'wdc');
  277. $res = $res1 && $res2;
  278. BaseModel::checkTrans($res);
  279. return $res;
  280. }
  281. //拼中上级增加一次排队结束机会
  282. public static function OverLikeNum($uid){
  283. BaseModel::beginTrans();
  284. $res=true;
  285. $userInfo=User::where('uid',$uid)->find();
  286. if($userInfo['spread_uid ']){
  287. $res1=User::bcInc($userInfo['spread_uid '], 'over_like_num',1, 'uid');
  288. $spreadInfo=User::where('uid',$userInfo['spread_uid '])->find();
  289. if($spreadInfo['over_like_num']==sys_config('invite_num_pd')){
  290. $res2=User::bcInc($userInfo['spread_uid '], ' over_num',1, 'uid');
  291. $res3=User::bcDec($userInfo['spread_uid '], ' over_like_num',sys_config('invite_num_pd'), 'uid');
  292. $res=$res1 && $res2 && $res3;
  293. }else{
  294. $res=$res1;
  295. }
  296. }
  297. BaseModel::checkTrans($res);
  298. return $res;
  299. }
  300. /**
  301. * 中奖奖励WDC
  302. * @param array $pinkRegimental 成功的团长编号
  303. * @return bool
  304. * @throws \Exception
  305. */
  306. public static function successGiveWdc($uid, $wdc = 0)
  307. {
  308. BaseModel::beginTrans();
  309. $userInfo=User::where('uid',$uid)->find();
  310. $sum_wdc = bcadd($userInfo['wdc'], $wdc, 2);
  311. $res1 = false != User::where('uid', $userInfo['uid'])->update(['wdc' => $sum_wdc]);
  312. $res2 = false != UserBill::income('中奖赠送酒币',$uid, 'wdc', 'success_give',$wdc,0, bcadd($userInfo['wdc'],$wdc, 2), '拼团中奖赠送' . floatval($wdc) . 'wdc');
  313. $res = $res1 && $res2;
  314. BaseModel::checkTrans($res);
  315. return $res;
  316. }
  317. /**
  318. * 成功发展下线送积分
  319. * @param $uid
  320. */
  321. public static function spreadSuccessGiveIntegral($uid)
  322. {
  323. $integral = sys_config('spread_success_give_integral');
  324. if ($integral > 0) {
  325. BaseModel::beginTrans();
  326. $balance = User::where('uid', $uid)->value('integral');
  327. $res1 = User::bcInc($uid, 'integral', $integral, 'uid');
  328. $res2 = UserBill::income('推广成功', $uid, 'integral', 'spread_success', $integral, 0, $balance, '推广成功送积分');
  329. $res = $res1 && $res2;
  330. BaseModel::checkTrans($res);
  331. }
  332. }
  333. /**
  334. * 关注公众号送积分
  335. * @param $uid
  336. */
  337. public static function focusWechatGiveIntegral($uid)
  338. {
  339. $integral = sys_config('focus_wechat_give_integral');
  340. if ($integral > 0) {
  341. BaseModel::beginTrans();
  342. $balance = User::where('uid', $uid)->value('integral');
  343. $res1 = User::bcInc($uid, 'integral', $integral, 'uid');
  344. $res2 = UserBill::income('关注公众号', $uid, 'integral', 'focus_wechat', $integral, 0, $balance, '关注公众号送积分');
  345. $res = $res1 && $res2;
  346. BaseModel::checkTrans($res);
  347. }
  348. }
  349. /**
  350. * 更新用户信息
  351. * @param $openid
  352. * @return bool
  353. */
  354. public static function updateUser($openid)
  355. {
  356. $userInfo = WechatService::getUserInfo($openid);
  357. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  358. self::edit($userInfo->toArray(), $openid, 'openid');
  359. $user = WechatUser::where("openid", $openid)->find();
  360. if (!UserBill::be(['uid' => $user['uid'], 'type' => 'focus_wechat', 'status' => 1]) && $user['subscribe'] == 1) {
  361. self::focusWechatGiveIntegral($user['uid']); //关注送积分
  362. }
  363. }
  364. /**
  365. * 用户存在就更新 不存在就添加
  366. * @param $openid
  367. */
  368. public static function saveUser($openid)
  369. {
  370. self::be(['openid' => $openid]) == true ? self::updateUser($openid) : self::setNewUser($openid);
  371. }
  372. /**
  373. * 用户取消关注
  374. * @param $openid
  375. * @return bool
  376. */
  377. public static function unSubscribe($openid)
  378. {
  379. return self::edit(['subscribe' => 0], $openid, 'openid');
  380. }
  381. /**
  382. * TODO 用uid获得Unionid
  383. * @param $uid
  384. * @return mixed
  385. */
  386. public static function uidToUnionid($uid)
  387. {
  388. return self::where('uid', $uid)->value('unionid');
  389. }
  390. /**
  391. * TODO 获取用户信息
  392. * @param $openid
  393. * @param $openidType
  394. * @return array
  395. * @throws \think\db\exception\DataNotFoundException
  396. * @throws \think\db\exception\ModelNotFoundException
  397. * @throws \think\exception\DbException
  398. */
  399. public static function getWechatInfo($openid, $openidType)
  400. {
  401. if (is_numeric($openid)) $openid = self::uidToOpenid($openid);
  402. $wechatInfo = self::where($openidType, $openid)->find();
  403. if (!$wechatInfo) {
  404. self::setNewUser($openid);
  405. $wechatInfo = self::where($openidType, $openid)->find();
  406. }
  407. if (!$wechatInfo) exception('获取用户信息失败!');
  408. return $wechatInfo->toArray();
  409. }
  410. }